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
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