Skip to content

Commit

Permalink
feat: add blake3 black box function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Oct 23, 2023
1 parent 7de37c6 commit acc3f81
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 3 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions acvm-repo/acir/src/circuit/black_box_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub enum BlackBoxFunc {
SHA256,
/// Calculates the Blake2s hash of the inputs.
Blake2s,
/// Calculates the Blake3 hash of the inputs.
Blake3,
/// Verifies a Schnorr signature over a curve which is "pairing friendly" with the curve on which the ACIR circuit is defined.
///
/// The exact curve which this signature uses will vary based on the curve being used by ACIR.
Expand Down Expand Up @@ -62,6 +64,7 @@ impl BlackBoxFunc {
BlackBoxFunc::SHA256 => "sha256",
BlackBoxFunc::SchnorrVerify => "schnorr_verify",
BlackBoxFunc::Blake2s => "blake2s",
BlackBoxFunc::Blake3 => "blake3",
BlackBoxFunc::Pedersen => "pedersen",
BlackBoxFunc::HashToField128Security => "hash_to_field_128_security",
BlackBoxFunc::EcdsaSecp256k1 => "ecdsa_secp256k1",
Expand All @@ -79,6 +82,7 @@ impl BlackBoxFunc {
"sha256" => Some(BlackBoxFunc::SHA256),
"schnorr_verify" => Some(BlackBoxFunc::SchnorrVerify),
"blake2s" => Some(BlackBoxFunc::Blake2s),
"blake3" => Some(BlackBoxFunc::Blake3),
"pedersen" => Some(BlackBoxFunc::Pedersen),
"hash_to_field_128_security" => Some(BlackBoxFunc::HashToField128Security),
"ecdsa_secp256k1" => Some(BlackBoxFunc::EcdsaSecp256k1),
Expand Down
8 changes: 8 additions & 0 deletions acvm-repo/acir/src/circuit/opcodes/black_box_function_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub enum BlackBoxFuncCall {
inputs: Vec<FunctionInput>,
outputs: Vec<Witness>,
},
Blake3 {
inputs: Vec<FunctionInput>,
outputs: Vec<Witness>,
},
SchnorrVerify {
public_key_x: FunctionInput,
public_key_y: FunctionInput,
Expand Down Expand Up @@ -131,6 +135,7 @@ impl BlackBoxFuncCall {
BlackBoxFunc::RANGE => BlackBoxFuncCall::RANGE { input: FunctionInput::dummy() },
BlackBoxFunc::SHA256 => BlackBoxFuncCall::SHA256 { inputs: vec![], outputs: vec![] },
BlackBoxFunc::Blake2s => BlackBoxFuncCall::Blake2s { inputs: vec![], outputs: vec![] },
BlackBoxFunc::Blake3 => BlackBoxFuncCall::Blake3 { inputs: vec![], outputs: vec![] },
BlackBoxFunc::SchnorrVerify => BlackBoxFuncCall::SchnorrVerify {
public_key_x: FunctionInput::dummy(),
public_key_y: FunctionInput::dummy(),
Expand Down Expand Up @@ -186,6 +191,7 @@ impl BlackBoxFuncCall {
BlackBoxFuncCall::RANGE { .. } => BlackBoxFunc::RANGE,
BlackBoxFuncCall::SHA256 { .. } => BlackBoxFunc::SHA256,
BlackBoxFuncCall::Blake2s { .. } => BlackBoxFunc::Blake2s,
BlackBoxFuncCall::Blake3 { .. } => BlackBoxFunc::Blake3,
BlackBoxFuncCall::SchnorrVerify { .. } => BlackBoxFunc::SchnorrVerify,
BlackBoxFuncCall::Pedersen { .. } => BlackBoxFunc::Pedersen,
BlackBoxFuncCall::HashToField128Security { .. } => BlackBoxFunc::HashToField128Security,
Expand All @@ -206,6 +212,7 @@ impl BlackBoxFuncCall {
match self {
BlackBoxFuncCall::SHA256 { inputs, .. }
| BlackBoxFuncCall::Blake2s { inputs, .. }
| BlackBoxFuncCall::Blake3 { inputs, .. }
| BlackBoxFuncCall::Keccak256 { inputs, .. }
| BlackBoxFuncCall::Pedersen { inputs, .. }
| BlackBoxFuncCall::HashToField128Security { inputs, .. } => inputs.to_vec(),
Expand Down Expand Up @@ -295,6 +302,7 @@ impl BlackBoxFuncCall {
match self {
BlackBoxFuncCall::SHA256 { outputs, .. }
| BlackBoxFuncCall::Blake2s { outputs, .. }
| BlackBoxFuncCall::Blake3 { outputs, .. }
| BlackBoxFuncCall::Keccak256 { outputs, .. }
| BlackBoxFuncCall::RecursiveAggregation {
output_aggregation_object: outputs, ..
Expand Down
3 changes: 2 additions & 1 deletion acvm-repo/acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ pub(super) fn transform_internal(
output_aggregation_object: outputs,
..
}
| acir::circuit::opcodes::BlackBoxFuncCall::Blake2s { outputs, .. } => {
| acir::circuit::opcodes::BlackBoxFuncCall::Blake2s { outputs, .. }
| acir::circuit::opcodes::BlackBoxFuncCall::Blake3 { outputs, .. } => {
for witness in outputs {
transformer.mark_solvable(*witness);
}
Expand Down
10 changes: 9 additions & 1 deletion acvm-repo/acvm/src/pwg/blackbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use acir::{
native_types::{Witness, WitnessMap},
FieldElement,
};
use acvm_blackbox_solver::{blake2s, keccak256, sha256};
use acvm_blackbox_solver::{blake2s, blake3, keccak256, sha256};

use super::{insert_value, OpcodeNotSolvable, OpcodeResolutionError};
use crate::BlackBoxFunctionSolver;
Expand Down Expand Up @@ -81,6 +81,14 @@ pub(crate) fn solve(
blake2s,
bb_func.get_black_box_func(),
),
BlackBoxFuncCall::Blake3 { inputs, outputs } => solve_generic_256_hash_opcode(
initial_witness,
inputs,
None,
outputs,
blake3,
bb_func.get_black_box_func(),
),
BlackBoxFuncCall::Keccak256 { inputs, outputs } => solve_generic_256_hash_opcode(
initial_witness,
inputs,
Expand Down
6 changes: 6 additions & 0 deletions acvm-repo/acvm_js/src/black_box_solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub fn blake2s256(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::blake2s(inputs).unwrap().into()
}

/// Calculates the Blake3 hash of the input bytes
#[wasm_bindgen]
pub fn blake3(inputs: &[u8]) -> Vec<u8> {
acvm::blackbox_solver::blake3(inputs).unwrap().into()
}

/// Calculates the Keccak256 hash of the input bytes
#[wasm_bindgen]
pub fn keccak256(inputs: &[u8]) -> Vec<u8> {
Expand Down
1 change: 1 addition & 0 deletions acvm-repo/blackbox_solver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ acir.workspace = true
thiserror.workspace = true

blake2 = "0.10.6"
blake3 = "1.5.0"
sha2 = "0.10.6"
sha3 = "0.10.6"
k256 = { version = "0.11.0", features = [
Expand Down
4 changes: 4 additions & 0 deletions acvm-repo/blackbox_solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub fn blake2s(inputs: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError> {
.map_err(|err| BlackBoxResolutionError::Failed(BlackBoxFunc::Blake2s, err))
}

pub fn blake3(inputs: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError> {
Ok(blake3::hash(inputs).into())
}

pub fn keccak256(inputs: &[u8]) -> Result<[u8; 32], BlackBoxResolutionError> {
generic_hash_256::<Keccak256>(inputs)
.map_err(|err| BlackBoxResolutionError::Failed(BlackBoxFunc::Keccak256, err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl GeneratedAcir {
BlackBoxFunc::Blake2s => {
BlackBoxFuncCall::Blake2s { inputs: inputs[0].clone(), outputs }
}
BlackBoxFunc::Blake3 => BlackBoxFuncCall::Blake3 { inputs: inputs[0].clone(), outputs },
BlackBoxFunc::HashToField128Security => BlackBoxFuncCall::HashToField128Security {
inputs: inputs[0].clone(),
output: outputs[0],
Expand Down Expand Up @@ -933,6 +934,7 @@ fn black_box_func_expected_input_size(name: BlackBoxFunc) -> Option<usize> {
BlackBoxFunc::Keccak256
| BlackBoxFunc::SHA256
| BlackBoxFunc::Blake2s
| BlackBoxFunc::Blake3
| BlackBoxFunc::Pedersen
| BlackBoxFunc::HashToField128Security => None,

Expand Down Expand Up @@ -961,7 +963,10 @@ fn black_box_expected_output_size(name: BlackBoxFunc) -> Option<usize> {
// or the operation.
BlackBoxFunc::AND | BlackBoxFunc::XOR => Some(1),
// 32 byte hash algorithms
BlackBoxFunc::Keccak256 | BlackBoxFunc::SHA256 | BlackBoxFunc::Blake2s => Some(32),
BlackBoxFunc::Keccak256
| BlackBoxFunc::SHA256
| BlackBoxFunc::Blake2s
| BlackBoxFunc::Blake3 => Some(32),
// Hash to field returns a field element
BlackBoxFunc::HashToField128Security => Some(1),
// Pedersen returns a point
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ fn simplify_black_box_func(
match bb_func {
BlackBoxFunc::SHA256 => simplify_hash(dfg, arguments, acvm::blackbox_solver::sha256),
BlackBoxFunc::Blake2s => simplify_hash(dfg, arguments, acvm::blackbox_solver::blake2s),
BlackBoxFunc::Blake3 => simplify_hash(dfg, arguments, acvm::blackbox_solver::blake3),
BlackBoxFunc::Keccak256 => {
match (dfg.get_array_constant(arguments[0]), dfg.get_numeric_constant(arguments[1])) {
(Some((input, _)), Some(num_bytes)) if array_is_constant(dfg, &input) => {
Expand Down
3 changes: 3 additions & 0 deletions noir_stdlib/src/hash.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ pub fn sha256<N>(_input : [u8; N]) -> [u8; 32] {}
#[foreign(blake2s)]
pub fn blake2s<N>(_input : [u8; N]) -> [u8; 32] {}

#[foreign(blake3)]
pub fn blake3<N>(_input : [u8; N]) -> [u8; 32] {}

pub fn pedersen<N>(input : [Field; N]) -> [Field; 2] {
pedersen_with_separator(input, 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const INFO_RESPONSE: &str = r#"{
"range",
"sha256",
"blake2s",
"blake3"
"keccak256",
"schnorr_verify",
"pedersen",
Expand Down
7 changes: 7 additions & 0 deletions tooling/nargo_cli/tests/execution_success/blake3/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "blake3"
type = "bin"
authors = [""]
compiler_version = "0.1"

[dependencies]
38 changes: 38 additions & 0 deletions tooling/nargo_cli/tests/execution_success/blake3/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# hello as bytes
# https://connor4312.github.io/blake3/index.html
x = [104, 101, 108, 108, 111]
result = [
0xea,
0x8f,
0x16,
0x3d,
0xb3,
0x86,
0x82,
0x92,
0x5e,
0x44,
0x91,
0xc5,
0xe5,
0x8d,
0x4b,
0xb3,
0x50,
0x6e,
0xf8,
0xc1,
0x4e,
0xb7,
0x8a,
0x86,
0xe9,
0x08,
0xc5,
0x62,
0x4a,
0x67,
0x20,
0x0f,
]
6 changes: 6 additions & 0 deletions tooling/nargo_cli/tests/execution_success/blake3/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std;

fn main(x: [u8; 5], result: [u8; 32]) {
let digest = std::hash::blake3(x);
assert(digest == result);
}

0 comments on commit acc3f81

Please sign in to comment.