mirror of
https://github.com/logos-storage/logos-storage-proofs.git
synced 2026-01-05 23:13:05 +00:00
fix storer test
This commit is contained in:
parent
82bf73592d
commit
c6a09293e7
@ -1,74 +1,74 @@
|
|||||||
pub mod utils;
|
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<Bn254>,
|
|
||||||
params: ProvingKey<Bn254>,
|
|
||||||
rng: ThreadRng,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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)]
|
#[cfg(test)]
|
||||||
mod 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::{
|
use crate::{
|
||||||
circuit_tests::utils::digest, circuit_tests::utils::merkelize, poseidon::hash,
|
circuit_tests::utils::{digest, merkelize},
|
||||||
|
poseidon::hash,
|
||||||
storage_proofs::StorageProofs,
|
storage_proofs::StorageProofs,
|
||||||
};
|
};
|
||||||
use ruint::aliases::U256;
|
|
||||||
|
pub struct CircuitsTests {
|
||||||
|
builder: CircomBuilder<Bn254>,
|
||||||
|
params: ProvingKey<Bn254>,
|
||||||
|
rng: ThreadRng,
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
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]
|
#[test]
|
||||||
fn test_poseidon_hash() {
|
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 wasm = "./src/circuit_tests/artifacts/poseidon-hash-test_js/poseidon-hash-test.wasm";
|
||||||
|
|
||||||
let mut hasher = CircuitsTests::new(wasm.to_string(), r1cs.to_string());
|
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]
|
#[test]
|
||||||
@ -86,17 +86,26 @@ mod test {
|
|||||||
"./src/circuit_tests/artifacts/poseidon-digest-test_js/poseidon-digest-test.wasm";
|
"./src/circuit_tests/artifacts/poseidon-digest-test_js/poseidon-digest-test.wasm";
|
||||||
|
|
||||||
let mut hasher = CircuitsTests::new(wasm.to_string(), r1cs.to_string());
|
let mut hasher = CircuitsTests::new(wasm.to_string(), r1cs.to_string());
|
||||||
let input: Vec<U256> = (0..256).map(|_| U256::from(1)).collect();
|
let input: Vec<U256> = (0..256).map(|c| U256::from(c)).collect();
|
||||||
hasher.poseidon_digest(&input, digest(&input, Some(16)));
|
assert!(hasher.poseidon_digest(&input, digest(&input, Some(16))));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_storer() {
|
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
|
// 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
|
// and hash is the hash of each vector generated using the digest function
|
||||||
let data = (0..4)
|
let data = (0..4)
|
||||||
.map(|_| {
|
.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));
|
let hash = digest(&preimages, Some(16));
|
||||||
(preimages, hash)
|
(preimages, hash)
|
||||||
})
|
})
|
||||||
@ -112,7 +121,7 @@ mod test {
|
|||||||
let siblings = &[
|
let siblings = &[
|
||||||
hashes[1],
|
hashes[1],
|
||||||
parent_hash_r,
|
parent_hash_r,
|
||||||
hashes[1],
|
hashes[0],
|
||||||
parent_hash_r,
|
parent_hash_r,
|
||||||
hashes[3],
|
hashes[3],
|
||||||
parent_hash_l,
|
parent_hash_l,
|
||||||
@ -120,9 +129,8 @@ mod test {
|
|||||||
parent_hash_l,
|
parent_hash_l,
|
||||||
];
|
];
|
||||||
|
|
||||||
let r1cs = "./src/circuit_tests/artifacts/storer-test.r1cs";
|
println!("hashes: {:?}", hashes);
|
||||||
let wasm = "./src/circuit_tests/artifacts/storer-test_js/storer-test.wasm";
|
println!("siblings: {:?}", siblings);
|
||||||
let mut prover = StorageProofs::new(wasm.to_string(), r1cs.to_string(), None);
|
|
||||||
|
|
||||||
let root = merkelize(hashes.as_slice());
|
let root = merkelize(hashes.as_slice());
|
||||||
let proof_bytes = &mut Vec::new();
|
let proof_bytes = &mut Vec::new();
|
||||||
|
|||||||
@ -37,8 +37,7 @@ pub fn merkelize(leafs: &[U256]) -> U256 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if merkle.len() % 2 == 1 {
|
if merkle.len() % 2 == 1 {
|
||||||
new_merkle
|
new_merkle.push(hash(&[merkle[merkle.len() - 2], merkle[merkle.len() - 2]]));
|
||||||
.push(hash(&[merkle[merkle.len() - 2], merkle[merkle.len() - 2]]));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
merkle = new_merkle;
|
merkle = new_merkle;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user