Skip to content

Commit

Permalink
feat(avm): hashing to simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Feb 9, 2024
1 parent 722a888 commit 97177ea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
35 changes: 34 additions & 1 deletion avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -256,6 +256,7 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
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
Expand Down Expand Up @@ -321,6 +322,38 @@ fn handle_foreign_call(
});
}

fn handle_black_box_function(avm_instrs: &mut Vec<AvmInstruction>, 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
************************************************************************/
Expand Down

0 comments on commit 97177ea

Please sign in to comment.