mirror of https://github.com/vacp2p/zerokit.git
leave our fork of `ark-circom` (#132)
* leave our fork of `ark-circom` --------- Co-authored-by: tyshkor <tyshko1@gmail.com>
This commit is contained in:
parent
9045e31006
commit
8cd4baba8a
|
@ -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 }
|
||||
|
|
|
@ -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<E: ark_ec::PairingEngine>(witness: Vec<BigInt>) -> Result<Vec<E::Fr>> {
|
||||
use ark_ff::{FpParameters, PrimeField};
|
||||
let modulus = <<E::Fr as PrimeField>::Params as FpParameters>::MODULUS;
|
||||
fn calculate_witness_element<E: ark_ec::pairing::Pairing>(
|
||||
witness: Vec<BigInt>,
|
||||
) -> Result<Vec<E::ScalarField>> {
|
||||
use ark_ff::PrimeField;
|
||||
let modulus = <E::ScalarField as PrimeField>::MODULUS;
|
||||
|
||||
// convert it to field elements
|
||||
use num_traits::Signed;
|
||||
|
@ -558,7 +557,7 @@ fn calculate_witness_element<E: ark_ec::PairingEngine>(witness: Vec<BigInt>) ->
|
|||
} 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());
|
||||
|
|
|
@ -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<u8> = 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<u8> = 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<u8> = 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<u8> = 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<u8> = 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::<u8>::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<W: Write>(&self, mut output_data: W) -> Result<()> {
|
||||
|
@ -775,7 +778,7 @@ impl RLN<'_> {
|
|||
/// let mut buffer = Cursor::new(Vec::<u8>::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<W: Write>(&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<R: Read, W: Write>(
|
||||
|
@ -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<R: Read, W: Write>(
|
||||
|
@ -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<u8> = 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<R: Read>(&mut self, mut input_data: R) -> Result<Vec<u8>> {
|
||||
// We read input RLN witness and we deserialize it
|
||||
// We read input RLN witness and we serialize_compressed it
|
||||
let mut witness_byte: Vec<u8> = 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<R: Read, W: Write>(mut input_data: R, mut output_data: W) -> Result<()> {
|
||||
|
@ -1037,7 +1040,7 @@ pub fn hash<R: Read, W: Write>(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<R: Read, W: Write>(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<u8> = 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();
|
||||
|
|
|
@ -13,8 +13,8 @@ pub fn to_bigint(el: &Fr) -> Result<BigInt> {
|
|||
}
|
||||
|
||||
pub fn fr_byte_size() -> usize {
|
||||
let mbs = <Fr as PrimeField>::size_in_bits();
|
||||
(mbs + 64 - (mbs % 64)) / 8
|
||||
let mbs = <Fr as PrimeField>::MODULUS_BIT_SIZE;
|
||||
((mbs + 64 - (mbs % 64)) / 8) as usize
|
||||
}
|
||||
|
||||
pub fn str_to_fr(input: &str, radix: u32) -> Result<Fr> {
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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<F> {
|
||||
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<F: PrimeField>(&mut self, num_elems: usize) -> Vec<F> {
|
||||
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 {
|
||||
|
|
|
@ -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<F: PrimeField> {
|
||||
|
@ -32,7 +32,7 @@ impl<F: PrimeField> Poseidon<F> {
|
|||
let (ark, mds) = find_poseidon_ark_and_mds::<F>(
|
||||
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,
|
||||
|
|
Loading…
Reference in New Issue