Skip to content

Commit

Permalink
feat: add public teardown to circuit structs (AztecProtocol#6191)
Browse files Browse the repository at this point in the history
See
[spec](https://66352ac52788c434c51f41cb--aztec-docs-dev.netlify.app/protocol-specs/gas-and-fees/kernel-tracking).

Only deviation is that I dropped the notion of PublicConstantData
because we were pushing `fee_payer` into it for
`PublicKernelCircuitPublicInputs`, only to lift it back to the top level
of `KernelCircuitPublicInputs`. So this simplifies the structs a bit.
  • Loading branch information
just-mitch authored May 3, 2024
1 parent 22625f8 commit 03e1b93
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 31 deletions.
11 changes: 4 additions & 7 deletions docs/docs/protocol-specs/gas-and-fees/kernel-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ PrivateCircuitPublicInputs --> Header
class PrivateKernelCircuitPublicInputs {
+u32 min_revertible_side_effect_counter
+AztecAddress fee_payer
+Field public_teardown_function_hash
+CallRequest public_teardown_call_request
+PrivateAccumulatedData end
+CombinedConstantData constants
}
Expand Down Expand Up @@ -208,6 +208,7 @@ It must:
- compute gas used for the revertible and non-revertible. Both sets can have a DA component, but the revertible set will also include the teardown gas allocations the user specified (if any). This ensures that the user effectively pre-pays for the gas consumed in teardown.
- ensure the gas used (across revertible and non-revertible) is less than the gas limits
- ensure that `fee_payer` is set, and set it in the `PublicKernelCircuitPublicInputs`
- set the `public_teardown_call_request` in the `PublicKernelCircuitPublicInputs`
- copy the constants from the `PrivateKernelData` to the `PublicKernelCircuitPublicInputs.constants`

# Mempool/Node Validation
Expand Down Expand Up @@ -292,7 +293,8 @@ class PublicKernelCircuitPublicInputs {
+PublicAccumulatedData end_non_revertible
+PublicAccumulatedData end
+CombinedConstantData constants
+PublicConstantData public_constants
+AztecAddress fee_payer
+CallRequest public_teardown_call_request
+u8 revert_code
}
PublicKernelCircuitPublicInputs --> PublicAccumulatedData
Expand All @@ -304,11 +306,6 @@ class CombinedConstantData {
+GlobalVariables global_variables
}
class PublicConstantData {
+AztecAddress fee_payer
+Field public_teardown_function_hash
}
class PublicAccumulatedData {
+Field encrypted_log_preimages_length
+Field unencrypted_log_preimages_length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::abis::{
accumulated_data::PrivateAccumulatedData, combined_constant_data::CombinedConstantData,
validation_requests::ValidationRequests
validation_requests::ValidationRequests, call_request::CallRequest
};
use crate::mocked::AggregationObject;

Expand All @@ -10,4 +10,5 @@ struct PrivateKernelCircuitPublicInputs {
validation_requests: ValidationRequests,
end: PrivateAccumulatedData,
constants: CombinedConstantData,
public_teardown_call_request: CallRequest,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
private_kernel_circuit_public_inputs::PrivateKernelCircuitPublicInputs,
public_kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs
},
gas::Gas, validation_requests::validation_requests_builder::ValidationRequestsBuilder
gas::Gas, validation_requests::validation_requests_builder::ValidationRequestsBuilder,
call_request::CallRequest
},
mocked::AggregationObject, partial_state_reference::PartialStateReference, traits::Empty
};
Expand All @@ -21,6 +22,7 @@ struct PrivateKernelCircuitPublicInputsBuilder {
validation_requests: ValidationRequestsBuilder,
end: PrivateAccumulatedDataBuilder,
constants: CombinedConstantData,
public_teardown_call_request: CallRequest,
}

impl PrivateKernelCircuitPublicInputsBuilder {
Expand All @@ -30,7 +32,8 @@ impl PrivateKernelCircuitPublicInputsBuilder {
min_revertible_side_effect_counter: self.min_revertible_side_effect_counter,
validation_requests: self.validation_requests.finish(),
end: self.end.finish(),
constants: self.constants
constants: self.constants,
public_teardown_call_request: self.public_teardown_call_request
}
}

Expand Down Expand Up @@ -58,7 +61,8 @@ impl PrivateKernelCircuitPublicInputsBuilder {
end_non_revertible,
end,
constants: self.constants,
revert_code: 0
revert_code: 0,
public_teardown_call_request: self.public_teardown_call_request
}
}
}
Expand All @@ -71,6 +75,7 @@ impl Empty for PrivateKernelCircuitPublicInputsBuilder {
validation_requests: ValidationRequestsBuilder::empty(),
end: PrivateAccumulatedDataBuilder::empty(),
constants: CombinedConstantData::empty(),
public_teardown_call_request: CallRequest::empty()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::abis::{
accumulated_data::PublicAccumulatedData, combined_constant_data::CombinedConstantData,
validation_requests::{RollupValidationRequests, ValidationRequests}
validation_requests::{RollupValidationRequests, ValidationRequests}, call_request::CallRequest
};
use crate::mocked::AggregationObject;

Expand All @@ -11,6 +11,7 @@ struct PublicKernelCircuitPublicInputs {
end: PublicAccumulatedData,
constants: CombinedConstantData,
revert_code: u8,
public_teardown_call_request: CallRequest,
}

impl PublicKernelCircuitPublicInputs {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
accumulated_data::{CombinedAccumulatedData, PublicAccumulatedDataBuilder},
combined_constant_data::CombinedConstantData,
kernel_circuit_public_inputs::{public_kernel_circuit_public_inputs::PublicKernelCircuitPublicInputs},
validation_requests::ValidationRequestsBuilder
validation_requests::ValidationRequestsBuilder, call_request::CallRequest
},
mocked::AggregationObject, traits::Empty
};
Expand All @@ -15,6 +15,7 @@ struct PublicKernelCircuitPublicInputsBuilder {
end: PublicAccumulatedDataBuilder,
constants: CombinedConstantData,
revert_code: u8,
public_teardown_call_request: CallRequest,
}

