Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions acvm-repo/brillig/src/black_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub enum BlackBoxOp {
Sha256 { message: HeapVector, output: HeapArray },
/// Calculates the Blake2s hash of the inputs.
Blake2s { message: HeapVector, output: HeapArray },
/// Calculates the Blake3 hash of the inputs.
Blake3 { message: HeapVector, output: HeapArray },
/// Calculates the Keccak256 hash of the inputs.
Keccak256 { message: HeapVector, output: HeapArray },
/// Verifies a ECDSA signature over the secp256k1 curve.
Expand Down
9 changes: 8 additions & 1 deletion acvm-repo/brillig_vm/src/black_box.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use acir::brillig::{BlackBoxOp, HeapArray, HeapVector, Value};
use acir::{BlackBoxFunc, FieldElement};
use acvm_blackbox_solver::{
blake2s, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, keccak256, sha256,
blake2s, blake3, ecdsa_secp256k1_verify, ecdsa_secp256r1_verify, keccak256, sha256,
BlackBoxFunctionSolver, BlackBoxResolutionError,
};

Expand Down Expand Up @@ -58,6 +58,12 @@ pub(crate) fn evaluate_black_box<Solver: BlackBoxFunctionSolver>(
memory.write_slice(registers.get(output.pointer).to_usize(), &to_value_vec(&bytes));
Ok(())
}
BlackBoxOp::Blake3 { message, output } => {
let message = to_u8_vec(read_heap_vector(memory, registers, message));
let bytes = blake3(message.as_slice())?;
memory.write_slice(registers.get(output.pointer).to_usize(), &to_value_vec(&bytes));
Ok(())
}
BlackBoxOp::Keccak256 { message, output } => {
let message = to_u8_vec(read_heap_vector(memory, registers, message));
let bytes = keccak256(message.as_slice())?;
Expand Down Expand Up @@ -171,6 +177,7 @@ fn black_box_function_from_op(op: &BlackBoxOp) -> BlackBoxFunc {
match op {
BlackBoxOp::Sha256 { .. } => BlackBoxFunc::SHA256,
BlackBoxOp::Blake2s { .. } => BlackBoxFunc::Blake2s,
BlackBoxOp::Blake3 { .. } => BlackBoxFunc::Blake3,
BlackBoxOp::Keccak256 { .. } => BlackBoxFunc::Keccak256,
BlackBoxOp::EcdsaSecp256k1 { .. } => BlackBoxFunc::EcdsaSecp256k1,
BlackBoxOp::EcdsaSecp256r1 { .. } => BlackBoxFunc::EcdsaSecp256r1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ pub(crate) fn convert_black_box_call(
unreachable!("ICE: Blake2s expects one array argument and one array result")
}
}
BlackBoxFunc::Blake3 => {
if let ([message], [BrilligVariable::BrilligArray(result_array)]) =
(function_arguments, function_results)
{
let message_vector = convert_array_or_vector(brillig_context, message, bb_func);
brillig_context.black_box_op_instruction(BlackBoxOp::Blake3 {
message: message_vector.to_heap_vector(),
output: result_array.to_heap_array(),
});
} else {
unreachable!("ICE: Blake3 expects one array argument and one array result")
}
}
BlackBoxFunc::Keccak256 => {
if let (
[message, BrilligVariable::Simple(array_size)],
Expand Down Expand Up @@ -181,9 +194,7 @@ pub(crate) fn convert_black_box_call(
BlackBoxFunc::RecursiveAggregation => unimplemented!(
"ICE: `BlackBoxFunc::RecursiveAggregation` is not implemented by the Brillig VM"
),
BlackBoxFunc::Blake3 => {
unimplemented!("ICE: `BlackBoxFunc::Blake3` is not implemented by the Brillig VM")
}

BlackBoxFunc::Keccakf1600 => {
unimplemented!("ICE: `BlackBoxFunc::Keccakf1600` is not implemented by the Brillig VM")
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_evaluator/src/brillig/brillig_ir/debug_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ impl DebugShow {
BlackBoxOp::Blake2s { message, output } => {
debug_println!(self.enable_debug_trace, " BLAKE2S {} -> {}", message, output);
}
BlackBoxOp::Blake3 { message, output } => {
debug_println!(self.enable_debug_trace, " BLAKE3 {} -> {}", message, output);
}
BlackBoxOp::EcdsaSecp256k1 {
hashed_msg,
public_key_x,
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/brillig_blake3/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "brillig_blake3"
type = "bin"
authors = [""]
compiler_version = ">=0.22.0"

[dependencies]
37 changes: 37 additions & 0 deletions test_programs/execution_success/brillig_blake3/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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 test_programs/execution_success/brillig_blake3/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use dep::std;

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