mirror of
https://github.com/arnaucube/plonky2-semaphore.git
synced 2026-01-21 21:21:29 +01:00
update to latest plonky2 version
This commit is contained in:
@@ -6,5 +6,5 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
plonky2 = { git = "https://github.com/mir-protocol/plonky2", branch = "semaphore-example" }
|
plonky2 = { git = "https://github.com/mir-protocol/plonky2" }
|
||||||
anyhow = "1.0.56"
|
anyhow = "1.0.56"
|
||||||
@@ -46,7 +46,7 @@ impl AccessSet {
|
|||||||
let mut pw = PartialWitness::new();
|
let mut pw = PartialWitness::new();
|
||||||
|
|
||||||
let targets = self.semaphore_circuit(&mut builder);
|
let targets = self.semaphore_circuit(&mut builder);
|
||||||
self.fill_semaphore_targets(&mut pw, private_key, topic, public_key_index, targets);
|
self.fill_semaphore_targets(&mut pw, private_key, topic, public_key_index, targets)?;
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let proof = data.prove(pw)?;
|
let proof = data.prove(pw)?;
|
||||||
@@ -56,7 +56,7 @@ impl AccessSet {
|
|||||||
nullifier,
|
nullifier,
|
||||||
proof: proof.proof,
|
proof: proof.proof,
|
||||||
},
|
},
|
||||||
data.to_verifier_data(),
|
data.verifier_data(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
use plonky2::field::field_types::Field;
|
use anyhow::Result;
|
||||||
use plonky2::hash::hash_types::{HashOutTarget, MerkleCapTarget};
|
use plonky2::field::types::Field;
|
||||||
|
use plonky2::hash::hash_types::HashOutTarget;
|
||||||
use plonky2::hash::merkle_proofs::MerkleProofTarget;
|
use plonky2::hash::merkle_proofs::MerkleProofTarget;
|
||||||
use plonky2::hash::poseidon::PoseidonHash;
|
use plonky2::hash::poseidon::PoseidonHash;
|
||||||
use plonky2::iop::target::Target;
|
use plonky2::iop::target::Target;
|
||||||
use plonky2::iop::witness::{PartialWitness, Witness};
|
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
|
||||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||||
|
|
||||||
use crate::access_set::AccessSet;
|
use crate::access_set::AccessSet;
|
||||||
@@ -44,7 +45,7 @@ impl AccessSet {
|
|||||||
builder.verify_merkle_proof::<PoseidonHash>(
|
builder.verify_merkle_proof::<PoseidonHash>(
|
||||||
[private_key, [zero; 4]].concat(),
|
[private_key, [zero; 4]].concat(),
|
||||||
&public_key_index_bits,
|
&public_key_index_bits,
|
||||||
&MerkleCapTarget(vec![merkle_root]),
|
merkle_root,
|
||||||
&merkle_proof,
|
&merkle_proof,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ impl AccessSet {
|
|||||||
topic: Digest,
|
topic: Digest,
|
||||||
public_key_index: usize,
|
public_key_index: usize,
|
||||||
targets: SemaphoreTargets,
|
targets: SemaphoreTargets,
|
||||||
) {
|
) -> Result<()> {
|
||||||
let SemaphoreTargets {
|
let SemaphoreTargets {
|
||||||
merkle_root,
|
merkle_root,
|
||||||
topic: topic_target,
|
topic: topic_target,
|
||||||
@@ -80,13 +81,13 @@ impl AccessSet {
|
|||||||
public_key_index: public_key_index_target,
|
public_key_index: public_key_index_target,
|
||||||
} = targets;
|
} = targets;
|
||||||
|
|
||||||
pw.set_hash_target(merkle_root, self.0.cap.0[0]);
|
pw.set_hash_target(merkle_root, self.0.cap.0[0])?;
|
||||||
pw.set_targets(&private_key_target, &private_key);
|
pw.set_target_arr(&private_key_target, &private_key)?;
|
||||||
pw.set_targets(&topic_target, &topic);
|
pw.set_target_arr(&topic_target, &topic)?;
|
||||||
pw.set_target(
|
pw.set_target(
|
||||||
public_key_index_target,
|
public_key_index_target,
|
||||||
F::from_canonical_usize(public_key_index),
|
F::from_canonical_usize(public_key_index),
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let merkle_proof = self.0.prove(public_key_index);
|
let merkle_proof = self.0.prove(public_key_index);
|
||||||
for (ht, h) in merkle_proof_target
|
for (ht, h) in merkle_proof_target
|
||||||
@@ -94,7 +95,8 @@ impl AccessSet {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.zip(merkle_proof.siblings)
|
.zip(merkle_proof.siblings)
|
||||||
{
|
{
|
||||||
pw.set_hash_target(ht, h);
|
pw.set_hash_target(ht, h)?;
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
use plonky2::iop::witness::{PartialWitness, Witness};
|
use anyhow::Result;
|
||||||
|
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
|
||||||
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
use plonky2::plonk::circuit_builder::CircuitBuilder;
|
||||||
use plonky2::plonk::circuit_data::{CircuitConfig, VerifierCircuitData, VerifierCircuitTarget};
|
use plonky2::plonk::circuit_data::{CircuitConfig, VerifierCircuitData};
|
||||||
use plonky2::plonk::proof::ProofWithPublicInputs;
|
use plonky2::plonk::proof::ProofWithPublicInputs;
|
||||||
|
|
||||||
use crate::access_set::AccessSet;
|
use crate::access_set::AccessSet;
|
||||||
@@ -14,7 +15,7 @@ impl AccessSet {
|
|||||||
topic1: Digest,
|
topic1: Digest,
|
||||||
signal1: Signal,
|
signal1: Signal,
|
||||||
verifier_data: &VerifierCircuitData<F, C, 2>,
|
verifier_data: &VerifierCircuitData<F, C, 2>,
|
||||||
) -> (Digest, Digest, PlonkyProof) {
|
) -> Result<(Digest, Digest, PlonkyProof, VerifierCircuitData<F, C, 2>)> {
|
||||||
let config = CircuitConfig::standard_recursion_zk_config();
|
let config = CircuitConfig::standard_recursion_zk_config();
|
||||||
let mut builder = CircuitBuilder::new(config);
|
let mut builder = CircuitBuilder::new(config);
|
||||||
let mut pw = PartialWitness::new();
|
let mut pw = PartialWitness::new();
|
||||||
@@ -45,7 +46,7 @@ impl AccessSet {
|
|||||||
proof: signal0.proof,
|
proof: signal0.proof,
|
||||||
public_inputs: public_inputs0,
|
public_inputs: public_inputs0,
|
||||||
},
|
},
|
||||||
);
|
)?;
|
||||||
let proof_target1 = builder.add_virtual_proof_with_pis(&verifier_data.common);
|
let proof_target1 = builder.add_virtual_proof_with_pis(&verifier_data.common);
|
||||||
pw.set_proof_with_pis_target(
|
pw.set_proof_with_pis_target(
|
||||||
&proof_target1,
|
&proof_target1,
|
||||||
@@ -53,25 +54,28 @@ impl AccessSet {
|
|||||||
proof: signal1.proof,
|
proof: signal1.proof,
|
||||||
public_inputs: public_inputs1,
|
public_inputs: public_inputs1,
|
||||||
},
|
},
|
||||||
);
|
)?;
|
||||||
|
|
||||||
let vd_target = VerifierCircuitTarget {
|
let vd_target =
|
||||||
constants_sigmas_cap: builder
|
builder.add_virtual_verifier_data(verifier_data.common.config.fri_config.cap_height);
|
||||||
.add_virtual_cap(verifier_data.common.config.fri_config.cap_height),
|
|
||||||
};
|
|
||||||
pw.set_cap_target(
|
pw.set_cap_target(
|
||||||
&vd_target.constants_sigmas_cap,
|
&vd_target.constants_sigmas_cap,
|
||||||
&verifier_data.verifier_only.constants_sigmas_cap,
|
&verifier_data.verifier_only.constants_sigmas_cap,
|
||||||
);
|
)?;
|
||||||
|
|
||||||
builder.verify_proof(proof_target0, &vd_target, &verifier_data.common);
|
builder.verify_proof::<C>(&proof_target0, &vd_target, &verifier_data.common);
|
||||||
builder.verify_proof(proof_target1, &vd_target, &verifier_data.common);
|
builder.verify_proof::<C>(&proof_target1, &vd_target, &verifier_data.common);
|
||||||
|
|
||||||
let data = builder.build();
|
let data = builder.build();
|
||||||
let recursive_proof = data.prove(pw).unwrap();
|
let recursive_proof = data.prove(pw).unwrap();
|
||||||
|
|
||||||
data.verify(recursive_proof.clone()).unwrap();
|
data.verify(recursive_proof.clone()).unwrap();
|
||||||
|
|
||||||
(signal0.nullifier, signal1.nullifier, recursive_proof.proof)
|
Ok((
|
||||||
|
signal0.nullifier,
|
||||||
|
signal1.nullifier,
|
||||||
|
recursive_proof.proof,
|
||||||
|
data.verifier_data(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ pub struct Signal {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use plonky2::field::field_types::Field;
|
use plonky2::field::types::{Field, Sample};
|
||||||
use plonky2::hash::merkle_tree::MerkleTree;
|
use plonky2::hash::merkle_tree::MerkleTree;
|
||||||
use plonky2::hash::poseidon::PoseidonHash;
|
use plonky2::hash::poseidon::PoseidonHash;
|
||||||
use plonky2::plonk::config::Hasher;
|
use plonky2::plonk::config::Hasher;
|
||||||
@@ -27,7 +27,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_semaphore() -> Result<()> {
|
fn test_semaphore() -> Result<()> {
|
||||||
let n = 1 << 20;
|
let n = 1 << 20;
|
||||||
let private_keys: Vec<Digest> = (0..n).map(|_| F::rand_arr()).collect();
|
let private_keys: Vec<Digest> = (0..n).map(|_| F::rand_array()).collect();
|
||||||
let public_keys: Vec<Vec<F>> = private_keys
|
let public_keys: Vec<Vec<F>> = private_keys
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&sk| {
|
.map(|&sk| {
|
||||||
@@ -39,7 +39,7 @@ mod tests {
|
|||||||
let access_set = AccessSet(MerkleTree::new(public_keys, 0));
|
let access_set = AccessSet(MerkleTree::new(public_keys, 0));
|
||||||
|
|
||||||
let i = 12;
|
let i = 12;
|
||||||
let topic = F::rand_arr();
|
let topic = F::rand_array();
|
||||||
|
|
||||||
let (signal, vd) = access_set.make_signal(private_keys[i], topic, i)?;
|
let (signal, vd) = access_set.make_signal(private_keys[i], topic, i)?;
|
||||||
access_set.verify_signal(topic, signal, &vd)
|
access_set.verify_signal(topic, signal, &vd)
|
||||||
|
|||||||
Reference in New Issue
Block a user