impl PublicKernelCircuitPublicInputsBuilder {
Expand All @@ -28,7 +29,8 @@ impl PublicKernelCircuitPublicInputsBuilder {
end_non_revertible: self.end_non_revertible.finish(),
end: self.end.finish(),
constants: self.constants,
revert_code: self.revert_code
revert_code: self.revert_code,
public_teardown_call_request: self.public_teardown_call_request
}
}
}
Expand All @@ -42,6 +44,7 @@ impl Empty for PublicKernelCircuitPublicInputsBuilder {
end: PublicAccumulatedDataBuilder::empty(),
constants: CombinedConstantData::empty(),
revert_code: 0 as u8,
public_teardown_call_request: CallRequest::empty()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct FixtureBuilder {
historical_header: Header,
tx_context: TxContext,
global_variables: GlobalVariables,
public_teardown_call_request: CallRequest,

// Accumulated data.
new_note_hashes: BoundedVec<NoteHashContext, MAX_NEW_NOTE_HASHES_PER_TX>,
Expand Down Expand Up @@ -111,7 +112,8 @@ impl FixtureBuilder {
start_state: PartialStateReference::empty(),
gas_used: Gas::empty(),
non_revertible_gas_used: Gas::empty(),
global_variables: GlobalVariables::empty()
global_variables: GlobalVariables::empty(),
public_teardown_call_request: CallRequest::empty()
}
}

Expand Down Expand Up @@ -189,7 +191,8 @@ impl FixtureBuilder {
min_revertible_side_effect_counter: self.min_revertible_side_effect_counter,
end,
validation_requests,
constants
constants,
public_teardown_call_request: self.public_teardown_call_request
}
}

