Skip to content

Commit

Permalink
feat: PartialStateReference and StateReference structs (AztecProt…
Browse files Browse the repository at this point in the history
…ocol#3827)

Fixes AztecProtocol#3820

**Note**: Temporarily disabled old state hash check in Rollup contract
because the values to compute it the old way are no longer populated and
I didn't want to touch contracts in this PR.
  • Loading branch information
benesjan authored Jan 11, 2024
1 parent 7acbcae commit 4df66da
Show file tree
Hide file tree
Showing 67 changed files with 798 additions and 1,052 deletions.
7 changes: 4 additions & 3 deletions l1-contracts/src/core/Rollup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ contract Rollup is IRollup {
_computePublicInputHash(_l2Block[:HeaderDecoder.BLOCK_HEADER_SIZE], txsHash, inHash);

// @todo @LHerskind Proper genesis state. If the state is empty, we allow anything for now.
if (rollupStateHash != bytes32(0) && rollupStateHash != oldStateHash) {
revert Errors.Rollup__InvalidStateHash(rollupStateHash, oldStateHash);
}
// TODO(#3936): Temporarily disabling this because L2Block encoding has not yet been updated.
// if (rollupStateHash != bytes32(0) && rollupStateHash != oldStateHash) {
// revert Errors.Rollup__InvalidStateHash(rollupStateHash, oldStateHash);
// }

bytes32[] memory publicInputs = new bytes32[](1);
publicInputs[0] = publicInputHash;
Expand Down
9 changes: 5 additions & 4 deletions noir/docs/docs/reference/NoirJS/backend_barretenberg/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@

## Functions

### flattenPublicInputs()
### publicInputsToWitnessMap()

```ts
flattenPublicInputs(publicInputs): string[]
publicInputsToWitnessMap(publicInputs, abi): WitnessMap
```

#### Parameters

| Parameter | Type |
| :------ | :------ |
| `publicInputs` | `WitnessMap` |
| `publicInputs` | `string`[] |
| `abi` | `Abi` |

#### Returns

`string`[]
`WitnessMap`

***

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The representation of a proof
| Member | Type | Description |
| :------ | :------ | :------ |
| `proof` | `Uint8Array` | **Description**<br /><br />An byte array representing the proof |
| `publicInputs` | `WitnessMap` | **Description**<br /><br />Public inputs of a proof |
| `publicInputs` | `string`[] | **Description**<br /><br />Public inputs of a proof |

***

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The representation of a proof
| Member | Type | Description |
| :------ | :------ | :------ |
| `proof` | `Uint8Array` | **Description**<br /><br />An byte array representing the proof |
| `publicInputs` | `WitnessMap` | **Description**<br /><br />Public inputs of a proof |
| `publicInputs` | `string`[] | **Description**<br /><br />Public inputs of a proof |

***

Expand Down
3 changes: 3 additions & 0 deletions yarn-project/accounts/src/testing/create_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export async function createAccounts(pxe: PXE, numberOfAccounts = 1): Promise<Ac
// Prepare deployments
for (let i = 0; i < numberOfAccounts; ++i) {
const account = getSchnorrAccount(pxe, GrumpkinScalar.random(), GrumpkinScalar.random());
// Unfortunately the function below is not stateless and we call it here because it takes a long time to run and
// the results get stored within the account object. By calling it here we increase the probability of all the
// accounts being deployed in the same block because it makes the deploy() method basically instant.
await account.getDeployMethod().then(d => d.simulate({ contractAddressSalt: account.salt }));
accounts.push(account);
}
Expand Down
18 changes: 9 additions & 9 deletions yarn-project/acir-simulator/src/client/view_data_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ export class ViewDataOracle extends TypedOracle {
return undefined;
}
return new BlockHeader(
block.endNoteHashTreeSnapshot.root,
block.endNullifierTreeSnapshot.root,
block.endContractTreeSnapshot.root,
block.endL1ToL2MessageTreeSnapshot.root,
block.endArchiveSnapshot.root,
block.header.state.partial.noteHashTree.root,
block.header.state.partial.nullifierTree.root,
block.header.state.partial.contractTree.root,
block.header.state.l1ToL2MessageTree.root,
block.archive.root,
new Fr(0), // TODO(#3441) privateKernelVkTreeRoot is not present in L2Block and it's not yet populated in noir
block.endPublicDataTreeSnapshot.root,
computeGlobalsHash(block.globalVariables),
block.header.state.partial.publicDataTree.root,
computeGlobalsHash(block.header.globalVariables),
);
}

Expand All @@ -145,10 +145,10 @@ export class ViewDataOracle extends TypedOracle {
if (!block) {
throw new Error(`Block ${i} not found`);
}
if (block.endNullifierTreeSnapshot.root.equals(nullifierTreeRoot)) {
if (block.header.state.partial.nullifierTree.root.equals(nullifierTreeRoot)) {
return i;
}
if (block.startNullifierTreeSnapshot.root.equals(nullifierTreeRoot)) {
if (block.header.state.partial.nullifierTree.root.equals(nullifierTreeRoot)) {
return i - 1;
}
}
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/aztec-node/src/aztec-node/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ export class AztecNodeService implements AztecNode {
* Returns the currently committed block header.
* @returns The current committed block header.
*/
// TODO(#3937): Nuke this
public async getBlockHeader(): Promise<BlockHeader> {
const committedDb = await this.#getWorldState('latest');
const [roots, globalsHash] = await Promise.all([this.getTreeRoots(), committedDb.getLatestGlobalVariablesHash()]);
Expand All @@ -585,7 +586,8 @@ export class AztecNodeService implements AztecNode {
this.log.info(`Simulating tx ${await tx.getTxHash()}`);
const blockNumber = (await this.blockSource.getBlockNumber()) + 1;
const newGlobalVariables = await this.globalVariableBuilder.buildGlobalVariables(new Fr(blockNumber));
const prevGlobalVariables = (await this.blockSource.getBlock(-1))?.globalVariables ?? GlobalVariables.empty();
const prevGlobalVariables =
(await this.blockSource.getBlock(-1))?.header.globalVariables ?? GlobalVariables.empty();

// Instantiate merkle trees so uncommitted updates by this simulation are local to it.
// TODO we should be able to remove this after https://github.com/AztecProtocol/aztec-packages/issues/1869
Expand Down
1 change: 1 addition & 0 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ export function siloNullifier(contract: AztecAddress, innerNullifier: Fr): Fr {
* @param publicDataTreeRoot - The root of the public data tree.
* @returns The block hash.
*/
// TODO(#3941)
export function computeBlockHashWithGlobals(
globals: GlobalVariables,
noteHashTreeRoot: Fr,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/structs/global_variables.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';
import { FieldsOf } from '@aztec/foundation/types';

import { FieldsOf } from '../index.js';
import { serializeToBuffer } from '../utils/index.js';

/**
Expand Down
36 changes: 36 additions & 0 deletions yarn-project/circuits.js/src/structs/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Fr } from '@aztec/foundation/fields';
import { BufferReader } from '@aztec/foundation/serialize';

import { NUM_FIELDS_PER_SHA256 } from '../constants.gen.js';
import { serializeToBuffer } from '../utils/serialize.js';
import { GlobalVariables } from './global_variables.js';
import { AppendOnlyTreeSnapshot } from './rollup/append_only_tree_snapshot.js';
import { StateReference } from './state_reference.js';

/** A header of an L2 block. */
export class Header {
constructor(
/** Snapshot of archive before the block is applied. */
public lastArchive: AppendOnlyTreeSnapshot,
/** Hash of the body of an L2 block. */
public bodyHash: [Fr, Fr],
/** State reference. */
public state: StateReference,
/** Global variables of an L2 block. */
public globalVariables: GlobalVariables,
) {}

toBuffer() {
return serializeToBuffer(this.lastArchive, this.bodyHash, this.state, this.globalVariables);
}

static fromBuffer(buffer: Buffer | BufferReader): Header {
const reader = BufferReader.asReader(buffer);
return new Header(
reader.readObject(AppendOnlyTreeSnapshot),
reader.readArray(NUM_FIELDS_PER_SHA256, Fr) as [Fr, Fr],
reader.readObject(StateReference),
reader.readObject(GlobalVariables),
);
}
}
3 changes: 3 additions & 0 deletions yarn-project/circuits.js/src/structs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './complete_address.js';
export * from './function_data.js';
export * from './function_leaf_preimage.js';
export * from './global_variables.js';
export * from './header.js';
export * from './kernel/combined_accumulated_data.js';
export * from './kernel/combined_constant_data.js';
export * from './kernel/block_header.js';
Expand All @@ -32,6 +33,8 @@ export * from './side_effects.js';
export * from './tx_context.js';
export * from './tx_request.js';
export * from './verification_key.js';
export * from './state_reference.js';
export * from './partial_state_reference.js';

export { FunctionSelector } from '@aztec/foundation/abi';
export * from '@aztec/foundation/aztec-address';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const STRING_ENCODING: BufferEncoding = 'hex';
/**
* Information about the tree roots used for both public and private kernels.
*/
// TODO(#3937): Nuke this
export class BlockHeader {
constructor(
/**
Expand Down
34 changes: 34 additions & 0 deletions yarn-project/circuits.js/src/structs/partial_state_reference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BufferReader } from '@aztec/foundation/serialize';

import { serializeToBuffer } from '../utils/serialize.js';
import { AppendOnlyTreeSnapshot } from './rollup/append_only_tree_snapshot.js';

/**
* Stores snapshots of trees which are commonly needed by base or merge rollup circuits.
*/
export class PartialStateReference {
constructor(
/** Snapshot of the note hash tree. */
public readonly noteHashTree: AppendOnlyTreeSnapshot,
/** Snapshot of the nullifier tree. */
public readonly nullifierTree: AppendOnlyTreeSnapshot,
/** Snapshot of the contract tree. */
public readonly contractTree: AppendOnlyTreeSnapshot,
/** Snapshot of the public data tree. */
public readonly publicDataTree: AppendOnlyTreeSnapshot,
) {}

static fromBuffer(buffer: Buffer | BufferReader): PartialStateReference {
const reader = BufferReader.asReader(buffer);
return new PartialStateReference(
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
);
}

toBuffer() {
return serializeToBuffer(this.noteHashTree, this.nullifierTree, this.contractTree, this.publicDataTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { BufferReader } from '@aztec/foundation/serialize';
import { NUM_FIELDS_PER_SHA256 } from '../../constants.gen.js';
import { serializeToBuffer } from '../../utils/serialize.js';
import { AggregationObject } from '../aggregation_object.js';
import { PartialStateReference } from '../partial_state_reference.js';
import { RollupTypes } from '../shared.js';
import { AppendOnlyTreeSnapshot } from './append_only_tree_snapshot.js';
import { ConstantRollupData } from './base_rollup.js';

/**
Expand All @@ -26,48 +26,19 @@ export class BaseOrMergeRollupPublicInputs {
/**
* Native aggregation state at the end of the rollup circuit.
*/
public endAggregationObject: AggregationObject,
public aggregationObject: AggregationObject,
/**
* Data which is forwarded through the rollup circuits unchanged.
*/
public constants: ConstantRollupData,

/**
* Snapshot of the note hash tree at the start of the rollup circuit.
*/
public startNoteHashTreeSnapshot: AppendOnlyTreeSnapshot,
/**
* Snapshot of the note hash tree at the end of the rollup circuit.
*/
public endNoteHashTreeSnapshot: AppendOnlyTreeSnapshot,

/**
* Snapshot of the nullifier tree at the start of the rollup circuit.
*/
public startNullifierTreeSnapshot: AppendOnlyTreeSnapshot,
/**
* Snapshot of the nullifier tree at the end of the rollup circuit.
*/
public endNullifierTreeSnapshot: AppendOnlyTreeSnapshot,

/**
* Snapshot of the contract tree at the start of the rollup circuit.
*/
public startContractTreeSnapshot: AppendOnlyTreeSnapshot,
/**
* Snapshot of the contract tree at the end of the rollup circuit.
*/
public endContractTreeSnapshot: AppendOnlyTreeSnapshot,

/**
* Snapshot of the public data tree at the start of the rollup circuit.
* Partial state reference at the start of the rollup circuit.
*/
public startPublicDataTreeSnapshot: AppendOnlyTreeSnapshot,
public start: PartialStateReference,
/**
* Snapshot of the public data tree at the end of the rollup circuit.
* Partial state reference at the end of the rollup circuit.
*/
public endPublicDataTreeSnapshot: AppendOnlyTreeSnapshot,

public end: PartialStateReference,
/**
* SHA256 hashes of calldata. Used to make public inputs constant-sized (to then be unpacked on-chain).
* Note: Length 2 for high and low.
Expand All @@ -88,14 +59,8 @@ export class BaseOrMergeRollupPublicInputs {
Fr.fromBuffer(reader),
reader.readObject(AggregationObject),
reader.readObject(ConstantRollupData),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(AppendOnlyTreeSnapshot),
reader.readObject(PartialStateReference),
reader.readObject(PartialStateReference),
reader.readArray(NUM_FIELDS_PER_SHA256, Fr) as [Fr, Fr],
);
}
Expand All @@ -108,20 +73,11 @@ export class BaseOrMergeRollupPublicInputs {
return serializeToBuffer(
this.rollupType,
this.rollupSubtreeHeight,
this.endAggregationObject,
this.aggregationObject,
this.constants,

this.startNoteHashTreeSnapshot,
this.endNoteHashTreeSnapshot,

this.startNullifierTreeSnapshot,
this.endNullifierTreeSnapshot,

this.startContractTreeSnapshot,
this.endContractTreeSnapshot,

this.startPublicDataTreeSnapshot,
this.endPublicDataTreeSnapshot,
this.start,
this.end,

this.calldataHash,
);
Expand Down
Loading

0 comments on commit 4df66da

Please sign in to comment.