storageproofs.rs to storage_proofs.rs
This commit is contained in:
parent
2ec658e6dc
commit
88d0a8c1a7
|
@ -12,22 +12,27 @@ use ruint::aliases::U256;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct StorageProofs<'a> {
|
pub struct StorageProofs {
|
||||||
builder: CircomBuilder<Bn254>,
|
builder: CircomBuilder<Bn254>,
|
||||||
params: ProvingKey<Bn254>,
|
params: ProvingKey<Bn254>,
|
||||||
rng: &'a ThreadRng,
|
rng: ThreadRng,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StorageProofs<'_> {
|
impl StorageProofs {
|
||||||
pub fn new(wtns: String, r1cs: String, zkey: Option<String>, rng: Option<&ThreadRng>) -> Self {
|
// TODO: add rng
|
||||||
let mut rng = rng.unwrap_or(&ThreadRng::default());
|
pub fn new(
|
||||||
|
wtns: String,
|
||||||
|
r1cs: String,
|
||||||
|
zkey: Option<String>, /* , rng: Option<ThreadRng> */
|
||||||
|
) -> Self {
|
||||||
|
let mut rng = ThreadRng::default();
|
||||||
let builder = CircomBuilder::new(CircomConfig::<Bn254>::new(wtns, r1cs).unwrap());
|
let builder = CircomBuilder::new(CircomConfig::<Bn254>::new(wtns, r1cs).unwrap());
|
||||||
let params: ProvingKey<Bn254> = match zkey {
|
let params: ProvingKey<Bn254> = match zkey {
|
||||||
Some(zkey) => {
|
Some(zkey) => {
|
||||||
let mut file = File::open(zkey).unwrap();
|
let mut file = File::open(zkey).unwrap();
|
||||||
read_zkey(&mut file).unwrap().0
|
read_zkey(&mut file).unwrap().0
|
||||||
}
|
}
|
||||||
None => generate_random_parameters::<Bn254, _, _>(builder.setup(), &mut *rng).unwrap(),
|
None => generate_random_parameters::<Bn254, _, _>(builder.setup(), &mut rng).unwrap(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
@ -39,10 +44,10 @@ impl StorageProofs<'_> {
|
||||||
|
|
||||||
pub fn prove(
|
pub fn prove(
|
||||||
&mut self,
|
&mut self,
|
||||||
chunks: Vec<Vec<U256>>,
|
chunks: &[U256],
|
||||||
siblings: Vec<Vec<U256>>,
|
siblings: &[U256],
|
||||||
hashes: Vec<U256>,
|
hashes: &[U256],
|
||||||
path: Vec<u32>,
|
path: &[i32],
|
||||||
root: U256,
|
root: U256,
|
||||||
salt: U256,
|
salt: U256,
|
||||||
proof_bytes: &mut Vec<u8>,
|
proof_bytes: &mut Vec<u8>,
|
||||||
|
@ -51,29 +56,23 @@ impl StorageProofs<'_> {
|
||||||
let mut builder = self.builder.clone();
|
let mut builder = self.builder.clone();
|
||||||
|
|
||||||
// vec of vecs is flattened, since wasm expects a contiguous array in memory
|
// vec of vecs is flattened, since wasm expects a contiguous array in memory
|
||||||
chunks
|
chunks.iter().for_each(|c| builder.push_input("chunks", *c));
|
||||||
.iter()
|
|
||||||
.flat_map(|c| c.into_iter())
|
|
||||||
.for_each(|c| builder.push_input("chunks", *c));
|
|
||||||
|
|
||||||
siblings
|
siblings
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|c| c.into_iter())
|
|
||||||
.for_each(|c| builder.push_input("siblings", *c));
|
.for_each(|c| builder.push_input("siblings", *c));
|
||||||
|
|
||||||
hashes.iter().for_each(|c| builder.push_input("hashes", *c));
|
hashes.iter().for_each(|c| builder.push_input("hashes", *c));
|
||||||
|
|
||||||
path.iter().for_each(|c| builder.push_input("path", *c));
|
path.iter().for_each(|c| builder.push_input("path", *c));
|
||||||
|
|
||||||
builder.push_input("root", root);
|
builder.push_input("root", root);
|
||||||
|
|
||||||
builder.push_input("salt", salt);
|
builder.push_input("salt", salt);
|
||||||
|
|
||||||
let circuit = builder.build().map_err(|e| e.to_string())?;
|
let circuit = builder.build().map_err(|e| e.to_string())?;
|
||||||
let inputs = circuit
|
let inputs = circuit
|
||||||
.get_public_inputs()
|
.get_public_inputs()
|
||||||
.ok_or("Unable to get public inputs!")?;
|
.ok_or("Unable to get public inputs!")?;
|
||||||
let proof = prove(circuit, &self.params, &*mut self.rng).map_err(|e| e.to_string())?;
|
let proof = prove(circuit, &self.params, &mut self.rng).map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
proof.serialize(proof_bytes).map_err(|e| e.to_string())?;
|
proof.serialize(proof_bytes).map_err(|e| e.to_string())?;
|
||||||
inputs
|
inputs
|
||||||
|
@ -83,7 +82,11 @@ impl StorageProofs<'_> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn verify<R: Read>(self, proof_bytes: R, mut public_inputs: R) -> Result<(), String> {
|
pub fn verify<RR: Read>(
|
||||||
|
&mut self,
|
||||||
|
proof_bytes: RR,
|
||||||
|
mut public_inputs: RR,
|
||||||
|
) -> Result<(), String> {
|
||||||
let inputs: Vec<Fr> =
|
let inputs: Vec<Fr> =
|
||||||
CanonicalDeserialize::deserialize(&mut public_inputs).map_err(|e| e.to_string())?;
|
CanonicalDeserialize::deserialize(&mut public_inputs).map_err(|e| e.to_string())?;
|
||||||
let proof = Proof::<Bn254>::deserialize(proof_bytes).map_err(|e| e.to_string())?;
|
let proof = Proof::<Bn254>::deserialize(proof_bytes).map_err(|e| e.to_string())?;
|
Loading…
Reference in New Issue