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.3 (#148)
Browse files Browse the repository at this point in the history
* feat!: migrate to ACVM 0.10.0

* chore: update barretenberg commit

* Update barretenberg_structures.rs

* feat: add test for `compute_merkle_root` constraint

* chore: switch to crates.io version of ACVM
  • Loading branch information
TomAFrench authored Apr 28, 2023
1 parent 7f42535 commit c9fb9e8
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 61 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ 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.10.3", 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
6 changes: 3 additions & 3 deletions flake.lock

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

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 @@ -200,15 +200,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 @@ -219,7 +218,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 @@ -394,7 +392,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 @@ -441,11 +439,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 @@ -541,10 +539,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_len = self.compute_merkle_root_constraints.len() as u32;
buffer.extend_from_slice(&compute_merkle_root_constraints_len.to_be_bytes());
for constraint in self.compute_merkle_root_constraints.iter() {
buffer.extend(&constraint.to_bytes());
}

Expand Down Expand Up @@ -611,7 +609,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 @@ -732,14 +730,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 @@ -765,18 +758,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 @@ -951,7 +943,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
59 changes: 57 additions & 2 deletions src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,12 @@ mod test {
use acvm::FieldElement;

use super::*;
use crate::barretenberg_structures::{
Constraint, LogicConstraint, PedersenConstraint, RangeConstraint, SchnorrConstraint,
use crate::{
barretenberg_structures::{
ComputeMerkleRootConstraint, Constraint, LogicConstraint, PedersenConstraint,
RangeConstraint, SchnorrConstraint,
},
merkle::{MerkleTree, MessageHasher},
};

#[test]
Expand Down Expand Up @@ -761,6 +765,57 @@ mod test {
test_composer_with_pk_vk(constraint_system, vec![case_1]);
}

#[test]
fn test_compute_merkle_root_constraint() {
use tempfile::tempdir;
let temp_dir = tempdir().unwrap();
let mut msg_hasher: blake2::Blake2s = MessageHasher::new();

let tree: MerkleTree<blake2::Blake2s, Barretenberg> = MerkleTree::new(3, &temp_dir);

let empty_leaf = vec![0; 64];

let index = FieldElement::zero();
let index_as_usize: usize = 0;
let mut index_bits = index.bits();
index_bits.reverse();

let leaf = msg_hasher.hash(&empty_leaf);

let root = tree.root();

let hash_path = tree.get_hash_path(index_as_usize);
let mut hash_path_ref = Vec::new();
for (i, path_pair) in hash_path.into_iter().enumerate() {
let path_bit = index_bits[i];
let hash = if !path_bit { path_pair.1 } else { path_pair.0 };
hash_path_ref.push(hash);
}
let hash_path_ref: Vec<FieldElement> = hash_path_ref.into_iter().collect();

let constraint = ComputeMerkleRootConstraint {
hash_path: (3..3 + hash_path_ref.len() as i32).collect(),
leaf: 0,
index: 1,
result: 2,
};

let constraint_system = ConstraintSystem::new()
.var_num(500)
.compute_merkle_root_constraints(vec![constraint]);

let mut witness_values = vec![leaf, index, root];
witness_values.extend(hash_path_ref);

let case_1 = WitnessResult {
witness: witness_values.into(),
public_inputs: vec![].into(),
result: true,
};

test_composer_with_pk_vk(constraint_system, vec![case_1]);
}

#[test]
fn test_logic_constraints() {
/*
Expand Down

0 comments on commit c9fb9e8

Please sign in to comment.