Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(avm-transpiler): minor rust fixes #4889

Merged
merged 1 commit into from
Mar 4, 2024
Merged
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
30 changes: 20 additions & 10 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
/// - TODO: support for avm external calls through this function
fn handle_foreign_call(
avm_instrs: &mut Vec<AvmInstruction>,
function: &String,
function: &str,
destinations: &Vec<ValueOrArray>,
inputs: &Vec<ValueOrArray>,
) {
match function.as_str() {
match function {
"avmOpcodeNoteHashExists" => handle_note_hash_exists(avm_instrs, destinations, inputs),
"emitNoteHash" | "emitNullifier" => handle_emit_note_hash_or_nullifier(
function.as_str() == "emitNullifier",
function == "emitNullifier",
avm_instrs,
destinations,
inputs,
Expand All @@ -252,7 +252,15 @@ fn handle_foreign_call(
"poseidon" => {
handle_single_field_hash_instruction(avm_instrs, function, destinations, inputs)
}
_ => handle_getter_instruction(avm_instrs, function, destinations, inputs),
// Getters.
_ if inputs.len() == 0 && destinations.len() == 1 => {
handle_getter_instruction(avm_instrs, function, destinations, inputs)
}
// Anything else.
_ => panic!(
"Transpiler doesn't know how to process ForeignCall function {}",
function
),
}
}

Expand Down Expand Up @@ -384,7 +392,7 @@ fn handle_nullifier_exists(
/// to reason about. In order to decrease user friction we will use two field outputs.
fn handle_2_field_hash_instruction(
avm_instrs: &mut Vec<AvmInstruction>,
function: &String,
function: &str,
destinations: &[ValueOrArray],
inputs: &[ValueOrArray],
) {
Expand All @@ -405,7 +413,7 @@ fn handle_2_field_hash_instruction(
_ => panic!("Keccak | Poseidon address destination should be a single value"),
};

let opcode = match function.as_str() {
let opcode = match function {
"keccak256" => AvmOpcode::KECCAK,
"sha256" => AvmOpcode::SHA256,
_ => panic!(
Expand Down Expand Up @@ -443,7 +451,7 @@ fn handle_2_field_hash_instruction(
/// representation.
fn handle_single_field_hash_instruction(
avm_instrs: &mut Vec<AvmInstruction>,
function: &String,
function: &str,
destinations: &[ValueOrArray],
inputs: &[ValueOrArray],
) {
Expand All @@ -461,7 +469,7 @@ fn handle_single_field_hash_instruction(
_ => panic!("Poseidon address destination should be a single value"),
};

let opcode = match function.as_str() {
let opcode = match function {
"poseidon" => AvmOpcode::POSEIDON,
_ => panic!(
"Transpiler doesn't know how to process ForeignCall function {:?}",
Expand Down Expand Up @@ -497,20 +505,21 @@ fn handle_single_field_hash_instruction(
/// - ...
fn handle_getter_instruction(
avm_instrs: &mut Vec<AvmInstruction>,
function: &String,
function: &str,
destinations: &Vec<ValueOrArray>,
inputs: &Vec<ValueOrArray>,
) {
// For the foreign calls we want to handle, we do not want inputs, as they are getters
assert!(inputs.is_empty());
assert!(destinations.len() == 1);

let dest_offset_maybe = destinations[0];
let dest_offset = match dest_offset_maybe {
ValueOrArray::MemoryAddress(dest_offset) => dest_offset.0,
_ => panic!("ForeignCall address destination should be a single value"),
};

let opcode = match function.as_str() {
let opcode = match function {
"address" => AvmOpcode::ADDRESS,
"storageAddress" => AvmOpcode::STORAGEADDRESS,
"origin" => AvmOpcode::ORIGIN,
Expand All @@ -529,6 +538,7 @@ fn handle_getter_instruction(
function
),
};

avm_instrs.push(AvmInstruction {
opcode,
indirect: Some(ALL_DIRECT),
Expand Down
Loading