Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat!: migrate to ACVM 0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Apr 26, 2023
1 parent 8fe7111 commit 880210c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 56 deletions.
12 changes: 4 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
acvm = { version = "0.9.0", features = ["bn254"] }
#acvm = { version = "0.9.0", features = ["bn254"] }
acvm = { git = "https://github.com/noir-lang/acvm", rev = "15d3c5a9be2dd92f266fcb7e672da17cada9fec5", features = ["bn254"] }

blake2 = "0.9.1"
dirs = { version = "3.0", optional = true }
reqwest = { version = "0.11.16", optional = true, default-features = false, features = ["stream", "rustls-tls"] }
Expand Down
2 changes: 1 addition & 1 deletion src/acvm_interop/proof_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ProofSystemCompiler for Barretenberg {
| BlackBoxFunc::RANGE
| BlackBoxFunc::SHA256
| BlackBoxFunc::Blake2s
| BlackBoxFunc::MerkleMembership
| BlackBoxFunc::ComputeMerkleRoot
| BlackBoxFunc::SchnorrVerify
| BlackBoxFunc::Pedersen
| BlackBoxFunc::HashToField128Security
Expand Down
37 changes: 15 additions & 22 deletions src/acvm_interop/pwg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ impl PartialWitnessGenerator for Barretenberg {
BlackBoxFunc::SHA256 => hash::sha256(initial_witness, func_call),
BlackBoxFunc::Blake2s => hash::blake2s(initial_witness, func_call),
BlackBoxFunc::EcdsaSecp256k1 => {
signature::ecdsa::secp256k1_prehashed(initial_witness, func_call)?
signature::ecdsa::secp256k1_prehashed(initial_witness, func_call)
}
BlackBoxFunc::AES | BlackBoxFunc::Keccak256 => {
return Err(OpcodeResolutionError::UnsupportedBlackBoxFunc(
func_call.name,
))

BlackBoxFunc::AND | BlackBoxFunc::XOR => {
logic::solve_logic_opcode(initial_witness, func_call)
}
BlackBoxFunc::MerkleMembership => {
BlackBoxFunc::RANGE => range::solve_range_opcode(initial_witness, func_call),
BlackBoxFunc::AES | BlackBoxFunc::Keccak256 => Err(
OpcodeResolutionError::UnsupportedBlackBoxFunc(func_call.name),
),
BlackBoxFunc::ComputeMerkleRoot => {
let mut inputs_iter = func_call.inputs.iter();

let _root = inputs_iter.next().expect("expected a root");
let root = witness_to_value(initial_witness, _root.witness)?;

let _leaf = inputs_iter.next().expect("expected a leaf");
let leaf = witness_to_value(initial_witness, _leaf.witness)?;

Expand All @@ -55,13 +55,8 @@ impl PartialWitnessGenerator for Barretenberg {
leaf,
);

let result = if &computed_merkle_root == root {
FieldElement::one()
} else {
FieldElement::zero()
};

initial_witness.insert(func_call.outputs[0], result);
initial_witness.insert(func_call.outputs[0], computed_merkle_root);
Ok(OpcodeResolution::Solved)
}
BlackBoxFunc::SchnorrVerify => {
// In barretenberg, if the signature fails, then the whole thing fails.
Expand Down Expand Up @@ -116,6 +111,7 @@ impl PartialWitnessGenerator for Barretenberg {
};

initial_witness.insert(func_call.outputs[0], result);
Ok(OpcodeResolution::Solved)
}
BlackBoxFunc::Pedersen => {
let inputs_iter = func_call.inputs.iter();
Expand All @@ -128,6 +124,7 @@ impl PartialWitnessGenerator for Barretenberg {
let (res_x, res_y) = self.encrypt(scalars);
initial_witness.insert(func_call.outputs[0], res_x);
initial_witness.insert(func_call.outputs[1], res_y);
Ok(OpcodeResolution::Solved)
}
BlackBoxFunc::HashToField128Security => {
let mut hasher = <Blake2s as blake2::Digest>::new();
Expand All @@ -149,6 +146,7 @@ impl PartialWitnessGenerator for Barretenberg {
assert_eq!(func_call.outputs.len(), 1);

initial_witness.insert(func_call.outputs[0], reduced_res);
Ok(OpcodeResolution::Solved)
}
BlackBoxFunc::FixedBaseScalarMul => {
let scalar = witness_to_value(initial_witness, func_call.inputs[0].witness)?;
Expand All @@ -157,13 +155,8 @@ impl PartialWitnessGenerator for Barretenberg {

initial_witness.insert(func_call.outputs[0], pub_x);
initial_witness.insert(func_call.outputs[1], pub_y);
Ok(OpcodeResolution::Solved)
}
BlackBoxFunc::AND | BlackBoxFunc::XOR => {
logic::solve_logic_opcode(initial_witness, func_call)?
}
BlackBoxFunc::RANGE => range::solve_range_opcode(initial_witness, func_call)?,
}

Ok(OpcodeResolution::Solved)
}
}
40 changes: 16 additions & 24 deletions src/barretenberg_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,14 @@ impl SchnorrConstraint {
}
}
#[derive(Clone, Hash, Debug)]
pub(crate) struct MerkleMembershipConstraint {
pub(crate) struct ComputeMerkleRootConstraint {
pub(crate) hash_path: Vec<i32>,
pub(crate) root: i32,
pub(crate) leaf: i32,
pub(crate) index: i32,
pub(crate) result: i32,
}

impl MerkleMembershipConstraint {
impl ComputeMerkleRootConstraint {
fn to_bytes(&self) -> Vec<u8> {
let mut buffer = Vec::new();

Expand All @@ -189,7 +188,6 @@ impl MerkleMembershipConstraint {
buffer.extend_from_slice(&constraint.to_be_bytes());
}

buffer.extend_from_slice(&self.root.to_be_bytes());
buffer.extend_from_slice(&self.leaf.to_be_bytes());
buffer.extend_from_slice(&self.result.to_be_bytes());
buffer.extend_from_slice(&self.index.to_be_bytes());
Expand Down Expand Up @@ -364,7 +362,7 @@ pub(crate) struct ConstraintSystem {
logic_constraints: Vec<LogicConstraint>,
range_constraints: Vec<RangeConstraint>,
sha256_constraints: Vec<Sha256Constraint>,
merkle_membership_constraints: Vec<MerkleMembershipConstraint>,
compute_merkle_root_constraints: Vec<ComputeMerkleRootConstraint>,
schnorr_constraints: Vec<SchnorrConstraint>,
ecdsa_secp256k1_constraints: Vec<EcdsaConstraint>,
blake2s_constraints: Vec<Blake2sConstraint>,
Expand Down Expand Up @@ -411,11 +409,11 @@ impl ConstraintSystem {
self
}

pub(crate) fn merkle_membership_constraints(
pub(crate) fn compute_merkle_root_constraints(
mut self,
merkle_membership_constraints: Vec<MerkleMembershipConstraint>,
compute_merkle_root_constraints: Vec<ComputeMerkleRootConstraint>,
) -> Self {
self.merkle_membership_constraints = merkle_membership_constraints;
self.compute_merkle_root_constraints = compute_merkle_root_constraints;
self
}

Expand Down Expand Up @@ -511,10 +509,10 @@ impl ConstraintSystem {
buffer.extend(&constraint.to_bytes());
}

// Serialize each Merkle Membership constraint
let merkle_membership_constraints_len = self.merkle_membership_constraints.len() as u32;
buffer.extend_from_slice(&merkle_membership_constraints_len.to_be_bytes());
for constraint in self.merkle_membership_constraints.iter() {
// Serialize each Compute Merkle Root constraint
let compute_merkle_root_constraints = self.compute_merkle_root_constraints.len() as u32;
buffer.extend_from_slice(&compute_merkle_root_constraints.to_be_bytes());
for constraint in self.compute_merkle_root_constraints.iter() {
buffer.extend(&constraint.to_bytes());
}

Expand Down Expand Up @@ -581,7 +579,7 @@ impl From<&Circuit> for ConstraintSystem {
let mut sha256_constraints: Vec<Sha256Constraint> = Vec::new();
let mut blake2s_constraints: Vec<Blake2sConstraint> = Vec::new();
let mut pedersen_constraints: Vec<PedersenConstraint> = Vec::new();
let mut merkle_membership_constraints: Vec<MerkleMembershipConstraint> = Vec::new();
let mut compute_merkle_root_constraints: Vec<ComputeMerkleRootConstraint> = Vec::new();
let mut schnorr_constraints: Vec<SchnorrConstraint> = Vec::new();
let mut ecdsa_secp256k1_constraints: Vec<EcdsaConstraint> = Vec::new();
let mut fixed_base_scalar_mul_constraints: Vec<FixedBaseScalarMulConstraint> = Vec::new();
Expand Down Expand Up @@ -702,14 +700,9 @@ impl From<&Circuit> for ConstraintSystem {

blake2s_constraints.push(blake2s_constraint);
}
BlackBoxFunc::MerkleMembership => {
BlackBoxFunc::ComputeMerkleRoot => {
let mut inputs_iter = gadget_call.inputs.iter().peekable();

// root
let root = {
let root_input = inputs_iter.next().expect("missing Merkle root");
root_input.witness.witness_index() as i32
};
// leaf
let leaf = {
let leaf_input = inputs_iter
Expand All @@ -735,18 +728,17 @@ impl From<&Circuit> for ConstraintSystem {
hash_path.push(path_elem_index);
}

// result
// computed root
let result = gadget_call.outputs[0].witness_index() as i32;

let constraint = MerkleMembershipConstraint {
let constraint = ComputeMerkleRootConstraint {
hash_path,
root,
leaf,
index,
result,
};

merkle_membership_constraints.push(constraint);
compute_merkle_root_constraints.push(constraint);
}
BlackBoxFunc::SchnorrVerify => {
let mut inputs_iter = gadget_call.inputs.iter();
Expand Down Expand Up @@ -921,7 +913,7 @@ impl From<&Circuit> for ConstraintSystem {
logic_constraints,
range_constraints,
sha256_constraints,
merkle_membership_constraints,
compute_merkle_root_constraints,
pedersen_constraints,
schnorr_constraints,
ecdsa_secp256k1_constraints,
Expand Down

0 comments on commit 880210c

Please sign in to comment.