Skip to content
Merged
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
42 changes: 42 additions & 0 deletions avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,48 @@ pub fn brillig_to_avm(brillig_bytecode: &[BrilligOpcode<FieldElement>]) -> (Vec<
destination.to_usize() as u32,
));
}
BrilligOpcode::ConditionalMov { destination, source_a, source_b, condition } => {
// Move source_a to destination, if condition is true jump to the next brillig opcode, else move source_b to destination
avm_instrs.push(generate_mov_instruction(
Some(
AddressingModeBuilder::default()
.direct_operand(source_a)
.direct_operand(destination)
.build(),
),
source_a.to_usize() as u32,
destination.to_usize() as u32,
));

unresolved_jumps.insert(
UnresolvedPCLocation {
instruction_index: avm_instrs.len(),
immediate_index: 0,
},
Label::BrilligPC { pc: brillig_pcs_to_avm_pcs.len() as u32 }, // We want to jump to the next brillig opcode
);

avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::JUMPI_32,
indirect: Some(
AddressingModeBuilder::default().direct_operand(condition).build(),
),
operands: vec![make_operand(16, &condition.to_usize())],
immediates: vec![make_unresolved_pc()],
..Default::default()
});

avm_instrs.push(generate_mov_instruction(
Some(
AddressingModeBuilder::default()
.direct_operand(source_b)
.direct_operand(destination)
.build(),
),
source_b.to_usize() as u32,
destination.to_usize() as u32,
));
}
BrilligOpcode::Load { destination, source_pointer } => {
avm_instrs.push(generate_mov_instruction(
Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,15 @@ pub contract AvmTest {
commitment
}

#[public]
fn conditional_move(x: [Field; 1], y: [Field; 1], b: bool) -> [Field; 1] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this actually triggers a CMOV, but there's no other way to test this given our current infra :)

if b {
x
} else {
y
}
}

/************************************************************************
* Misc
************************************************************************/
Expand Down
17 changes: 17 additions & 0 deletions yarn-project/simulator/src/public/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,23 @@ describe('AVM simulator: transpiled Noir contracts', () => {
expect(results.output).toEqual(expectedResult);
});

it('conditional move operations', async () => {
const calldata: Fr[] = [new Fr(27), new Fr(28), new Fr(1)];

const bytecode = getAvmTestContractBytecode('conditional_move');

let context = initContext({ env: initExecutionEnvironment({ calldata }) });
let results = await new AvmSimulator(context).executeBytecode(bytecode);
expect(results.reverted).toBe(false);
expect(results.output).toEqual([new Fr(27)]);

calldata[2] = new Fr(0);
context = initContext({ env: initExecutionEnvironment({ calldata }) });
results = await new AvmSimulator(context).executeBytecode(bytecode);
expect(results.reverted).toBe(false);
expect(results.output).toEqual([new Fr(28)]);
});

describe('U128 addition and overflows', () => {
it('U128 addition', async () => {
const calldata: Fr[] = [
Expand Down