|
| 1 | +use crate::api::error::PhotonApiError; |
1 | 2 | use crate::api::method::get_multiple_new_address_proofs::MerkleContextWithNewAddressProof;
|
| 3 | +use crate::api::method::get_validity_proof::prover::gnark::negate_g1; |
2 | 4 | use crate::api::method::get_validity_proof::prover::structs::{
|
3 | 5 | GnarkProofJson, InclusionHexInputsForProver, NonInclusionHexInputsForProver, ProofABC,
|
4 | 6 | };
|
@@ -64,73 +66,125 @@ pub fn hash_to_hex(hash: &Hash) -> String {
|
64 | 66 | fn pubkey_to_hex(pubkey: &SerializablePubkey) -> String {
|
65 | 67 | let bytes = pubkey.to_bytes_vec();
|
66 | 68 | let hex = hex::encode(bytes);
|
| 69 | + |
67 | 70 | format!("0x{}", hex)
|
68 | 71 | }
|
69 | 72 |
|
70 |
| -fn deserialize_hex_string_to_bytes(hex_str: &str) -> Vec<u8> { |
| 73 | +pub fn deserialize_hex_string_to_bytes(hex_str: &str) -> Result<Vec<u8>, PhotonApiError> { |
71 | 74 | let hex_str = if hex_str.starts_with("0x") {
|
72 | 75 | &hex_str[2..]
|
73 | 76 | } else {
|
74 | 77 | hex_str
|
75 | 78 | };
|
76 |
| - |
77 |
| - // Left pad with 0s if the length is not 64 |
78 | 79 | let hex_str = format!("{:0>64}", hex_str);
|
79 | 80 |
|
80 |
| - hex::decode(&hex_str).expect("Failed to decode hex string") |
| 81 | + hex::decode(hex_str) |
| 82 | + .map_err(|_| PhotonApiError::UnexpectedError("Failed to decode hex string".to_string())) |
81 | 83 | }
|
82 | 84 |
|
83 |
| -pub fn proof_from_json_struct(json: GnarkProofJson) -> ProofABC { |
84 |
| - let proof_ax = deserialize_hex_string_to_bytes(&json.ar[0]); |
85 |
| - let proof_ay = deserialize_hex_string_to_bytes(&json.ar[1]); |
86 |
| - let proof_a = [proof_ax, proof_ay].concat(); |
| 85 | +pub fn proof_from_json_struct(json: GnarkProofJson) -> Result<ProofABC, PhotonApiError> { |
| 86 | + let proof_a_x = deserialize_hex_string_to_bytes(&json.ar[0])?; |
| 87 | + let proof_a_y = deserialize_hex_string_to_bytes(&json.ar[1])?; |
| 88 | + let proof_a: [u8; 64] = [proof_a_x, proof_a_y].concat().try_into().map_err(|_| { |
| 89 | + PhotonApiError::UnexpectedError("Failed to convert proof_a to [u8; 64]".to_string()) |
| 90 | + })?; |
| 91 | + let proof_a = negate_g1(&proof_a)?; |
87 | 92 |
|
88 |
| - let proof_bx0 = deserialize_hex_string_to_bytes(&json.bs[0][0]); |
89 |
| - let proof_bx1 = deserialize_hex_string_to_bytes(&json.bs[0][1]); |
90 |
| - let proof_by0 = deserialize_hex_string_to_bytes(&json.bs[1][0]); |
91 |
| - let proof_by1 = deserialize_hex_string_to_bytes(&json.bs[1][1]); |
92 |
| - let proof_b = [proof_bx0, proof_bx1, proof_by0, proof_by1].concat(); |
| 93 | + let proof_b_x_0 = deserialize_hex_string_to_bytes(&json.bs[0][0])?; |
| 94 | + let proof_b_x_1 = deserialize_hex_string_to_bytes(&json.bs[0][1])?; |
| 95 | + let proof_b_y_0 = deserialize_hex_string_to_bytes(&json.bs[1][0])?; |
| 96 | + let proof_b_y_1 = deserialize_hex_string_to_bytes(&json.bs[1][1])?; |
| 97 | + let proof_b: [u8; 128] = [proof_b_x_0, proof_b_x_1, proof_b_y_0, proof_b_y_1] |
| 98 | + .concat() |
| 99 | + .try_into() |
| 100 | + .map_err(|_| { |
| 101 | + PhotonApiError::UnexpectedError("Failed to convert proof_b to [u8; 128]".to_string()) |
| 102 | + })?; |
93 | 103 |
|
94 |
| - let proof_cx = deserialize_hex_string_to_bytes(&json.krs[0]); |
95 |
| - let proof_cy = deserialize_hex_string_to_bytes(&json.krs[1]); |
96 |
| - let proof_c = [proof_cx, proof_cy].concat(); |
| 104 | + let proof_c_x = deserialize_hex_string_to_bytes(&json.krs[0])?; |
| 105 | + let proof_c_y = deserialize_hex_string_to_bytes(&json.krs[1])?; |
| 106 | + let proof_c: [u8; 64] = [proof_c_x, proof_c_y].concat().try_into().map_err(|_| { |
| 107 | + PhotonApiError::UnexpectedError("Failed to convert proof_c to [u8; 64]".to_string()) |
| 108 | + })?; |
97 | 109 |
|
98 |
| - ProofABC { |
| 110 | + Ok(ProofABC { |
99 | 111 | a: proof_a,
|
100 | 112 | b: proof_b,
|
101 | 113 | c: proof_c,
|
102 |
| - } |
| 114 | + }) |
103 | 115 | }
|
104 | 116 |
|
105 | 117 | pub fn get_public_input_hash(
|
106 | 118 | account_proofs: &[MerkleProofWithContext],
|
107 | 119 | new_address_proofs: &[MerkleContextWithNewAddressProof],
|
108 |
| -) -> [u8; 32] { |
109 |
| - let account_hashes: Vec<[u8; 32]> = account_proofs |
| 120 | +) -> Result<[u8; 32], PhotonApiError> { |
| 121 | + let account_hashes: Result<Vec<[u8; 32]>, PhotonApiError> = account_proofs |
110 | 122 | .iter()
|
111 |
| - .map(|x| x.hash.to_vec().clone().try_into().unwrap()) |
112 |
| - .collect::<Vec<[u8; 32]>>(); |
113 |
| - let account_roots: Vec<[u8; 32]> = account_proofs |
| 123 | + .map(|x| { |
| 124 | + x.hash.to_vec().try_into().map_err(|_| { |
| 125 | + PhotonApiError::UnexpectedError("Failed to convert hash to [u8; 32]".to_string()) |
| 126 | + }) |
| 127 | + }) |
| 128 | + .collect(); |
| 129 | + let account_hashes = account_hashes?; |
| 130 | + |
| 131 | + let account_roots: Result<Vec<[u8; 32]>, PhotonApiError> = account_proofs |
114 | 132 | .iter()
|
115 |
| - .map(|x| x.root.to_vec().clone().try_into().unwrap()) |
116 |
| - .collect::<Vec<[u8; 32]>>(); |
117 |
| - let inclusion_hash_chain: [u8; 32] = |
118 |
| - create_two_inputs_hash_chain(&account_roots, &account_hashes).unwrap(); |
119 |
| - let new_address_hashes: Vec<[u8; 32]> = new_address_proofs |
| 133 | + .map(|x| { |
| 134 | + x.root.to_vec().try_into().map_err(|_| { |
| 135 | + PhotonApiError::UnexpectedError("Failed to convert root to [u8; 32]".to_string()) |
| 136 | + }) |
| 137 | + }) |
| 138 | + .collect(); |
| 139 | + let account_roots = account_roots?; |
| 140 | + |
| 141 | + let inclusion_hash_chain = create_two_inputs_hash_chain(&account_roots, &account_hashes) |
| 142 | + .map_err(|e| { |
| 143 | + PhotonApiError::UnexpectedError(format!("Failed to create hash chain: {}", e)) |
| 144 | + })?; |
| 145 | + |
| 146 | + let new_address_hashes: Result<Vec<[u8; 32]>, PhotonApiError> = new_address_proofs |
120 | 147 | .iter()
|
121 |
| - .map(|x| x.address.try_to_vec().unwrap().clone().try_into().unwrap()) |
122 |
| - .collect::<Vec<[u8; 32]>>(); |
123 |
| - let new_address_roots: Vec<[u8; 32]> = new_address_proofs |
| 148 | + .map(|x| { |
| 149 | + x.address |
| 150 | + .try_to_vec() |
| 151 | + .map_err(|e| { |
| 152 | + PhotonApiError::UnexpectedError(format!("Failed to serialize address: {}", e)) |
| 153 | + })? |
| 154 | + .try_into() |
| 155 | + .map_err(|_| { |
| 156 | + PhotonApiError::UnexpectedError( |
| 157 | + "Failed to convert address bytes to [u8; 32]".to_string(), |
| 158 | + ) |
| 159 | + }) |
| 160 | + }) |
| 161 | + .collect(); |
| 162 | + let new_address_hashes = new_address_hashes?; |
| 163 | + |
| 164 | + let new_address_roots: Result<Vec<[u8; 32]>, PhotonApiError> = new_address_proofs |
124 | 165 | .iter()
|
125 |
| - .map(|x| x.root.to_vec().clone().try_into().unwrap()) |
126 |
| - .collect::<Vec<[u8; 32]>>(); |
| 166 | + .map(|x| { |
| 167 | + x.root.to_vec().try_into().map_err(|_| { |
| 168 | + PhotonApiError::UnexpectedError( |
| 169 | + "Failed to convert new address root to [u8; 32]".to_string(), |
| 170 | + ) |
| 171 | + }) |
| 172 | + }) |
| 173 | + .collect(); |
| 174 | + let new_address_roots = new_address_roots?; |
| 175 | + |
127 | 176 | let non_inclusion_hash_chain =
|
128 |
| - create_two_inputs_hash_chain(&new_address_roots, &new_address_hashes).unwrap(); |
| 177 | + create_two_inputs_hash_chain(&new_address_roots, &new_address_hashes).map_err(|e| { |
| 178 | + PhotonApiError::UnexpectedError(format!("Failed to create hash chain: {}", e)) |
| 179 | + })?; |
| 180 | + |
129 | 181 | if non_inclusion_hash_chain != [0u8; 32] {
|
130 |
| - non_inclusion_hash_chain |
| 182 | + Ok(non_inclusion_hash_chain) |
131 | 183 | } else if inclusion_hash_chain != [0u8; 32] {
|
132 |
| - inclusion_hash_chain |
| 184 | + Ok(inclusion_hash_chain) |
133 | 185 | } else {
|
134 |
| - create_two_inputs_hash_chain(&[inclusion_hash_chain], &[non_inclusion_hash_chain]).unwrap() |
| 186 | + create_two_inputs_hash_chain(&[inclusion_hash_chain], &[non_inclusion_hash_chain]).map_err( |
| 187 | + |e| PhotonApiError::UnexpectedError(format!("Failed to create hash chain: {}", e)), |
| 188 | + ) |
135 | 189 | }
|
136 | 190 | }
|
0 commit comments