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-simulator): add U128 overflow tests to AVM simulator #6281

Merged
merged 1 commit into from
May 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,19 @@ contract AvmTest {
a + b
}

#[aztec(public-vm)]
fn u128_addition_overflow() -> U128 {
let max_u128: U128 = U128::from_hex("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
let one: U128 = U128::from_integer(1);
max_u128 + one
}

#[aztec(public-vm)]
fn u128_from_integer_overflow() -> U128 {
let should_overflow: Field = 2.pow_32(128); // U128::max() + 1;
U128::from_integer(should_overflow)
}

/************************************************************************
* Hashing functions
************************************************************************/
Expand Down
46 changes: 32 additions & 14 deletions yarn-project/simulator/src/avm/avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,40 @@ describe('AVM simulator: transpiled Noir contracts', () => {
expect(isAvmBytecode(bytecode));
});

it('U128 addition', async () => {
const calldata: Fr[] = [
// First U128
new Fr(1),
new Fr(2),
// Second U128
new Fr(3),
new Fr(4),
];
const context = initContext({ env: initExecutionEnvironment({ calldata }) });
describe('U128 addition and overflows', () => {
it('U128 addition', async () => {
const calldata: Fr[] = [
// First U128
new Fr(1),
new Fr(2),
// Second U128
new Fr(3),
new Fr(4),
];
const context = initContext({ env: initExecutionEnvironment({ calldata }) });

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

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

it('Expect failure on U128::add() overflow', async () => {
const bytecode = getAvmTestContractBytecode('u128_addition_overflow');
const results = await new AvmSimulator(initContext()).executeBytecode(bytecode);
expect(results.reverted).toBe(true);
expect(results.revertReason?.message).toEqual('Reverted with output: attempt to add with overflow');
});

it('Expect failure on U128::from_integer() overflow', async () => {
const bytecode = getAvmTestContractBytecode('u128_from_integer_overflow');
const results = await new AvmSimulator(initContext()).executeBytecode(bytecode);
expect(results.reverted).toBe(true);
expect(results.revertReason?.message).toEqual(undefined);
// Note: compiler intrinsic messages (like below) are not known to the AVM
//expect(results.revertReason?.message).toEqual("Reverted with output: call to assert_max_bit_size 'self.__assert_max_bit_size(bit_size)'");
});
});

it('Assertion message', async () => {
Expand Down
Loading