@aztec/blob-lib 4.0.0-nightly.20250907 → 4.0.0-nightly.20260107
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/dest/batched_blob.d.ts +26 -0
- package/dest/batched_blob.d.ts.map +1 -0
- package/dest/batched_blob.js +20 -0
- package/dest/blob.d.ts +50 -99
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob.js +78 -169
- package/dest/blob_batching.d.ts +41 -123
- package/dest/blob_batching.d.ts.map +1 -1
- package/dest/blob_batching.js +129 -203
- package/dest/blob_utils.d.ts +40 -0
- package/dest/blob_utils.d.ts.map +1 -0
- package/dest/blob_utils.js +69 -0
- package/dest/circuit_types/blob_accumulator.d.ts +23 -0
- package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/blob_accumulator.js +62 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts +23 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_accumulator.js +66 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts +16 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_batching_challenges.js +26 -0
- package/dest/circuit_types/index.d.ts +4 -0
- package/dest/circuit_types/index.d.ts.map +1 -0
- package/dest/circuit_types/index.js +4 -0
- package/dest/encoding/block_blob_data.d.ts +22 -0
- package/dest/encoding/block_blob_data.d.ts.map +1 -0
- package/dest/encoding/block_blob_data.js +65 -0
- package/dest/encoding/block_end_marker.d.ts +11 -0
- package/dest/encoding/block_end_marker.d.ts.map +1 -0
- package/dest/encoding/block_end_marker.js +41 -0
- package/dest/encoding/block_end_state_field.d.ts +12 -0
- package/dest/encoding/block_end_state_field.d.ts.map +1 -0
- package/dest/encoding/block_end_state_field.js +39 -0
- package/dest/encoding/checkpoint_blob_data.d.ts +15 -0
- package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
- package/dest/encoding/checkpoint_blob_data.js +67 -0
- package/dest/encoding/checkpoint_end_marker.d.ts +8 -0
- package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -0
- package/dest/encoding/checkpoint_end_marker.js +28 -0
- package/dest/encoding/fixtures.d.ts +41 -0
- package/dest/encoding/fixtures.d.ts.map +1 -0
- package/dest/encoding/fixtures.js +140 -0
- package/dest/encoding/index.d.ts +10 -0
- package/dest/encoding/index.d.ts.map +1 -0
- package/dest/encoding/index.js +9 -0
- package/dest/encoding/tx_blob_data.d.ts +19 -0
- package/dest/encoding/tx_blob_data.d.ts.map +1 -0
- package/dest/encoding/tx_blob_data.js +79 -0
- package/dest/encoding/tx_start_marker.d.ts +16 -0
- package/dest/encoding/tx_start_marker.d.ts.map +1 -0
- package/dest/encoding/tx_start_marker.js +77 -0
- package/dest/errors.d.ts +1 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/hash.d.ts +43 -0
- package/dest/hash.d.ts.map +1 -0
- package/dest/hash.js +80 -0
- package/dest/index.d.ts +7 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +6 -16
- package/dest/interface.d.ts +1 -2
- package/dest/interface.d.ts.map +1 -1
- package/dest/kzg_context.d.ts +8 -0
- package/dest/kzg_context.d.ts.map +1 -0
- package/dest/kzg_context.js +14 -0
- package/dest/sponge_blob.d.ts +12 -14
- package/dest/sponge_blob.d.ts.map +1 -1
- package/dest/sponge_blob.js +26 -30
- package/dest/testing.d.ts +10 -23
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +37 -53
- package/dest/types.d.ts +17 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +4 -0
- package/package.json +10 -7
- package/src/batched_blob.ts +26 -0
- package/src/blob.ts +81 -195
- package/src/blob_batching.ts +168 -231
- package/src/blob_utils.ts +82 -0
- package/src/circuit_types/blob_accumulator.ts +96 -0
- package/src/circuit_types/final_blob_accumulator.ts +76 -0
- package/src/circuit_types/final_blob_batching_challenges.ts +30 -0
- package/src/circuit_types/index.ts +4 -0
- package/src/encoding/block_blob_data.ts +102 -0
- package/src/encoding/block_end_marker.ts +55 -0
- package/src/encoding/block_end_state_field.ts +59 -0
- package/src/encoding/checkpoint_blob_data.ts +95 -0
- package/src/encoding/checkpoint_end_marker.ts +40 -0
- package/src/encoding/fixtures.ts +210 -0
- package/src/encoding/index.ts +9 -0
- package/src/encoding/tx_blob_data.ts +116 -0
- package/src/encoding/tx_start_marker.ts +97 -0
- package/src/hash.ts +89 -0
- package/src/index.ts +6 -19
- package/src/interface.ts +0 -1
- package/src/kzg_context.ts +16 -0
- package/src/sponge_blob.ts +28 -31
- package/src/testing.ts +48 -59
- package/src/types.ts +17 -0
- package/dest/blob_batching_public_inputs.d.ts +0 -71
- package/dest/blob_batching_public_inputs.d.ts.map +0 -1
- package/dest/blob_batching_public_inputs.js +0 -168
- package/dest/encoding.d.ts +0 -66
- package/dest/encoding.d.ts.map +0 -1
- package/dest/encoding.js +0 -113
- package/src/blob_batching_public_inputs.ts +0 -252
- package/src/encoding.ts +0 -138
package/dest/blob_batching.d.ts
CHANGED
|
@@ -1,119 +1,20 @@
|
|
|
1
|
-
import { BLS12Fr, BLS12Point
|
|
2
|
-
import {
|
|
1
|
+
import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
|
|
2
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
+
import { BatchedBlob } from './batched_blob.js';
|
|
3
4
|
import { Blob } from './blob.js';
|
|
5
|
+
import { BlobAccumulator, FinalBlobAccumulator, FinalBlobBatchingChallenges } from './circuit_types/index.js';
|
|
4
6
|
/**
|
|
5
7
|
* A class to create, manage, and prove batched EVM blobs.
|
|
6
|
-
|
|
7
|
-
export declare class BatchedBlob {
|
|
8
|
-
/** Hash of Cs (to link to L1 blob hashes). */
|
|
9
|
-
readonly blobCommitmentsHash: Fr;
|
|
10
|
-
/** Challenge point z such that p_i(z) = y_i. */
|
|
11
|
-
readonly z: Fr;
|
|
12
|
-
/** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
13
|
-
readonly y: BLS12Fr;
|
|
14
|
-
/** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
|
|
15
|
-
readonly commitment: BLS12Point;
|
|
16
|
-
/** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
17
|
-
readonly q: BLS12Point;
|
|
18
|
-
constructor(
|
|
19
|
-
/** Hash of Cs (to link to L1 blob hashes). */
|
|
20
|
-
blobCommitmentsHash: Fr,
|
|
21
|
-
/** Challenge point z such that p_i(z) = y_i. */
|
|
22
|
-
z: Fr,
|
|
23
|
-
/** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
24
|
-
y: BLS12Fr,
|
|
25
|
-
/** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
|
|
26
|
-
commitment: BLS12Point,
|
|
27
|
-
/** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
28
|
-
q: BLS12Point);
|
|
29
|
-
/**
|
|
30
|
-
* Get the final batched opening proof from multiple blobs.
|
|
31
|
-
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
32
|
-
* beforehand from ALL blobs.
|
|
33
|
-
*
|
|
34
|
-
* @returns A batched blob.
|
|
35
|
-
*/
|
|
36
|
-
static batch(blobs: Blob[]): Promise<BatchedBlob>;
|
|
37
|
-
/**
|
|
38
|
-
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
39
|
-
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
40
|
-
* beforehand from ALL blobs.
|
|
41
|
-
*/
|
|
42
|
-
static newAccumulator(blobs: Blob[]): Promise<BatchedBlobAccumulator>;
|
|
43
|
-
/**
|
|
44
|
-
* Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
|
|
45
|
-
* Used in BatchedBlobAccumulator as 'finalZ' and finalGamma':
|
|
46
|
-
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
47
|
-
* - where z_i = H(H(fields of blob_i), C_i) = Blob.challengeZ,
|
|
48
|
-
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
49
|
-
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
50
|
-
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }, for all blob evaluations y_i (see above) and commitments C_i.
|
|
51
|
-
* @returns Challenges z and gamma.
|
|
52
|
-
*/
|
|
53
|
-
static precomputeBatchedBlobChallenges(blobs: Blob[]): Promise<FinalBlobBatchingChallenges>;
|
|
54
|
-
static precomputeEmptyBatchedBlobChallenges(): Promise<FinalBlobBatchingChallenges>;
|
|
55
|
-
getEthVersionedBlobHash(): Buffer;
|
|
56
|
-
static getEthVersionedBlobHash(commitment: Buffer): Buffer;
|
|
57
|
-
/**
|
|
58
|
-
* Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
|
|
59
|
-
*
|
|
60
|
-
* input[:32] - versioned_hash
|
|
61
|
-
* input[32:64] - z
|
|
62
|
-
* input[64:96] - y
|
|
63
|
-
* input[96:144] - commitment C
|
|
64
|
-
* input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
|
|
65
|
-
*
|
|
66
|
-
* See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
|
|
67
|
-
*/
|
|
68
|
-
getEthBlobEvaluationInputs(): `0x${string}`;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
|
|
72
|
-
* - Checking equality in each block merge circuit and propagating up
|
|
73
|
-
* - Checking final z_acc == z in root circuit
|
|
74
|
-
* - Checking final gamma_acc == gamma in root circuit
|
|
75
|
-
*
|
|
76
|
-
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
77
|
-
* - where z_i = H(H(fields of blob_i), C_i),
|
|
78
|
-
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
79
|
-
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
80
|
-
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
|
|
81
|
-
* for all blob evaluations y_i (see above) and commitments C_i.
|
|
82
|
-
*
|
|
83
|
-
* Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
|
|
84
|
-
*/
|
|
85
|
-
export declare class FinalBlobBatchingChallenges {
|
|
86
|
-
readonly z: Fr;
|
|
87
|
-
readonly gamma: BLS12Fr;
|
|
88
|
-
constructor(z: Fr, gamma: BLS12Fr);
|
|
89
|
-
equals(other: FinalBlobBatchingChallenges): boolean;
|
|
90
|
-
static empty(): FinalBlobBatchingChallenges;
|
|
91
|
-
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobBatchingChallenges;
|
|
92
|
-
toBuffer(): Buffer<ArrayBufferLike>;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* See noir-projects/noir-protocol-circuits/crates/blob/src/blob_batching_public_inputs.nr -> BlobAccumulatorPublicInputs
|
|
8
|
+
* See noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr
|
|
96
9
|
*/
|
|
97
10
|
export declare class BatchedBlobAccumulator {
|
|
98
|
-
/** Hash of Cs (to link to L1 blob hashes). */
|
|
99
11
|
readonly blobCommitmentsHashAcc: Fr;
|
|
100
|
-
/** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
|
|
101
12
|
readonly zAcc: Fr;
|
|
102
|
-
/** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
103
13
|
readonly yAcc: BLS12Fr;
|
|
104
|
-
/** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
|
|
105
14
|
readonly cAcc: BLS12Point;
|
|
106
|
-
/** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
107
15
|
readonly qAcc: BLS12Point;
|
|
108
|
-
/**
|
|
109
|
-
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
110
|
-
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
111
|
-
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
112
|
-
*/
|
|
113
16
|
readonly gammaAcc: Fr;
|
|
114
|
-
/** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
|
|
115
17
|
readonly gammaPow: BLS12Fr;
|
|
116
|
-
/** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
|
|
117
18
|
readonly finalBlobChallenges: FinalBlobBatchingChallenges;
|
|
118
19
|
constructor(
|
|
119
20
|
/** Hash of Cs (to link to L1 blob hashes). */
|
|
@@ -136,38 +37,51 @@ export declare class BatchedBlobAccumulator {
|
|
|
136
37
|
gammaPow: BLS12Fr,
|
|
137
38
|
/** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
|
|
138
39
|
finalBlobChallenges: FinalBlobBatchingChallenges);
|
|
139
|
-
/**
|
|
140
|
-
* Init the first accumulation state of the epoch.
|
|
141
|
-
* We assume the input blob has not been evaluated at z.
|
|
142
|
-
*
|
|
143
|
-
* First state of the accumulator:
|
|
144
|
-
* - v_acc := sha256(C_0)
|
|
145
|
-
* - z_acc := z_0
|
|
146
|
-
* - y_acc := gamma^0 * y_0 = y_0
|
|
147
|
-
* - c_acc := gamma^0 * c_0 = c_0
|
|
148
|
-
* - gamma_acc := poseidon2(y_0.limbs)
|
|
149
|
-
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
150
|
-
*
|
|
151
|
-
* @returns An initial blob accumulator.
|
|
152
|
-
*/
|
|
153
|
-
static initialize(blob: Blob, finalBlobChallenges: FinalBlobBatchingChallenges): Promise<BatchedBlobAccumulator>;
|
|
154
40
|
/**
|
|
155
41
|
* Create the empty accumulation state of the epoch.
|
|
156
42
|
* @returns An empty blob accumulator with challenges.
|
|
157
43
|
*/
|
|
158
44
|
static newWithChallenges(finalBlobChallenges: FinalBlobBatchingChallenges): BatchedBlobAccumulator;
|
|
45
|
+
/**
|
|
46
|
+
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
47
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
48
|
+
* beforehand from ALL blobs.
|
|
49
|
+
*/
|
|
50
|
+
static fromBlobFields(blobFieldsPerCheckpoint: Fr[][]): Promise<BatchedBlobAccumulator>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the final batched opening proof from multiple blobs.
|
|
53
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
54
|
+
* beforehand from ALL blobs.
|
|
55
|
+
*
|
|
56
|
+
* @returns A batched blob.
|
|
57
|
+
*/
|
|
58
|
+
static batch(blobFieldsPerCheckpoint: Fr[][], verifyProof?: boolean): Promise<BatchedBlob>;
|
|
59
|
+
/**
|
|
60
|
+
* Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
|
|
61
|
+
* Used in BatchedBlobAccumulator as 'finalZ' and finalGamma':
|
|
62
|
+
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
63
|
+
* - where z_i = H(H(fields of blob_i), C_i) = Blob.challengeZ,
|
|
64
|
+
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
65
|
+
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
66
|
+
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }, for all blob evaluations y_i (see above) and commitments C_i.
|
|
67
|
+
*
|
|
68
|
+
* @param blobs - The blobs to precompute the challenges for. Each sub-array is the blobs for an L1 block.
|
|
69
|
+
* @returns Challenges z and gamma.
|
|
70
|
+
*/
|
|
71
|
+
static precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint: Fr[][]): Promise<FinalBlobBatchingChallenges>;
|
|
159
72
|
/**
|
|
160
73
|
* Given blob i, accumulate all state.
|
|
161
74
|
* We assume the input blob has not been evaluated at z.
|
|
162
75
|
* @returns An updated blob accumulator.
|
|
163
76
|
*/
|
|
164
|
-
|
|
77
|
+
accumulateBlob(blob: Blob, blobFieldsHash: Fr): Promise<BatchedBlobAccumulator>;
|
|
165
78
|
/**
|
|
166
79
|
* Given blobs, accumulate all state.
|
|
167
80
|
* We assume the input blobs have not been evaluated at z.
|
|
81
|
+
* @param blobFields - The blob fields of a checkpoint to accumulate.
|
|
168
82
|
* @returns An updated blob accumulator.
|
|
169
83
|
*/
|
|
170
|
-
|
|
84
|
+
accumulateFields(blobFields: Fr[]): Promise<BatchedBlobAccumulator>;
|
|
171
85
|
/**
|
|
172
86
|
* Finalize accumulation state of the epoch.
|
|
173
87
|
* We assume ALL blobs in the epoch have been accumulated.
|
|
@@ -179,10 +93,14 @@ export declare class BatchedBlobAccumulator {
|
|
|
179
93
|
* - c := c_acc (final commitment to be checked on L1)
|
|
180
94
|
* - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
|
|
181
95
|
*
|
|
96
|
+
* @param verifyProof - Whether to verify the KZG proof.
|
|
182
97
|
* @returns A batched blob.
|
|
183
98
|
*/
|
|
184
|
-
finalize(): Promise<BatchedBlob>;
|
|
99
|
+
finalize(verifyProof?: boolean): Promise<BatchedBlob>;
|
|
100
|
+
verify(): boolean;
|
|
185
101
|
isEmptyState(): boolean;
|
|
186
102
|
clone(): BatchedBlobAccumulator;
|
|
103
|
+
toBlobAccumulator(): BlobAccumulator;
|
|
104
|
+
toFinalBlobAccumulator(): FinalBlobAccumulator;
|
|
187
105
|
}
|
|
188
|
-
//# sourceMappingURL=
|
|
106
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmxvYl9iYXRjaGluZy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2Jsb2JfYmF0Y2hpbmcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBR0EsT0FBTyxFQUFFLE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSxnQ0FBZ0MsQ0FBQztBQUNyRSxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFFcEQsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLG1CQUFtQixDQUFDO0FBQ2hELE9BQU8sRUFBRSxJQUFJLEVBQUUsTUFBTSxXQUFXLENBQUM7QUFFakMsT0FBTyxFQUFFLGVBQWUsRUFBRSxvQkFBb0IsRUFBRSwyQkFBMkIsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBSTlHOzs7R0FHRztBQUNILHFCQUFhLHNCQUFzQjthQUdmLHNCQUFzQixFQUFFLEVBQUU7YUFFMUIsSUFBSSxFQUFFLEVBQUU7YUFFUixJQUFJLEVBQUUsT0FBTzthQUViLElBQUksRUFBRSxVQUFVO2FBRWhCLElBQUksRUFBRSxVQUFVO2FBTWhCLFFBQVEsRUFBRSxFQUFFO2FBRVosUUFBUSxFQUFFLE9BQU87YUFFakIsbUJBQW1CLEVBQUUsMkJBQTJCO0lBcEJsRTtJQUNFLDhDQUE4QztJQUM5QixzQkFBc0IsRUFBRSxFQUFFO0lBQzFDLHNFQUFzRTtJQUN0RCxJQUFJLEVBQUUsRUFBRTtJQUN4Qix5R0FBeUc7SUFDekYsSUFBSSxFQUFFLE9BQU87SUFDN0IscUdBQXFHO0lBQ3JGLElBQUksRUFBRSxVQUFVO0lBQ2hDLG9HQUFvRztJQUNwRixJQUFJLEVBQUUsVUFBVTtJQUNoQzs7OztPQUlHO0lBQ2EsUUFBUSxFQUFFLEVBQUU7SUFDNUIsdUdBQXVHO0lBQ3ZGLFFBQVEsRUFBRSxPQUFPO0lBQ2pDLG9HQUFvRztJQUNwRixtQkFBbUIsRUFBRSwyQkFBMkIsRUFDOUQ7SUFFSjs7O09BR0c7SUFDSCxNQUFNLENBQUMsaUJBQWlCLENBQUMsbUJBQW1CLEVBQUUsMkJBQTJCLEdBQUcsc0JBQXNCLENBV2pHO0lBRUQ7Ozs7T0FJRztJQUNILE9BQWEsY0FBYyxDQUFDLHVCQUF1QixFQUFFLEVBQUUsRUFBRSxFQUFFLEdBQUcsT0FBTyxDQUFDLHNCQUFzQixDQUFDLENBRzVGO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsT0FBYSxLQUFLLENBQUMsdUJBQXVCLEVBQUUsRUFBRSxFQUFFLEVBQUUsRUFBRSxXQUFXLFVBQVEsR0FBRyxPQUFPLENBQUMsV0FBVyxDQUFDLENBZTdGO0lBRUQ7Ozs7Ozs7Ozs7O09BV0c7SUFDSCxPQUFhLCtCQUErQixDQUFDLHVCQUF1QixFQUFFLEVBQUUsRUFBRSxFQUFFLEdBQUcsT0FBTyxDQUFDLDJCQUEyQixDQUFDLENBbUNsSDtJQUVEOzs7O09BSUc7SUFDRyxjQUFjLENBQUMsSUFBSSxFQUFFLElBQUksRUFBRSxjQUFjLEVBQUUsRUFBRSxtQ0F1Q2xEO0lBRUQ7Ozs7O09BS0c7SUFDRyxnQkFBZ0IsQ0FBQyxVQUFVLEVBQUUsRUFBRSxFQUFFLG1DQWtCdEM7SUFFRDs7Ozs7Ozs7Ozs7OztPQWFHO0lBQ0csUUFBUSxDQUFDLFdBQVcsVUFBUSxHQUFHLE9BQU8sQ0FBQyxXQUFXLENBQUMsQ0FzQnhEO0lBRUQsTUFBTSxZQU9MO0lBRUQsWUFBWSxZQVVYO0lBRUQsS0FBSywyQkFXSjtJQUVELGlCQUFpQixvQkFTaEI7SUFFRCxzQkFBc0IseUJBRXJCO0NBQ0YifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blob_batching.d.ts","sourceRoot":"","sources":["../src/blob_batching.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"blob_batching.d.ts","sourceRoot":"","sources":["../src/blob_batching.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AACrE,OAAO,EAAE,EAAE,EAAE,MAAM,gCAAgC,CAAC;AAEpD,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,eAAe,EAAE,oBAAoB,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAI9G;;;GAGG;AACH,qBAAa,sBAAsB;aAGf,sBAAsB,EAAE,EAAE;aAE1B,IAAI,EAAE,EAAE;aAER,IAAI,EAAE,OAAO;aAEb,IAAI,EAAE,UAAU;aAEhB,IAAI,EAAE,UAAU;aAMhB,QAAQ,EAAE,EAAE;aAEZ,QAAQ,EAAE,OAAO;aAEjB,mBAAmB,EAAE,2BAA2B;IApBlE;IACE,8CAA8C;IAC9B,sBAAsB,EAAE,EAAE;IAC1C,sEAAsE;IACtD,IAAI,EAAE,EAAE;IACxB,yGAAyG;IACzF,IAAI,EAAE,OAAO;IAC7B,qGAAqG;IACrF,IAAI,EAAE,UAAU;IAChC,oGAAoG;IACpF,IAAI,EAAE,UAAU;IAChC;;;;OAIG;IACa,QAAQ,EAAE,EAAE;IAC5B,uGAAuG;IACvF,QAAQ,EAAE,OAAO;IACjC,oGAAoG;IACpF,mBAAmB,EAAE,2BAA2B,EAC9D;IAEJ;;;OAGG;IACH,MAAM,CAAC,iBAAiB,CAAC,mBAAmB,EAAE,2BAA2B,GAAG,sBAAsB,CAWjG;IAED;;;;OAIG;IACH,OAAa,cAAc,CAAC,uBAAuB,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAG5F;IAED;;;;;;OAMG;IACH,OAAa,KAAK,CAAC,uBAAuB,EAAE,EAAE,EAAE,EAAE,EAAE,WAAW,UAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,CAe7F;IAED;;;;;;;;;;;OAWG;IACH,OAAa,+BAA+B,CAAC,uBAAuB,EAAE,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAmClH;IAED;;;;OAIG;IACG,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,mCAuClD;IAED;;;;;OAKG;IACG,gBAAgB,CAAC,UAAU,EAAE,EAAE,EAAE,mCAkBtC;IAED;;;;;;;;;;;;;OAaG;IACG,QAAQ,CAAC,WAAW,UAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,CAsBxD;IAED,MAAM,YAOL;IAED,YAAY,YAUX;IAED,KAAK,2BAWJ;IAED,iBAAiB,oBAShB;IAED,sBAAsB,yBAErB;CACF"}
|
package/dest/blob_batching.js
CHANGED
|
@@ -1,25 +1,52 @@
|
|
|
1
|
-
import { AZTEC_MAX_EPOCH_DURATION,
|
|
2
|
-
import { poseidon2Hash
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
|
|
1
|
+
import { AZTEC_MAX_EPOCH_DURATION, BLOBS_PER_CHECKPOINT } from '@aztec/constants';
|
|
2
|
+
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
3
|
+
import { sha256ToField } from '@aztec/foundation/crypto/sha256';
|
|
4
|
+
import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
|
|
5
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
|
+
import { BatchedBlob } from './batched_blob.js';
|
|
7
|
+
import { getBlobsPerL1Block } from './blob_utils.js';
|
|
8
|
+
import { BlobAccumulator, FinalBlobAccumulator, FinalBlobBatchingChallenges } from './circuit_types/index.js';
|
|
9
|
+
import { computeBlobFieldsHash, hashNoirBigNumLimbs } from './hash.js';
|
|
10
|
+
import { getKzg } from './kzg_context.js';
|
|
9
11
|
/**
|
|
10
12
|
* A class to create, manage, and prove batched EVM blobs.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
* See noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr
|
|
14
|
+
*/ export class BatchedBlobAccumulator {
|
|
15
|
+
blobCommitmentsHashAcc;
|
|
16
|
+
zAcc;
|
|
17
|
+
yAcc;
|
|
18
|
+
cAcc;
|
|
19
|
+
qAcc;
|
|
20
|
+
gammaAcc;
|
|
21
|
+
gammaPow;
|
|
22
|
+
finalBlobChallenges;
|
|
23
|
+
constructor(/** Hash of Cs (to link to L1 blob hashes). */ blobCommitmentsHashAcc, /** Challenge point z_acc. Final value used such that p_i(z) = y_i. */ zAcc, /** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */ yAcc, /** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */ cAcc, /** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */ qAcc, /**
|
|
24
|
+
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
25
|
+
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
26
|
+
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
27
|
+
*/ gammaAcc, /** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */ gammaPow, /** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */ finalBlobChallenges){
|
|
28
|
+
this.blobCommitmentsHashAcc = blobCommitmentsHashAcc;
|
|
29
|
+
this.zAcc = zAcc;
|
|
30
|
+
this.yAcc = yAcc;
|
|
31
|
+
this.cAcc = cAcc;
|
|
32
|
+
this.qAcc = qAcc;
|
|
33
|
+
this.gammaAcc = gammaAcc;
|
|
34
|
+
this.gammaPow = gammaPow;
|
|
35
|
+
this.finalBlobChallenges = finalBlobChallenges;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create the empty accumulation state of the epoch.
|
|
39
|
+
* @returns An empty blob accumulator with challenges.
|
|
40
|
+
*/ static newWithChallenges(finalBlobChallenges) {
|
|
41
|
+
return new BatchedBlobAccumulator(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO, BLS12Point.ZERO, Fr.ZERO, BLS12Fr.ZERO, finalBlobChallenges);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
45
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
46
|
+
* beforehand from ALL blobs.
|
|
47
|
+
*/ static async fromBlobFields(blobFieldsPerCheckpoint) {
|
|
48
|
+
const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint);
|
|
49
|
+
return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
|
|
23
50
|
}
|
|
24
51
|
/**
|
|
25
52
|
* Get the final batched opening proof from multiple blobs.
|
|
@@ -27,24 +54,18 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
27
54
|
* beforehand from ALL blobs.
|
|
28
55
|
*
|
|
29
56
|
* @returns A batched blob.
|
|
30
|
-
*/ static async batch(
|
|
31
|
-
const
|
|
32
|
-
if (
|
|
33
|
-
throw new Error(`Too many
|
|
57
|
+
*/ static async batch(blobFieldsPerCheckpoint, verifyProof = false) {
|
|
58
|
+
const numCheckpoints = blobFieldsPerCheckpoint.length;
|
|
59
|
+
if (numCheckpoints > AZTEC_MAX_EPOCH_DURATION) {
|
|
60
|
+
throw new Error(`Too many checkpoints sent to batch(). The maximum is ${AZTEC_MAX_EPOCH_DURATION}. Got ${numCheckpoints}.`);
|
|
34
61
|
}
|
|
35
62
|
// Precalculate the values (z and gamma) and initialize the accumulator:
|
|
36
|
-
let acc = await this.
|
|
63
|
+
let acc = await this.fromBlobFields(blobFieldsPerCheckpoint);
|
|
37
64
|
// Now we can create a multi opening proof of all input blobs:
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
43
|
-
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
44
|
-
* beforehand from ALL blobs.
|
|
45
|
-
*/ static async newAccumulator(blobs) {
|
|
46
|
-
const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobs);
|
|
47
|
-
return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
|
|
65
|
+
for (const blobFields of blobFieldsPerCheckpoint){
|
|
66
|
+
acc = await acc.accumulateFields(blobFields);
|
|
67
|
+
}
|
|
68
|
+
return await acc.finalize(verifyProof);
|
|
48
69
|
}
|
|
49
70
|
/**
|
|
50
71
|
* Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
|
|
@@ -54,27 +75,44 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
54
75
|
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
55
76
|
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
56
77
|
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }, for all blob evaluations y_i (see above) and commitments C_i.
|
|
78
|
+
*
|
|
79
|
+
* @param blobs - The blobs to precompute the challenges for. Each sub-array is the blobs for an L1 block.
|
|
57
80
|
* @returns Challenges z and gamma.
|
|
58
|
-
*/ static async precomputeBatchedBlobChallenges(
|
|
59
|
-
//
|
|
60
|
-
let z
|
|
61
|
-
|
|
62
|
-
for(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
81
|
+
*/ static async precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint) {
|
|
82
|
+
// Compute the final challenge z to evaluate the blobs.
|
|
83
|
+
let z;
|
|
84
|
+
const allBlobs = [];
|
|
85
|
+
for (const blobFields of blobFieldsPerCheckpoint){
|
|
86
|
+
// Compute the hash of all the fields in the block.
|
|
87
|
+
const blobFieldsHash = await computeBlobFieldsHash(blobFields);
|
|
88
|
+
const blobs = getBlobsPerL1Block(blobFields);
|
|
89
|
+
for (const blob of blobs){
|
|
90
|
+
// Compute the challenge z for each blob and accumulate it.
|
|
91
|
+
const challengeZ = await blob.computeChallengeZ(blobFieldsHash);
|
|
92
|
+
if (!z) {
|
|
93
|
+
z = challengeZ;
|
|
94
|
+
} else {
|
|
95
|
+
z = await poseidon2Hash([
|
|
96
|
+
z,
|
|
97
|
+
challengeZ
|
|
98
|
+
]);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
allBlobs.push(...blobs);
|
|
102
|
+
}
|
|
103
|
+
if (!z) {
|
|
104
|
+
throw new Error('No blobs to precompute challenges for.');
|
|
67
105
|
}
|
|
68
106
|
// Now we have a shared challenge for all blobs, evaluate them...
|
|
69
|
-
const proofObjects =
|
|
70
|
-
const evaluations = proofObjects.map((
|
|
107
|
+
const proofObjects = allBlobs.map((b)=>b.evaluate(z));
|
|
108
|
+
const evaluations = await Promise.all(proofObjects.map(({ y })=>hashNoirBigNumLimbs(y)));
|
|
71
109
|
// ...and find the challenge for the linear combination of blobs.
|
|
72
|
-
let gamma =
|
|
110
|
+
let gamma = evaluations[0];
|
|
73
111
|
// We start at i = 1, because gamma is initialized as the first blob's evaluation.
|
|
74
|
-
for(let i = 1; i <
|
|
112
|
+
for(let i = 1; i < allBlobs.length; i++){
|
|
75
113
|
gamma = await poseidon2Hash([
|
|
76
114
|
gamma,
|
|
77
|
-
|
|
115
|
+
evaluations[i]
|
|
78
116
|
]);
|
|
79
117
|
}
|
|
80
118
|
gamma = await poseidon2Hash([
|
|
@@ -83,160 +121,36 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
83
121
|
]);
|
|
84
122
|
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
85
123
|
}
|
|
86
|
-
static async precomputeEmptyBatchedBlobChallenges() {
|
|
87
|
-
const blobs = [
|
|
88
|
-
await Blob.fromFields([])
|
|
89
|
-
];
|
|
90
|
-
// We need to precompute the final challenge values to evaluate the blobs.
|
|
91
|
-
const z = blobs[0].challengeZ;
|
|
92
|
-
// Now we have a shared challenge for all blobs, evaluate them...
|
|
93
|
-
const proofObjects = blobs.map((b)=>computeKzgProof(b.data, z.toBuffer()));
|
|
94
|
-
const evaluations = proofObjects.map(([_, evaluation])=>BLS12Fr.fromBuffer(Buffer.from(evaluation)));
|
|
95
|
-
// ...and find the challenge for the linear combination of blobs.
|
|
96
|
-
let gamma = await hashNoirBigNumLimbs(evaluations[0]);
|
|
97
|
-
gamma = await poseidon2Hash([
|
|
98
|
-
gamma,
|
|
99
|
-
z
|
|
100
|
-
]);
|
|
101
|
-
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
102
|
-
}
|
|
103
|
-
// Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
|
|
104
|
-
getEthVersionedBlobHash() {
|
|
105
|
-
const hash = sha256(this.commitment.compress());
|
|
106
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
107
|
-
return hash;
|
|
108
|
-
}
|
|
109
|
-
static getEthVersionedBlobHash(commitment) {
|
|
110
|
-
const hash = sha256(commitment);
|
|
111
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
112
|
-
return hash;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
|
|
116
|
-
*
|
|
117
|
-
* input[:32] - versioned_hash
|
|
118
|
-
* input[32:64] - z
|
|
119
|
-
* input[64:96] - y
|
|
120
|
-
* input[96:144] - commitment C
|
|
121
|
-
* input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
|
|
122
|
-
*
|
|
123
|
-
* See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
|
|
124
|
-
*/ getEthBlobEvaluationInputs() {
|
|
125
|
-
const buf = Buffer.concat([
|
|
126
|
-
this.getEthVersionedBlobHash(),
|
|
127
|
-
this.z.toBuffer(),
|
|
128
|
-
this.y.toBuffer(),
|
|
129
|
-
this.commitment.compress(),
|
|
130
|
-
this.q.compress()
|
|
131
|
-
]);
|
|
132
|
-
return `0x${buf.toString('hex')}`;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
|
|
137
|
-
* - Checking equality in each block merge circuit and propagating up
|
|
138
|
-
* - Checking final z_acc == z in root circuit
|
|
139
|
-
* - Checking final gamma_acc == gamma in root circuit
|
|
140
|
-
*
|
|
141
|
-
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
142
|
-
* - where z_i = H(H(fields of blob_i), C_i),
|
|
143
|
-
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
144
|
-
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
145
|
-
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
|
|
146
|
-
* for all blob evaluations y_i (see above) and commitments C_i.
|
|
147
|
-
*
|
|
148
|
-
* Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
|
|
149
|
-
*/ export class FinalBlobBatchingChallenges {
|
|
150
|
-
z;
|
|
151
|
-
gamma;
|
|
152
|
-
constructor(z, gamma){
|
|
153
|
-
this.z = z;
|
|
154
|
-
this.gamma = gamma;
|
|
155
|
-
}
|
|
156
|
-
equals(other) {
|
|
157
|
-
return this.z.equals(other.z) && this.gamma.equals(other.gamma);
|
|
158
|
-
}
|
|
159
|
-
static empty() {
|
|
160
|
-
return new FinalBlobBatchingChallenges(Fr.ZERO, BLS12Fr.ZERO);
|
|
161
|
-
}
|
|
162
|
-
static fromBuffer(buffer) {
|
|
163
|
-
const reader = BufferReader.asReader(buffer);
|
|
164
|
-
return new FinalBlobBatchingChallenges(Fr.fromBuffer(reader), reader.readObject(BLS12Fr));
|
|
165
|
-
}
|
|
166
|
-
toBuffer() {
|
|
167
|
-
return serializeToBuffer(this.z, this.gamma);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* See noir-projects/noir-protocol-circuits/crates/blob/src/blob_batching_public_inputs.nr -> BlobAccumulatorPublicInputs
|
|
172
|
-
*/ export class BatchedBlobAccumulator {
|
|
173
|
-
blobCommitmentsHashAcc;
|
|
174
|
-
zAcc;
|
|
175
|
-
yAcc;
|
|
176
|
-
cAcc;
|
|
177
|
-
qAcc;
|
|
178
|
-
gammaAcc;
|
|
179
|
-
gammaPow;
|
|
180
|
-
finalBlobChallenges;
|
|
181
|
-
constructor(/** Hash of Cs (to link to L1 blob hashes). */ blobCommitmentsHashAcc, /** Challenge point z_acc. Final value used such that p_i(z) = y_i. */ zAcc, /** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */ yAcc, /** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */ cAcc, /** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */ qAcc, /**
|
|
182
|
-
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
183
|
-
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
184
|
-
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
185
|
-
*/ gammaAcc, /** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */ gammaPow, /** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */ finalBlobChallenges){
|
|
186
|
-
this.blobCommitmentsHashAcc = blobCommitmentsHashAcc;
|
|
187
|
-
this.zAcc = zAcc;
|
|
188
|
-
this.yAcc = yAcc;
|
|
189
|
-
this.cAcc = cAcc;
|
|
190
|
-
this.qAcc = qAcc;
|
|
191
|
-
this.gammaAcc = gammaAcc;
|
|
192
|
-
this.gammaPow = gammaPow;
|
|
193
|
-
this.finalBlobChallenges = finalBlobChallenges;
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* Init the first accumulation state of the epoch.
|
|
197
|
-
* We assume the input blob has not been evaluated at z.
|
|
198
|
-
*
|
|
199
|
-
* First state of the accumulator:
|
|
200
|
-
* - v_acc := sha256(C_0)
|
|
201
|
-
* - z_acc := z_0
|
|
202
|
-
* - y_acc := gamma^0 * y_0 = y_0
|
|
203
|
-
* - c_acc := gamma^0 * c_0 = c_0
|
|
204
|
-
* - gamma_acc := poseidon2(y_0.limbs)
|
|
205
|
-
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
206
|
-
*
|
|
207
|
-
* @returns An initial blob accumulator.
|
|
208
|
-
*/ static async initialize(blob, finalBlobChallenges) {
|
|
209
|
-
const [q, evaluation] = computeKzgProof(blob.data, finalBlobChallenges.z.toBuffer());
|
|
210
|
-
const firstY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
211
|
-
// Here, i = 0, so:
|
|
212
|
-
return new BatchedBlobAccumulator(sha256ToField([
|
|
213
|
-
blob.commitment
|
|
214
|
-
]), blob.challengeZ, firstY, BLS12Point.decompress(blob.commitment), BLS12Point.decompress(Buffer.from(q)), await hashNoirBigNumLimbs(firstY), finalBlobChallenges.gamma, finalBlobChallenges);
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Create the empty accumulation state of the epoch.
|
|
218
|
-
* @returns An empty blob accumulator with challenges.
|
|
219
|
-
*/ static newWithChallenges(finalBlobChallenges) {
|
|
220
|
-
return new BatchedBlobAccumulator(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO, BLS12Point.ZERO, Fr.ZERO, BLS12Fr.ZERO, finalBlobChallenges);
|
|
221
|
-
}
|
|
222
124
|
/**
|
|
223
125
|
* Given blob i, accumulate all state.
|
|
224
126
|
* We assume the input blob has not been evaluated at z.
|
|
225
127
|
* @returns An updated blob accumulator.
|
|
226
|
-
*/ async
|
|
128
|
+
*/ async accumulateBlob(blob, blobFieldsHash) {
|
|
129
|
+
const { proof, y: thisY } = blob.evaluate(this.finalBlobChallenges.z);
|
|
130
|
+
const thisC = BLS12Point.decompress(blob.commitment);
|
|
131
|
+
const thisQ = BLS12Point.decompress(proof);
|
|
132
|
+
const blobChallengeZ = await blob.computeChallengeZ(blobFieldsHash);
|
|
227
133
|
if (this.isEmptyState()) {
|
|
228
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Init the first accumulation state of the epoch.
|
|
136
|
+
* - v_acc := sha256(C_0)
|
|
137
|
+
* - z_acc := z_0
|
|
138
|
+
* - y_acc := gamma^0 * y_0 = y_0
|
|
139
|
+
* - c_acc := gamma^0 * c_0 = c_0
|
|
140
|
+
* - gamma_acc := poseidon2(y_0.limbs)
|
|
141
|
+
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
142
|
+
*/ return new BatchedBlobAccumulator(sha256ToField([
|
|
143
|
+
blob.commitment
|
|
144
|
+
]), blobChallengeZ, thisY, thisC, thisQ, await hashNoirBigNumLimbs(thisY), this.finalBlobChallenges.gamma, this.finalBlobChallenges);
|
|
229
145
|
} else {
|
|
230
|
-
const [q, evaluation] = computeKzgProof(blob.data, this.finalBlobChallenges.z.toBuffer());
|
|
231
|
-
const thisY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
232
146
|
// Moving from i - 1 to i, so:
|
|
233
147
|
return new BatchedBlobAccumulator(sha256ToField([
|
|
234
148
|
this.blobCommitmentsHashAcc,
|
|
235
149
|
blob.commitment
|
|
236
150
|
]), await poseidon2Hash([
|
|
237
151
|
this.zAcc,
|
|
238
|
-
|
|
239
|
-
]), this.yAcc.add(thisY.mul(this.gammaPow)), this.cAcc.add(
|
|
152
|
+
blobChallengeZ
|
|
153
|
+
]), this.yAcc.add(thisY.mul(this.gammaPow)), this.cAcc.add(thisC.mul(this.gammaPow)), this.qAcc.add(thisQ.mul(this.gammaPow)), await poseidon2Hash([
|
|
240
154
|
this.gammaAcc,
|
|
241
155
|
await hashNoirBigNumLimbs(thisY)
|
|
242
156
|
]), this.gammaPow.mul(this.finalBlobChallenges.gamma), this.finalBlobChallenges);
|
|
@@ -245,12 +159,19 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
245
159
|
/**
|
|
246
160
|
* Given blobs, accumulate all state.
|
|
247
161
|
* We assume the input blobs have not been evaluated at z.
|
|
162
|
+
* @param blobFields - The blob fields of a checkpoint to accumulate.
|
|
248
163
|
* @returns An updated blob accumulator.
|
|
249
|
-
*/ async
|
|
164
|
+
*/ async accumulateFields(blobFields) {
|
|
165
|
+
const blobs = getBlobsPerL1Block(blobFields);
|
|
166
|
+
if (blobs.length > BLOBS_PER_CHECKPOINT) {
|
|
167
|
+
throw new Error(`Too many blobs to accumulate. The maximum is ${BLOBS_PER_CHECKPOINT} per checkpoint. Got ${blobs.length}.`);
|
|
168
|
+
}
|
|
169
|
+
// Compute the hash of all the fields in the block.
|
|
170
|
+
const blobFieldsHash = await computeBlobFieldsHash(blobFields);
|
|
250
171
|
// Initialize the acc to iterate over:
|
|
251
172
|
let acc = this.clone();
|
|
252
|
-
for(
|
|
253
|
-
acc = await acc.
|
|
173
|
+
for (const blob of blobs){
|
|
174
|
+
acc = await acc.accumulateBlob(blob, blobFieldsHash);
|
|
254
175
|
}
|
|
255
176
|
return acc;
|
|
256
177
|
}
|
|
@@ -265,8 +186,9 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
265
186
|
* - c := c_acc (final commitment to be checked on L1)
|
|
266
187
|
* - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
|
|
267
188
|
*
|
|
189
|
+
* @param verifyProof - Whether to verify the KZG proof.
|
|
268
190
|
* @returns A batched blob.
|
|
269
|
-
*/ async finalize() {
|
|
191
|
+
*/ async finalize(verifyProof = false) {
|
|
270
192
|
// All values in acc are final, apart from gamma := poseidon2(gammaAcc, z):
|
|
271
193
|
const calculatedGamma = await poseidon2Hash([
|
|
272
194
|
this.gammaAcc,
|
|
@@ -279,10 +201,14 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
279
201
|
if (!calculatedGamma.equals(this.finalBlobChallenges.gamma.toBN254Fr())) {
|
|
280
202
|
throw new Error(`Blob batching mismatch: accumulated gamma ${calculatedGamma} does not equal injected gamma ${this.finalBlobChallenges.gamma.toBN254Fr()}`);
|
|
281
203
|
}
|
|
282
|
-
|
|
204
|
+
const batchedBlob = new BatchedBlob(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.qAcc);
|
|
205
|
+
if (verifyProof && !this.verify()) {
|
|
283
206
|
throw new Error(`KZG proof did not verify.`);
|
|
284
207
|
}
|
|
285
|
-
return
|
|
208
|
+
return batchedBlob;
|
|
209
|
+
}
|
|
210
|
+
verify() {
|
|
211
|
+
return getKzg().verifyKzgProof(this.cAcc.compress(), this.zAcc.toBuffer(), this.yAcc.toBuffer(), this.qAcc.compress());
|
|
286
212
|
}
|
|
287
213
|
isEmptyState() {
|
|
288
214
|
return this.blobCommitmentsHashAcc.isZero() && this.zAcc.isZero() && this.yAcc.isZero() && this.cAcc.isZero() && this.qAcc.isZero() && this.gammaAcc.isZero() && this.gammaPow.isZero();
|
|
@@ -290,10 +216,10 @@ const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
|
290
216
|
clone() {
|
|
291
217
|
return new BatchedBlobAccumulator(Fr.fromBuffer(this.blobCommitmentsHashAcc.toBuffer()), Fr.fromBuffer(this.zAcc.toBuffer()), BLS12Fr.fromBuffer(this.yAcc.toBuffer()), BLS12Point.fromBuffer(this.cAcc.toBuffer()), BLS12Point.fromBuffer(this.qAcc.toBuffer()), Fr.fromBuffer(this.gammaAcc.toBuffer()), BLS12Fr.fromBuffer(this.gammaPow.toBuffer()), FinalBlobBatchingChallenges.fromBuffer(this.finalBlobChallenges.toBuffer()));
|
|
292
218
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
219
|
+
toBlobAccumulator() {
|
|
220
|
+
return new BlobAccumulator(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.gammaAcc, this.gammaPow);
|
|
221
|
+
}
|
|
222
|
+
toFinalBlobAccumulator() {
|
|
223
|
+
return new FinalBlobAccumulator(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc);
|
|
224
|
+
}
|
|
299
225
|
}
|