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

fix(avm-transpiler): FDIV and U128 test case #5200

Merged
merged 1 commit into from
Mar 14, 2024
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
2 changes: 1 addition & 1 deletion avm-transpiler/src/opcodes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// All AVM opcodes
/// Keep updated with TS and yellow paper!
#[derive(Copy, Clone)]
#[derive(PartialEq, Copy, Clone)]
pub enum AvmOpcode {
// Compute
ADD,
Expand Down
6 changes: 5 additions & 1 deletion avm-transpiler/src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ pub fn brillig_to_avm(brillig: &Brillig) -> Vec<u8> {
avm_instrs.push(AvmInstruction {
opcode: avm_opcode,
indirect: Some(ALL_DIRECT),
tag: Some(AvmTypeTag::FIELD),
tag: if avm_opcode == AvmOpcode::FDIV {
None
Copy link
Contributor

Choose a reason for hiding this comment

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

@fcarreiro We define FDIV without any tag? In this way, we can save the tag in the serialization. That's good.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fcarreiro All good. I did not see the first comment above.

} else {
Some(AvmTypeTag::FIELD)
},
operands: vec![
AvmOperand::U32 {
value: lhs.to_usize() as u32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ contract AvmTest {
200 as Field
}

#[aztec(public-vm)]
fn addU128(a: U128, b: U128) -> pub U128 {
a + b
}

// /************************************************************************
// * Hashing functions
// ************************************************************************/
Expand Down
18 changes: 18 additions & 0 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ describe('AVM simulator', () => {
expect(results.output).toEqual([new Fr(3)]);
});

it('Should execute contract function that performs U128 addition', async () => {
const calldata: Fr[] = [
// First U128
new Fr(1),
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the only test, I would welcome to have integer values which fit into U128 but not into other lower integer types such as U64, etc... This way we know this is really working for truly U128 integers.
Just replace the values by: 11111111111....., 22222222.... and it remains very readable.

new Fr(2),
// Second U128
new Fr(3),
new Fr(4),
];
const context = initContext({ env: initExecutionEnvironment({ calldata }) });

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

expect(results.reverted).toBe(false);
expect(results.output).toEqual([new Fr(4), new Fr(6)]);
});

describe.each([
['avm_setOpcodeUint8', 8n],
// ['avm_setOpcodeUint16', 60000n],
Expand Down
Loading