Skip to content

Commit

Permalink
refactor: nuking old BlockHeader (AztecProtocol#4154)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jan 30, 2024
1 parent 90b4651 commit 997791a
Show file tree
Hide file tree
Showing 107 changed files with 2,817 additions and 2,850 deletions.
8 changes: 5 additions & 3 deletions docs/docs/developers/contracts/syntax/context.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ The call context contains information about the current call being made:
- is_static_call: This will be set if and only if the current call is a static call. In a static call, state changing altering operations are not allowed.
- is_contract_deployment: This will be set if and only if the current call is the contract's constructor.

### Block Header
### Header

Another structure that is contained within the context is the Block Header object. This object is a special one as it contains all of the roots of Aztec's data trees.
Another structure that is contained within the context is the Header object.
In the private context this is a header of a block which used to generate proofs against.
In the public context this TBD TODO(#4262)

#include_code block-header /yarn-project/noir-protocol-circuits/src/crates/types/src/abis/block_header.nr rust
#include_code header /yarn-project/noir-protocol-circuits/src/crates/types/src/header.nr rust

### Contract Deployment Data

Expand Down
10 changes: 5 additions & 5 deletions l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ library Constants {
uint256 internal constant MAX_NOTES_PER_PAGE = 10;
uint256 internal constant VIEW_NOTE_ORACLE_RETURN_LENGTH = 212;
uint256 internal constant CALL_CONTEXT_LENGTH = 8;
uint256 internal constant BLOCK_HEADER_LENGTH = 7;
uint256 internal constant HEADER_LENGTH = 18;
uint256 internal constant FUNCTION_DATA_LENGTH = 4;
uint256 internal constant CONTRACT_DEPLOYMENT_DATA_LENGTH = 6;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 189;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH = 200;
uint256 internal constant CONTRACT_STORAGE_UPDATE_REQUEST_LENGTH = 3;
uint256 internal constant CONTRACT_STORAGE_READ_LENGTH = 2;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH = 190;
uint256 internal constant GET_NOTES_ORACLE_RETURN_LENGTH = 674;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 199;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 87;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 177;
uint256 internal constant CALL_PRIVATE_FUNCTION_RETURN_SIZE = 210;
uint256 internal constant PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 98;
uint256 internal constant PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH = 188;
uint256 internal constant COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP = 2048;
uint256 internal constant PUBLIC_DATA_WRITES_NUM_BYTES_PER_BASE_ROLLUP = 1024;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Destroys the backend
generateFinalProof(decompressedWitness): Promise<ProofData>
```

Generate a final proof. This is the proof for the circuit which will verify
intermediate proofs and or can be seen as the proof created for regular circuits.

#### Parameters

| Parameter | Type |
Expand All @@ -65,10 +68,6 @@ generateFinalProof(decompressedWitness): Promise<ProofData>

[`Backend`](../interfaces/Backend.md).[`generateFinalProof`](../interfaces/Backend.md#generatefinalproof)

#### Description

Generates a final proof (not meant to be verified in another circuit)

***

### generateIntermediateProof()
Expand All @@ -77,6 +76,14 @@ Generates a final proof (not meant to be verified in another circuit)
generateIntermediateProof(witness): Promise<ProofData>
```

Generates an intermediate proof. This is the proof that can be verified
in another circuit.

This is sometimes referred to as a recursive proof.
We avoid this terminology as the only property of this proof
that matters is the fact that it is easy to verify in another circuit.
We _could_ choose to verify this proof outside of a circuit just as easily.

#### Parameters

| Parameter | Type |
Expand Down Expand Up @@ -105,6 +112,16 @@ const intermediateProof = await backend.generateIntermediateProof(witness);
generateIntermediateProofArtifacts(proofData, numOfPublicInputs): Promise<object>
```

Generates artifacts that will be passed to a circuit that will verify this proof.

Instead of passing the proof and verification key as a byte array, we pass them
as fields which makes it cheaper to verify in a circuit.

The proof that is passed here will have been created using the `generateIntermediateProof`
method.

The number of public inputs denotes how many public inputs are in the inner proof.

#### Parameters

| Parameter | Type | Default value |
Expand Down
30 changes: 7 additions & 23 deletions yarn-project/acir-simulator/src/acvm/deserialize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
BlockHeader,
CallContext,
ContractDeploymentData,
ContractStorageRead,
ContractStorageUpdateRequest,
HEADER_LENGTH,
Header,
MAX_NEW_COMMITMENTS_PER_CALL,
MAX_NEW_L2_TO_L1_MSGS_PER_CALL,
MAX_NEW_NULLIFIERS_PER_CALL,
Expand Down Expand Up @@ -100,16 +101,7 @@ export function extractPrivateCircuitPublicInputs(
const encryptedLogPreimagesLength = witnessReader.readField();
const unencryptedLogPreimagesLength = witnessReader.readField();

const blockHeader = new BlockHeader(
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
Fr.ZERO, // TODO(#3441)
witnessReader.readField(),
witnessReader.readField(),
);
const header = Header.fromFieldArray(witnessReader.readFieldArray(HEADER_LENGTH));

const contractDeploymentData = new ContractDeploymentData(
new Point(witnessReader.readField(), witnessReader.readField()),
Expand Down Expand Up @@ -138,7 +130,7 @@ export function extractPrivateCircuitPublicInputs(
unencryptedLogsHash,
encryptedLogPreimagesLength,
unencryptedLogPreimagesLength,
blockHeader,
header,
contractDeploymentData,
chainId,
version,
Expand Down Expand Up @@ -184,16 +176,8 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
const unencryptedLogsHash = witnessReader.readFieldArray(NUM_FIELDS_PER_SHA256);
const unencryptedLogPreimagesLength = witnessReader.readField();

const blockHeader = new BlockHeader(
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
witnessReader.readField(),
Fr.ZERO, // TODO(#3441)
witnessReader.readField(),
witnessReader.readField(),
);
const header = Header.fromFieldArray(witnessReader.readFieldArray(HEADER_LENGTH));

const proverAddress = AztecAddress.fromField(witnessReader.readField());

return new PublicCircuitPublicInputs(
Expand All @@ -211,7 +195,7 @@ export function extractPublicCircuitPublicInputs(partialWitness: ACVMWitness, ac
newL2ToL1Msgs,
unencryptedLogsHash,
unencryptedLogPreimagesLength,
blockHeader,
header,
proverAddress,
);
}
20 changes: 5 additions & 15 deletions yarn-project/acir-simulator/src/acvm/oracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ACVMField } from '../acvm_types.js';
import { frToNumber, fromACVMField } from '../deserialize.js';
import {
toACVMField,
toACVMHeader,
toAcvmCallPrivateStackItem,
toAcvmEnqueuePublicFunctionResult,
toAcvmL1ToL2MessageLoadOracleInputs,
Expand Down Expand Up @@ -124,25 +125,14 @@ export class Oracle {
return witness.toFieldArray().map(toACVMField);
}

async getBlockHeader([blockNumber]: ACVMField[]): Promise<ACVMField[]> {
async getHeader([blockNumber]: ACVMField[]): Promise<ACVMField[]> {
const parsedBlockNumber = frToNumber(fromACVMField(blockNumber));

const blockHeader = await this.typedOracle.getBlockHeader(parsedBlockNumber);
if (!blockHeader) {
const header = await this.typedOracle.getHeader(parsedBlockNumber);
if (!header) {
throw new Error(`Block header not found for block ${parsedBlockNumber}.`);
}
return blockHeader.toArray().map(toACVMField);
}

// TODO(#3564) - Nuke this oracle and inject the number directly to context
async getNullifierRootBlockNumber([nullifierTreeRoot]: ACVMField[]): Promise<ACVMField> {
const parsedRoot = fromACVMField(nullifierTreeRoot);

const blockNumber = await this.typedOracle.getNullifierRootBlockNumber(parsedRoot);
if (!blockNumber) {
throw new Error(`Block header not found for block ${parsedRoot}.`);
}
return toACVMField(blockNumber);
return toACVMHeader(header);
}

async getAuthWitness([messageHash]: ACVMField[]): Promise<ACVMField[]> {
Expand Down
9 changes: 2 additions & 7 deletions yarn-project/acir-simulator/src/acvm/oracle/typed_oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PublicKey,
UnencryptedL2Log,
} from '@aztec/circuit-types';
import { BlockHeader, GrumpkinPrivateKey, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
import { GrumpkinPrivateKey, Header, PrivateCallStackItem, PublicCallRequest } from '@aztec/circuits.js';
import { FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -111,12 +111,7 @@ export abstract class TypedOracle {
throw new Error('Not available.');
}

getBlockHeader(_blockNumber: number): Promise<BlockHeader | undefined> {
throw new Error('Not available.');
}

// TODO(#3564) - Nuke this oracle and inject the number directly to context
getNullifierRootBlockNumber(_nullifierTreeRoot: Fr): Promise<number | undefined> {
getHeader(_blockNumber: number): Promise<Header | undefined> {
throw new Error('Not available.');
}

Expand Down
18 changes: 5 additions & 13 deletions yarn-project/acir-simulator/src/acvm/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
BlockHeader,
CallContext,
ContractDeploymentData,
FunctionData,
GlobalVariables,
Header,
PrivateCallStackItem,
PrivateCircuitPublicInputs,
PublicCallRequest,
Expand Down Expand Up @@ -103,19 +103,11 @@ export function toACVMContractDeploymentData(contractDeploymentData: ContractDep

/**
* Converts a block header into ACVM fields.
* @param blockHeader - The block header object to convert.
* @param header - The block header object to convert.
* @returns The ACVM fields.
*/
export function toACVMBlockHeader(blockHeader: BlockHeader): ACVMField[] {
return [
toACVMField(blockHeader.noteHashTreeRoot),
toACVMField(blockHeader.nullifierTreeRoot),
toACVMField(blockHeader.contractTreeRoot),
toACVMField(blockHeader.l1ToL2MessageTreeRoot),
toACVMField(blockHeader.archiveRoot),
toACVMField(blockHeader.publicDataTreeRoot),
toACVMField(blockHeader.globalVariablesHash),
];
export function toACVMHeader(header: Header): ACVMField[] {
return header.toFieldArray().map(toACVMField);
}

/**
Expand Down Expand Up @@ -157,7 +149,7 @@ export function toACVMPublicInputs(publicInputs: PrivateCircuitPublicInputs): AC
toACVMField(publicInputs.encryptedLogPreimagesLength),
toACVMField(publicInputs.unencryptedLogPreimagesLength),

...toACVMBlockHeader(publicInputs.blockHeader),
...toACVMHeader(publicInputs.historicalHeader),

...toACVMContractDeploymentData(publicInputs.contractDeploymentData),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AuthWitness, FunctionL2Logs, L1NotePayload, Note, UnencryptedL2Log } from '@aztec/circuit-types';
import {
BlockHeader,
CallContext,
ContractDeploymentData,
FunctionData,
FunctionSelector,
Header,
PublicCallRequest,
ReadRequestMembershipWitness,
SideEffect,
Expand All @@ -19,9 +19,9 @@ import { createDebugLogger } from '@aztec/foundation/log';

import {
NoteData,
toACVMBlockHeader,
toACVMCallContext,
toACVMContractDeploymentData,
toACVMHeader,
toACVMWitness,
} from '../acvm/index.js';
import { PackedArgsCache } from '../common/packed_args_cache.js';
Expand Down Expand Up @@ -64,8 +64,8 @@ export class ClientExecutionContext extends ViewDataOracle {
private readonly argsHash: Fr,
private readonly txContext: TxContext,
private readonly callContext: CallContext,
/** Data required to reconstruct the block hash, it contains historical roots. */
protected readonly blockHeader: BlockHeader,
/** Header of a block whose state is used during private execution. */
protected readonly historicalHeader: Header,
/** List of transient auth witnesses to be used during this simulation */
protected readonly authWitnesses: AuthWitness[],
private readonly packedArgsCache: PackedArgsCache,
Expand All @@ -74,7 +74,7 @@ export class ClientExecutionContext extends ViewDataOracle {
private readonly curve: Grumpkin,
protected log = createDebugLogger('aztec:simulator:client_execution_context'),
) {
super(contractAddress, blockHeader, authWitnesses, db, undefined, log);
super(contractAddress, historicalHeader, authWitnesses, db, undefined, log);
}

// We still need this function until we can get user-defined ordering of structs for fn arguments
Expand All @@ -97,7 +97,7 @@ export class ClientExecutionContext extends ViewDataOracle {

const fields = [
...toACVMCallContext(this.callContext),
...toACVMBlockHeader(this.blockHeader),
...toACVMHeader(this.historicalHeader),
...toACVMContractDeploymentData(contractDeploymentData),

this.txContext.chainId,
Expand Down Expand Up @@ -341,7 +341,7 @@ export class ClientExecutionContext extends ViewDataOracle {
argsHash,
derivedTxContext,
derivedCallContext,
this.blockHeader,
this.historicalHeader,
this.authWitnesses,
this.packedArgsCache,
this.noteCache,
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/acir-simulator/src/client/db_oracle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { L2Block, MerkleTreeId, NullifierMembershipWitness, PublicDataWitness } from '@aztec/circuit-types';
import { BlockHeader, CompleteAddress } from '@aztec/circuits.js';
import { CompleteAddress, Header } from '@aztec/circuits.js';
import { FunctionArtifactWithDebugMetadata, FunctionSelector } from '@aztec/foundation/abi';
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
Expand Down Expand Up @@ -111,9 +111,9 @@ export interface DBOracle extends CommitmentsDB {
* Retrieve the databases view of the Block Header object.
* This structure is fed into the circuits simulator and is used to prove against certain historical roots.
*
* @returns A Promise that resolves to a BlockHeader object.
* @returns A Promise that resolves to a Header object.
*/
getBlockHeader(): Promise<BlockHeader>;
getHeader(): Promise<Header>;

/**
* Fetch the index of the leaf in the respective tree
Expand Down
Loading

0 comments on commit 997791a

Please sign in to comment.