diff --git a/rln/Cargo.toml b/rln/Cargo.toml index 6d119e1..c89daa9 100644 --- a/rln/Cargo.toml +++ b/rln/Cargo.toml @@ -13,14 +13,14 @@ doctest = false [dependencies] # ZKP Generation -ark-ec = { version = "=0.3.0", default-features = false } -ark-ff = { version = "=0.3.0", default-features = false, features = [ "asm"] } -ark-std = { version = "=0.3.0", default-features = false } -ark-bn254 = { version = "=0.3.0" } -ark-groth16 = { git = "https://github.com/arkworks-rs/groth16", rev = "765817f", default-features = false } -ark-relations = { version = "=0.3.0", default-features = false, features = [ "std" ] } -ark-serialize = { version = "=0.3.0", default-features = false } -ark-circom = { git = "https://github.com/vacp2p/ark-circom", rev = "0e587145cb05e08b2d1a01509eb578670088eb2f", default-features = false, features = ["circom-2"] } +ark-ec = { version = "=0.4.1", default-features = false } +ark-ff = { version = "=0.4.1", default-features = false, features = [ "asm"] } +ark-std = { version = "=0.4.0", default-features = false } +ark-bn254 = { version = "=0.4.0" } +ark-groth16 = { version = "=0.4.0", features = ["parallel"], default-features = false } +ark-relations = { version = "=0.4.0", default-features = false, features = [ "std" ] } +ark-serialize = { version = "=0.4.1", default-features = false } +ark-circom = { git = "https://github.com/gakonst/ark-circom", default-features = false, features = ["circom-2"] } # WASM wasmer = { version = "2.3.0", default-features = false } diff --git a/rln/src/protocol.rs b/rln/src/protocol.rs index 9524461..1bd1cfb 100644 --- a/rln/src/protocol.rs +++ b/rln/src/protocol.rs @@ -1,10 +1,7 @@ // This crate collects all the underlying primitives used to implement RLN use ark_circom::{CircomReduction, WitnessCalculator}; -use ark_groth16::{ - create_proof_with_reduction_and_matrices, prepare_verifying_key, - verify_proof as ark_verify_proof, Proof as ArkProof, ProvingKey, VerifyingKey, -}; +use ark_groth16::{prepare_verifying_key, Groth16, Proof as ArkProof, ProvingKey, VerifyingKey}; use ark_relations::r1cs::ConstraintMatrices; use ark_relations::r1cs::SynthesisError; use ark_std::{rand::thread_rng, UniformRand}; @@ -541,9 +538,11 @@ pub enum ProofError { SynthesisError(#[from] SynthesisError), } -fn calculate_witness_element(witness: Vec) -> Result> { - use ark_ff::{FpParameters, PrimeField}; - let modulus = <::Params as FpParameters>::MODULUS; +fn calculate_witness_element( + witness: Vec, +) -> Result> { + use ark_ff::PrimeField; + let modulus = ::MODULUS; // convert it to field elements use num_traits::Signed; @@ -558,7 +557,7 @@ fn calculate_witness_element(witness: Vec) -> } else { w.to_biguint().ok_or(Report::msg("not a biguint value"))? }; - witness_vec.push(E::Fr::from(w)) + witness_vec.push(E::ScalarField::from(w)) } Ok(witness_vec) @@ -587,7 +586,7 @@ pub fn generate_proof_with_witness( #[cfg(debug_assertions)] let now = Instant::now(); - let proof = create_proof_with_reduction_and_matrices::<_, CircomReduction>( + let proof = Groth16::<_, CircomReduction>::create_proof_with_reduction_and_matrices( &proving_key.0, r, s, @@ -681,7 +680,7 @@ pub fn generate_proof( #[cfg(debug_assertions)] let now = Instant::now(); - let proof = create_proof_with_reduction_and_matrices::<_, CircomReduction>( + let proof = Groth16::<_, CircomReduction>::create_proof_with_reduction_and_matrices( &proving_key.0, r, s, @@ -726,7 +725,7 @@ pub fn verify_proof( #[cfg(debug_assertions)] let now = Instant::now(); - let verified = ark_verify_proof(&pvk, proof, &inputs)?; + let verified = Groth16::<_, CircomReduction>::verify_proof(&pvk, proof, &inputs)?; #[cfg(debug_assertions)] println!("verify took: {:.2?}", now.elapsed()); diff --git a/rln/src/public.rs b/rln/src/public.rs index 8f8c968..421aec9 100644 --- a/rln/src/public.rs +++ b/rln/src/public.rs @@ -13,6 +13,7 @@ use cfg_if::cfg_if; use color_eyre::Result; use num_bigint::BigInt; use std::io::Cursor; +// use rkyv::Deserialize; cfg_if! { if #[cfg(not(target_arch = "wasm32"))] { @@ -407,7 +408,7 @@ impl RLN<'_> { mut input_data: R, mut output_data: W, ) -> Result<()> { - // We read input RLN witness and we deserialize it + // We read input RLN witness and we serialize_compressed it let mut serialized: Vec = Vec::new(); input_data.read_to_end(&mut serialized)?; let (rln_witness, _) = deserialize_witness(&serialized)?; @@ -421,7 +422,7 @@ impl RLN<'_> { let proof = generate_proof(self.witness_calculator, &self.proving_key, &rln_witness)?; // Note: we export a serialization of ark-groth16::Proof not semaphore::Proof - proof.serialize(&mut output_data)?; + proof.serialize_compressed(&mut output_data)?; Ok(()) } @@ -467,7 +468,7 @@ impl RLN<'_> { // [ proof<128> | root<32> | epoch<32> | share_x<32> | share_y<32> | nullifier<32> | rln_identifier<32> ] let mut input_byte: Vec = Vec::new(); input_data.read_to_end(&mut input_byte)?; - let proof = ArkProof::deserialize(&mut Cursor::new(&input_byte[..128]))?; + let proof = ArkProof::deserialize_compressed(&mut Cursor::new(&input_byte[..128]))?; let (proof_values, _) = deserialize_proof_values(&input_byte[128..]); @@ -526,7 +527,7 @@ impl RLN<'_> { mut input_data: R, mut output_data: W, ) -> Result<()> { - // We read input RLN witness and we deserialize it + // We read input RLN witness and we serialize_compressed it let mut witness_byte: Vec = Vec::new(); input_data.read_to_end(&mut witness_byte)?; let (rln_witness, _) = proof_inputs_to_rln_witness(&mut self.tree, &witness_byte)?; @@ -536,7 +537,7 @@ impl RLN<'_> { // Note: we export a serialization of ark-groth16::Proof not semaphore::Proof // This proof is compressed, i.e. 128 bytes long - proof.serialize(&mut output_data)?; + proof.serialize_compressed(&mut output_data)?; output_data.write_all(&serialize_proof_values(&proof_values))?; Ok(()) @@ -561,7 +562,7 @@ impl RLN<'_> { // Note: we export a serialization of ark-groth16::Proof not semaphore::Proof // This proof is compressed, i.e. 128 bytes long - proof.serialize(&mut output_data)?; + proof.serialize_compressed(&mut output_data)?; output_data.write_all(&serialize_proof_values(&proof_values))?; Ok(()) } @@ -597,7 +598,8 @@ impl RLN<'_> { let mut serialized: Vec = Vec::new(); input_data.read_to_end(&mut serialized)?; let mut all_read = 0; - let proof = ArkProof::deserialize(&mut Cursor::new(&serialized[..128].to_vec()))?; + let proof = + ArkProof::deserialize_compressed(&mut Cursor::new(&serialized[..128].to_vec()))?; all_read += 128; let (proof_values, read) = deserialize_proof_values(&serialized[all_read..]); all_read += read; @@ -672,7 +674,8 @@ impl RLN<'_> { let mut serialized: Vec = Vec::new(); input_data.read_to_end(&mut serialized)?; let mut all_read = 0; - let proof = ArkProof::deserialize(&mut Cursor::new(&serialized[..128].to_vec()))?; + let proof = + ArkProof::deserialize_compressed(&mut Cursor::new(&serialized[..128].to_vec()))?; all_read += 128; let (proof_values, read) = deserialize_proof_values(&serialized[all_read..]); all_read += read; @@ -745,7 +748,7 @@ impl RLN<'_> { /// let mut buffer = Cursor::new(Vec::::new()); /// rln.key_gen(&mut buffer).unwrap(); /// - /// // We deserialize the keygen output + /// // We serialize_compressed the keygen output /// let (identity_secret_hash, id_commitment) = deserialize_identity_pair(buffer.into_inner()); /// ``` pub fn key_gen(&self, mut output_data: W) -> Result<()> { @@ -775,7 +778,7 @@ impl RLN<'_> { /// let mut buffer = Cursor::new(Vec::::new()); /// rln.extended_key_gen(&mut buffer).unwrap(); /// - /// // We deserialize the keygen output + /// // We serialize_compressed the keygen output /// let (identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment) = deserialize_identity_tuple(buffer.into_inner()); /// ``` pub fn extended_key_gen(&self, mut output_data: W) -> Result<()> { @@ -810,7 +813,7 @@ impl RLN<'_> { /// rln.seeded_key_gen(&mut input_buffer, &mut output_buffer) /// .unwrap(); /// - /// // We deserialize the keygen output + /// // We serialize_compressed the keygen output /// let (identity_secret_hash, id_commitment) = deserialize_identity_pair(output_buffer.into_inner()); /// ``` pub fn seeded_key_gen( @@ -853,7 +856,7 @@ impl RLN<'_> { /// rln.seeded_key_gen(&mut input_buffer, &mut output_buffer) /// .unwrap(); /// - /// // We deserialize the keygen output + /// // We serialize_compressed the keygen output /// let (identity_trapdoor, identity_nullifier, identity_secret_hash, id_commitment) = deserialize_identity_tuple(buffer.into_inner()); /// ``` pub fn seeded_extended_key_gen( @@ -912,7 +915,7 @@ impl RLN<'_> { mut input_proof_data_2: R, mut output_data: W, ) -> Result<()> { - // We deserialize the two proofs and we get the corresponding RLNProofValues objects + // We serialize_compressed the two proofs and we get the corresponding RLNProofValues objects let mut serialized: Vec = Vec::new(); input_proof_data_1.read_to_end(&mut serialized)?; // We skip deserialization of the zk-proof at the beginning @@ -956,7 +959,7 @@ impl RLN<'_> { /// /// The function returns the corresponding [`RLNWitnessInput`](crate::protocol::RLNWitnessInput) object serialized using [`rln::protocol::serialize_witness`](crate::protocol::serialize_witness)). pub fn get_serialized_rln_witness(&mut self, mut input_data: R) -> Result> { - // We read input RLN witness and we deserialize it + // We read input RLN witness and we serialize_compressed it let mut witness_byte: Vec = Vec::new(); input_data.read_to_end(&mut witness_byte)?; let (rln_witness, _) = proof_inputs_to_rln_witness(&mut self.tree, &witness_byte)?; @@ -1004,7 +1007,7 @@ impl Default for RLN<'_> { /// hash(&mut input_buffer, &mut output_buffer) /// .unwrap(); /// -/// // We deserialize the keygen output +/// // We serialize_compressed the keygen output /// let field_element = deserialize_field_element(output_buffer.into_inner()); /// ``` pub fn hash(mut input_data: R, mut output_data: W) -> Result<()> { @@ -1037,7 +1040,7 @@ pub fn hash(mut input_data: R, mut output_data: W) -> Result< /// poseidon_hash(&mut input_buffer, &mut output_buffer) /// .unwrap(); /// -/// // We deserialize the hash output +/// // We serialize_compressed the hash output /// let hash_result = deserialize_field_element(output_buffer.into_inner()); /// ``` pub fn poseidon_hash(mut input_data: R, mut output_data: W) -> Result<()> { @@ -1056,6 +1059,7 @@ mod test { use super::*; use ark_std::{rand::thread_rng, UniformRand}; use rand::Rng; + // use rkyv::Deserialize; #[test] // We test merkle batch Merkle tree additions @@ -1280,7 +1284,7 @@ mod test { let serialized_proof = output_buffer.into_inner(); // Before checking public verify API, we check that the (deserialized) proof generated by prove is actually valid - let proof = ArkProof::deserialize(&mut Cursor::new(&serialized_proof)).unwrap(); + let proof = ArkProof::deserialize_compressed(&mut Cursor::new(&serialized_proof)).unwrap(); let verified = verify_proof(&rln.verification_key, &proof, &proof_values); assert!(verified.unwrap()); @@ -1407,7 +1411,7 @@ mod test { let mut input_buffer = Cursor::new(serialized); - // We read input RLN witness and we deserialize it + // We read input RLN witness and we serialize_compressed it let mut witness_byte: Vec = Vec::new(); input_buffer.read_to_end(&mut witness_byte).unwrap(); let (rln_witness, _) = proof_inputs_to_rln_witness(&mut rln.tree, &witness_byte).unwrap(); diff --git a/rln/src/utils.rs b/rln/src/utils.rs index 804414a..1108a28 100644 --- a/rln/src/utils.rs +++ b/rln/src/utils.rs @@ -13,8 +13,8 @@ pub fn to_bigint(el: &Fr) -> Result { } pub fn fr_byte_size() -> usize { - let mbs = ::size_in_bits(); - (mbs + 64 - (mbs % 64)) / 8 + let mbs = ::MODULUS_BIT_SIZE; + ((mbs + 64 - (mbs % 64)) / 8) as usize } pub fn str_to_fr(input: &str, radix: u32) -> Result { diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 17c3442..8560037 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -5,12 +5,12 @@ edition = "2021" license = "MIT OR Apache-2.0" [dependencies] -ark-ff = { version = "=0.3.0", default-features = false, features = ["asm"] } +ark-ff = { version = "=0.4.1", default-features = false, features = ["asm"] } num-bigint = { version = "=0.4.3", default-features = false, features = ["rand"] } color-eyre = "=0.6.2" [dev-dependencies] -ark-bn254 = "=0.3.0" +ark-bn254 = "=0.4.0" num-traits = "0.2.11" hex-literal = "0.3.4" tiny-keccak = { version = "2.0.2", features = ["keccak"] } diff --git a/utils/src/poseidon/poseidon_constants.rs b/utils/src/poseidon/poseidon_constants.rs index 1bbda7c..679dfdc 100644 --- a/utils/src/poseidon/poseidon_constants.rs +++ b/utils/src/poseidon/poseidon_constants.rs @@ -11,7 +11,7 @@ #![allow(dead_code)] -use ark_ff::{FpParameters, PrimeField}; +use ark_ff::PrimeField; use num_bigint::BigUint; pub struct PoseidonGrainLFSR { @@ -129,8 +129,8 @@ impl PoseidonGrainLFSR { &mut self, num_elems: usize, ) -> Vec { - assert_eq!(F::Params::MODULUS_BITS as u64, self.prime_num_bits); - let modulus: BigUint = F::Params::MODULUS.into(); + assert_eq!(F::MODULUS_BIT_SIZE as u64, self.prime_num_bits); + let modulus: BigUint = F::MODULUS.into(); let mut res = Vec::new(); for _ in 0..num_elems { @@ -163,7 +163,7 @@ impl PoseidonGrainLFSR { } pub fn get_field_elements_mod_p(&mut self, num_elems: usize) -> Vec { - assert_eq!(F::Params::MODULUS_BITS as u64, self.prime_num_bits); + assert_eq!(F::MODULUS_BIT_SIZE as u64, self.prime_num_bits); let mut res = Vec::new(); for _ in 0..num_elems { diff --git a/utils/src/poseidon/poseidon_hash.rs b/utils/src/poseidon/poseidon_hash.rs index 9104e22..802c5db 100644 --- a/utils/src/poseidon/poseidon_hash.rs +++ b/utils/src/poseidon/poseidon_hash.rs @@ -4,7 +4,7 @@ // and adapted to work over arkworks field traits and custom data structures use crate::poseidon_constants::find_poseidon_ark_and_mds; -use ark_ff::{FpParameters, PrimeField}; +use ark_ff::PrimeField; #[derive(Debug, Clone, PartialEq, Eq)] pub struct RoundParamenters { @@ -32,7 +32,7 @@ impl Poseidon { let (ark, mds) = find_poseidon_ark_and_mds::( 1, // is_field = 1 0, // is_sbox_inverse = 0 - F::Params::MODULUS_BITS as u64, + F::MODULUS_BIT_SIZE as u64, t, n_rounds_f as u64, n_rounds_p as u64,