From 97177eaa3cf9a1f9d508b3ee1c92e7d0d43e1e20 Mon Sep 17 00:00:00 2001 From: Maddiaa0 <47148561+Maddiaa0@users.noreply.github.com> Date: Fri, 9 Feb 2024 01:42:34 +0000 Subject: [PATCH] feat(avm): hashing to simulator --- avm-transpiler/src/transpile.rs | 35 ++++++++++++++++++- .../contracts/avm_test_contract/src/main.nr | 10 ++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/avm-transpiler/src/transpile.rs b/avm-transpiler/src/transpile.rs index 62dceb654c66..48b63297d212 100644 --- a/avm-transpiler/src/transpile.rs +++ b/avm-transpiler/src/transpile.rs @@ -1,7 +1,7 @@ use acvm::acir::brillig::Opcode as BrilligOpcode; use acvm::acir::circuit::brillig::Brillig; -use acvm::brillig_vm::brillig::{BinaryFieldOp, BinaryIntOp, ValueOrArray}; +use acvm::brillig_vm::brillig::{BinaryFieldOp, BinaryIntOp, BlackBoxOp, ValueOrArray}; use crate::instructions::{ AvmInstruction, AvmOperand, AvmTypeTag, FIRST_OPERAND_INDIRECT, ZEROTH_OPERAND_INDIRECT, @@ -256,6 +256,7 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec { BrilligOpcode::ForeignCall { function, destinations, inputs, destination_value_types:_, input_value_types:_ } => { handle_foreign_call(&mut avm_instrs, function, destinations, inputs); }, + BrilligOpcode::BlackBox(operation) => handle_black_box_function(&mut avm_instrs, operation), _ => panic!( "Transpiler doesn't know how to process {:?} brillig instruction", brillig_instr @@ -321,6 +322,38 @@ fn handle_foreign_call( }); } +fn handle_black_box_function(avm_instrs: &mut Vec, operation: &BlackBoxOp) { + match operation { + BlackBoxOp::Keccak256 { message, output } => { + // For the meantime, we output keccak as 32 u8s + let hash_offset = message.pointer.0; + let hash_size = message.size.0; + + let out_offset = output.pointer.0; + let out_size = output.size; + assert!(out_size == 32); + + avm_instrs.push(AvmInstruction { + opcode: AvmOpcode::KECCAK, + operands: vec![AvmOperand::U32 { + value: hash_offset as u32, + }, + AvmOperand::U32 { + value: hash_size as u32, + }, + AvmOperand::U32 { + value: out_offset as u32, + }], + ..Default::default() + }); + }, + _ => panic!( + "Transpiler doesn't know how to process BlackBoxOp {:?}", + operation + ), + } +} + /// Compute an array that maps each Brillig pc to an AVM pc. /// This must be done before transpiling to properly transpile jump destinations. /// This is necessary for two reasons: diff --git a/yarn-project/noir-contracts/contracts/avm_test_contract/src/main.nr b/yarn-project/noir-contracts/contracts/avm_test_contract/src/main.nr index 405432acb7cb..97b59ee22cee 100644 --- a/yarn-project/noir-contracts/contracts/avm_test_contract/src/main.nr +++ b/yarn-project/noir-contracts/contracts/avm_test_contract/src/main.nr @@ -19,6 +19,16 @@ contract AvmTest { argA + argB } + /************************************************************************ + * Hashing functions + ************************************************************************/ + #[aztec(public-vm)] + fn oneFieldKeccak(data: [u8; 32]) -> pub Field { + // let data_as_bytes = data.to_be_bytes(32); + let output = dep::std::hash::keccak256(data, 32); + output[0] as Field + } + /************************************************************************ * AvmContext functions ************************************************************************/