diff --git a/src/circuit_tests/mod.rs b/src/circuit_tests/mod.rs index 9f54895..f37f905 100644 --- a/src/circuit_tests/mod.rs +++ b/src/circuit_tests/mod.rs @@ -1,74 +1,74 @@ pub mod utils; -use ark_bn254::Bn254; -use ark_circom::{CircomBuilder, CircomConfig}; -use ark_groth16::{ - create_random_proof as prove, generate_random_parameters, prepare_inputs, - prepare_verifying_key, verify_proof_with_prepared_inputs, ProvingKey, -}; -use ark_std::rand::rngs::ThreadRng; -use ruint::aliases::U256; - -pub struct CircuitsTests { - builder: CircomBuilder, - params: ProvingKey, - rng: ThreadRng, -} - -impl CircuitsTests { - pub fn new(wtns: String, r1cs: String) -> CircuitsTests { - let mut rng = ThreadRng::default(); - let builder = CircomBuilder::new(CircomConfig::::new(wtns, r1cs).unwrap()); - let params = generate_random_parameters::(builder.setup(), &mut rng).unwrap(); - - CircuitsTests { - builder, - params, - rng, - } - } - - pub fn poseidon_hash(&mut self, elements: &[U256], hash: U256) { - let mut builder = self.builder.clone(); - - elements.iter().for_each(|c| builder.push_input("in", *c)); - builder.push_input("hash", hash); - - let circuit = builder.build().unwrap(); - let inputs = circuit.get_public_inputs().unwrap(); - let proof = prove(circuit, &self.params, &mut self.rng).unwrap(); - let vk = prepare_verifying_key(&self.params.vk); - let public_inputs = prepare_inputs(&vk, &inputs).unwrap(); - - assert!(verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok()); - } - - pub fn poseidon_digest(&mut self, elements: &[U256], hash: U256) { - let mut builder = self.builder.clone(); - - elements - .iter() - .for_each(|c| builder.push_input("block", *c)); - builder.push_input("hash", hash); - - let circuit = builder.build().unwrap(); - let inputs = circuit.get_public_inputs().unwrap(); - let proof = prove(circuit, &self.params, &mut self.rng).unwrap(); - let vk = prepare_verifying_key(&self.params.vk); - let public_inputs = prepare_inputs(&vk, &inputs).unwrap(); - - assert!(verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok()); - } -} - #[cfg(test)] mod test { - use super::CircuitsTests; + use ark_bn254::Bn254; + use ark_circom::{CircomBuilder, CircomConfig}; + use ark_groth16::{ + create_random_proof as prove, generate_random_parameters, prepare_inputs, + prepare_verifying_key, verify_proof_with_prepared_inputs, ProvingKey, + }; + use ark_std::rand::{distributions::Alphanumeric, rngs::ThreadRng, Rng}; + use ruint::aliases::U256; + use crate::{ - circuit_tests::utils::digest, circuit_tests::utils::merkelize, poseidon::hash, + circuit_tests::utils::{digest, merkelize}, + poseidon::hash, storage_proofs::StorageProofs, }; - use ruint::aliases::U256; + + pub struct CircuitsTests { + builder: CircomBuilder, + params: ProvingKey, + rng: ThreadRng, + } + + impl CircuitsTests { + pub fn new(wtns: String, r1cs: String) -> CircuitsTests { + let mut rng = ThreadRng::default(); + let builder = CircomBuilder::new(CircomConfig::::new(wtns, r1cs).unwrap()); + let params = + generate_random_parameters::(builder.setup(), &mut rng).unwrap(); + + CircuitsTests { + builder, + params, + rng, + } + } + + pub fn poseidon_hash(&mut self, elements: &[U256], hash: U256) -> bool { + let mut builder = self.builder.clone(); + + elements.iter().for_each(|c| builder.push_input("in", *c)); + builder.push_input("hash", hash); + + let circuit = builder.build().unwrap(); + let inputs = circuit.get_public_inputs().unwrap(); + let proof = prove(circuit, &self.params, &mut self.rng).unwrap(); + let vk = prepare_verifying_key(&self.params.vk); + let public_inputs = prepare_inputs(&vk, &inputs).unwrap(); + verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok() + } + + pub fn poseidon_digest(&mut self, elements: &[U256], hash: U256) -> bool { + let mut builder = self.builder.clone(); + + elements + .iter() + .for_each(|c| builder.push_input("block", *c)); + builder.push_input("hash", hash); + + let circuit = builder.build().unwrap(); + let inputs = circuit.get_public_inputs().unwrap(); + + let proof = prove(circuit, &self.params, &mut self.rng).unwrap(); + let vk = prepare_verifying_key(&self.params.vk); + let public_inputs = prepare_inputs(&vk, &inputs).unwrap(); + + verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok() + } + } #[test] fn test_poseidon_hash() { @@ -76,7 +76,7 @@ mod test { let wasm = "./src/circuit_tests/artifacts/poseidon-hash-test_js/poseidon-hash-test.wasm"; let mut hasher = CircuitsTests::new(wasm.to_string(), r1cs.to_string()); - hasher.poseidon_hash(&[U256::from(1)], hash(&[U256::from(1)])); + assert!(hasher.poseidon_hash(&[U256::from(1)], hash(&[U256::from(1)]))); } #[test] @@ -86,17 +86,26 @@ mod test { "./src/circuit_tests/artifacts/poseidon-digest-test_js/poseidon-digest-test.wasm"; let mut hasher = CircuitsTests::new(wasm.to_string(), r1cs.to_string()); - let input: Vec = (0..256).map(|_| U256::from(1)).collect(); - hasher.poseidon_digest(&input, digest(&input, Some(16))); + let input: Vec = (0..256).map(|c| U256::from(c)).collect(); + assert!(hasher.poseidon_digest(&input, digest(&input, Some(16)))); } #[test] fn test_storer() { + let r1cs = "./src/circuit_tests/artifacts/storer-test.r1cs"; + let wasm = "./src/circuit_tests/artifacts/storer-test_js/storer-test.wasm"; + let mut prover = StorageProofs::new(wasm.to_string(), r1cs.to_string(), None); + // generate a tuple of (preimages, hash), where preimages is a vector of 256 U256s // and hash is the hash of each vector generated using the digest function let data = (0..4) .map(|_| { - let preimages: Vec = (0..256).map(|_| U256::from(1)).collect(); + let rng = ThreadRng::default(); + let preimages: Vec = rng + .sample_iter(Alphanumeric) + .take(256) + .map(|c| U256::from(c)) + .collect(); let hash = digest(&preimages, Some(16)); (preimages, hash) }) @@ -112,7 +121,7 @@ mod test { let siblings = &[ hashes[1], parent_hash_r, - hashes[1], + hashes[0], parent_hash_r, hashes[3], parent_hash_l, @@ -120,9 +129,8 @@ mod test { parent_hash_l, ]; - let r1cs = "./src/circuit_tests/artifacts/storer-test.r1cs"; - let wasm = "./src/circuit_tests/artifacts/storer-test_js/storer-test.wasm"; - let mut prover = StorageProofs::new(wasm.to_string(), r1cs.to_string(), None); + println!("hashes: {:?}", hashes); + println!("siblings: {:?}", siblings); let root = merkelize(hashes.as_slice()); let proof_bytes = &mut Vec::new(); diff --git a/src/circuit_tests/utils.rs b/src/circuit_tests/utils.rs index a8faf69..4899b51 100644 --- a/src/circuit_tests/utils.rs +++ b/src/circuit_tests/utils.rs @@ -37,8 +37,7 @@ pub fn merkelize(leafs: &[U256]) -> U256 { } if merkle.len() % 2 == 1 { - new_merkle - .push(hash(&[merkle[merkle.len() - 2], merkle[merkle.len() - 2]])); + new_merkle.push(hash(&[merkle[merkle.len() - 2], merkle[merkle.len() - 2]])); } merkle = new_merkle;