fix storer test

This commit is contained in:
Dmitriy Ryajov 2023-03-27 18:38:42 -06:00
parent 82bf73592d
commit c6a09293e7
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4
2 changed files with 81 additions and 74 deletions

View File

@ -1,25 +1,34 @@
pub mod utils;
use ark_bn254::Bn254;
use ark_circom::{CircomBuilder, CircomConfig};
use ark_groth16::{
#[cfg(test)]
mod test {
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;
};
use ark_std::rand::{distributions::Alphanumeric, rngs::ThreadRng, Rng};
use ruint::aliases::U256;
pub struct CircuitsTests {
use crate::{
circuit_tests::utils::{digest, merkelize},
poseidon::hash,
storage_proofs::StorageProofs,
};
pub struct CircuitsTests {
builder: CircomBuilder<Bn254>,
params: ProvingKey<Bn254>,
rng: ThreadRng,
}
}
impl CircuitsTests {
impl CircuitsTests {
pub fn new(wtns: String, r1cs: String) -> CircuitsTests {
let mut rng = ThreadRng::default();
let builder = CircomBuilder::new(CircomConfig::<Bn254>::new(wtns, r1cs).unwrap());
let params = generate_random_parameters::<Bn254, _, _>(builder.setup(), &mut rng).unwrap();
let params =
generate_random_parameters::<Bn254, _, _>(builder.setup(), &mut rng).unwrap();
CircuitsTests {
builder,
@ -28,7 +37,7 @@ impl CircuitsTests {
}
}
pub fn poseidon_hash(&mut self, elements: &[U256], hash: U256) {
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));
@ -39,11 +48,10 @@ impl CircuitsTests {
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());
verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok()
}
pub fn poseidon_digest(&mut self, elements: &[U256], hash: U256) {
pub fn poseidon_digest(&mut self, elements: &[U256], hash: U256) -> bool {
let mut builder = self.builder.clone();
elements
@ -53,22 +61,14 @@ impl CircuitsTests {
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());
verify_proof_with_prepared_inputs(&vk, &proof, &public_inputs).is_ok()
}
}
}
#[cfg(test)]
mod test {
use super::CircuitsTests;
use crate::{
circuit_tests::utils::digest, circuit_tests::utils::merkelize, poseidon::hash,
storage_proofs::StorageProofs,
};
use ruint::aliases::U256;
#[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<U256> = (0..256).map(|_| U256::from(1)).collect();
hasher.poseidon_digest(&input, digest(&input, Some(16)));
let input: Vec<U256> = (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<U256> = (0..256).map(|_| U256::from(1)).collect();
let rng = ThreadRng::default();
let preimages: Vec<U256> = 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();

View File

@ -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;