|
| 1 | +use crate::types::{ArgWithTypeJSON, EntryFunctionArgumentsJSON, HexEncodedBytes}; |
| 2 | +use anyhow::{Error, Result}; |
| 3 | +use halo2_backend::arithmetic::CurveAffine; |
| 4 | +use halo2_backend::helpers::SerdeFormat; |
| 5 | +use halo2_backend::plonk::VerifyingKey; |
| 6 | +use halo2_backend::poly::commitment::Params; |
| 7 | +use halo2_proofs::halo2curves::bn256::{Bn256, Fr, G1Affine}; |
| 8 | +use halo2_proofs::halo2curves::ff::{FromUniformBytes, PrimeField}; |
| 9 | +use halo2_proofs::plonk::Circuit; |
| 10 | +use halo2_proofs::poly::kzg::commitment::ParamsKZG; |
| 11 | +use halo2_verifier::circuit::{generate_serialized_circuit, generate_serialized_protocol}; |
| 12 | +use halo2_verifier::params::serialize_kzg_params; |
| 13 | +use serde_json::json; |
| 14 | + |
| 15 | +pub mod types; |
| 16 | + |
| 17 | +/// module names shared by move verifier and native verifier |
| 18 | +const VERIFIER_MODULE: &str = "verifier_api"; |
| 19 | +const PARAMS_MODULE: &str = "param_store"; |
| 20 | +const VK_MODULE: &str = "vk_store"; |
| 21 | + |
| 22 | +/// function names for move verifier |
| 23 | +const PUBLISH_PROTOCOL_FUNC: &str = "publish_protocol"; |
| 24 | +const VERIFY_PROOF_FUNC: &str = "verify_proof"; |
| 25 | + |
| 26 | +/// function names for native verifier |
| 27 | +const PUBLISH_PARAMS_FUNC: &str = "publish_serialized_params"; |
| 28 | +const PUBLISH_VK_FUNC: &str = "publish_serialized_vk"; |
| 29 | +const PUBLISH_CIRCUIT_FUNC: &str = "publish_serialized_circuit"; |
| 30 | + |
| 31 | +/// Move verifier related APIs |
| 32 | +/// build publish protocol transaction payload for aptos. |
| 33 | +/// Returns a structure which can be serialized to json string, |
| 34 | +/// and when output the json to file, it can be run by `aptos move run`. |
| 35 | +pub fn build_publish_protocol_transaction_payload<ConcreteCircuit>( |
| 36 | + params: &ParamsKZG<Bn256>, |
| 37 | + circuit: &ConcreteCircuit, |
| 38 | + verifier_address: String, |
| 39 | +) -> Result<EntryFunctionArgumentsJSON, Error> |
| 40 | +where |
| 41 | + ConcreteCircuit: Circuit<Fr>, |
| 42 | +{ |
| 43 | + let protocol = generate_serialized_protocol(params, circuit) |
| 44 | + .expect("generate circuit info should not fail"); |
| 45 | + |
| 46 | + let args: Vec<_> = protocol |
| 47 | + .into_iter() |
| 48 | + .map(|arg| ArgWithTypeJSON { |
| 49 | + arg_type: "hex".to_string(), |
| 50 | + value: json!(arg |
| 51 | + .into_iter() |
| 52 | + .map(|i| HexEncodedBytes(i).to_string()) |
| 53 | + .collect::<Vec<_>>()), |
| 54 | + }) |
| 55 | + .collect(); |
| 56 | + let json = EntryFunctionArgumentsJSON { |
| 57 | + function_id: format!( |
| 58 | + "{}::{}::{}", |
| 59 | + verifier_address, VERIFIER_MODULE, PUBLISH_PROTOCOL_FUNC |
| 60 | + ), |
| 61 | + type_args: vec![], |
| 62 | + args, |
| 63 | + }; |
| 64 | + Ok(json) |
| 65 | +} |
| 66 | + |
| 67 | +/// Move verifier related APIs |
| 68 | +/// Build verify proof transaction payload for aptos. |
| 69 | +/// Returns a structure which can be serialized to json string, |
| 70 | +/// and when output the json to file, it can be run by `aptos move run`. |
| 71 | +#[allow(clippy::let_and_return)] |
| 72 | +pub fn build_verify_proof_transaction_payload( |
| 73 | + proof: Vec<u8>, |
| 74 | + proof_kzg_variant: u8, |
| 75 | + instances: Vec<Vec<Fr>>, |
| 76 | + verifier_address: String, |
| 77 | + param_address: String, |
| 78 | + protocol_address: String, |
| 79 | +) -> EntryFunctionArgumentsJSON { |
| 80 | + let instances = instances |
| 81 | + .iter() |
| 82 | + .map(|fr| { |
| 83 | + fr.iter() |
| 84 | + .map(|f| HexEncodedBytes(f.to_repr().as_ref().to_vec()).to_string()) |
| 85 | + .collect::<Vec<_>>() |
| 86 | + }) |
| 87 | + .collect::<Vec<_>>(); |
| 88 | + let json = EntryFunctionArgumentsJSON { |
| 89 | + function_id: format!( |
| 90 | + "{}::{}::{}", |
| 91 | + verifier_address, VERIFIER_MODULE, VERIFY_PROOF_FUNC |
| 92 | + ), |
| 93 | + type_args: vec![], |
| 94 | + args: vec![ |
| 95 | + ArgWithTypeJSON { |
| 96 | + arg_type: "address".to_string(), |
| 97 | + value: json!(param_address), |
| 98 | + }, |
| 99 | + ArgWithTypeJSON { |
| 100 | + arg_type: "address".to_string(), |
| 101 | + value: json!(protocol_address), |
| 102 | + }, |
| 103 | + ArgWithTypeJSON { |
| 104 | + arg_type: "hex".to_string(), |
| 105 | + value: json!(instances), |
| 106 | + }, |
| 107 | + ArgWithTypeJSON { |
| 108 | + arg_type: "hex".to_string(), |
| 109 | + value: json!(HexEncodedBytes(proof.clone()).to_string()), |
| 110 | + }, |
| 111 | + ArgWithTypeJSON { |
| 112 | + arg_type: "u8".to_string(), |
| 113 | + value: json!(proof_kzg_variant), |
| 114 | + }, |
| 115 | + ], |
| 116 | + }; |
| 117 | + |
| 118 | + json |
| 119 | +} |
| 120 | + |
| 121 | +/// Native verifier related APIs |
| 122 | +/// Build publish serialized kzg params transaction payload for aptos. |
| 123 | +pub fn build_publish_params_transaction_payload( |
| 124 | + params: &ParamsKZG<Bn256>, |
| 125 | + params_store_address: String, |
| 126 | +) -> Result<EntryFunctionArgumentsJSON, Error> { |
| 127 | + let params_bytes = serialize_kzg_params(¶ms.verifier_params()) |
| 128 | + .map_err(|e| Error::msg(format!("serialize kzg params failed: {}", e)))?; |
| 129 | + let json = EntryFunctionArgumentsJSON { |
| 130 | + function_id: format!( |
| 131 | + "{}::{}::{}", |
| 132 | + params_store_address, PARAMS_MODULE, PUBLISH_PARAMS_FUNC |
| 133 | + ), |
| 134 | + type_args: vec![], |
| 135 | + args: vec![ArgWithTypeJSON { |
| 136 | + arg_type: "hex".to_string(), |
| 137 | + value: json!(HexEncodedBytes(params_bytes).to_string()), |
| 138 | + }], |
| 139 | + }; |
| 140 | + Ok(json) |
| 141 | +} |
| 142 | + |
| 143 | +/// Native verifier related APIs |
| 144 | +/// Build publish serialized vk transaction payload for aptos. |
| 145 | +pub fn build_publish_vk_transaction_payload( |
| 146 | + vk: &VerifyingKey<G1Affine>, |
| 147 | + vk_store_address: String, |
| 148 | +) -> EntryFunctionArgumentsJSON { |
| 149 | + let vk_bytes = vk.to_bytes(SerdeFormat::RawBytes); |
| 150 | + let json = EntryFunctionArgumentsJSON { |
| 151 | + function_id: format!("{}::{}::{}", vk_store_address, VK_MODULE, PUBLISH_VK_FUNC), |
| 152 | + type_args: vec![], |
| 153 | + args: vec![ArgWithTypeJSON { |
| 154 | + arg_type: "hex".to_string(), |
| 155 | + value: json!(HexEncodedBytes(vk_bytes).to_string()), |
| 156 | + }], |
| 157 | + }; |
| 158 | + json |
| 159 | +} |
| 160 | + |
| 161 | +/// Native verifier related APIs |
| 162 | +/// Build publish serialized circuit transaction payload for aptos. |
| 163 | +pub fn build_publish_circuit_transaction_payload<C, P, ConcreteCircuit>( |
| 164 | + params: &P, |
| 165 | + circuit: &ConcreteCircuit, |
| 166 | + vk_store_address: String, |
| 167 | +) -> Result<EntryFunctionArgumentsJSON, Error> |
| 168 | +where |
| 169 | + C: CurveAffine, |
| 170 | + P: Params<C>, |
| 171 | + ConcreteCircuit: Circuit<C::Scalar>, |
| 172 | + C::Scalar: FromUniformBytes<64>, |
| 173 | + C::ScalarExt: FromUniformBytes<64>, |
| 174 | +{ |
| 175 | + let circuit_bytes = generate_serialized_circuit(params, circuit) |
| 176 | + .map_err(|e| Error::msg(format!("generate serialized circuit failed: {}", e)))?; |
| 177 | + |
| 178 | + let json = EntryFunctionArgumentsJSON { |
| 179 | + function_id: format!( |
| 180 | + "{}::{}::{}", |
| 181 | + vk_store_address, VK_MODULE, PUBLISH_CIRCUIT_FUNC |
| 182 | + ), |
| 183 | + type_args: vec![], |
| 184 | + args: vec![ArgWithTypeJSON { |
| 185 | + arg_type: "hex".to_string(), |
| 186 | + value: json!(HexEncodedBytes(circuit_bytes).to_string()), |
| 187 | + }], |
| 188 | + }; |
| 189 | + Ok(json) |
| 190 | +} |
0 commit comments