From 16b067e61138170c3b84d663cbb7e470bcb9d448 Mon Sep 17 00:00:00 2001 From: Tom French Date: Fri, 19 May 2023 09:44:26 +0100 Subject: [PATCH 01/13] feat: update to ACVM 0.13.0 --- Cargo.lock | 12 +- Cargo.toml | 3 + src/acvm_interop/proof_system.rs | 1 - src/acvm_interop/pwg.rs | 124 +--------- src/acvm_interop/pwg/merkle.rs | 185 --------------- src/barretenberg_structures.rs | 34 +-- src/lib.rs | 2 - src/merkle.rs | 394 ------------------------------- 8 files changed, 12 insertions(+), 743 deletions(-) delete mode 100644 src/acvm_interop/pwg/merkle.rs delete mode 100644 src/merkle.rs diff --git a/Cargo.lock b/Cargo.lock index de104a3a..d7ebe7e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,8 +5,7 @@ version = 3 [[package]] name = "acir" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c831802bad8d6810bfdb57cd00913157dfe710a185dc360c9a94eecd043bbf12" +source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" dependencies = [ "acir_field", "flate2", @@ -18,8 +17,7 @@ dependencies = [ [[package]] name = "acir_field" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d455746f51b4421548e42d34e2ce46b6db637cb048095d78766612d26bf526db" +source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" dependencies = [ "ark-bn254", "ark-ff", @@ -32,8 +30,7 @@ dependencies = [ [[package]] name = "acvm" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd8e75cf737b52195a8c109e0ee776e468e1c2ec42f0f88aa4465cd76a09b4ed" +source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" dependencies = [ "acir", "acvm_stdlib", @@ -70,8 +67,7 @@ dependencies = [ [[package]] name = "acvm_stdlib" version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2e87549b987593eeb1902990006443f2fe5817db927035f081008e4b8a46a49" +source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" dependencies = [ "acir", ] diff --git a/Cargo.toml b/Cargo.toml index 4ff06804..45be0507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,3 +57,6 @@ js = [ "dep:getrandom", "wasmer/js-default" ] + +[patch.crates-io] +acvm = { git = "https://github.com/noir-lang/acvm", rev = "94d5d20e1eb985cb15eb27556bbe8654172c6a1a" } \ No newline at end of file diff --git a/src/acvm_interop/proof_system.rs b/src/acvm_interop/proof_system.rs index 74d670ea..fa9b559c 100644 --- a/src/acvm_interop/proof_system.rs +++ b/src/acvm_interop/proof_system.rs @@ -34,7 +34,6 @@ impl ProofSystemCompiler for Barretenberg { | BlackBoxFunc::SHA256 | BlackBoxFunc::Blake2s | BlackBoxFunc::Keccak256 - | BlackBoxFunc::ComputeMerkleRoot | BlackBoxFunc::SchnorrVerify | BlackBoxFunc::Pedersen | BlackBoxFunc::HashToField128Security diff --git a/src/acvm_interop/pwg.rs b/src/acvm_interop/pwg.rs index 3d7c2928..816252c1 100644 --- a/src/acvm_interop/pwg.rs +++ b/src/acvm_interop/pwg.rs @@ -1,17 +1,14 @@ use acvm::acir::circuit::opcodes::FunctionInput; use acvm::acir::native_types::{Witness, WitnessMap}; use acvm::acir::BlackBoxFunc; -use acvm::pwg::{hash, logic, range, signature, witness_to_value}; -use acvm::{pwg::OpcodeResolution, FieldElement}; -use acvm::{OpcodeResolutionError, PartialWitnessGenerator}; +use acvm::pwg::{witness_to_value, OpcodeResolution, OpcodeResolutionError}; +use acvm::{FieldElement, PartialWitnessGenerator}; use crate::pedersen::Pedersen; use crate::scalar_mul::ScalarMul; use crate::schnorr::SchnorrSig; use crate::Barretenberg; -mod merkle; - impl PartialWitnessGenerator for Barretenberg { fn aes( &self, @@ -24,86 +21,6 @@ impl PartialWitnessGenerator for Barretenberg { )) } - fn and( - &self, - initial_witness: &mut WitnessMap, - lhs: &FunctionInput, - rhs: &FunctionInput, - output: &Witness, - ) -> Result { - logic::and(initial_witness, lhs, rhs, output) - } - - fn xor( - &self, - initial_witness: &mut WitnessMap, - lhs: &FunctionInput, - rhs: &FunctionInput, - output: &Witness, - ) -> Result { - logic::xor(initial_witness, lhs, rhs, output) - } - - fn range( - &self, - initial_witness: &mut WitnessMap, - input: &FunctionInput, - ) -> Result { - range::solve_range_opcode(initial_witness, input) - } - - fn sha256( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::sha256(initial_witness, inputs, outputs) - } - - fn blake2s( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::blake2s256(initial_witness, inputs, outputs) - } - - fn compute_merkle_root( - &self, - initial_witness: &mut WitnessMap, - leaf: &FunctionInput, - index: &FunctionInput, - hash_path: &[FunctionInput], - output: &Witness, - ) -> Result { - let leaf = witness_to_value(initial_witness, leaf.witness)?; - - let index = witness_to_value(initial_witness, index.witness)?; - - let hash_path: Result, _> = hash_path - .iter() - .map(|input| witness_to_value(initial_witness, input.witness)) - .collect(); - - let computed_merkle_root = merkle::compute_merkle_root( - |left, right| self.compress_native(left, right), - hash_path?, - index, - leaf, - ) - .map_err(|err| { - OpcodeResolutionError::BlackBoxFunctionFailed( - BlackBoxFunc::ComputeMerkleRoot, - err.to_string(), - ) - })?; - - initial_witness.insert(*output, computed_merkle_root); - Ok(OpcodeResolution::Solved) - } - fn schnorr_verify( &self, initial_witness: &mut WitnessMap, @@ -212,34 +129,6 @@ impl PartialWitnessGenerator for Barretenberg { Ok(OpcodeResolution::Solved) } - fn hash_to_field_128_security( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - output: &Witness, - ) -> Result { - hash::hash_to_field_128_security(initial_witness, inputs, output) - } - - fn ecdsa_secp256k1( - &self, - initial_witness: &mut WitnessMap, - public_key_x: &[FunctionInput], - public_key_y: &[FunctionInput], - signature: &[FunctionInput], - message: &[FunctionInput], - outputs: &Witness, - ) -> Result { - signature::ecdsa::secp256k1_prehashed( - initial_witness, - public_key_x, - public_key_y, - signature, - message, - *outputs, - ) - } - fn fixed_base_scalar_mul( &self, initial_witness: &mut WitnessMap, @@ -259,13 +148,4 @@ impl PartialWitnessGenerator for Barretenberg { initial_witness.insert(outputs[1], pub_y); Ok(OpcodeResolution::Solved) } - - fn keccak256( - &self, - initial_witness: &mut WitnessMap, - inputs: &[FunctionInput], - outputs: &[Witness], - ) -> Result { - hash::keccak256(initial_witness, inputs, outputs) - } } diff --git a/src/acvm_interop/pwg/merkle.rs b/src/acvm_interop/pwg/merkle.rs deleted file mode 100644 index d194ccb3..00000000 --- a/src/acvm_interop/pwg/merkle.rs +++ /dev/null @@ -1,185 +0,0 @@ -use acvm::FieldElement; - -use crate::Error; - -pub(super) fn compute_merkle_root( - hash_func: impl Fn(&FieldElement, &FieldElement) -> Result, - hash_path: Vec<&FieldElement>, - index: &FieldElement, - leaf: &FieldElement, -) -> Result { - let mut index_bits: Vec = index.bits(); - index_bits.reverse(); - - assert!( - hash_path.len() <= index_bits.len(), - "hash path exceeds max depth of tree" - ); - index_bits.into_iter().zip(hash_path.into_iter()).fold( - Ok(*leaf), - |current_node, (path_bit, path_elem)| match current_node { - Ok(current_node) => { - let (left, right) = if !path_bit { - (¤t_node, path_elem) - } else { - (path_elem, ¤t_node) - }; - hash_func(left, right) - } - Err(_) => current_node, - }, - ) -} - -#[cfg(test)] -mod tests { - use crate::merkle::{MerkleTree, MessageHasher}; - use crate::Error; - use crate::{pedersen::Pedersen, Barretenberg}; - use acvm::FieldElement; - - #[test] - fn test_check_membership() -> Result<(), Error> { - struct Test<'a> { - // Index of the leaf in the MerkleTree - index: &'a str, - // Returns true if the leaf is indeed a part of the MerkleTree at the specified index - result: bool, - // The message is used to derive the leaf at `index` by using the specified hash - message: Vec, - // If this is true, then before checking for membership - // we update the tree with the message at that index - should_update_tree: bool, - error_msg: &'a str, - } - // Note these test cases are not independent. - // i.e. If you update index 0, then this will be saved for the next test - let tests = vec![ - Test { - index : "0", - result : true, - message : vec![0;64], - should_update_tree: false, - error_msg : "this should always be true, since the tree is initialized with 64 zeroes" - }, - Test { - index : "0", - result : false, - message : vec![10;64], - should_update_tree: false, - error_msg : "this should be false, since the tree was not updated, however the message which derives the leaf has changed" - }, - Test { - index : "0", - result : true, - message : vec![1;64], - should_update_tree: true, - error_msg : "this should be true, since we are updating the tree" - }, - Test { - index : "0", - result : true, - message : vec![1;64], - should_update_tree: false, - error_msg : "this should be true since the index at 4 has not been changed yet, so it would be [0;64]" - }, - Test { - index : "4", - result : true, - message : vec![0;64], - should_update_tree: false, - error_msg : "this should be true since the index at 4 has not been changed yet, so it would be [0;64]" - }, - ]; - - let mut msg_hasher: blake2::Blake2s256 = MessageHasher::new(); - - let mut tree: MerkleTree = MerkleTree::new(3); - - for test_vector in tests { - let index = FieldElement::try_from_str(test_vector.index).unwrap(); - let index_as_usize: usize = test_vector.index.parse().unwrap(); - let mut index_bits = index.bits(); - index_bits.reverse(); - - let leaf = msg_hasher.hash(&test_vector.message); - - let mut root = tree.root(); - if test_vector.should_update_tree { - root = tree.update_message(index_as_usize, &test_vector.message)?; - } - - 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 = hash_path_ref.iter().collect(); - - let bb = Barretenberg::new(); - let computed_merkle_root = super::compute_merkle_root( - |left, right| bb.compress_native(left, right), - hash_path_ref, - &index, - &leaf, - )?; - let is_leaf_in_tree = root == computed_merkle_root; - - assert_eq!( - is_leaf_in_tree, test_vector.result, - "{}", - test_vector.error_msg - ); - } - - Ok(()) - } - - // This test uses `update_leaf` directly rather than `update_message` - #[test] - fn simple_shield() -> Result<(), Error> { - let mut tree: MerkleTree = MerkleTree::new(3); - - let barretenberg = Barretenberg::new(); - let pubkey_x = FieldElement::from_hex( - "0x0bff8247aa94b08d1c680d7a3e10831bd8c8cf2ea2c756b0d1d89acdcad877ad", - ) - .unwrap(); - let pubkey_y = FieldElement::from_hex( - "0x2a5d7253a6ed48462fedb2d350cc768d13956310f54e73a8a47914f34a34c5c4", - ) - .unwrap(); - let (note_commitment_x, _) = barretenberg.encrypt(vec![pubkey_x, pubkey_y])?; - dbg!(note_commitment_x.to_hex()); - let leaf = note_commitment_x; - - let index = FieldElement::try_from_str("0").unwrap(); - let index_as_usize: usize = 0_usize; - let mut index_bits = index.bits(); - index_bits.reverse(); - - let root = tree.update_leaf(index_as_usize, leaf)?; - - 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 = hash_path_ref.iter().collect(); - let bb = Barretenberg::new(); - let computed_merkle_root = super::compute_merkle_root( - |left, right| bb.compress_native(left, right), - hash_path_ref, - &index, - &leaf, - )?; - - assert_eq!(root, computed_merkle_root); - - Ok(()) - } -} diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 394f6822..06460661 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -744,7 +744,9 @@ impl TryFrom<&Circuit> for ConstraintSystem { let mut block_constraints: Vec = Vec::new(); let mut keccak_constraints: Vec = Vec::new(); let mut pedersen_constraints: Vec = Vec::new(); - let mut compute_merkle_root_constraints: Vec = Vec::new(); + // ACVM doesn't generate `ComputeMerkleRootConstraint`s anymore. + // We maintain this to maintain the serialization format. + let compute_merkle_root_constraints: Vec = Vec::new(); let mut schnorr_constraints: Vec = Vec::new(); let mut ecdsa_secp256k1_constraints: Vec = Vec::new(); let mut fixed_base_scalar_mul_constraints: Vec = Vec::new(); @@ -859,36 +861,6 @@ impl TryFrom<&Circuit> for ConstraintSystem { blake2s_constraints.push(blake2s_constraint); } - BlackBoxFuncCall::ComputeMerkleRoot { - leaf, - index, - hash_path: hash_path_inputs, - output, - } => { - // leaf - let leaf = leaf.witness.witness_index() as i32; - // index - let index = index.witness.witness_index() as i32; - - let mut hash_path = Vec::new(); - for path_elem in hash_path_inputs.iter() { - let path_elem_index = path_elem.witness.witness_index() as i32; - - hash_path.push(path_elem_index); - } - - // computed root - let result = output.witness_index() as i32; - - let constraint = ComputeMerkleRootConstraint { - hash_path, - leaf, - index, - result, - }; - - compute_merkle_root_constraints.push(constraint); - } BlackBoxFuncCall::SchnorrVerify { public_key_x, public_key_y, diff --git a/src/lib.rs b/src/lib.rs index 6690b437..5b6812d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,6 @@ mod barretenberg_structures; mod composer; #[cfg(any(feature = "native", feature = "wasm"))] mod crs; -#[cfg(test)] -mod merkle; mod pedersen; mod pippenger; mod scalar_mul; diff --git a/src/merkle.rs b/src/merkle.rs deleted file mode 100644 index 60edc29b..00000000 --- a/src/merkle.rs +++ /dev/null @@ -1,394 +0,0 @@ -// TODO(#166): Rework this module to return results -use acvm::FieldElement; -use std::{collections::BTreeMap, convert::TryInto}; - -use crate::{pedersen::Pedersen, Barretenberg, Error}; - -// Hashes the leaves up the path, on the way to the root -pub(crate) trait PathHasher { - fn new() -> Self; - fn hash(&self, left: &FieldElement, right: &FieldElement) -> Result; -} - -impl PathHasher for Barretenberg { - fn hash(&self, left: &FieldElement, right: &FieldElement) -> Result { - self.compress_native(left, right) - } - - fn new() -> Self { - Barretenberg::new() - } -} - -// Hashes the message into a leaf -pub(crate) trait MessageHasher { - fn new() -> Self; - fn hash(&mut self, msg: &[u8]) -> FieldElement; -} - -impl MessageHasher for blake2::Blake2s256 { - fn new() -> Self { - use blake2::Digest; - ::new() - } - - fn hash(&mut self, msg: &[u8]) -> FieldElement { - use blake2::Digest; - - self.update(msg); - - let res = self.clone().finalize(); - self.reset(); - FieldElement::from_be_bytes_reduce(&res[..]) - } -} - -// This impl should be redone in a more efficient and readable way. -// We should have a separate impl for SparseMerkle and regular merkle -// With Regular merkle we need to ensure that updates are done sequentially -// -// With sparse merkle, one can update at any index - -type HashPath = Vec<(FieldElement, FieldElement)>; - -#[allow(dead_code)] -fn flatten_path(path: Vec<(FieldElement, FieldElement)>) -> Vec { - path.into_iter() - .flat_map(|(left, right)| std::iter::once(left).chain(std::iter::once(right))) - .collect() -} - -pub(crate) struct MerkleTree { - depth: u32, - total_size: u32, - db: BTreeMap<&'static [u8], Vec>, - preimages_tree: BTreeMap<[u8; 16], Vec>, - hashes_tree: BTreeMap<[u8; 16], Vec>, - barretenberg: PH, - msg_hasher: MH, -} - -impl MerkleTree { - pub(crate) fn new(depth: u32) -> Self { - let barretenberg = PH::new(); - let mut msg_hasher = MH::new(); - - assert!((1..=20).contains(&depth)); // Why can depth != 0 and depth not more than 20? - - let db = BTreeMap::new(); - let preimages_tree = BTreeMap::new(); - let hashes_tree = BTreeMap::new(); - - let total_size = 1u32 << depth; - - let mut hashes: Vec<_> = (0..total_size * 2 - 2) - .map(|_| FieldElement::zero()) - .collect(); - - let zero_message = [0u8; 64]; - let pre_images = (0..total_size).map(|_| zero_message.to_vec()); - - let mut current = msg_hasher.hash(&zero_message); - - let mut offset = 0usize; - let mut layer_size = total_size as usize; // XXX: On 32 bit architectures, this `as` cast may silently truncate, when total_size > 2^32? - while offset < hashes.len() { - for i in 0..layer_size { - hashes[offset + i] = current; - } - current = barretenberg.hash(¤t, ¤t).unwrap(); - - offset += layer_size; - layer_size /= 2; - } - - let mut merkle_tree = MerkleTree { - depth, - total_size, - barretenberg, - db, - preimages_tree, - hashes_tree, - msg_hasher, - }; - - let root = current; - merkle_tree.insert_root(root); - - for (index, hash) in hashes.into_iter().enumerate() { - merkle_tree.insert_hash(index as u32, hash) - } - - for (index, image) in pre_images.into_iter().enumerate() { - merkle_tree.insert_preimage(index as u32, image) - } - - merkle_tree.insert_depth(depth); - merkle_tree.insert_empty_index(0); - - merkle_tree - } - - fn insert_root(&mut self, value: FieldElement) { - self.db.insert("ROOT".as_bytes(), value.to_be_bytes()); - } - - fn fetch_root(&self) -> FieldElement { - let value = self - .db - .get("ROOT".as_bytes()) - .expect("merkle root should always be present"); - FieldElement::from_be_bytes_reduce(value) - } - - fn insert_depth(&mut self, value: u32) { - self.db - .insert("DEPTH".as_bytes(), value.to_be_bytes().into()); - } - - fn fetch_depth(&self) -> u32 { - let value = self - .db - .get("DEPTH".as_bytes()) - .expect("depth should always be present"); - u32::from_be_bytes(value.to_vec().try_into().unwrap()) - } - - fn insert_empty_index(&mut self, index: u32) { - // First fetch the depth to see that this is less than - let depth = self.fetch_depth(); - let total_size = 1 << depth; - if index > total_size { - panic!("trying to insert at index {index}, but total width is {total_size}") - } - self.db - .insert("EMPTY".as_bytes(), index.to_be_bytes().into()); - } - - fn fetch_empty_index(&self) -> u32 { - let value = self - .db - .get("EMPTY".as_bytes()) - .expect("empty index should always be present"); - u32::from_be_bytes(value.to_vec().try_into().unwrap()) - } - - fn insert_preimage(&mut self, index: u32, value: Vec) { - let index = index as u128; - self.preimages_tree.insert(index.to_be_bytes(), value); - } - - #[allow(dead_code)] - fn fetch_preimage(&self, index: usize) -> Vec { - let index = index as u128; - self.preimages_tree - .get(&index.to_be_bytes()) - .unwrap() - .to_vec() - } - - fn fetch_hash(&self, index: usize) -> FieldElement { - let index = index as u128; - - let i_vec = self.hashes_tree.get(&index.to_be_bytes()).unwrap(); - FieldElement::from_be_bytes_reduce(i_vec) - } - - fn insert_hash(&mut self, index: u32, hash: FieldElement) { - let index = index as u128; - - self.hashes_tree - .insert(index.to_be_bytes(), hash.to_be_bytes()); - } - - fn find_hash_from_value(&self, leaf_value: &FieldElement) -> Option { - for index_db_lef_hash in self.hashes_tree.iter() { - let (key, db_leaf_hash) = index_db_lef_hash; - let index = u128::from_be_bytes(key.to_vec().try_into().unwrap()); - - if db_leaf_hash.to_vec() == leaf_value.to_be_bytes() { - return Some(index); - } - } - None - } - - pub(crate) fn get_hash_path(&self, mut index: usize) -> HashPath { - let mut path = HashPath::with_capacity(self.depth as usize); - - let mut offset = 0usize; - let mut layer_size = self.total_size; - for _ in 0..self.depth { - index &= (!0) - 1; - path.push(( - self.fetch_hash(offset + index), - self.fetch_hash(offset + index + 1), - )); - offset += layer_size as usize; - layer_size /= 2; - index /= 2; - } - path - } - /// Updates the message at index and computes the new tree root - pub(crate) fn update_message( - &mut self, - index: usize, - new_message: &[u8], - ) -> Result { - let current = self.msg_hasher.hash(new_message); - self.insert_preimage(index as u32, new_message.to_vec()); - self.update_leaf(index, current) - } - - fn check_if_index_valid_and_increment(&mut self, mut index: usize) { - // Fetch the empty index - let empty_index = self.fetch_empty_index() as usize; - if empty_index == index { - // increment the empty index - index += 1; - self.insert_empty_index(index as u32); - } else { - panic!("this is an regular append-only merkle tree. Tried to insert at {index}, but next empty is at {empty_index}"); - } - } - - #[allow(dead_code)] - pub(crate) fn find_index_from_leaf(&self, leaf_value: &FieldElement) -> Option { - let index = self.find_hash_from_value(leaf_value); - index.map(|val| val as usize) - } - - #[allow(dead_code)] - // TODO: this gets updated to be -1 on the latest barretenberg branch - pub(crate) fn find_index_for_empty_leaf(&self) -> usize { - let index = self.fetch_empty_index(); - index as usize - } - - /// Update the element at index and compute the new tree root - pub(crate) fn update_leaf( - &mut self, - mut index: usize, - mut current: FieldElement, - ) -> Result { - // Note that this method does not update the list of messages [preimages]| - // use `update_message` to do this - self.check_if_index_valid_and_increment(index); - - let mut offset = 0usize; - let mut layer_size = self.total_size; - for _ in 0..self.depth { - self.insert_hash((offset + index) as u32, current); - - index &= (!0) - 1; - current = self.barretenberg.hash( - &self.fetch_hash(offset + index), - &self.fetch_hash(offset + index + 1), - )?; - - offset += layer_size as usize; - layer_size /= 2; - index /= 2; - } - - self.insert_root(current); - Ok(current) - } - - #[allow(dead_code)] - /// Gets a message at `index`. This is not the leaf - pub(crate) fn get_message_at_index(&self, index: usize) -> Vec { - self.fetch_preimage(index) - } - - pub(crate) fn root(&self) -> FieldElement { - self.fetch_root() - } - - #[allow(dead_code)] - pub(crate) fn depth(&self) -> u32 { - self.depth - } -} - -#[test] -fn basic_interop_initial_root() { - // Test that the initial root is computed correctly - let tree: MerkleTree = MerkleTree::new(3); - // Copied from barretenberg by copying the stdout from MemoryTree - let expected_hex = "04ccfbbb859b8605546e03dcaf41393476642859ff7f99446c054b841f0e05c8"; - assert_eq!(tree.root().to_hex(), expected_hex) -} - -#[test] -fn basic_interop_hashpath() { - // Test that the hashpath is correct - let tree: MerkleTree = MerkleTree::new(3); - - let path = tree.get_hash_path(0); - - let expected_hash_path = vec![ - ( - "1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", - "1cdcf02431ba623767fe389337d011df1048dcc24b98ed81cec97627bab454a0", - ), - ( - "0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", - "0b5e9666e7323ce925c28201a97ddf4144ac9d148448ed6f49f9008719c1b85b", - ), - ( - "22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", - "22ec636f8ad30ef78c42b7fe2be4a4cacf5a445cfb5948224539f59a11d70775", - ), - ]; - - for (got, expected_segment) in path.into_iter().zip(expected_hash_path) { - assert_eq!(got.0.to_hex().as_str(), expected_segment.0); - assert_eq!(got.1.to_hex().as_str(), expected_segment.1) - } -} - -#[test] -fn basic_interop_update() -> Result<(), Error> { - // Test that computing the HashPath is correct - let mut tree: MerkleTree = MerkleTree::new(3); - - tree.update_message(0, &[0; 64])?; - tree.update_message(1, &[1; 64])?; - tree.update_message(2, &[2; 64])?; - tree.update_message(3, &[3; 64])?; - tree.update_message(4, &[4; 64])?; - tree.update_message(5, &[5; 64])?; - tree.update_message(6, &[6; 64])?; - let root = tree.update_message(7, &[7; 64])?; - - assert_eq!( - "0ef8e14db4762ebddadb23b2225f93ca200a4c9bd37130b4d028c971bbad16b5", - root.to_hex() - ); - - let path = tree.get_hash_path(2); - - let expected_hash_path = vec![ - ( - "06c2335d6f7acb84bbc7d0892cefebb7ca31169a89024f24814d5785e0d05324", - "12dc36b01cbd8a6248b04e08f0ec91aa6d11a91f030b4a7b1460281859942185", - ), - ( - "1f399ea0d6aaf602c7cbcb6ae8cda0e6b6487836c017163888ed4fd38b548389", - "220dd1b310caa4a6af755b4c893d956c48f31642b487164b258f2973aac2c28f", - ), - ( - "25cbb3084647221ffcb535945bb65bd70e0809834dc7a6d865a3f2bb046cdc29", - "2cc463fc8c9a4eda416f3e490876672f644708dd0330a915f6835d8396fa8f20", - ), - ]; - - for (got, expected_segment) in path.into_iter().zip(expected_hash_path) { - assert_eq!(got.0.to_hex().as_str(), expected_segment.0); - assert_eq!(got.1.to_hex().as_str(), expected_segment.1) - } - - Ok(()) -} From 1de589ae1b74973f12fb715611b36b3efb4ecf5d Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 22 May 2023 14:34:25 +0100 Subject: [PATCH 02/13] chore: remove lingering `ComputeMerkleRoot` test --- src/composer.rs | 59 +++---------------------------------------------- 1 file changed, 3 insertions(+), 56 deletions(-) diff --git a/src/composer.rs b/src/composer.rs index dcc772ef..508890df 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -410,13 +410,9 @@ mod test { use tokio::test; use super::*; - use crate::{ - barretenberg_structures::{ - BlockConstraint, ComputeMerkleRootConstraint, Constraint, Keccak256Constraint, - LogicConstraint, MemOpBarretenberg, PedersenConstraint, RangeConstraint, - SchnorrConstraint, - }, - merkle::{MerkleTree, MessageHasher}, + use crate::barretenberg_structures::{ + BlockConstraint, Constraint, Keccak256Constraint, LogicConstraint, MemOpBarretenberg, + PedersenConstraint, RangeConstraint, SchnorrConstraint, }; #[test] @@ -892,55 +888,6 @@ mod test { test_composer_with_pk_vk(constraint_system, vec![case_1, case_2]).await } - #[test] - async fn test_compute_merkle_root_constraint() -> Result<(), Error> { - let mut msg_hasher: blake2::Blake2s256 = MessageHasher::new(); - - let tree: MerkleTree = MerkleTree::new(3); - - 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 = 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]).await - } - #[test] async fn test_logic_constraints() -> Result<(), Error> { /* From 9e7a9c0f5eb33fabf692d9bf9d54d36a98948d2b Mon Sep 17 00:00:00 2001 From: Tom French Date: Mon, 22 May 2023 16:57:39 +0100 Subject: [PATCH 03/13] chore: remove unused `blake2s` dependency --- Cargo.lock | 1 - Cargo.toml | 1 - 2 files changed, 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d7ebe7e3..4b59bcb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -52,7 +52,6 @@ dependencies = [ "acvm", "barretenberg-sys", "bincode", - "blake2", "bytesize", "getrandom", "pkg-config", diff --git a/Cargo.toml b/Cargo.toml index 45be0507..bcc497ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,6 @@ getrandom = { version = "0.2", optional = true } pkg-config = "0.3" [dev-dependencies] -blake2 = "0.10.6" tokio = { version = "1.0", features = [ "macros" ] } [features] From 3e7409d482f7c8ef2983edb2b5fa26aa3c53024d Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Mon, 22 May 2023 18:36:57 +0100 Subject: [PATCH 04/13] fix: Allow async functions without send on async trait (#198) --- Cargo.lock | 8 ++++---- Cargo.toml | 2 +- src/acvm_interop/common_reference_string.rs | 2 +- src/composer.rs | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b59bcb1..ecedff8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ version = 3 [[package]] name = "acir" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir_field", "flate2", @@ -17,7 +17,7 @@ dependencies = [ [[package]] name = "acir_field" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "ark-bn254", "ark-ff", @@ -30,7 +30,7 @@ dependencies = [ [[package]] name = "acvm" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir", "acvm_stdlib", @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "acvm_stdlib" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=94d5d20e1eb985cb15eb27556bbe8654172c6a1a#94d5d20e1eb985cb15eb27556bbe8654172c6a1a" +source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" dependencies = [ "acir", ] diff --git a/Cargo.toml b/Cargo.toml index bcc497ba..d14ac00e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,4 +58,4 @@ js = [ ] [patch.crates-io] -acvm = { git = "https://github.com/noir-lang/acvm", rev = "94d5d20e1eb985cb15eb27556bbe8654172c6a1a" } \ No newline at end of file +acvm = { git = "https://github.com/noir-lang/acvm", rev = "9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" } diff --git a/src/acvm_interop/common_reference_string.rs b/src/acvm_interop/common_reference_string.rs index 2122ce00..a52c239a 100644 --- a/src/acvm_interop/common_reference_string.rs +++ b/src/acvm_interop/common_reference_string.rs @@ -3,7 +3,7 @@ use acvm::{acir::circuit::Circuit, async_trait, CommonReferenceString}; use crate::{composer::Composer, BackendError, Barretenberg}; // TODO(#185): Ensure CRS download works in JS -#[async_trait] +#[async_trait(?Send)] impl CommonReferenceString for Barretenberg { type Error = BackendError; diff --git a/src/composer.rs b/src/composer.rs index 508890df..494ccf39 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -6,7 +6,7 @@ use crate::{crs::CRS, Barretenberg, Error, FIELD_BYTES}; const NUM_RESERVED_GATES: u32 = 4; // this must be >= num_roots_cut_out_of_vanishing_polynomial (found under prover settings in barretenberg) -#[async_trait] +#[async_trait(?Send)] pub(crate) trait Composer { fn get_circuit_size(&self, constraint_system: &ConstraintSystem) -> Result; From 86b99c8c30aeab74da3dbe6d93234963ea21beb0 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Tue, 23 May 2023 09:55:13 +0100 Subject: [PATCH 05/13] feat!: Replace `js` feature with `wasm32` target (#202) feat!: Replace `js` feature with wasm32 target --- Cargo.toml | 40 ++++++++++++++++++++++++---------------- src/lib.rs | 28 ++++++++++++++++------------ 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 46abab51..fa46929f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["cdylib"] + [dependencies] acvm = { version = "0.12.0", features = ["bn254"] } bincode = "1.3.3" @@ -18,22 +21,38 @@ serde = { version = "1.0.136", features = ["derive"] } serde-big-array = "0.5.1" thiserror = "1.0.21" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Native barretenberg-sys = { version = "0.1.2", optional = true } # Wasm -wasmer = { version = "2.3", optional = true, default-features = false } +getrandom = { version = "0.2", optional = true } rust-embed = { version = "6.6.0", optional = true, features = [ "debug-embed", "interpolate-folder-path", "include-exclude", ] } -getrandom = { version = "0.2", optional = true } +wasmer = { version = "2.3", optional = true, default-features = false, features = [ + "sys-default", + "cranelift", + "default-compiler", + "default-cranelift", + "default-universal" +] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = [ "js" ] } +rust-embed = { version = "6.6.0", features = [ + "debug-embed", + "interpolate-folder-path", + "include-exclude", +] } +wasmer = { version = "2.3", default-features = false, features = [ "js-default" ] } [build-dependencies] pkg-config = "0.3" -[dev-dependencies] +[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tokio = { version = "1.0", features = [ "macros" ] } [features] @@ -42,20 +61,9 @@ native = [ "dep:barretenberg-sys" ] wasm = [ - "wasmer", - "dep:rust-embed", - "dep:getrandom", - "wasmer/sys-default", - "wasmer/cranelift", - "wasmer/default-compiler", - "wasmer/default-cranelift", - "wasmer/default-universal" -] -js = [ - "wasmer", + "dep:wasmer", "dep:rust-embed", - "dep:getrandom", - "wasmer/js-default" + "dep:getrandom" ] [patch.crates-io] diff --git a/src/lib.rs b/src/lib.rs index 8573c879..cbedb4d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,14 +4,18 @@ // `acvm-backend-barretenberg` can either interact with the Barretenberg backend through a static library // or through an embedded wasm binary. It does not make sense to include both of these backends at the same time. // We then throw a compilation error if both flags are set. -// TODO: handle JS target. #[cfg(all(feature = "native", feature = "wasm"))] compile_error!("feature \"native\" and feature \"wasm\" cannot be enabled at the same time"); +#[cfg(all(feature = "native", target_arch = "wasm32"))] +compile_error!("feature \"native\" cannot be enabled for a \"wasm32\" target"); + +#[cfg(all(feature = "wasm", target_arch = "wasm32"))] +compile_error!("feature \"wasm\" cannot be enabled for a \"wasm32\" target"); + mod acvm_interop; mod barretenberg_structures; mod composer; -#[cfg(any(feature = "native", feature = "wasm"))] mod crs; mod pedersen; mod pippenger; @@ -120,9 +124,9 @@ const FIELD_BYTES: usize = 32; #[derive(Debug)] pub struct Barretenberg { - #[cfg(feature = "wasm")] + #[cfg(not(feature = "native"))] memory: wasmer::Memory, - #[cfg(feature = "wasm")] + #[cfg(not(feature = "native"))] instance: wasmer::Instance, } @@ -276,7 +280,7 @@ mod wasm { pub(super) fn transfer_to_heap(&self, arr: &[u8], offset: usize) { let memory = &self.memory; - #[cfg(feature = "js")] + #[cfg(target_arch = "wasm32")] { let view = memory.uint8view(); for (byte_id, cell_id) in (offset..(offset + arr.len())).enumerate() { @@ -284,7 +288,7 @@ mod wasm { } } - #[cfg(not(feature = "js"))] + #[cfg(not(target_arch = "wasm32"))] { for (byte_id, cell) in memory.uint8view()[offset..(offset + arr.len())] .iter() @@ -306,13 +310,13 @@ mod wasm { let memory = &self.memory; let end = start + length; - #[cfg(feature = "js")] + #[cfg(target_arch = "wasm32")] return memory .uint8view() .subarray(start as u32, end as u32) .to_vec(); - #[cfg(not(feature = "js"))] + #[cfg(not(target_arch = "wasm32"))] return memory.view()[start..end] .iter() .map(|cell| cell.get()) @@ -431,7 +435,7 @@ mod wasm { let mut ptr_end = 0; let byte_view = env.memory.uint8view(); - #[cfg(feature = "js")] + #[cfg(target_arch = "wasm32")] for (i, cell) in byte_view.to_vec()[ptr as usize..].iter().enumerate() { if cell != &0_u8 { ptr_end = i; @@ -440,7 +444,7 @@ mod wasm { } } - #[cfg(not(feature = "js"))] + #[cfg(not(target_arch = "wasm32"))] for (i, cell) in byte_view[ptr as usize..].iter().enumerate() { if cell.get() != 0 { ptr_end = i; @@ -449,11 +453,11 @@ mod wasm { } } - #[cfg(feature = "js")] + #[cfg(target_arch = "wasm32")] let str_vec: Vec<_> = byte_view.to_vec()[ptr as usize..=(ptr + ptr_end as i32) as usize].to_vec(); - #[cfg(not(feature = "js"))] + #[cfg(not(target_arch = "wasm32"))] let str_vec: Vec<_> = byte_view[ptr as usize..=(ptr + ptr_end as i32) as usize] .iter() .cloned() From 328fee0e33fbb1296b22b64ab1f0eb7b4c100b39 Mon Sep 17 00:00:00 2001 From: Tom French Date: Wed, 24 May 2023 09:23:33 +0100 Subject: [PATCH 06/13] chore: bump ACVM commit --- Cargo.lock | 21 +++++++++++++++++---- Cargo.toml | 2 +- src/acvm_interop/proof_system.rs | 1 + src/barretenberg_structures.rs | 4 ++-- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a748b45..10d1fedf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,9 +5,10 @@ version = 3 [[package]] name = "acir" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" +source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" dependencies = [ "acir_field", + "brillig_vm", "flate2", "rmp-serde", "serde", @@ -17,7 +18,7 @@ dependencies = [ [[package]] name = "acir_field" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" +source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" dependencies = [ "ark-bn254", "ark-ff", @@ -30,7 +31,7 @@ dependencies = [ [[package]] name = "acvm" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" +source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" dependencies = [ "acir", "acvm_stdlib", @@ -67,7 +68,7 @@ dependencies = [ [[package]] name = "acvm_stdlib" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=9f9fc216a6d09ca97352ffd365bfd347e94ad8eb#9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" +source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" dependencies = [ "acir", ] @@ -362,6 +363,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "brillig_vm" +version = "0.1.0" +source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +dependencies = [ + "acir_field", + "num-bigint", + "serde", +] + [[package]] name = "bstr" version = "1.3.0" @@ -937,8 +948,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index fa46929f..5f7513be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,4 +67,4 @@ wasm = [ ] [patch.crates-io] -acvm = { git = "https://github.com/noir-lang/acvm", rev = "9f9fc216a6d09ca97352ffd365bfd347e94ad8eb" } +acvm = { git = "https://github.com/noir-lang/acvm", rev = "3c6740af75125afc8ebb4379f781f8274015e2e2" } diff --git a/src/acvm_interop/proof_system.rs b/src/acvm_interop/proof_system.rs index fa9b559c..ecef812f 100644 --- a/src/acvm_interop/proof_system.rs +++ b/src/acvm_interop/proof_system.rs @@ -27,6 +27,7 @@ impl ProofSystemCompiler for Barretenberg { Opcode::ROM(_) => true, Opcode::RAM(_) => true, Opcode::Oracle(_) => true, + Opcode::Brillig(_) => true, Opcode::BlackBoxFuncCall(func) => match func.get_black_box_func() { BlackBoxFunc::AND | BlackBoxFunc::XOR diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index e1321855..25fc5144 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -1069,8 +1069,8 @@ impl TryFrom<&Circuit> for ConstraintSystem { } }; } - Opcode::Directive(_) | Opcode::Oracle(_) => { - // Directives & Oracles are only needed by the pwg + Opcode::Directive(_) | Opcode::Oracle(_) | Opcode::Brillig(_) => { + // Directives, Oracles and Brillig are only needed by the pwg } Opcode::Block(_) => { // Block is managed by ACVM From f4aa8346203ab572aafc69513cbf1fe3cceb743e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Rodr=C3=ADguez?= Date: Thu, 1 Jun 2023 11:20:59 +0200 Subject: [PATCH 07/13] feat!: Add latest BB to acvm 0.13 (#214) * chore: remove `compute_merkle_root` constraints * chore: add "Brillig" to cspell * feat: added keccakvar constraints * feat!: added keccakvar constraints (#213) feat: added keccakvar constraints * chore: update bb sys and bb * fix: changes to update to latest bb * fix: fix wasm hash_index * chore: updated bb-sys pointer --------- Co-authored-by: TomAFrench --- Cargo.lock | 5 +- Cargo.toml | 2 +- cspell.json | 14 +----- flake.lock | 7 +-- flake.nix | 2 +- src/acvm_interop/pwg.rs | 2 +- src/barretenberg_structures.rs | 90 +++++++++++++++++----------------- src/composer.rs | 1 + src/lib.rs | 11 ++++- src/pedersen.rs | 27 +++++++--- 10 files changed, 86 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10d1fedf..5650c3da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,9 +270,8 @@ dependencies = [ [[package]] name = "barretenberg-sys" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bc96e40cc45e7d5622cbc57a140aee926a7fb6e4f7d07dda758daa9256f3317" +version = "0.1.3" +source = "git+https://github.com/noir-lang/barretenberg-sys#c04e3f46e87c95a364c476bf8f0d310e994c5597" dependencies = [ "bindgen", "cc", diff --git a/Cargo.toml b/Cargo.toml index 5f7513be..10e5ba00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ thiserror = "1.0.21" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Native -barretenberg-sys = { version = "0.1.2", optional = true } +barretenberg-sys = { git = "https://github.com/noir-lang/barretenberg-sys", optional = true } # Wasm getrandom = { version = "0.2", optional = true } diff --git a/cspell.json b/cspell.json index 304e6db4..545e765a 100644 --- a/cspell.json +++ b/cspell.json @@ -9,6 +9,7 @@ "bbmalloc", "bbfree", "bindgen", + "Brillig", "cout", "fdstat", "filestat", @@ -18,7 +19,6 @@ "keccak", "linearization", "logstr", - "merkle", "nargo", "PAIRINGSBN", "pedersen", @@ -32,20 +32,10 @@ "preimages", "preprocess", "pubkey", - "QARITH", - "QECC", - "QLOGIC", - "QRANGE", "reqwest", - "rollups", "schnorr", "secp", - "Shleft", - "Shright", - "Spilsbury", "subslice", - "TURBOVERIFIER", - "vals", "wasi", "subarray", "ESUCCESS", @@ -77,4 +67,4 @@ "wasmer", "getrandom" ] -} +} \ No newline at end of file diff --git a/flake.lock b/flake.lock index cbf59563..cef53262 100644 --- a/flake.lock +++ b/flake.lock @@ -10,16 +10,17 @@ ] }, "locked": { - "lastModified": 1685019724, - "narHash": "sha256-QWsYyrOda1u0qAQVifybjdibeP6NCWzk4cJ2mtrzA2E=", + "lastModified": 1685544246, + "narHash": "sha256-OECUSjN/pqJgS2TjOHwv02qH4NkHKk3BghD3XbEHUKw=", "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "ad282152836f5e3a5c8f34256b29bdea9d16b854", + "rev": "aebfe95c547b0022a9baed3901e945bb386503be", "type": "github" }, "original": { "owner": "AztecProtocol", "repo": "barretenberg", + "rev": "46dba6250e5b1eea792a6b4f6a299c1ecb88d694", "type": "github" } }, diff --git a/flake.nix b/flake.nix index 59c2415a..37ec06f6 100644 --- a/flake.nix +++ b/flake.nix @@ -38,7 +38,7 @@ }; barretenberg = { - url = "github:AztecProtocol/barretenberg"; + url = "github:AztecProtocol/barretenberg?rev=46dba6250e5b1eea792a6b4f6a299c1ecb88d694"; # All of these inputs (a.k.a. dependencies) need to align with inputs we # use so they use the `inputs.*.follows` syntax to reference our inputs inputs = { diff --git a/src/acvm_interop/pwg.rs b/src/acvm_interop/pwg.rs index 816252c1..3d820039 100644 --- a/src/acvm_interop/pwg.rs +++ b/src/acvm_interop/pwg.rs @@ -121,7 +121,7 @@ impl PartialWitnessGenerator for Barretenberg { .collect(); let scalars: Vec<_> = scalars?.into_iter().cloned().collect(); - let (res_x, res_y) = self.encrypt(scalars).map_err(|err| { + let (res_x, res_y) = self.encrypt(scalars, 0).map_err(|err| { OpcodeResolutionError::BlackBoxFunctionFailed(BlackBoxFunc::Pedersen, err.to_string()) })?; initial_witness.insert(outputs[0], res_x); diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 25fc5144..35bbf88a 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -208,32 +208,6 @@ impl SchnorrConstraint { buffer } } -#[derive(Clone, Hash, Debug, Serialize, Deserialize)] -pub(crate) struct ComputeMerkleRootConstraint { - pub(crate) hash_path: Vec, - pub(crate) leaf: i32, - pub(crate) index: i32, - pub(crate) result: i32, -} - -impl ComputeMerkleRootConstraint { - fn to_bytes(&self) -> Vec { - let mut buffer = Vec::new(); - - let hash_path_len = self.hash_path.len() as u32; - - buffer.extend_from_slice(&hash_path_len.to_be_bytes()); - for constraint in self.hash_path.iter() { - buffer.extend_from_slice(&constraint.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()); - - buffer - } -} #[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct Sha256Constraint { @@ -337,9 +311,40 @@ impl Keccak256Constraint { } } +#[derive(Clone, Hash, Debug, Serialize, Deserialize)] +pub(crate) struct Keccak256VarConstraint { + pub(crate) inputs: Vec<(i32, i32)>, + pub(crate) result: [i32; 32], + pub(crate) var_message_size: i32, +} + +impl Keccak256VarConstraint { + fn to_bytes(&self) -> Vec { + let mut buffer = Vec::new(); + + let inputs_len = self.inputs.len() as u32; + buffer.extend_from_slice(&inputs_len.to_be_bytes()); + for constraint in self.inputs.iter() { + buffer.extend_from_slice(&constraint.0.to_be_bytes()); + buffer.extend_from_slice(&constraint.1.to_be_bytes()); + } + + let result_len = self.result.len() as u32; + buffer.extend_from_slice(&result_len.to_be_bytes()); + for constraint in self.result.iter() { + buffer.extend_from_slice(&constraint.to_be_bytes()); + } + + buffer.extend_from_slice(&self.var_message_size.to_be_bytes()); + + buffer + } +} + #[derive(Clone, Hash, Debug, Serialize, Deserialize)] pub(crate) struct PedersenConstraint { pub(crate) inputs: Vec, + pub(crate) hash_index: u32, pub(crate) result_x: i32, pub(crate) result_y: i32, } @@ -354,6 +359,8 @@ impl PedersenConstraint { buffer.extend_from_slice(&constraint.to_be_bytes()); } + buffer.extend_from_slice(&self.hash_index.to_be_bytes()); + buffer.extend_from_slice(&self.result_x.to_be_bytes()); buffer.extend_from_slice(&self.result_y.to_be_bytes()); @@ -429,12 +436,12 @@ pub(crate) struct ConstraintSystem { logic_constraints: Vec, range_constraints: Vec, sha256_constraints: Vec, - compute_merkle_root_constraints: Vec, schnorr_constraints: Vec, ecdsa_secp256k1_constraints: Vec, blake2s_constraints: Vec, block_constraints: Vec, keccak_constraints: Vec, + keccak_var_constraints: Vec, pedersen_constraints: Vec, hash_to_field_constraints: Vec, fixed_base_scalar_mul_constraints: Vec, @@ -478,14 +485,6 @@ impl ConstraintSystem { self } - pub(crate) fn compute_merkle_root_constraints( - mut self, - compute_merkle_root_constraints: Vec, - ) -> Self { - self.compute_merkle_root_constraints = compute_merkle_root_constraints; - self - } - pub(crate) fn schnorr_constraints( mut self, schnorr_constraints: Vec, @@ -591,13 +590,6 @@ impl ConstraintSystem { buffer.extend(&constraint.to_bytes()); } - // 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()); - } - // Serialize each Schnorr constraint let schnorr_len = self.schnorr_constraints.len() as u32; buffer.extend_from_slice(&schnorr_len.to_be_bytes()); @@ -626,6 +618,13 @@ impl ConstraintSystem { buffer.extend(&constraint.to_bytes()); } + // Serialize each Keccak Var constraint + let keccak_var_len = self.keccak_var_constraints.len() as u32; + buffer.extend_from_slice(&keccak_var_len.to_be_bytes()); + for constraint in self.keccak_var_constraints.iter() { + buffer.extend(&constraint.to_bytes()); + } + // Serialize each Pedersen constraint let pedersen_len = self.pedersen_constraints.len() as u32; buffer.extend_from_slice(&pedersen_len.to_be_bytes()); @@ -749,10 +748,8 @@ impl TryFrom<&Circuit> for ConstraintSystem { let mut blake2s_constraints: Vec = Vec::new(); let mut block_constraints: Vec = Vec::new(); let mut keccak_constraints: Vec = Vec::new(); + let keccak_var_constraints: Vec = Vec::new(); let mut pedersen_constraints: Vec = Vec::new(); - // ACVM doesn't generate `ComputeMerkleRootConstraint`s anymore. - // We maintain this to maintain the serialization format. - let compute_merkle_root_constraints: Vec = Vec::new(); let mut schnorr_constraints: Vec = Vec::new(); let mut ecdsa_secp256k1_constraints: Vec = Vec::new(); let mut fixed_base_scalar_mul_constraints: Vec = Vec::new(); @@ -927,6 +924,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { let constraint = PedersenConstraint { inputs, + hash_index: 0, result_x, result_y, }; @@ -1091,13 +1089,13 @@ impl TryFrom<&Circuit> for ConstraintSystem { logic_constraints, range_constraints, sha256_constraints, - compute_merkle_root_constraints, pedersen_constraints, schnorr_constraints, ecdsa_secp256k1_constraints, blake2s_constraints, block_constraints, keccak_constraints, + keccak_var_constraints, hash_to_field_constraints, constraints, fixed_base_scalar_mul_constraints, diff --git a/src/composer.rs b/src/composer.rs index dea94b95..27259ee0 100644 --- a/src/composer.rs +++ b/src/composer.rs @@ -733,6 +733,7 @@ mod test { async fn test_ped_constraints() -> Result<(), Error> { let constraint = PedersenConstraint { inputs: vec![1, 2], + hash_index: 0, result_x: 3, result_y: 4, }; diff --git a/src/lib.rs b/src/lib.rs index cbedb4d2..561da2df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -141,7 +141,10 @@ fn smoke() -> Result<(), Error> { use crate::pedersen::Pedersen; let b = Barretenberg::new(); - let (x, y) = b.encrypt(vec![acvm::FieldElement::zero(), acvm::FieldElement::one()])?; + let (x, y) = b.encrypt( + vec![acvm::FieldElement::zero(), acvm::FieldElement::one()], + 0, + )?; dbg!(x.to_hex(), y.to_hex()); Ok(()) } @@ -213,6 +216,12 @@ mod wasm { } } + impl From for WASMValue { + fn from(value: u32) -> Self { + WASMValue(Some(Value::I32(value as i32))) + } + } + impl From for WASMValue { fn from(value: i32) -> Self { WASMValue(Some(Value::I32(value))) diff --git a/src/pedersen.rs b/src/pedersen.rs index b23bfa0e..a925ca38 100644 --- a/src/pedersen.rs +++ b/src/pedersen.rs @@ -9,7 +9,11 @@ pub(crate) trait Pedersen { right: &FieldElement, ) -> Result; fn compress_many(&self, inputs: Vec) -> Result; - fn encrypt(&self, inputs: Vec) -> Result<(FieldElement, FieldElement), Error>; + fn encrypt( + &self, + inputs: Vec, + hash_index: u32, + ) -> Result<(FieldElement, FieldElement), Error>; } #[cfg(feature = "native")] @@ -49,14 +53,19 @@ impl Pedersen for Barretenberg { Ok(FieldElement::from_be_bytes_reduce(&result_bytes)) } - fn encrypt(&self, inputs: Vec) -> Result<(FieldElement, FieldElement), Error> { + fn encrypt( + &self, + inputs: Vec, + hash_index: u32, + ) -> Result<(FieldElement, FieldElement), Error> { use super::native::field_to_array; let mut inputs_buf = Vec::new(); for f in inputs { inputs_buf.push(field_to_array(&f)?); } - let (point_x_bytes, point_y_bytes) = barretenberg_sys::pedersen::encrypt(&inputs_buf); + let (point_x_bytes, point_y_bytes) = + barretenberg_sys::pedersen::encrypt(&inputs_buf, hash_index); let point_x = FieldElement::from_be_bytes_reduce(&point_x_bytes); let point_y = FieldElement::from_be_bytes_reduce(&point_y_bytes); @@ -108,7 +117,11 @@ impl Pedersen for Barretenberg { Ok(FieldElement::from_be_bytes_reduce(&result_bytes)) } - fn encrypt(&self, inputs: Vec) -> Result<(FieldElement, FieldElement), Error> { + fn encrypt( + &self, + inputs: Vec, + hash_index: u32, + ) -> Result<(FieldElement, FieldElement), Error> { use super::FIELD_BYTES; use crate::barretenberg_structures::Assignments; @@ -117,8 +130,8 @@ impl Pedersen for Barretenberg { let result_ptr: usize = 0; self.call_multiple( - "pedersen_plookup_commit", - vec![&input_ptr, &result_ptr.into()], + "pedersen_plookup_commit_with_hash_index", + vec![&input_ptr, &result_ptr.into(), &hash_index.into()], )?; let result_bytes: [u8; 2 * FIELD_BYTES] = self.read_memory(result_ptr); @@ -174,7 +187,7 @@ fn basic_interop() -> Result<(), Error> { #[test] fn pedersen_hash_to_point() -> Result<(), Error> { let barretenberg = Barretenberg::new(); - let (x, y) = barretenberg.encrypt(vec![FieldElement::zero(), FieldElement::one()])?; + let (x, y) = barretenberg.encrypt(vec![FieldElement::zero(), FieldElement::one()], 0)?; let expected_x = FieldElement::from_hex( "0x0c5e1ddecd49de44ed5e5798d3f6fb7c71fe3d37f5bee8664cf88a445b5ba0af", ) From c39987c595a5521b68693557107d07575f5dffff Mon Sep 17 00:00:00 2001 From: sirasistant Date: Thu, 1 Jun 2023 09:39:55 +0000 Subject: [PATCH 08/13] feat: update acvm pointer --- Cargo.lock | 15 ++++++----- Cargo.toml | 4 +-- src/acvm_interop/proof_system.rs | 2 -- src/acvm_interop/pwg.rs | 14 ++-------- src/barretenberg_structures.rs | 44 +++++++++++++++++++++++++++++--- src/lib.rs | 3 --- 6 files changed, 52 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5650c3da..667f7aa3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,7 +5,7 @@ version = 3 [[package]] name = "acir" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" dependencies = [ "acir_field", "brillig_vm", @@ -18,7 +18,7 @@ dependencies = [ [[package]] name = "acir_field" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" dependencies = [ "ark-bn254", "ark-ff", @@ -31,7 +31,7 @@ dependencies = [ [[package]] name = "acvm" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" dependencies = [ "acir", "acvm_stdlib", @@ -68,7 +68,7 @@ dependencies = [ [[package]] name = "acvm_stdlib" version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" dependencies = [ "acir", ] @@ -270,8 +270,9 @@ dependencies = [ [[package]] name = "barretenberg-sys" -version = "0.1.3" -source = "git+https://github.com/noir-lang/barretenberg-sys#c04e3f46e87c95a364c476bf8f0d310e994c5597" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f8fd58d1ca43e920a1a3b55d52c65ac25cd29f2820d4b2b1c24adafa2c403c" dependencies = [ "bindgen", "cc", @@ -365,7 +366,7 @@ dependencies = [ [[package]] name = "brillig_vm" version = "0.1.0" -source = "git+https://github.com/noir-lang/acvm?rev=3c6740af75125afc8ebb4379f781f8274015e2e2#3c6740af75125afc8ebb4379f781f8274015e2e2" +source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" dependencies = [ "acir_field", "num-bigint", diff --git a/Cargo.toml b/Cargo.toml index 10e5ba00..c76b30a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ thiserror = "1.0.21" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # Native -barretenberg-sys = { git = "https://github.com/noir-lang/barretenberg-sys", optional = true } +barretenberg-sys = { version = "0.2.0", optional = true } # Wasm getrandom = { version = "0.2", optional = true } @@ -67,4 +67,4 @@ wasm = [ ] [patch.crates-io] -acvm = { git = "https://github.com/noir-lang/acvm", rev = "3c6740af75125afc8ebb4379f781f8274015e2e2" } +acvm = { git = "https://github.com/noir-lang/acvm", rev = "1bd8b981a865a8eea6460721c2526857d7241641" } diff --git a/src/acvm_interop/proof_system.rs b/src/acvm_interop/proof_system.rs index ecef812f..b7f7d365 100644 --- a/src/acvm_interop/proof_system.rs +++ b/src/acvm_interop/proof_system.rs @@ -40,8 +40,6 @@ impl ProofSystemCompiler for Barretenberg { | BlackBoxFunc::HashToField128Security | BlackBoxFunc::EcdsaSecp256k1 | BlackBoxFunc::FixedBaseScalarMul => true, - - BlackBoxFunc::AES => false, }, } } diff --git a/src/acvm_interop/pwg.rs b/src/acvm_interop/pwg.rs index 3d820039..599d3dbe 100644 --- a/src/acvm_interop/pwg.rs +++ b/src/acvm_interop/pwg.rs @@ -10,17 +10,6 @@ use crate::schnorr::SchnorrSig; use crate::Barretenberg; impl PartialWitnessGenerator for Barretenberg { - fn aes( - &self, - _initial_witness: &mut WitnessMap, - _inputs: &[FunctionInput], - _outputs: &[Witness], - ) -> Result { - Err(OpcodeResolutionError::UnsupportedBlackBoxFunc( - BlackBoxFunc::AES, - )) - } - fn schnorr_verify( &self, initial_witness: &mut WitnessMap, @@ -113,6 +102,7 @@ impl PartialWitnessGenerator for Barretenberg { &self, initial_witness: &mut WitnessMap, inputs: &[FunctionInput], + domain_separator: u32, outputs: &[Witness], ) -> Result { let scalars: Result, _> = inputs @@ -121,7 +111,7 @@ impl PartialWitnessGenerator for Barretenberg { .collect(); let scalars: Vec<_> = scalars?.into_iter().cloned().collect(); - let (res_x, res_y) = self.encrypt(scalars, 0).map_err(|err| { + let (res_x, res_y) = self.encrypt(scalars, domain_separator).map_err(|err| { OpcodeResolutionError::BlackBoxFunctionFailed(BlackBoxFunc::Pedersen, err.to_string()) })?; initial_witness.insert(outputs[0], res_x); diff --git a/src/barretenberg_structures.rs b/src/barretenberg_structures.rs index 35bbf88a..f78cd598 100644 --- a/src/barretenberg_structures.rs +++ b/src/barretenberg_structures.rs @@ -748,7 +748,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { let mut blake2s_constraints: Vec = Vec::new(); let mut block_constraints: Vec = Vec::new(); let mut keccak_constraints: Vec = Vec::new(); - let keccak_var_constraints: Vec = Vec::new(); + let mut keccak_var_constraints: Vec = Vec::new(); let mut pedersen_constraints: Vec = Vec::new(); let mut schnorr_constraints: Vec = Vec::new(); let mut ecdsa_secp256k1_constraints: Vec = Vec::new(); @@ -910,6 +910,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { } BlackBoxFuncCall::Pedersen { inputs: gadget_call_inputs, + domain_separator, outputs, } => { let mut inputs = Vec::new(); @@ -924,7 +925,7 @@ impl TryFrom<&Circuit> for ConstraintSystem { let constraint = PedersenConstraint { inputs, - hash_index: 0, + hash_index: *domain_separator, result_x, result_y, }; @@ -1062,8 +1063,43 @@ impl TryFrom<&Circuit> for ConstraintSystem { keccak_constraints.push(keccak_constraint); } - BlackBoxFuncCall::AES { .. } => { - return Err(Error::UnsupportedBlackBoxFunc(BlackBoxFunc::AES)) + BlackBoxFuncCall::Keccak256VariableLength { + inputs, + var_message_size, + outputs, + } => { + let mut keccak_inputs: Vec<(i32, i32)> = Vec::new(); + for input in inputs.iter() { + let witness_index = input.witness.witness_index() as i32; + let num_bits = input.num_bits as i32; + keccak_inputs.push((witness_index, num_bits)); + } + + let var_message_size = var_message_size.witness.witness_index() as i32; + + assert_eq!(outputs.len(), 32); + + let mut outputs_iter = outputs.iter(); + let mut result = [0i32; 32]; + for (i, res) in result.iter_mut().enumerate() { + let out_byte = + outputs_iter.next().ok_or_else(|| { + Error::MalformedBlackBoxFunc( + BlackBoxFunc::Keccak256, + format!("Missing rest of output. Tried to get byte {i} but failed"), + ) + })?; + + let out_byte_index = out_byte.witness_index() as i32; + *res = out_byte_index + } + let keccak_var_constraint = Keccak256VarConstraint { + inputs: keccak_inputs, + var_message_size, + result, + }; + + keccak_var_constraints.push(keccak_var_constraint); } }; } diff --git a/src/lib.rs b/src/lib.rs index 561da2df..96f9f8d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,9 +93,6 @@ enum Error { #[error("Malformed Black Box Function: {0} - {1}")] MalformedBlackBoxFunc(BlackBoxFunc, String), - #[error("Unsupported Black Box Function: {0}")] - UnsupportedBlackBoxFunc(BlackBoxFunc), - #[error(transparent)] FromFeature(#[from] FeatureError), From f51e196313c45586488786e23751c1cab58a6327 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Thu, 1 Jun 2023 11:17:04 +0100 Subject: [PATCH 09/13] fix: Add `lib` crate-type to build (#215) add lib crate type --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c76b30a3..2666535c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] [dependencies] acvm = { version = "0.12.0", features = ["bn254"] } From 6d9ad94848d7e07711f76caa8f22ea6a414e1bc1 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 1 Jun 2023 12:10:12 +0100 Subject: [PATCH 10/13] Update Cargo.toml --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2666535c..32ad6fa8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT OR Apache-2.0" crate-type = ["cdylib", "lib"] [dependencies] -acvm = { version = "0.12.0", features = ["bn254"] } +acvm = { version = "0.13.0", features = ["bn254"] } bincode = "1.3.3" bytesize = "1.2" reqwest = { version = "0.11.16", default-features = false, features = ["rustls-tls"] } From a7e48601ac2424204c07d770724fe68e57aa3af4 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Thu, 1 Jun 2023 12:10:49 +0100 Subject: [PATCH 11/13] Update Cargo.toml --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 32ad6fa8..4f8148c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,3 @@ wasm = [ "dep:rust-embed", "dep:getrandom" ] - -[patch.crates-io] -acvm = { git = "https://github.com/noir-lang/acvm", rev = "1bd8b981a865a8eea6460721c2526857d7241641" } From 60407151f3960cdf8aa452027359424fb5c8d5e1 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Thu, 1 Jun 2023 14:35:59 +0000 Subject: [PATCH 12/13] update cargo lock --- Cargo.lock | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 667f7aa3..28eea5d8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,8 +4,9 @@ version = 3 [[package]] name = "acir" -version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf2c137e1046d3af8763216302861aaca592b570784723f8af88ef2c5290a612" dependencies = [ "acir_field", "brillig_vm", @@ -17,8 +18,9 @@ dependencies = [ [[package]] name = "acir_field" -version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32828c2249c5a6bdadfe6dcf79e78ca4f76fbcf63ff691b85ddfdca3eb801ff4" dependencies = [ "ark-bn254", "ark-ff", @@ -30,8 +32,9 @@ dependencies = [ [[package]] name = "acvm" -version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f9dd32921b0d61eaad0c2b169abd4f88070e958aa68ec140e13db0348451c8" dependencies = [ "acir", "acvm_stdlib", @@ -67,8 +70,9 @@ dependencies = [ [[package]] name = "acvm_stdlib" -version = "0.12.0" -source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fc8628157a94db9e6d5ec4c19011957e99818a5d897c8e22418a7115e58674f" dependencies = [ "acir", ] @@ -365,8 +369,9 @@ dependencies = [ [[package]] name = "brillig_vm" -version = "0.1.0" -source = "git+https://github.com/noir-lang/acvm?rev=1bd8b981a865a8eea6460721c2526857d7241641#1bd8b981a865a8eea6460721c2526857d7241641" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ec0f59febbf23ab79da0ad05d4321801a921dca3c557ab2bc40be2a5508df1" dependencies = [ "acir_field", "num-bigint", From 94a8b399a70bff8fac5b7152cf82053cf8ebdd34 Mon Sep 17 00:00:00 2001 From: vezenovm Date: Thu, 1 Jun 2023 16:29:44 +0000 Subject: [PATCH 13/13] remove original commit from barretenberg in flake.nix and flake.lock --- flake.lock | 1 - flake.nix | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/flake.lock b/flake.lock index cef53262..6bfc9462 100644 --- a/flake.lock +++ b/flake.lock @@ -20,7 +20,6 @@ "original": { "owner": "AztecProtocol", "repo": "barretenberg", - "rev": "46dba6250e5b1eea792a6b4f6a299c1ecb88d694", "type": "github" } }, diff --git a/flake.nix b/flake.nix index 37ec06f6..59c2415a 100644 --- a/flake.nix +++ b/flake.nix @@ -38,7 +38,7 @@ }; barretenberg = { - url = "github:AztecProtocol/barretenberg?rev=46dba6250e5b1eea792a6b4f6a299c1ecb88d694"; + url = "github:AztecProtocol/barretenberg"; # All of these inputs (a.k.a. dependencies) need to align with inputs we # use so they use the `inputs.*.follows` syntax to reference our inputs inputs = {