Skip to content

Commit

Permalink
feat(avm): add toradixle to avm test contract and test files
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasRidhuan committed May 10, 2024
1 parent 93a4ca0 commit 70b3e11
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ fn handle_black_box_function(avm_instrs: &mut Vec<AvmInstruction>, operation: &B
avm_instrs.push(AvmInstruction {
opcode: AvmOpcode::TORADIXLE,
indirect: Some(FIRST_OPERAND_INDIRECT),
tag: Some(AvmTypeTag::FIELD),
tag: None,
operands: vec![
AvmOperand::U32 {
value: input_offset as u32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,10 @@ contract AvmTest {
fn send_l2_to_l1_msg(recipient: EthAddress, content: Field) {
context.message_portal(recipient, content)
}

#[aztec(public-vm)]
fn to_radix_le(input: Field ) -> [u8; 10] {
let result: [u8] =input.to_le_radix(2, 10);
result.as_array()
}
}
13 changes: 13 additions & 0 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,19 @@ describe('AVM simulator: transpiled Noir contracts', () => {
expect(results.revertReason?.message).toEqual('Assertion failed: Values are not equal');
});
});
it('conversions', async () => {
const calldata: Fr[] = [new Fr(0b1011101010100)];
const context = initContext({ env: initExecutionEnvironment({ calldata }) });

const bytecode = getAvmTestContractBytecode('to_radix_le');
const results = await new AvmSimulator(context).executeBytecode(bytecode);

expect(results.reverted).toBe(false);
const expectedResults = '1011101010100'.split('').reverse().slice(0, 10).map(Number);
for (let i = 0; i < 10; i++) {
expect(results.output[i]).toEqual(new Fr(expectedResults[i]));
}
});
});

function getAvmTestContractBytecode(functionName: string): Buffer {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DAGasLeft, L2GasLeft } from '../opcodes/context_getters.js';
import { ToRadixLE } from '../opcodes/conversion.js';
import { Keccak, Pedersen, Poseidon2, Sha256 } from '../opcodes/hashing.js';
import type { Instruction } from '../opcodes/index.js';
import {
Expand Down Expand Up @@ -136,6 +137,8 @@ const INSTRUCTION_SET = () =>
[Poseidon2.opcode, Poseidon2],
[Sha256.opcode, Sha256],
[Pedersen.opcode, Pedersen],
// Conversions
[ToRadixLE.opcode, ToRadixLE],
]);

interface Serializable {
Expand Down

0 comments on commit 70b3e11

Please sign in to comment.