Expand Down Expand Up @@ -219,7 +222,8 @@ impl FixtureBuilder {
end,
validation_requests,
constants,
revert_code: self.revert_code
revert_code: self.revert_code,
public_teardown_call_request: self.public_teardown_call_request
}
}

Expand Down Expand Up @@ -479,6 +483,7 @@ impl Empty for FixtureBuilder {
start_state: PartialStateReference::empty(),
gas_used: Gas::empty(),
non_revertible_gas_used: Gas::empty(),
public_teardown_call_request: CallRequest::empty()
}
}
}
23 changes: 17 additions & 6 deletions yarn-project/circuit-types/src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
PartialPrivateTailPublicInputsForPublic,
PrivateKernelTailCircuitPublicInputs,
Proof,
type PublicCallRequest,
PublicCallRequest,
SideEffect,
computeContractClassId,
getContractClassFromArtifact,
Expand Down Expand Up @@ -46,11 +46,13 @@ export const mockTx = (
numberOfNonRevertiblePublicCallRequests = MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX / 2,
numberOfRevertiblePublicCallRequests = MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX / 2,
publicCallRequests = [],
publicTeardownCallRequest = PublicCallRequest.empty(),
}: {
hasLogs?: boolean;
numberOfNonRevertiblePublicCallRequests?: number;
numberOfRevertiblePublicCallRequests?: number;
publicCallRequests?: PublicCallRequest[];
publicTeardownCallRequest?: PublicCallRequest;
} = {},
) => {
const totalPublicCallRequests =
Expand Down Expand Up @@ -78,11 +80,17 @@ export const mockTx = (
? publicCallRequests.slice().sort((a, b) => b.callContext.sideEffectCounter - a.callContext.sideEffectCounter)
: times(totalPublicCallRequests, i => makePublicCallRequest(seed + 0x100 + i));

data.forPublic.end.publicCallStack = makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, i =>
i < numberOfRevertiblePublicCallRequests ? publicCallRequests[i].toCallRequest() : CallRequest.empty(),
);
data.forPublic.endNonRevertibleData.publicCallStack = makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, i =>
i < numberOfNonRevertiblePublicCallRequests
? publicCallRequests[numberOfRevertiblePublicCallRequests + i].toCallRequest()
: CallRequest.empty(),
);

data.forPublic.publicTeardownCallRequest = publicTeardownCallRequest.toCallRequest();

if (hasLogs) {
let i = 1; // 0 used in first nullifier
encryptedLogs.functionLogs.forEach((log, j) => {
Expand All @@ -97,17 +105,20 @@ export const mockTx = (
}
});
}

data.forPublic.end.publicCallStack = makeTuple(MAX_PUBLIC_CALL_STACK_LENGTH_PER_TX, i =>
i < numberOfRevertiblePublicCallRequests ? publicCallRequests[i].toCallRequest() : CallRequest.empty(),
);
} else {
data.forRollup!.end.newNullifiers[0] = firstNullifier.value;
data.forRollup!.end.encryptedLogsHash = Fr.fromBuffer(encryptedLogs.hash());
data.forRollup!.end.unencryptedLogsHash = Fr.fromBuffer(unencryptedLogs.hash());
}

const tx = new Tx(data, new Proof(Buffer.alloc(0)), encryptedLogs, unencryptedLogs, publicCallRequests);
const tx = new Tx(
data,
new Proof(Buffer.alloc(0)),
encryptedLogs,
unencryptedLogs,
publicCallRequests,
publicTeardownCallRequest,
);

return tx;
};
Expand Down
27 changes: 25 additions & 2 deletions yarn-project/circuit-types/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class Tx {
* Preimages of the public call stack entries from the private kernel circuit output.
*/
public readonly enqueuedPublicFunctionCalls: PublicCallRequest[],
/**
* Public function call to be run by the sequencer as part of teardown.
*/
public readonly publicTeardownFunctionCall: PublicCallRequest,
) {
const kernelPublicCallStackSize = data.numberOfPublicCallRequests();
if (kernelPublicCallStackSize !== enqueuedPublicFunctionCalls.length) {
Expand Down Expand Up @@ -65,6 +69,7 @@ export class Tx {
reader.readObject(EncryptedTxL2Logs),
reader.readObject(UnencryptedTxL2Logs),
reader.readArray(reader.readNumber(), PublicCallRequest),
reader.readObject(PublicCallRequest),
);
}

Expand All @@ -80,6 +85,7 @@ export class Tx {
this.unencryptedLogs,
this.enqueuedPublicFunctionCalls.length,
this.enqueuedPublicFunctionCalls,
this.publicTeardownFunctionCall,
]);
}

