Skip to content

Commit

Permalink
Merge branch 'master' into get-nullified-notes
Browse files Browse the repository at this point in the history
  • Loading branch information
nventuro committed Jan 26, 2024
2 parents 97574ff + 9e6250a commit 1680997
Show file tree
Hide file tree
Showing 32 changed files with 1,327 additions and 1,787 deletions.
104 changes: 58 additions & 46 deletions l1-contracts/src/core/libraries/HeaderLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ import {Hash} from "./Hash.sol";
* | byte start | num bytes | name
* | --- | --- | ---
* | | | Header {
* | | | GlobalVariables {
* | 0x0000 | 0x20 | chainId
* | 0x0020 | 0x20 | version
* | 0x0040 | 0x20 | blockNumber
* | 0x0060 | 0x20 | timestamp
* | | | }
* | 0x0000 | 0x20 | lastArchive.root
* | 0x0020 | 0x04 | lastArchive.nextAvailableLeafIndex
* | 0x0024 | 0x20 | bodyHash
* | | | StateReference {
* | 0x0080 | 0x20 | l1ToL2MessageTree.root
* | 0x00a0 | 0x04 | l1ToL2MessageTree.nextAvailableLeafIndex
* | 0x0044 | 0x20 | l1ToL2MessageTree.root
* | 0x0064 | 0x04 | l1ToL2MessageTree.nextAvailableLeafIndex
* | | | PartialStateReference {
* | 0x00a4 | 0x20 | noteHashTree.root
* | 0x00c4 | 0x04 | noteHashTree.nextAvailableLeafIndex
* | 0x00c8 | 0x20 | nullifierTree.root
* | 0x00e8 | 0x04 | nullifierTree.nextAvailableLeafIndex
* | 0x00ec | 0x20 | contractTree.root
* | 0x010c | 0x04 | contractTree.nextAvailableLeafIndex
* | 0x0110 | 0x20 | publicDataTree.root
* | 0x0130 | 0x04 | publicDataTree.nextAvailableLeafIndex
* | 0x0068 | 0x20 | noteHashTree.root
* | 0x0088 | 0x04 | noteHashTree.nextAvailableLeafIndex
* | 0x008c | 0x20 | nullifierTree.root
* | 0x00ac | 0x04 | nullifierTree.nextAvailableLeafIndex
* | 0x00b0 | 0x20 | contractTree.root
* | 0x00d0 | 0x04 | contractTree.nextAvailableLeafIndex
* | 0x00d4 | 0x20 | publicDataTree.root
* | 0x00f4 | 0x04 | publicDataTree.nextAvailableLeafIndex
* | | | }
* | | | }
* | 0x0134 | 0x20 | lastArchive.root
* | 0x0154 | 0x04 | lastArchive.nextAvailableLeafIndex
* | 0x0158 | 0x20 | bodyHash
* | | | GlobalVariables {
* | 0x00f8 | 0x20 | chainId
* | 0x0118 | 0x20 | version
* | 0x0138 | 0x20 | blockNumber
* | 0x0158 | 0x20 | timestamp
* | | | }
* | | | }
* | --- | --- | ---
*/
Expand All @@ -54,13 +54,6 @@ library HeaderLib {
uint32 nextAvailableLeafIndex;
}

struct GlobalVariables {
uint256 chainId;
uint256 version;
uint256 blockNumber;
uint256 timestamp;
}

