This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: merge `barretenberg_static_lib` and `barretenberg_wasm` * fix: disable dead code warnings * chore: run smoke test on both native and wasm backends * chore: define proper interface for wasm internals of `Barretenberg` * feat: run tests on both native and wasm backends * chore: move barretenberg back up to crate root * feat: standardise interface of `StandardComposer` * feat: manage CRS on barretenberg struct * feat: remove `StandardComposer` * feat: disallow compiling for both native and wasm * feat: make usage of `Barretenberg` struct immutable * chore: comment nits * feat!: implement `Backend` on `Barretenberg` * chore: remove unused import * feat: inline black box function logic from `common` * fix: address compilation issues * chore: clippy * chore: use private traits to enforce interface on native/wasm code * native build by default * chore: misc changes * feat: prefer `G2` over `CRS` where no g1 data is used * chore: typo * chore: run clippy on wasm feature * chore: Update nix to expose wasm feature targets * chore: Comment updates * chore: replace cfg_if with modules * feat: standardise pow2ceil implementation * Update acvm_backend_barretenberg/src/composer.rs --------- Co-authored-by: Blaine Bublitz <[email protected]>
- Loading branch information
1 parent
2ea62f0
commit ba1d0d6
Showing
42 changed files
with
1,259 additions
and
2,086 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[workspace] | ||
|
||
members = ["common", "barretenberg_wasm", "barretenberg_static_lib", "aztec_backend_wasm"] | ||
members = ["common", "acvm_backend_barretenberg", "aztec_backend_wasm"] | ||
|
||
[workspace.package] | ||
authors = ["The Noir Team <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use std::env; | ||
|
||
// Useful for printing debugging messages during the build | ||
// macro_rules! p { | ||
// ($($tokens: tt)*) => { | ||
// println!("cargo:warning={}", format!($($tokens)*)) | ||
// } | ||
// } | ||
|
||
fn main() -> Result<(), String> { | ||
let native_backend = env::var("CARGO_FEATURE_NATIVE").is_ok(); | ||
|
||
if native_backend { | ||
Ok(()) | ||
} else { | ||
match env::var("BARRETENBERG_BIN_DIR") { | ||
Ok(bindir) => { | ||
println!("cargo:rustc-env=BARRETENBERG_BIN_DIR={bindir}"); | ||
Ok(()) | ||
} | ||
Err(_) => { | ||
if let Ok(bindir) = pkg_config::get_variable("barretenberg", "bindir") { | ||
println!("cargo:rustc-env=BARRETENBERG_BIN_DIR={bindir}"); | ||
Ok(()) | ||
} else { | ||
Err("Unable to locate barretenberg.wasm - Please set the BARRETENBERG_BIN_DIR env var to the directory where it exists".into()) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use crate::Barretenberg; | ||
|
||
mod proof_system; | ||
mod pwg; | ||
mod smart_contract; | ||
|
||
impl common::acvm::Backend for Barretenberg {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
use common::acvm::acir::BlackBoxFunc; | ||
use common::acvm::acir::{circuit::opcodes::BlackBoxFuncCall, native_types::Witness}; | ||
use common::acvm::pwg::{hash, logic, range, signature, witness_to_value}; | ||
use common::acvm::{FieldElement, OpcodeResolution}; | ||
use common::acvm::{OpcodeResolutionError, PartialWitnessGenerator}; | ||
|
||
use std::collections::BTreeMap; | ||
|
||
use crate::pedersen::Pedersen; | ||
use crate::scalar_mul::ScalarMul; | ||
use crate::schnorr::SchnorrSig; | ||
use crate::Barretenberg; | ||
|
||
use blake2::{Blake2s, Digest}; | ||
|
||
mod merkle; | ||
|
||
impl PartialWitnessGenerator for Barretenberg { | ||
fn solve_black_box_function_call( | ||
&self, | ||
initial_witness: &mut BTreeMap<Witness, FieldElement>, | ||
func_call: &BlackBoxFuncCall, | ||
) -> Result<OpcodeResolution, OpcodeResolutionError> { | ||
match func_call.name { | ||
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)? | ||
} | ||
BlackBoxFunc::AES | BlackBoxFunc::Keccak256 => { | ||
return Err(OpcodeResolutionError::UnsupportedBlackBoxFunc( | ||
func_call.name, | ||
)) | ||
} | ||
BlackBoxFunc::MerkleMembership => { | ||
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)?; | ||
|
||
let _index = inputs_iter.next().expect("expected an index"); | ||
let index = witness_to_value(initial_witness, _index.witness)?; | ||
|
||
let hash_path: Result<Vec<_>, _> = inputs_iter | ||
.map(|input| witness_to_value(initial_witness, input.witness)) | ||
.collect(); | ||
|
||
let valid_proof = merkle::check_membership(self, hash_path?, root, index, leaf); | ||
|
||
let result = if valid_proof { | ||
FieldElement::one() | ||
} else { | ||
FieldElement::zero() | ||
}; | ||
|
||
initial_witness.insert(func_call.outputs[0], result); | ||
} | ||
BlackBoxFunc::SchnorrVerify => { | ||
// In barretenberg, if the signature fails, then the whole thing fails. | ||
// | ||
|
||
let mut inputs_iter = func_call.inputs.iter(); | ||
|
||
let _pub_key_x = inputs_iter | ||
.next() | ||
.expect("expected `x` component for public key"); | ||
let pub_key_x = | ||
witness_to_value(initial_witness, _pub_key_x.witness)?.to_be_bytes(); | ||
|
||
let _pub_key_y = inputs_iter | ||
.next() | ||
.expect("expected `y` component for public key"); | ||
let pub_key_y = | ||
witness_to_value(initial_witness, _pub_key_y.witness)?.to_be_bytes(); | ||
|
||
let pub_key_bytes: Vec<u8> = pub_key_x | ||
.iter() | ||
.copied() | ||
.chain(pub_key_y.to_vec()) | ||
.collect(); | ||
let pub_key: [u8; 64] = pub_key_bytes.try_into().unwrap(); | ||
|
||
let mut signature = [0u8; 64]; | ||
for (i, sig) in signature.iter_mut().enumerate() { | ||
let _sig_i = inputs_iter.next().unwrap_or_else(|| { | ||
panic!("signature should be 64 bytes long, found only {i} bytes") | ||
}); | ||
let sig_i = witness_to_value(initial_witness, _sig_i.witness)?; | ||
*sig = *sig_i.to_be_bytes().last().unwrap() | ||
} | ||
|
||
let mut message = Vec::new(); | ||
for msg in inputs_iter { | ||
let msg_i_field = witness_to_value(initial_witness, msg.witness)?; | ||
let msg_i = *msg_i_field.to_be_bytes().last().unwrap(); | ||
message.push(msg_i); | ||
} | ||
|
||
let valid_signature = self.verify_signature(pub_key, signature, &message); | ||
if !valid_signature { | ||
dbg!("signature has failed to verify"); | ||
} | ||
|
||
let result = if valid_signature { | ||
FieldElement::one() | ||
} else { | ||
FieldElement::zero() | ||
}; | ||
|
||
initial_witness.insert(func_call.outputs[0], result); | ||
} | ||
BlackBoxFunc::Pedersen => { | ||
let inputs_iter = func_call.inputs.iter(); | ||
|
||
let scalars: Result<Vec<_>, _> = inputs_iter | ||
.map(|input| witness_to_value(initial_witness, input.witness)) | ||
.collect(); | ||
let scalars: Vec<_> = scalars?.into_iter().cloned().collect(); | ||
|
||
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); | ||
} | ||
BlackBoxFunc::HashToField128Security => { | ||
let mut hasher = <Blake2s as blake2::Digest>::new(); | ||
|
||
// 0. For each input in the vector of inputs, check if we have their witness assignments (Can do this outside of match, since they all have inputs) | ||
for input_index in func_call.inputs.iter() { | ||
let witness = &input_index.witness; | ||
let num_bits = input_index.num_bits; | ||
|
||
let assignment = witness_to_value(initial_witness, *witness)?; | ||
|
||
let bytes = assignment.fetch_nearest_bytes(num_bits as usize); | ||
|
||
hasher.update(bytes); | ||
} | ||
let result = hasher.finalize(); | ||
|
||
let reduced_res = FieldElement::from_be_bytes_reduce(&result); | ||
assert_eq!(func_call.outputs.len(), 1); | ||
|
||
initial_witness.insert(func_call.outputs[0], reduced_res); | ||
} | ||
BlackBoxFunc::FixedBaseScalarMul => { | ||
let scalar = witness_to_value(initial_witness, func_call.inputs[0].witness)?; | ||
|
||
let (pub_x, pub_y) = self.fixed_base(scalar); | ||
|
||
initial_witness.insert(func_call.outputs[0], pub_x); | ||
initial_witness.insert(func_call.outputs[1], pub_y); | ||
} | ||
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) | ||
} | ||
} |
Oops, something went wrong.