Expand All @@ -94,6 +100,7 @@ export class Tx {
unencryptedLogs: this.unencryptedLogs.toBuffer().toString('hex'),
proof: this.proof.toBuffer().toString('hex'),
enqueuedPublicFunctions: this.enqueuedPublicFunctionCalls.map(f => f.toBuffer().toString('hex')) ?? [],
publicTeardownFunctionCall: this.publicTeardownFunctionCall.toBuffer().toString('hex'),
};
}

Expand All @@ -119,7 +126,15 @@ export class Tx {
const enqueuedPublicFunctions = obj.enqueuedPublicFunctions
? obj.enqueuedPublicFunctions.map((x: string) => PublicCallRequest.fromBuffer(Buffer.from(x, 'hex')))
: [];
return new Tx(publicInputs, Proof.fromBuffer(proof), encryptedLogs, unencryptedLogs, enqueuedPublicFunctions);
const publicTeardownFunctionCall = PublicCallRequest.fromBuffer(Buffer.from(obj.publicTeardownFunctionCall, 'hex'));
return new Tx(
publicInputs,
Proof.fromBuffer(proof),
encryptedLogs,
unencryptedLogs,
enqueuedPublicFunctions,
publicTeardownFunctionCall,
);
}

/**
Expand Down Expand Up @@ -198,7 +213,15 @@ export class Tx {
const enqueuedPublicFunctions = tx.enqueuedPublicFunctionCalls.map(x => {
return PublicCallRequest.fromBuffer(x.toBuffer());
});
return new Tx(publicInputs, proof, encryptedLogs, unencryptedLogs, enqueuedPublicFunctions);
const publicTeardownFunctionCall = PublicCallRequest.fromBuffer(tx.publicTeardownFunctionCall.toBuffer());
return new Tx(
publicInputs,
proof,
encryptedLogs,
unencryptedLogs,
enqueuedPublicFunctions,
publicTeardownFunctionCall,
);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Fr } from '@aztec/foundation/fields';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

import { AggregationObject } from '../aggregation_object.js';
import { CallRequest } from '../call_request.js';
import { ValidationRequests } from '../validation_requests.js';
import { CombinedConstantData } from './combined_constant_data.js';
import { PrivateAccumulatedData } from './private_accumulated_data.js';
Expand Down Expand Up @@ -31,6 +32,10 @@ export class PrivateKernelCircuitPublicInputs {
* Data which is not modified by the circuits.
*/
public constants: CombinedConstantData,
/**
* The call request for the public teardown function
*/
public publicTeardownCallRequest: CallRequest,
) {}

toBuffer() {
Expand All @@ -40,6 +45,7 @@ export class PrivateKernelCircuitPublicInputs {
this.validationRequests,
this.end,
this.constants,
this.publicTeardownCallRequest,
);
}

Expand All @@ -56,6 +62,7 @@ export class PrivateKernelCircuitPublicInputs {
reader.readObject(ValidationRequests),
reader.readObject(PrivateAccumulatedData),
reader.readObject(CombinedConstantData),
reader.readObject(CallRequest),
);
}

Expand All @@ -66,6 +73,7 @@ export class PrivateKernelCircuitPublicInputs {
ValidationRequests.empty(),
PrivateAccumulatedData.empty(),
CombinedConstantData.empty(),
CallRequest.empty(),
);
}
}
Loading

0 comments on commit 03e1b93

Please sign in to comment.