@aztec/prover-client 0.42.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dest/config.d.ts +21 -0
- package/dest/config.d.ts.map +1 -0
- package/dest/config.js +31 -0
- package/dest/index.d.ts +4 -0
- package/dest/index.d.ts.map +1 -0
- package/dest/index.js +3 -0
- package/dest/mocks/fixtures.d.ts +22 -0
- package/dest/mocks/fixtures.d.ts.map +1 -0
- package/dest/mocks/fixtures.js +95 -0
- package/dest/mocks/test_context.d.ts +32 -0
- package/dest/mocks/test_context.d.ts.map +1 -0
- package/dest/mocks/test_context.js +116 -0
- package/dest/orchestrator/block-building-helpers.d.ts +36 -0
- package/dest/orchestrator/block-building-helpers.d.ts.map +1 -0
- package/dest/orchestrator/block-building-helpers.js +236 -0
- package/dest/orchestrator/orchestrator.d.ts +113 -0
- package/dest/orchestrator/orchestrator.d.ts.map +1 -0
- package/dest/orchestrator/orchestrator.js +574 -0
- package/dest/orchestrator/proving-state.d.ts +68 -0
- package/dest/orchestrator/proving-state.d.ts.map +1 -0
- package/dest/orchestrator/proving-state.js +142 -0
- package/dest/orchestrator/tx-proving-state.d.ts +35 -0
- package/dest/orchestrator/tx-proving-state.d.ts.map +1 -0
- package/dest/orchestrator/tx-proving-state.js +92 -0
- package/dest/prover-agent/index.d.ts +4 -0
- package/dest/prover-agent/index.d.ts.map +1 -0
- package/dest/prover-agent/index.js +4 -0
- package/dest/prover-agent/memory-proving-queue.d.ts +64 -0
- package/dest/prover-agent/memory-proving-queue.d.ts.map +1 -0
- package/dest/prover-agent/memory-proving-queue.js +187 -0
- package/dest/prover-agent/prover-agent.d.ts +30 -0
- package/dest/prover-agent/prover-agent.d.ts.map +1 -0
- package/dest/prover-agent/prover-agent.js +115 -0
- package/dest/prover-agent/proving-error.d.ts +5 -0
- package/dest/prover-agent/proving-error.d.ts.map +1 -0
- package/dest/prover-agent/proving-error.js +9 -0
- package/dest/prover-agent/rpc.d.ts +5 -0
- package/dest/prover-agent/rpc.d.ts.map +1 -0
- package/dest/prover-agent/rpc.js +53 -0
- package/dest/tx-prover/tx-prover.d.ts +65 -0
- package/dest/tx-prover/tx-prover.d.ts.map +1 -0
- package/dest/tx-prover/tx-prover.js +122 -0
- package/package.json +87 -0
- package/src/config.ts +59 -0
- package/src/index.ts +4 -0
- package/src/mocks/fixtures.ts +182 -0
- package/src/mocks/test_context.ts +217 -0
- package/src/orchestrator/block-building-helpers.ts +470 -0
- package/src/orchestrator/orchestrator.ts +883 -0
- package/src/orchestrator/proving-state.ts +210 -0
- package/src/orchestrator/tx-proving-state.ts +139 -0
- package/src/prover-agent/index.ts +3 -0
- package/src/prover-agent/memory-proving-queue.ts +303 -0
- package/src/prover-agent/prover-agent.ts +144 -0
- package/src/prover-agent/proving-error.ts +9 -0
- package/src/prover-agent/rpc.ts +91 -0
- package/src/tx-prover/tx-prover.ts +171 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { type L2Block, type MerkleTreeId, type ProvingResult } from '@aztec/circuit-types';
|
|
2
|
+
import {
|
|
3
|
+
type AppendOnlyTreeSnapshot,
|
|
4
|
+
type BaseOrMergeRollupPublicInputs,
|
|
5
|
+
type Fr,
|
|
6
|
+
type GlobalVariables,
|
|
7
|
+
type L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH,
|
|
8
|
+
type NESTED_RECURSIVE_PROOF_LENGTH,
|
|
9
|
+
type NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP,
|
|
10
|
+
type Proof,
|
|
11
|
+
type RECURSIVE_PROOF_LENGTH,
|
|
12
|
+
type RecursiveProof,
|
|
13
|
+
type RootParityInput,
|
|
14
|
+
type RootRollupPublicInputs,
|
|
15
|
+
type VerificationKeyAsFields,
|
|
16
|
+
type VerificationKeys,
|
|
17
|
+
} from '@aztec/circuits.js';
|
|
18
|
+
import { type Tuple } from '@aztec/foundation/serialize';
|
|
19
|
+
|
|
20
|
+
import { type TxProvingState } from './tx-proving-state.js';
|
|
21
|
+
|
|
22
|
+
export type MergeRollupInputData = {
|
|
23
|
+
inputs: [BaseOrMergeRollupPublicInputs | undefined, BaseOrMergeRollupPublicInputs | undefined];
|
|
24
|
+
proofs: [
|
|
25
|
+
RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined,
|
|
26
|
+
RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined,
|
|
27
|
+
];
|
|
28
|
+
verificationKeys: [VerificationKeyAsFields | undefined, VerificationKeyAsFields | undefined];
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type TreeSnapshots = Map<MerkleTreeId, AppendOnlyTreeSnapshot>;
|
|
32
|
+
|
|
33
|
+
enum PROVING_STATE_LIFECYCLE {
|
|
34
|
+
PROVING_STATE_CREATED,
|
|
35
|
+
PROVING_STATE_FULL,
|
|
36
|
+
PROVING_STATE_RESOLVED,
|
|
37
|
+
PROVING_STATE_REJECTED,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The current state of the proving schedule. Contains the raw inputs (txs) and intermediate state to generate every constituent proof in the tree.
|
|
42
|
+
* Carries an identifier so we can identify if the proving state is discarded and a new one started.
|
|
43
|
+
* Captures resolve and reject callbacks to provide a promise base interface to the consumer of our proving.
|
|
44
|
+
*/
|
|
45
|
+
export class ProvingState {
|
|
46
|
+
private provingStateLifecycle = PROVING_STATE_LIFECYCLE.PROVING_STATE_CREATED;
|
|
47
|
+
private mergeRollupInputs: MergeRollupInputData[] = [];
|
|
48
|
+
private rootParityInputs: Array<RootParityInput<typeof RECURSIVE_PROOF_LENGTH> | undefined> = [];
|
|
49
|
+
private finalRootParityInputs: RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined;
|
|
50
|
+
public rootRollupPublicInputs: RootRollupPublicInputs | undefined;
|
|
51
|
+
public finalAggregationObject: Fr[] | undefined;
|
|
52
|
+
public finalProof: Proof | undefined;
|
|
53
|
+
public block: L2Block | undefined;
|
|
54
|
+
private txs: TxProvingState[] = [];
|
|
55
|
+
constructor(
|
|
56
|
+
public readonly totalNumTxs: number,
|
|
57
|
+
private completionCallback: (result: ProvingResult) => void,
|
|
58
|
+
private rejectionCallback: (reason: string) => void,
|
|
59
|
+
public readonly globalVariables: GlobalVariables,
|
|
60
|
+
public readonly newL1ToL2Messages: Tuple<Fr, typeof NUMBER_OF_L1_L2_MESSAGES_PER_ROLLUP>,
|
|
61
|
+
numRootParityInputs: number,
|
|
62
|
+
public readonly messageTreeSnapshot: AppendOnlyTreeSnapshot,
|
|
63
|
+
public readonly messageTreeRootSiblingPath: Tuple<Fr, typeof L1_TO_L2_MSG_SUBTREE_SIBLING_PATH_LENGTH>,
|
|
64
|
+
public readonly privateKernelVerificationKeys: VerificationKeys,
|
|
65
|
+
) {
|
|
66
|
+
this.rootParityInputs = Array.from({ length: numRootParityInputs }).map(_ => undefined);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Returns the number of levels of merge rollups
|
|
70
|
+
public get numMergeLevels() {
|
|
71
|
+
return BigInt(Math.ceil(Math.log2(this.totalNumTxs)) - 1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Adds a transaction to the proving state, returns it's index
|
|
75
|
+
// Will update the proving life cycle if this is the last transaction
|
|
76
|
+
public addNewTx(tx: TxProvingState) {
|
|
77
|
+
this.txs.push(tx);
|
|
78
|
+
if (this.txs.length === this.totalNumTxs) {
|
|
79
|
+
this.provingStateLifecycle = PROVING_STATE_LIFECYCLE.PROVING_STATE_FULL;
|
|
80
|
+
}
|
|
81
|
+
return this.txs.length - 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Returns the number of received transactions
|
|
85
|
+
public get transactionsReceived() {
|
|
86
|
+
return this.txs.length;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Returns the final set of root parity inputs
|
|
90
|
+
public get finalRootParityInput() {
|
|
91
|
+
return this.finalRootParityInputs;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Sets the final set of root parity inputs
|
|
95
|
+
public set finalRootParityInput(input: RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined) {
|
|
96
|
+
this.finalRootParityInputs = input;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Returns the set of root parity inputs
|
|
100
|
+
public get rootParityInput() {
|
|
101
|
+
return this.rootParityInputs;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Returns true if this proving state is still valid, false otherwise
|
|
105
|
+
public verifyState() {
|
|
106
|
+
return (
|
|
107
|
+
this.provingStateLifecycle === PROVING_STATE_LIFECYCLE.PROVING_STATE_CREATED ||
|
|
108
|
+
this.provingStateLifecycle === PROVING_STATE_LIFECYCLE.PROVING_STATE_FULL
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Returns true if we are still able to accept transactions, false otherwise
|
|
113
|
+
public isAcceptingTransactions() {
|
|
114
|
+
return this.provingStateLifecycle === PROVING_STATE_LIFECYCLE.PROVING_STATE_CREATED;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Returns the complete set of transaction proving state objects
|
|
118
|
+
public get allTxs() {
|
|
119
|
+
return this.txs;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Stores the inputs to a merge circuit and determines if the circuit is ready to be executed
|
|
124
|
+
* @param mergeInputs - The inputs to store
|
|
125
|
+
* @param indexWithinMerge - The index in the set of inputs to this merge circuit
|
|
126
|
+
* @param indexOfMerge - The global index of this merge circuit
|
|
127
|
+
* @returns True if the merge circuit is ready to be executed, false otherwise
|
|
128
|
+
*/
|
|
129
|
+
public storeMergeInputs(
|
|
130
|
+
mergeInputs: [
|
|
131
|
+
BaseOrMergeRollupPublicInputs,
|
|
132
|
+
RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>,
|
|
133
|
+
VerificationKeyAsFields,
|
|
134
|
+
],
|
|
135
|
+
indexWithinMerge: number,
|
|
136
|
+
indexOfMerge: number,
|
|
137
|
+
) {
|
|
138
|
+
if (!this.mergeRollupInputs[indexOfMerge]) {
|
|
139
|
+
const mergeInputData: MergeRollupInputData = {
|
|
140
|
+
inputs: [undefined, undefined],
|
|
141
|
+
proofs: [undefined, undefined],
|
|
142
|
+
verificationKeys: [undefined, undefined],
|
|
143
|
+
};
|
|
144
|
+
mergeInputData.inputs[indexWithinMerge] = mergeInputs[0];
|
|
145
|
+
mergeInputData.proofs[indexWithinMerge] = mergeInputs[1];
|
|
146
|
+
mergeInputData.verificationKeys[indexWithinMerge] = mergeInputs[2];
|
|
147
|
+
this.mergeRollupInputs[indexOfMerge] = mergeInputData;
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
const mergeInputData = this.mergeRollupInputs[indexOfMerge];
|
|
151
|
+
mergeInputData.inputs[indexWithinMerge] = mergeInputs[0];
|
|
152
|
+
mergeInputData.proofs[indexWithinMerge] = mergeInputs[1];
|
|
153
|
+
mergeInputData.verificationKeys[indexWithinMerge] = mergeInputs[2];
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Returns a specific transaction proving state
|
|
158
|
+
public getTxProvingState(txIndex: number) {
|
|
159
|
+
return this.txs[txIndex];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Returns a set of merge rollup inputs
|
|
163
|
+
public getMergeInputs(indexOfMerge: number) {
|
|
164
|
+
return this.mergeRollupInputs[indexOfMerge];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Returns true if we have sufficient inputs to execute the root rollup
|
|
168
|
+
public isReadyForRootRollup() {
|
|
169
|
+
return !(
|
|
170
|
+
this.mergeRollupInputs[0] === undefined ||
|
|
171
|
+
this.finalRootParityInput === undefined ||
|
|
172
|
+
this.mergeRollupInputs[0].inputs.findIndex(p => !p) !== -1
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Stores a set of root parity inputs at the given index
|
|
177
|
+
public setRootParityInputs(inputs: RootParityInput<typeof RECURSIVE_PROOF_LENGTH>, index: number) {
|
|
178
|
+
this.rootParityInputs[index] = inputs;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Returns true if we have sufficient root parity inputs to execute the root parity circuit
|
|
182
|
+
public areRootParityInputsReady() {
|
|
183
|
+
return this.rootParityInputs.findIndex(p => !p) === -1;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Attempts to reject the proving state promise with a reason of 'cancelled'
|
|
187
|
+
public cancel() {
|
|
188
|
+
this.reject('Proving cancelled');
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Attempts to reject the proving state promise with the given reason
|
|
192
|
+
// Does nothing if not in a valid state
|
|
193
|
+
public reject(reason: string) {
|
|
194
|
+
if (!this.verifyState()) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
this.provingStateLifecycle = PROVING_STATE_LIFECYCLE.PROVING_STATE_REJECTED;
|
|
198
|
+
this.rejectionCallback(reason);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Attempts to resolve the proving state promise with the given result
|
|
202
|
+
// Does nothing if not in a valid state
|
|
203
|
+
public resolve(result: ProvingResult) {
|
|
204
|
+
if (!this.verifyState()) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
this.provingStateLifecycle = PROVING_STATE_LIFECYCLE.PROVING_STATE_RESOLVED;
|
|
208
|
+
this.completionCallback(result);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AVM_REQUEST,
|
|
3
|
+
type AvmProvingRequest,
|
|
4
|
+
type MerkleTreeId,
|
|
5
|
+
type ProcessedTx,
|
|
6
|
+
type PublicKernelRequest,
|
|
7
|
+
PublicKernelType,
|
|
8
|
+
} from '@aztec/circuit-types';
|
|
9
|
+
import {
|
|
10
|
+
type AppendOnlyTreeSnapshot,
|
|
11
|
+
type BaseRollupInputs,
|
|
12
|
+
NESTED_RECURSIVE_PROOF_LENGTH,
|
|
13
|
+
type Proof,
|
|
14
|
+
type RecursiveProof,
|
|
15
|
+
type VerificationKeyData,
|
|
16
|
+
makeRecursiveProofFromBinary,
|
|
17
|
+
} from '@aztec/circuits.js';
|
|
18
|
+
|
|
19
|
+
export enum TX_PROVING_CODE {
|
|
20
|
+
NOT_READY,
|
|
21
|
+
READY,
|
|
22
|
+
COMPLETED,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type PublicFunction = {
|
|
26
|
+
vmRequest: AvmProvingRequest | undefined;
|
|
27
|
+
vmProof: Proof | undefined;
|
|
28
|
+
previousProofType: PublicKernelType;
|
|
29
|
+
previousKernelProven: boolean;
|
|
30
|
+
publicKernelRequest: PublicKernelRequest;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Type encapsulating the instruction to the orchestrator as to what
|
|
34
|
+
// needs to be proven next
|
|
35
|
+
export type TxProvingInstruction = {
|
|
36
|
+
code: TX_PROVING_CODE;
|
|
37
|
+
function: PublicFunction | undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Helper class to manage the proving cycle of a transaction
|
|
42
|
+
* This includes the public VMs and the public kernels
|
|
43
|
+
* Also stores the inputs to the base rollup for this transaction and the tree snapshots
|
|
44
|
+
*/
|
|
45
|
+
export class TxProvingState {
|
|
46
|
+
private publicFunctions: PublicFunction[] = [];
|
|
47
|
+
|
|
48
|
+
constructor(
|
|
49
|
+
public readonly processedTx: ProcessedTx,
|
|
50
|
+
public readonly baseRollupInputs: BaseRollupInputs,
|
|
51
|
+
public readonly treeSnapshots: Map<MerkleTreeId, AppendOnlyTreeSnapshot>,
|
|
52
|
+
privateKernelVk: VerificationKeyData,
|
|
53
|
+
) {
|
|
54
|
+
let previousKernelProof: RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH> | undefined =
|
|
55
|
+
makeRecursiveProofFromBinary(processedTx.proof, NESTED_RECURSIVE_PROOF_LENGTH);
|
|
56
|
+
let previousProofType = PublicKernelType.NON_PUBLIC;
|
|
57
|
+
for (let i = 0; i < processedTx.publicProvingRequests.length; i++) {
|
|
58
|
+
const provingRequest = processedTx.publicProvingRequests[i];
|
|
59
|
+
const kernelRequest = provingRequest.type === AVM_REQUEST ? provingRequest.kernelRequest : provingRequest;
|
|
60
|
+
// the first circuit has a valid previous proof, it came from private
|
|
61
|
+
if (previousKernelProof) {
|
|
62
|
+
kernelRequest.inputs.previousKernel.proof = previousKernelProof;
|
|
63
|
+
kernelRequest.inputs.previousKernel.vk = privateKernelVk;
|
|
64
|
+
}
|
|
65
|
+
const vmRequest = provingRequest.type === AVM_REQUEST ? provingRequest : undefined;
|
|
66
|
+
const publicFunction: PublicFunction = {
|
|
67
|
+
vmRequest,
|
|
68
|
+
vmProof: undefined,
|
|
69
|
+
previousProofType,
|
|
70
|
+
previousKernelProven: i === 0,
|
|
71
|
+
publicKernelRequest: kernelRequest,
|
|
72
|
+
};
|
|
73
|
+
this.publicFunctions.push(publicFunction);
|
|
74
|
+
previousKernelProof = undefined;
|
|
75
|
+
previousProofType = kernelRequest.type;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Updates the transaction's proving state after completion of a kernel proof
|
|
80
|
+
// Returns an instruction as to the next stage of tx proving
|
|
81
|
+
public getNextPublicKernelFromKernelProof(
|
|
82
|
+
provenIndex: number,
|
|
83
|
+
proof: RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>,
|
|
84
|
+
verificationKey: VerificationKeyData,
|
|
85
|
+
): TxProvingInstruction {
|
|
86
|
+
const kernelRequest = this.getPublicFunctionState(provenIndex).publicKernelRequest;
|
|
87
|
+
const nextKernelIndex = provenIndex + 1;
|
|
88
|
+
if (nextKernelIndex >= this.publicFunctions.length) {
|
|
89
|
+
// The next kernel index is greater than our set of functions, we are done!
|
|
90
|
+
return { code: TX_PROVING_CODE.COMPLETED, function: undefined };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// There is more work to do, are we ready?
|
|
94
|
+
const nextFunction = this.publicFunctions[nextKernelIndex];
|
|
95
|
+
|
|
96
|
+
// pass both the proof and verification key forward to the next circuit
|
|
97
|
+
nextFunction.publicKernelRequest.inputs.previousKernel.proof = proof;
|
|
98
|
+
nextFunction.publicKernelRequest.inputs.previousKernel.vk = verificationKey;
|
|
99
|
+
|
|
100
|
+
// We need to update this so the state machine knows this proof is ready
|
|
101
|
+
nextFunction.previousKernelProven = true;
|
|
102
|
+
nextFunction.previousProofType = kernelRequest.type;
|
|
103
|
+
if (nextFunction.vmProof === undefined) {
|
|
104
|
+
// The VM proof for the next function is not ready
|
|
105
|
+
return { code: TX_PROVING_CODE.NOT_READY, function: undefined };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// The VM proof is ready, we can continue
|
|
109
|
+
return { code: TX_PROVING_CODE.READY, function: nextFunction };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Updates the transaction's proving state after completion of a VM proof
|
|
113
|
+
// Returns an instruction as to the next stage of tx proving
|
|
114
|
+
public getNextPublicKernelFromVMProof(provenIndex: number, proof: Proof): TxProvingInstruction {
|
|
115
|
+
const provenFunction = this.publicFunctions[provenIndex];
|
|
116
|
+
provenFunction.vmProof = proof;
|
|
117
|
+
|
|
118
|
+
if (!provenFunction.previousKernelProven) {
|
|
119
|
+
// The previous kernel is not yet ready
|
|
120
|
+
return { code: TX_PROVING_CODE.NOT_READY, function: undefined };
|
|
121
|
+
}
|
|
122
|
+
// The previous kernel is ready so we can prove this kernel
|
|
123
|
+
return { code: TX_PROVING_CODE.READY, function: provenFunction };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Returns the public function state at the given index
|
|
127
|
+
// Throws if out of bounds
|
|
128
|
+
public getPublicFunctionState(functionIndex: number) {
|
|
129
|
+
if (functionIndex < 0 || functionIndex >= this.publicFunctions.length) {
|
|
130
|
+
throw new Error(`Requested public function index was out of bounds`);
|
|
131
|
+
}
|
|
132
|
+
return this.publicFunctions[functionIndex];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Returns the number of public kernels required by this transaction
|
|
136
|
+
public getNumPublicKernels() {
|
|
137
|
+
return this.publicFunctions.length;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ProofAndVerificationKey,
|
|
3
|
+
type ProvingJob,
|
|
4
|
+
type ProvingJobSource,
|
|
5
|
+
type ProvingRequest,
|
|
6
|
+
type ProvingRequestResult,
|
|
7
|
+
ProvingRequestType,
|
|
8
|
+
type PublicInputsAndRecursiveProof,
|
|
9
|
+
type PublicKernelNonTailRequest,
|
|
10
|
+
type PublicKernelTailRequest,
|
|
11
|
+
type ServerCircuitProver,
|
|
12
|
+
} from '@aztec/circuit-types';
|
|
13
|
+
import type {
|
|
14
|
+
AvmCircuitInputs,
|
|
15
|
+
BaseOrMergeRollupPublicInputs,
|
|
16
|
+
BaseParityInputs,
|
|
17
|
+
BaseRollupInputs,
|
|
18
|
+
KernelCircuitPublicInputs,
|
|
19
|
+
MergeRollupInputs,
|
|
20
|
+
NESTED_RECURSIVE_PROOF_LENGTH,
|
|
21
|
+
PrivateKernelEmptyInputData,
|
|
22
|
+
PublicKernelCircuitPublicInputs,
|
|
23
|
+
RECURSIVE_PROOF_LENGTH,
|
|
24
|
+
RootParityInput,
|
|
25
|
+
RootParityInputs,
|
|
26
|
+
RootRollupInputs,
|
|
27
|
+
RootRollupPublicInputs,
|
|
28
|
+
} from '@aztec/circuits.js';
|
|
29
|
+
import { randomBytes } from '@aztec/foundation/crypto';
|
|
30
|
+
import { AbortedError, TimeoutError } from '@aztec/foundation/error';
|
|
31
|
+
import { MemoryFifo } from '@aztec/foundation/fifo';
|
|
32
|
+
import { createDebugLogger } from '@aztec/foundation/log';
|
|
33
|
+
import { type PromiseWithResolvers, promiseWithResolvers } from '@aztec/foundation/promise';
|
|
34
|
+
|
|
35
|
+
type ProvingJobWithResolvers<T extends ProvingRequest = ProvingRequest> = {
|
|
36
|
+
id: string;
|
|
37
|
+
request: T;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
attempts: number;
|
|
40
|
+
} & PromiseWithResolvers<ProvingRequestResult<T['type']>>;
|
|
41
|
+
|
|
42
|
+
const MAX_RETRIES = 3;
|
|
43
|
+
|
|
44
|
+
const defaultIdGenerator = () => randomBytes(4).toString('hex');
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A helper class that sits in between services that need proofs created and agents that can create them.
|
|
48
|
+
* The queue accumulates jobs and provides them to agents in FIFO order.
|
|
49
|
+
*/
|
|
50
|
+
export class MemoryProvingQueue implements ServerCircuitProver, ProvingJobSource {
|
|
51
|
+
private log = createDebugLogger('aztec:prover-client:prover-pool:queue');
|
|
52
|
+
private queue = new MemoryFifo<ProvingJobWithResolvers>();
|
|
53
|
+
private jobsInProgress = new Map<string, ProvingJobWithResolvers>();
|
|
54
|
+
|
|
55
|
+
constructor(private generateId = defaultIdGenerator) {}
|
|
56
|
+
|
|
57
|
+
async getProvingJob({ timeoutSec = 1 } = {}): Promise<ProvingJob<ProvingRequest> | undefined> {
|
|
58
|
+
try {
|
|
59
|
+
const job = await this.queue.get(timeoutSec);
|
|
60
|
+
if (!job) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (job.signal?.aborted) {
|
|
65
|
+
this.log.debug(`Job ${job.id} type=${job.request.type} has been aborted`);
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this.jobsInProgress.set(job.id, job);
|
|
70
|
+
return {
|
|
71
|
+
id: job.id,
|
|
72
|
+
request: job.request,
|
|
73
|
+
};
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (err instanceof TimeoutError) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
resolveProvingJob<T extends ProvingRequestType>(jobId: string, result: ProvingRequestResult<T>): Promise<void> {
|
|
84
|
+
const job = this.jobsInProgress.get(jobId);
|
|
85
|
+
if (!job) {
|
|
86
|
+
return Promise.reject(new Error('Job not found'));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.jobsInProgress.delete(jobId);
|
|
90
|
+
|
|
91
|
+
if (job.signal?.aborted) {
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
job.resolve(result);
|
|
96
|
+
return Promise.resolve();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
rejectProvingJob(jobId: string, err: any): Promise<void> {
|
|
100
|
+
const job = this.jobsInProgress.get(jobId);
|
|
101
|
+
if (!job) {
|
|
102
|
+
return Promise.reject(new Error('Job not found'));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.jobsInProgress.delete(jobId);
|
|
106
|
+
|
|
107
|
+
if (job.signal?.aborted) {
|
|
108
|
+
return Promise.resolve();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (job.attempts < MAX_RETRIES) {
|
|
112
|
+
job.attempts++;
|
|
113
|
+
this.log.warn(
|
|
114
|
+
`Job id=${job.id} type=${ProvingRequestType[job.request.type]} failed with error: ${err}. Retry ${
|
|
115
|
+
job.attempts
|
|
116
|
+
}/${MAX_RETRIES}`,
|
|
117
|
+
);
|
|
118
|
+
this.queue.put(job);
|
|
119
|
+
} else {
|
|
120
|
+
this.log.error(`Job id=${job.id} type=${ProvingRequestType[job.request.type]} failed with error: ${err}`);
|
|
121
|
+
job.reject(err);
|
|
122
|
+
}
|
|
123
|
+
return Promise.resolve();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private enqueue<T extends ProvingRequest>(
|
|
127
|
+
request: T,
|
|
128
|
+
signal?: AbortSignal,
|
|
129
|
+
): Promise<ProvingRequestResult<T['type']>> {
|
|
130
|
+
const { promise, resolve, reject } = promiseWithResolvers<ProvingRequestResult<T['type']>>();
|
|
131
|
+
const item: ProvingJobWithResolvers<T> = {
|
|
132
|
+
id: this.generateId(),
|
|
133
|
+
request,
|
|
134
|
+
signal,
|
|
135
|
+
promise,
|
|
136
|
+
resolve,
|
|
137
|
+
reject,
|
|
138
|
+
attempts: 1,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (signal) {
|
|
142
|
+
signal.addEventListener('abort', () => reject(new AbortedError('Operation has been aborted')));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this.log.debug(
|
|
146
|
+
`Adding id=${item.id} type=${ProvingRequestType[request.type]} proving job to queue depth=${this.queue.length()}`,
|
|
147
|
+
);
|
|
148
|
+
// TODO (alexg) remove the `any`
|
|
149
|
+
if (!this.queue.put(item as any)) {
|
|
150
|
+
throw new Error();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return promise;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
getEmptyPrivateKernelProof(
|
|
157
|
+
inputs: PrivateKernelEmptyInputData,
|
|
158
|
+
signal?: AbortSignal,
|
|
159
|
+
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
|
|
160
|
+
return this.enqueue({ type: ProvingRequestType.PRIVATE_KERNEL_EMPTY, inputs }, signal);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Creates a proof for the given input.
|
|
165
|
+
* @param input - Input to the circuit.
|
|
166
|
+
*/
|
|
167
|
+
getBaseParityProof(
|
|
168
|
+
inputs: BaseParityInputs,
|
|
169
|
+
signal?: AbortSignal,
|
|
170
|
+
): Promise<RootParityInput<typeof RECURSIVE_PROOF_LENGTH>> {
|
|
171
|
+
return this.enqueue(
|
|
172
|
+
{
|
|
173
|
+
type: ProvingRequestType.BASE_PARITY,
|
|
174
|
+
inputs,
|
|
175
|
+
},
|
|
176
|
+
signal,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Creates a proof for the given input.
|
|
182
|
+
* @param input - Input to the circuit.
|
|
183
|
+
*/
|
|
184
|
+
getRootParityProof(
|
|
185
|
+
inputs: RootParityInputs,
|
|
186
|
+
signal?: AbortSignal,
|
|
187
|
+
): Promise<RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH>> {
|
|
188
|
+
return this.enqueue(
|
|
189
|
+
{
|
|
190
|
+
type: ProvingRequestType.ROOT_PARITY,
|
|
191
|
+
inputs,
|
|
192
|
+
},
|
|
193
|
+
signal,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Creates a proof for the given input.
|
|
199
|
+
* @param input - Input to the circuit.
|
|
200
|
+
*/
|
|
201
|
+
getBaseRollupProof(
|
|
202
|
+
input: BaseRollupInputs,
|
|
203
|
+
signal?: AbortSignal,
|
|
204
|
+
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>> {
|
|
205
|
+
return this.enqueue(
|
|
206
|
+
{
|
|
207
|
+
type: ProvingRequestType.BASE_ROLLUP,
|
|
208
|
+
inputs: input,
|
|
209
|
+
},
|
|
210
|
+
signal,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Creates a proof for the given input.
|
|
216
|
+
* @param input - Input to the circuit.
|
|
217
|
+
*/
|
|
218
|
+
getMergeRollupProof(
|
|
219
|
+
input: MergeRollupInputs,
|
|
220
|
+
signal?: AbortSignal,
|
|
221
|
+
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>> {
|
|
222
|
+
return this.enqueue(
|
|
223
|
+
{
|
|
224
|
+
type: ProvingRequestType.MERGE_ROLLUP,
|
|
225
|
+
inputs: input,
|
|
226
|
+
},
|
|
227
|
+
signal,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Creates a proof for the given input.
|
|
233
|
+
* @param input - Input to the circuit.
|
|
234
|
+
*/
|
|
235
|
+
getRootRollupProof(
|
|
236
|
+
input: RootRollupInputs,
|
|
237
|
+
signal?: AbortSignal,
|
|
238
|
+
): Promise<PublicInputsAndRecursiveProof<RootRollupPublicInputs>> {
|
|
239
|
+
return this.enqueue(
|
|
240
|
+
{
|
|
241
|
+
type: ProvingRequestType.ROOT_ROLLUP,
|
|
242
|
+
inputs: input,
|
|
243
|
+
},
|
|
244
|
+
signal,
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Create a public kernel proof.
|
|
250
|
+
* @param kernelRequest - Object containing the details of the proof required
|
|
251
|
+
*/
|
|
252
|
+
getPublicKernelProof(
|
|
253
|
+
kernelRequest: PublicKernelNonTailRequest,
|
|
254
|
+
signal?: AbortSignal,
|
|
255
|
+
): Promise<PublicInputsAndRecursiveProof<PublicKernelCircuitPublicInputs>> {
|
|
256
|
+
return this.enqueue(
|
|
257
|
+
{
|
|
258
|
+
type: ProvingRequestType.PUBLIC_KERNEL_NON_TAIL,
|
|
259
|
+
kernelType: kernelRequest.type,
|
|
260
|
+
inputs: kernelRequest.inputs,
|
|
261
|
+
},
|
|
262
|
+
signal,
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Create a public kernel tail proof.
|
|
268
|
+
* @param kernelRequest - Object containing the details of the proof required
|
|
269
|
+
*/
|
|
270
|
+
getPublicTailProof(
|
|
271
|
+
kernelRequest: PublicKernelTailRequest,
|
|
272
|
+
signal?: AbortSignal,
|
|
273
|
+
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
|
|
274
|
+
return this.enqueue(
|
|
275
|
+
{
|
|
276
|
+
type: ProvingRequestType.PUBLIC_KERNEL_TAIL,
|
|
277
|
+
kernelType: kernelRequest.type,
|
|
278
|
+
inputs: kernelRequest.inputs,
|
|
279
|
+
},
|
|
280
|
+
signal,
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Creates an AVM proof.
|
|
286
|
+
*/
|
|
287
|
+
getAvmProof(inputs: AvmCircuitInputs, signal?: AbortSignal | undefined): Promise<ProofAndVerificationKey> {
|
|
288
|
+
return this.enqueue(
|
|
289
|
+
{
|
|
290
|
+
type: ProvingRequestType.PUBLIC_VM,
|
|
291
|
+
inputs,
|
|
292
|
+
},
|
|
293
|
+
signal,
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Verifies a circuit proof
|
|
299
|
+
*/
|
|
300
|
+
verifyProof(): Promise<void> {
|
|
301
|
+
return Promise.reject('not implemented');
|
|
302
|
+
}
|
|
303
|
+
}
|