struct PartialStateReference {
AppendOnlyTreeSnapshot noteHashTree;
AppendOnlyTreeSnapshot nullifierTree;
Expand All @@ -74,11 +67,18 @@ library HeaderLib {
PartialStateReference partialStateReference;
}

struct GlobalVariables {
uint256 chainId;
uint256 version;
uint256 blockNumber;
uint256 timestamp;
}

struct Header {
GlobalVariables globalVariables;
StateReference stateReference;
AppendOnlyTreeSnapshot lastArchive;
bytes32 bodyHash;
StateReference stateReference;
GlobalVariables globalVariables;
}

/**
Expand Down Expand Up @@ -132,24 +132,36 @@ library HeaderLib {

Header memory header;

header.globalVariables.chainId = uint256(bytes32(_header[:0x20]));
header.globalVariables.version = uint256(bytes32(_header[0x20:0x40]));
header.globalVariables.blockNumber = uint256(bytes32(_header[0x40:0x60]));
header.globalVariables.timestamp = uint256(bytes32(_header[0x60:0x80]));
header.stateReference.l1ToL2MessageTree =
AppendOnlyTreeSnapshot(bytes32(_header[0x80:0xa0]), uint32(bytes4(_header[0xa0:0xa4])));
header.stateReference.partialStateReference.noteHashTree =
AppendOnlyTreeSnapshot(bytes32(_header[0xa4:0xc4]), uint32(bytes4(_header[0xc4:0xc8])));
header.stateReference.partialStateReference.nullifierTree =
AppendOnlyTreeSnapshot(bytes32(_header[0xc8:0xe8]), uint32(bytes4(_header[0xe8:0xec])));
header.stateReference.partialStateReference.contractTree =
AppendOnlyTreeSnapshot(bytes32(_header[0xec:0x10c]), uint32(bytes4(_header[0x10c:0x110])));
header.stateReference.partialStateReference.publicDataTree =
AppendOnlyTreeSnapshot(bytes32(_header[0x110:0x130]), uint32(bytes4(_header[0x130:0x134])));
header.lastArchive =
AppendOnlyTreeSnapshot(bytes32(_header[0x134:0x154]), uint32(bytes4(_header[0x154:0x158])));

header.bodyHash = bytes32(_header[0x158:0x178]);
// Reading lastArchive
header.lastArchive = AppendOnlyTreeSnapshot(
bytes32(_header[0x0000:0x0020]), uint32(bytes4(_header[0x0020:0x0024]))
);

// Reading bodyHash
header.bodyHash = bytes32(_header[0x0024:0x0044]);

// Reading StateReference
header.stateReference.l1ToL2MessageTree = AppendOnlyTreeSnapshot(
bytes32(_header[0x0044:0x0064]), uint32(bytes4(_header[0x0064:0x0068]))
);
header.stateReference.partialStateReference.noteHashTree = AppendOnlyTreeSnapshot(
bytes32(_header[0x0068:0x0088]), uint32(bytes4(_header[0x0088:0x008c]))
);
header.stateReference.partialStateReference.nullifierTree = AppendOnlyTreeSnapshot(
bytes32(_header[0x008c:0x00ac]), uint32(bytes4(_header[0x00ac:0x00b0]))
);
header.stateReference.partialStateReference.contractTree = AppendOnlyTreeSnapshot(
bytes32(_header[0x00b0:0x00d0]), uint32(bytes4(_header[0x00d0:0x00d4]))
);
header.stateReference.partialStateReference.publicDataTree = AppendOnlyTreeSnapshot(
bytes32(_header[0x00d4:0x00f4]), uint32(bytes4(_header[0x00f4:0x00f8]))
);

// Reading GlobalVariables
header.globalVariables.chainId = uint256(bytes32(_header[0x00f8:0x0118]));
header.globalVariables.version = uint256(bytes32(_header[0x0118:0x0138]));
header.globalVariables.blockNumber = uint256(bytes32(_header[0x0138:0x0158]));
header.globalVariables.timestamp = uint256(bytes32(_header[0x0158:0x0178]));

return header;
}
Expand Down
6 changes: 3 additions & 3 deletions l1-contracts/test/Rollup.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract RollupTest is DecoderBase {
bytes memory body = data.body;

assembly {
mstore(add(header, 0x20), 0x420)
mstore(add(header, add(0x20, 0x00f8)), 0x420)
}

bytes32 txsHash = availabilityOracle.publish(body);
Expand All @@ -83,7 +83,7 @@ contract RollupTest is DecoderBase {
bytes memory body = data.body;

assembly {
mstore(add(header, 0x40), 0x420)
mstore(add(header, add(0x20, 0x0118)), 0x420)
}

bytes32 txsHash = availabilityOracle.publish(body);
Expand All @@ -100,7 +100,7 @@ contract RollupTest is DecoderBase {

uint256 ts = block.timestamp + 1;
assembly {
mstore(add(header, 0x80), ts)
mstore(add(header, add(0x20, 0x0158)), ts)
}

bytes32 txsHash = availabilityOracle.publish(body);
Expand Down
6 changes: 3 additions & 3 deletions l1-contracts/test/decoders/Decoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {Hash} from "../../src/core/libraries/Hash.sol";
import {DataStructures} from "../../src/core/libraries/DataStructures.sol";

import {DecoderHelper} from "./helpers/DecoderHelper.sol";
import {HeaderDecoderHelper} from "./helpers/HeaderDecoderHelper.sol";
import {HeaderLibHelper} from "./helpers/HeaderLibHelper.sol";
import {MessagesDecoderHelper} from "./helpers/MessagesDecoderHelper.sol";
import {TxsDecoderHelper} from "./helpers/TxsDecoderHelper.sol";
import {HeaderLib} from "../../src/core/libraries/HeaderLib.sol";
Expand All @@ -27,13 +27,13 @@ import {AvailabilityOracle} from "../../src/core/availability_oracle/Availabilit
*/
contract DecoderTest is DecoderBase {
DecoderHelper internal helper;
HeaderDecoderHelper internal headerHelper;
HeaderLibHelper internal headerHelper;
MessagesDecoderHelper internal messagesHelper;
TxsDecoderHelper internal txsHelper;

function setUp() public virtual {
helper = new DecoderHelper();
headerHelper = new HeaderDecoderHelper();
headerHelper = new HeaderLibHelper();
messagesHelper = new MessagesDecoderHelper();
txsHelper = new TxsDecoderHelper();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.18;

import {HeaderLib} from "../../../src/core/libraries/HeaderLib.sol";

contract HeaderDecoderHelper {
contract HeaderLibHelper {
// A wrapper used such that we get "calldata" and not memory
function decode(bytes calldata _header) public pure returns (HeaderLib.Header memory) {
return HeaderLib.decode(_header);
Expand Down
2 changes: 1 addition & 1 deletion l1-contracts/test/fixtures/empty_block_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
}
}
},
"header": "0x0000000000000000000000000000000000000000000000000000000000007a690000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001801864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f80000000041ed250ed73db6e70805c4efcf0056e8695b79cd3ba418e827c184dee6c6fb0e0000000601a005071a487e4891787073a91504fe6ea55280bc6f65a021fd6f7ca1f10aa02000000015b89af3c0d0bf66ac691697d317108e8f51bb8e3217d56f152b80867ad25d4a7",
"header": "0x1a005071a487e4891787073a91504fe6ea55280bc6f65a021fd6f7ca1f10aa02000000015b89af3c0d0bf66ac691697d317108e8f51bb8e3217d56f152b80867ad25d4a71864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f800000001016642d9ccd8346c403aa4c3fa451178b22534a27035cdaa6ec34ae53b29c50cb000001000bcfa3e9f1a8922ee92c6dc964d6595907c1804a86753774322b468f69d4f278000001801864fcdaa80ff2719154fa7c8a9050662972707168d69eac9db6fd3110829f80000000041ed250ed73db6e70805c4efcf0056e8695b79cd3ba418e827c184dee6c6fb0e0000000600000000000000000000000000000000000000000000000000000000000007a69000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000",
"l1ToL2MessagesHash": "0x076a27c79e5ace2a3d47f9dd2e83e4ff6ea8872b3c2218f66c92b89b55f36560",
"publicInputsHash": "0x1476d6b0483d0d5fc4c9d3b04f50105d97a06bb01054ec7acccac3ff6c85f960"
}
Expand Down
8 changes: 4 additions & 4 deletions l1-contracts/test/fixtures/empty_block_1.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions l1-contracts/test/fixtures/mixed_block_0.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions l1-contracts/test/fixtures/mixed_block_1.json

Large diffs are not rendered by default.

57 changes: 30 additions & 27 deletions yarn-project/acir-simulator/src/avm/avm_memory_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,43 @@ import { Fr } from '@aztec/foundation/fields';

import { strict as assert } from 'assert';

export interface MemoryValue {
add(rhs: MemoryValue): MemoryValue;
sub(rhs: MemoryValue): MemoryValue;
mul(rhs: MemoryValue): MemoryValue;
div(rhs: MemoryValue): MemoryValue;
export abstract class MemoryValue {
public abstract add(rhs: MemoryValue): MemoryValue;
public abstract sub(rhs: MemoryValue): MemoryValue;
public abstract mul(rhs: MemoryValue): MemoryValue;
public abstract div(rhs: MemoryValue): MemoryValue;

// We need this to be able to build an instance of the subclasses.
public abstract build(n: bigint): MemoryValue;

// Use sparingly.
toBigInt(): bigint;
public abstract toBigInt(): bigint;
}

export interface IntegralValue extends MemoryValue {
shl(rhs: IntegralValue): IntegralValue;
shr(rhs: IntegralValue): IntegralValue;
and(rhs: IntegralValue): IntegralValue;
or(rhs: IntegralValue): IntegralValue;
xor(rhs: IntegralValue): IntegralValue;
not(): IntegralValue;
export abstract class IntegralValue extends MemoryValue {
public abstract shl(rhs: IntegralValue): IntegralValue;
public abstract shr(rhs: IntegralValue): IntegralValue;
public abstract and(rhs: IntegralValue): IntegralValue;
public abstract or(rhs: IntegralValue): IntegralValue;
public abstract xor(rhs: IntegralValue): IntegralValue;
public abstract not(): IntegralValue;
}

// TODO: Optimize calculation of mod, etc. Can only do once per class?
abstract class UnsignedInteger implements IntegralValue {
abstract class UnsignedInteger extends IntegralValue {
private readonly bitmask: bigint;
private readonly mod: bigint;

protected constructor(private n: bigint, private bits: bigint) {
super();
assert(bits > 0);
// x % 2^n == x & (2^n - 1)
this.mod = 1n << bits;
this.bitmask = this.mod - 1n;
assert(n < this.mod);
}

// We need this to be able to build an instance of the subclass
// and not of type UnsignedInteger.
protected abstract build(n: bigint): UnsignedInteger;
public abstract build(n: bigint): UnsignedInteger;

public add(rhs: UnsignedInteger): UnsignedInteger {
assert(this.bits == rhs.bits);
Expand Down Expand Up @@ -93,18 +95,14 @@ abstract class UnsignedInteger implements IntegralValue {
public toBigInt(): bigint {
return this.n;
}

public equals(rhs: UnsignedInteger) {
return this.bits == rhs.bits && this.toBigInt() == rhs.toBigInt();
}
}

export class Uint8 extends UnsignedInteger {
constructor(n: number | bigint) {
super(BigInt(n), 8n);
}

protected build(n: bigint): Uint8 {
public build(n: bigint): Uint8 {
return new Uint8(n);
}
}
Expand All @@ -114,7 +112,7 @@ export class Uint16 extends UnsignedInteger {
super(BigInt(n), 16n);
}

protected build(n: bigint): Uint16 {
public build(n: bigint): Uint16 {
return new Uint16(n);
}
}
Expand All @@ -124,7 +122,7 @@ export class Uint32 extends UnsignedInteger {
super(BigInt(n), 32n);
}

protected build(n: bigint): Uint32 {
public build(n: bigint): Uint32 {
return new Uint32(n);
}
}
Expand All @@ -134,7 +132,7 @@ export class Uint64 extends UnsignedInteger {
super(BigInt(n), 64n);
}

protected build(n: bigint): Uint64 {
public build(n: bigint): Uint64 {
return new Uint64(n);
}
}
Expand All @@ -144,19 +142,24 @@ export class Uint128 extends UnsignedInteger {
super(BigInt(n), 128n);
}

protected build(n: bigint): Uint128 {
public build(n: bigint): Uint128 {
return new Uint128(n);
}
}

export class Field implements MemoryValue {
export class Field extends MemoryValue {
public static readonly MODULUS: bigint = Fr.MODULUS;
private readonly rep: Fr;

constructor(v: number | bigint | Fr) {
super();
this.rep = new Fr(v);
}

public build(n: bigint): Field {
return new Field(n);
}

public add(rhs: Field): Field {
return new Field(this.rep.add(rhs.rep));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ describe('External Calls', () => {
const addr = new Fr(123456n);

const argsOffset = 2;
const args = [new Fr(1n), new Fr(2n), new Fr(3n)];
const args = [new Field(1n), new Field(2n), new Field(3n)];
const argsSize = args.length;

const retOffset = 8;
const retSize = 2;

const successOffset = 7;

machineState.memory.set(0, gas);
machineState.memory.set(1, addr);
machineState.memory.set(0, new Field(gas));
machineState.memory.set(1, new Field(addr));
machineState.memory.setSlice(2, args);

const otherContextInstructions: [Opcode, any[]][] = [
Expand All @@ -72,10 +72,10 @@ describe('External Calls', () => {
await instruction.execute(machineState, journal);

const successValue = machineState.memory.get(successOffset);
expect(successValue).toEqual(new Fr(1n));
expect(successValue).toEqual(new Field(1n));

const retValue = machineState.memory.getSlice(retOffset, retSize);
expect(retValue).toEqual([new Fr(1n), new Fr(2n)]);
expect(retValue).toEqual([new Field(1n), new Field(2n)]);

// Check that the storage call has been merged into the parent journal
const { storageWrites } = journal.flush();
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('External Calls', () => {

// No revert has occurred, but the nested execution has failed
const successValue = machineState.memory.get(successOffset);
expect(successValue).toEqual(new Fr(0n));
expect(successValue).toEqual(new Field(0n));
});
});
});
Loading

0 comments on commit 1680997

Please sign in to comment.