@aztec/blob-lib 1.0.0-nightly.20250608 → 1.0.0-nightly.20250610

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.
@@ -0,0 +1,381 @@
1
+ import { AZTEC_MAX_EPOCH_DURATION, BLOBS_PER_BLOCK } from '@aztec/constants';
2
+ import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
3
+ import { BLS12Field, BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
4
+ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
5
+
6
+ // Importing directly from 'c-kzg' does not work:
7
+ import cKzg from 'c-kzg';
8
+
9
+ import { Blob, VERSIONED_HASH_VERSION_KZG } from './blob.js';
10
+ import { BlobAccumulatorPublicInputs, FinalBlobAccumulatorPublicInputs } from './blob_batching_public_inputs.js';
11
+
12
+ const { computeKzgProof, verifyKzgProof } = cKzg;
13
+
14
+ /**
15
+ * A class to create, manage, and prove batched EVM blobs.
16
+ */
17
+ export class BatchedBlob {
18
+ constructor(
19
+ /** Hash of Cs (to link to L1 blob hashes). */
20
+ public readonly blobCommitmentsHash: Fr,
21
+ /** Challenge point z such that p_i(z) = y_i. */
22
+ public readonly z: Fr,
23
+ /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
24
+ public readonly y: BLS12Fr,
25
+ /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
26
+ public readonly commitment: BLS12Point,
27
+ /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
28
+ public readonly q: BLS12Point,
29
+ ) {}
30
+
31
+ /**
32
+ * Get the final batched opening proof from multiple blobs.
33
+ *
34
+ * TODO(MW): Using the old Blob struct means there are ignored values (e.g. blob.evaluationY, because we now evaluate at shared z).
35
+ * When switching to batching, create new class w/o useless values.
36
+ *
37
+ * @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
38
+ * beforehand from ALL blobs.
39
+ *
40
+ * @returns A batched blob.
41
+ */
42
+ static async batch(blobs: Blob[]): Promise<BatchedBlob> {
43
+ const numBlobs = blobs.length;
44
+ if (numBlobs > BLOBS_PER_BLOCK * AZTEC_MAX_EPOCH_DURATION) {
45
+ throw new Error(
46
+ `Too many blobs (${numBlobs}) sent to batch(). The maximum is ${BLOBS_PER_BLOCK * AZTEC_MAX_EPOCH_DURATION}.`,
47
+ );
48
+ }
49
+ // Precalculate the values (z and gamma) and initialize the accumulator:
50
+ let acc = await this.newAccumulator(blobs);
51
+ // Now we can create a multi opening proof of all input blobs:
52
+ acc = await acc.accumulateBlobs(blobs);
53
+ return await acc.finalize();
54
+ }
55
+
56
+ /**
57
+ * Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
58
+ * @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
59
+ * beforehand from ALL blobs.
60
+ */
61
+ static async newAccumulator(blobs: Blob[]): Promise<BatchedBlobAccumulator> {
62
+ const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobs);
63
+ return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
64
+ }
65
+
66
+ /**
67
+ * Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
68
+ * Used in BatchedBlobAccumulator as 'finalZ' and finalGamma':
69
+ * - z = H(...H(H(z_0, z_1) z_2)..z_n)
70
+ * - where z_i = H(H(fields of blob_i), C_i) = Blob.challengeZ,
71
+ * - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
72
+ * - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
73
+ * - 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.
74
+ * @returns Challenges z and gamma.
75
+ */
76
+ static async precomputeBatchedBlobChallenges(blobs: Blob[]): Promise<FinalBlobBatchingChallenges> {
77
+ // We need to precompute the final challenge values to evaluate the blobs.
78
+ let z = blobs[0].challengeZ;
79
+ // We start at i = 1, because z is initialised as the first blob's challenge.
80
+ for (let i = 1; i < blobs.length; i++) {
81
+ z = await poseidon2Hash([z, blobs[i].challengeZ]);
82
+ }
83
+ // Now we have a shared challenge for all blobs, evaluate them...
84
+ const proofObjects = blobs.map(b => computeKzgProof(b.data, z.toBuffer()));
85
+ const evaluations = proofObjects.map(([_, evaluation]) => BLS12Fr.fromBuffer(Buffer.from(evaluation)));
86
+ // ...and find the challenge for the linear combination of blobs.
87
+ let gamma = await hashNoirBigNumLimbs(evaluations[0]);
88
+ // We start at i = 1, because gamma is initialised as the first blob's evaluation.
89
+ for (let i = 1; i < blobs.length; i++) {
90
+ gamma = await poseidon2Hash([gamma, await hashNoirBigNumLimbs(evaluations[i])]);
91
+ }
92
+ gamma = await poseidon2Hash([gamma, z]);
93
+
94
+ return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
95
+ }
96
+
97
+ static async precomputeEmptyBatchedBlobChallenges(): Promise<FinalBlobBatchingChallenges> {
98
+ const blobs = [await Blob.fromFields([])];
99
+ // We need to precompute the final challenge values to evaluate the blobs.
100
+ const z = blobs[0].challengeZ;
101
+ // Now we have a shared challenge for all blobs, evaluate them...
102
+ const proofObjects = blobs.map(b => computeKzgProof(b.data, z.toBuffer()));
103
+ const evaluations = proofObjects.map(([_, evaluation]) => BLS12Fr.fromBuffer(Buffer.from(evaluation)));
104
+ // ...and find the challenge for the linear combination of blobs.
105
+ let gamma = await hashNoirBigNumLimbs(evaluations[0]);
106
+ gamma = await poseidon2Hash([gamma, z]);
107
+
108
+ return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
109
+ }
110
+
111
+ // Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
112
+ getEthVersionedBlobHash(): Buffer {
113
+ const hash = sha256(this.commitment.compress());
114
+ hash[0] = VERSIONED_HASH_VERSION_KZG;
115
+ return hash;
116
+ }
117
+
118
+ static getEthVersionedBlobHash(commitment: Buffer): Buffer {
119
+ const hash = sha256(commitment);
120
+ hash[0] = VERSIONED_HASH_VERSION_KZG;
121
+ return hash;
122
+ }
123
+
124
+ /**
125
+ * Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
126
+ *
127
+ * input[:32] - versioned_hash
128
+ * input[32:64] - z
129
+ * input[64:96] - y
130
+ * input[96:144] - commitment C
131
+ * input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
132
+ *
133
+ * See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
134
+ */
135
+ getEthBlobEvaluationInputs(): `0x${string}` {
136
+ const buf = Buffer.concat([
137
+ this.getEthVersionedBlobHash(),
138
+ this.z.toBuffer(),
139
+ this.y.toBuffer(),
140
+ this.commitment.compress(),
141
+ this.q.compress(),
142
+ ]);
143
+ return `0x${buf.toString('hex')}`;
144
+ }
145
+ }
146
+
147
+ /**
148
+ * Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
149
+ * - Checking equality in each block merge circuit and propagating up
150
+ * - Checking final z_acc == z in root circuit
151
+ * - Checking final gamma_acc == gamma in root circuit
152
+ *
153
+ * - z = H(...H(H(z_0, z_1) z_2)..z_n)
154
+ * - where z_i = H(H(fields of blob_i), C_i),
155
+ * - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
156
+ * - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
157
+ * - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
158
+ * for all blob evaluations y_i (see above) and commitments C_i.
159
+ *
160
+ * Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
161
+ */
162
+ export class FinalBlobBatchingChallenges {
163
+ constructor(
164
+ public readonly z: Fr,
165
+ public readonly gamma: BLS12Fr,
166
+ ) {}
167
+
168
+ equals(other: FinalBlobBatchingChallenges) {
169
+ return this.z.equals(other.z) && this.gamma.equals(other.gamma);
170
+ }
171
+
172
+ static empty(): FinalBlobBatchingChallenges {
173
+ return new FinalBlobBatchingChallenges(Fr.ZERO, BLS12Fr.ZERO);
174
+ }
175
+
176
+ static fromBuffer(buffer: Buffer | BufferReader): FinalBlobBatchingChallenges {
177
+ const reader = BufferReader.asReader(buffer);
178
+ return new FinalBlobBatchingChallenges(Fr.fromBuffer(reader), reader.readObject(BLS12Fr));
179
+ }
180
+
181
+ toBuffer() {
182
+ return serializeToBuffer(this.z, this.gamma);
183
+ }
184
+ }
185
+
186
+ /**
187
+ * See noir-projects/noir-protocol-circuits/crates/blob/src/blob_batching_public_inputs.nr -> BlobAccumulatorPublicInputs
188
+ */
189
+ export class BatchedBlobAccumulator {
190
+ constructor(
191
+ /** Hash of Cs (to link to L1 blob hashes). */
192
+ public readonly blobCommitmentsHashAcc: Fr,
193
+ /** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
194
+ public readonly zAcc: Fr,
195
+ /** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
196
+ public readonly yAcc: BLS12Fr,
197
+ /** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
198
+ public readonly cAcc: BLS12Point,
199
+ /** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
200
+ public readonly qAcc: BLS12Point,
201
+ /**
202
+ * Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
203
+ * TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
204
+ * as a BLS12Fr field elt. Is this safe? Is there a skew?
205
+ */
206
+ public readonly gammaAcc: Fr,
207
+ /** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
208
+ public readonly gammaPow: BLS12Fr,
209
+ /** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
210
+ public readonly finalBlobChallenges: FinalBlobBatchingChallenges,
211
+ ) {}
212
+
213
+ /**
214
+ * Init the first accumulation state of the epoch.
215
+ * We assume the input blob has not been evaluated at z.
216
+ *
217
+ * First state of the accumulator:
218
+ * - v_acc := sha256(C_0)
219
+ * - z_acc := z_0
220
+ * - y_acc := gamma^0 * y_0 = y_0
221
+ * - c_acc := gamma^0 * c_0 = c_0
222
+ * - gamma_acc := poseidon2(y_0.limbs)
223
+ * - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
224
+ *
225
+ * TODO(MW): When moved to batching, we should ONLY evaluate individual blobs at z => won't need finalZ input.
226
+ * @returns An initial blob accumulator.
227
+ */
228
+ static async initialize(
229
+ blob: Blob,
230
+ finalBlobChallenges: FinalBlobBatchingChallenges,
231
+ ): Promise<BatchedBlobAccumulator> {
232
+ const [q, evaluation] = computeKzgProof(blob.data, finalBlobChallenges.z.toBuffer());
233
+ const firstY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
234
+ // Here, i = 0, so:
235
+ return new BatchedBlobAccumulator(
236
+ sha256ToField([blob.commitment]), // blobCommitmentsHashAcc = sha256(C_0)
237
+ blob.challengeZ, // zAcc = z_0
238
+ firstY, // yAcc = gamma^0 * y_0 = 1 * y_0
239
+ BLS12Point.decompress(blob.commitment), // cAcc = gamma^0 * C_0 = 1 * C_0
240
+ BLS12Point.decompress(Buffer.from(q)), // qAcc = gamma^0 * Q_0 = 1 * Q_0
241
+ await hashNoirBigNumLimbs(firstY), // gammaAcc = poseidon2(y_0.limbs)
242
+ finalBlobChallenges.gamma, // gammaPow = gamma^(i + 1) = gamma^1 = gamma
243
+ finalBlobChallenges,
244
+ );
245
+ }
246
+
247
+ /**
248
+ * Create the empty accumulation state of the epoch.
249
+ * @returns An empty blob accumulator with challenges.
250
+ */
251
+ static newWithChallenges(finalBlobChallenges: FinalBlobBatchingChallenges): BatchedBlobAccumulator {
252
+ return new BatchedBlobAccumulator(
253
+ Fr.ZERO,
254
+ Fr.ZERO,
255
+ BLS12Fr.ZERO,
256
+ BLS12Point.ZERO,
257
+ BLS12Point.ZERO,
258
+ Fr.ZERO,
259
+ BLS12Fr.ZERO,
260
+ finalBlobChallenges,
261
+ );
262
+ }
263
+
264
+ /**
265
+ * Given blob i, accumulate all state.
266
+ * We assume the input blob has not been evaluated at z.
267
+ * TODO(MW): Currently returning new accumulator. May be better to mutate in future?
268
+ * @returns An updated blob accumulator.
269
+ */
270
+ async accumulate(blob: Blob) {
271
+ if (this.isEmptyState()) {
272
+ return BatchedBlobAccumulator.initialize(blob, this.finalBlobChallenges);
273
+ } else {
274
+ const [q, evaluation] = computeKzgProof(blob.data, this.finalBlobChallenges.z.toBuffer());
275
+ const thisY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
276
+
277
+ // Moving from i - 1 to i, so:
278
+ return new BatchedBlobAccumulator(
279
+ sha256ToField([this.blobCommitmentsHashAcc, blob.commitment]), // blobCommitmentsHashAcc := sha256(blobCommitmentsHashAcc, C_i)
280
+ await poseidon2Hash([this.zAcc, blob.challengeZ]), // zAcc := poseidon2(zAcc, z_i)
281
+ this.yAcc.add(thisY.mul(this.gammaPow)), // yAcc := yAcc + (gamma^i * y_i)
282
+ this.cAcc.add(BLS12Point.decompress(blob.commitment).mul(this.gammaPow)), // cAcc := cAcc + (gamma^i * C_i)
283
+ this.qAcc.add(BLS12Point.decompress(Buffer.from(q)).mul(this.gammaPow)), // qAcc := qAcc + (gamma^i * C_i)
284
+ await poseidon2Hash([this.gammaAcc, await hashNoirBigNumLimbs(thisY)]), // gammaAcc := poseidon2(gammaAcc, poseidon2(y_i.limbs))
285
+ this.gammaPow.mul(this.finalBlobChallenges.gamma), // gammaPow = gamma^(i + 1) = gamma^i * final_gamma
286
+ this.finalBlobChallenges,
287
+ );
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Given blobs, accumulate all state.
293
+ * We assume the input blobs have not been evaluated at z.
294
+ * @returns An updated blob accumulator.
295
+ */
296
+ async accumulateBlobs(blobs: Blob[]) {
297
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
298
+ let acc: BatchedBlobAccumulator = this; // TODO(MW): this.clone()
299
+ for (let i = 0; i < blobs.length; i++) {
300
+ acc = await acc.accumulate(blobs[i]);
301
+ }
302
+ return acc;
303
+ }
304
+
305
+ /**
306
+ * Finalize accumulation state of the epoch.
307
+ * We assume ALL blobs in the epoch have been accumulated.
308
+ *
309
+ * Final accumulated values:
310
+ * - v := v_acc (hash of all commitments (C_i s) to be checked on L1)
311
+ * - z := z_acc (final challenge, at which all blobs are evaluated)
312
+ * - y := y_acc (final opening to be checked on L1)
313
+ * - c := c_acc (final commitment to be checked on L1)
314
+ * - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
315
+ *
316
+ * @returns A batched blob.
317
+ */
318
+ async finalize(): Promise<BatchedBlob> {
319
+ // All values in acc are final, apart from gamma := poseidon2(gammaAcc, z):
320
+ const calculatedGamma = await poseidon2Hash([this.gammaAcc, this.zAcc]);
321
+ // Check final values:
322
+ if (!this.zAcc.equals(this.finalBlobChallenges.z)) {
323
+ throw new Error(
324
+ `Blob batching mismatch: accumulated z ${this.zAcc} does not equal injected z ${this.finalBlobChallenges.z}`,
325
+ );
326
+ }
327
+ if (!calculatedGamma.equals(this.finalBlobChallenges.gamma.toBN254Fr())) {
328
+ throw new Error(
329
+ `Blob batching mismatch: accumulated gamma ${calculatedGamma} does not equal injected gamma ${this.finalBlobChallenges.gamma.toBN254Fr()}`,
330
+ );
331
+ }
332
+ if (!verifyKzgProof(this.cAcc.compress(), this.zAcc.toBuffer(), this.yAcc.toBuffer(), this.qAcc.compress())) {
333
+ throw new Error(`KZG proof did not verify.`);
334
+ }
335
+
336
+ return new BatchedBlob(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.qAcc);
337
+ }
338
+
339
+ /**
340
+ * Converts to a struct for the public inputs of our rollup circuits.
341
+ * @returns A BlobAccumulatorPublicInputs instance.
342
+ */
343
+ toBlobAccumulatorPublicInputs() {
344
+ return new BlobAccumulatorPublicInputs(
345
+ this.blobCommitmentsHashAcc,
346
+ this.zAcc,
347
+ this.yAcc,
348
+ this.cAcc,
349
+ this.gammaAcc,
350
+ this.gammaPow,
351
+ );
352
+ }
353
+
354
+ /**
355
+ * Converts to a struct for the public inputs of our root rollup circuit.
356
+ * Warning: MUST be final accumulator state.
357
+ * @returns A FinalBlobAccumulatorPublicInputs instance.
358
+ */
359
+ toFinalBlobAccumulatorPublicInputs() {
360
+ return new FinalBlobAccumulatorPublicInputs(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc);
361
+ }
362
+
363
+ isEmptyState() {
364
+ return (
365
+ this.blobCommitmentsHashAcc.isZero() &&
366
+ this.zAcc.isZero() &&
367
+ this.yAcc.isZero() &&
368
+ this.cAcc.isZero() &&
369
+ this.qAcc.isZero() &&
370
+ this.gammaAcc.isZero() &&
371
+ this.gammaPow.isZero()
372
+ );
373
+ }
374
+ }
375
+
376
+ // To mimic the hash accumulation in the rollup circuits, here we hash
377
+ // each u128 limb of the noir bignum struct representing the BLS field.
378
+ async function hashNoirBigNumLimbs(field: BLS12Field): Promise<Fr> {
379
+ const num = field.toNoirBigNum();
380
+ return await poseidon2Hash(num.limbs.map(Fr.fromHexString));
381
+ }
@@ -0,0 +1,241 @@
1
+ import { BLS12_FQ_LIMBS, BLS12_FR_LIMBS } from '@aztec/constants';
2
+ import { BLS12Fq, BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
3
+ import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
+
5
+ import { inspect } from 'util';
6
+
7
+ import { Blob } from './blob.js';
8
+ import { BatchedBlob, BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
9
+
10
+ /**
11
+ * See nr BlobAccumulatorPublicInputs and ts BatchedBlobAccumulator for documentation.
12
+ */
13
+ export class BlobAccumulatorPublicInputs {
14
+ constructor(
15
+ public blobCommitmentsHashAcc: Fr,
16
+ public zAcc: Fr,
17
+ public yAcc: BLS12Fr,
18
+ public cAcc: BLS12Point,
19
+ public gammaAcc: Fr,
20
+ public gammaPowAcc: BLS12Fr,
21
+ ) {}
22
+
23
+ static empty(): BlobAccumulatorPublicInputs {
24
+ return new BlobAccumulatorPublicInputs(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO, Fr.ZERO, BLS12Fr.ZERO);
25
+ }
26
+
27
+ equals(other: BlobAccumulatorPublicInputs) {
28
+ return (
29
+ this.blobCommitmentsHashAcc.equals(other.blobCommitmentsHashAcc) &&
30
+ this.zAcc.equals(other.zAcc) &&
31
+ this.yAcc.equals(other.yAcc) &&
32
+ this.cAcc.equals(other.cAcc) &&
33
+ this.gammaAcc.equals(other.gammaAcc) &&
34
+ this.gammaPowAcc.equals(other.gammaPowAcc)
35
+ );
36
+ }
37
+
38
+ static fromBuffer(buffer: Buffer | BufferReader): BlobAccumulatorPublicInputs {
39
+ const reader = BufferReader.asReader(buffer);
40
+ return new BlobAccumulatorPublicInputs(
41
+ Fr.fromBuffer(reader),
42
+ Fr.fromBuffer(reader),
43
+ BLS12Fr.fromBuffer(reader),
44
+ BLS12Point.fromBuffer(reader),
45
+ Fr.fromBuffer(reader),
46
+ BLS12Fr.fromBuffer(reader),
47
+ );
48
+ }
49
+
50
+ toBuffer() {
51
+ return serializeToBuffer(
52
+ this.blobCommitmentsHashAcc,
53
+ this.zAcc,
54
+ this.yAcc,
55
+ this.cAcc,
56
+ this.gammaAcc,
57
+ this.gammaPowAcc,
58
+ );
59
+ }
60
+
61
+ /**
62
+ * Given blobs, accumulate all public inputs state.
63
+ * We assume the input blobs have not been evaluated at z.
64
+ * NOTE: Does NOT accumulate non circuit values including Q. This exists to simulate/check exactly what the circuit is doing
65
+ * and is unsafe for other use. For that reason, a toBatchedBlobAccumulator does not exist. See evaluateBlobs() oracle for usage.
66
+ * @returns An updated blob accumulator.
67
+ */
68
+ async accumulateBlobs(blobs: Blob[], finalBlobChallenges: FinalBlobBatchingChallenges) {
69
+ let acc = new BatchedBlobAccumulator(
70
+ this.blobCommitmentsHashAcc,
71
+ this.zAcc,
72
+ this.yAcc,
73
+ this.cAcc,
74
+ BLS12Point.ZERO,
75
+ this.gammaAcc,
76
+ this.gammaPowAcc,
77
+ finalBlobChallenges,
78
+ );
79
+ acc = await acc.accumulateBlobs(blobs);
80
+ return new BlobAccumulatorPublicInputs(
81
+ acc.blobCommitmentsHashAcc,
82
+ acc.zAcc,
83
+ acc.yAcc,
84
+ acc.cAcc,
85
+ acc.gammaAcc,
86
+ acc.gammaPow,
87
+ );
88
+ }
89
+
90
+ toFields() {
91
+ return [
92
+ this.blobCommitmentsHashAcc,
93
+ this.zAcc,
94
+ ...this.yAcc.toNoirBigNum().limbs.map(Fr.fromString),
95
+ ...this.cAcc.x.toNoirBigNum().limbs.map(Fr.fromString),
96
+ ...this.cAcc.y.toNoirBigNum().limbs.map(Fr.fromString),
97
+ new Fr(this.cAcc.isInfinite),
98
+ this.gammaAcc,
99
+ ...this.gammaPowAcc.toNoirBigNum().limbs.map(Fr.fromString),
100
+ ];
101
+ }
102
+
103
+ static fromFields(fields: Fr[] | FieldReader): BlobAccumulatorPublicInputs {
104
+ const reader = FieldReader.asReader(fields);
105
+ return new BlobAccumulatorPublicInputs(
106
+ reader.readField(),
107
+ reader.readField(),
108
+ BLS12Fr.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FR_LIMBS).map(f => f.toString()) }),
109
+ new BLS12Point(
110
+ BLS12Fq.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FQ_LIMBS).map(f => f.toString()) }),
111
+ BLS12Fq.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FQ_LIMBS).map(f => f.toString()) }),
112
+ reader.readBoolean(),
113
+ ),
114
+ reader.readField(),
115
+ BLS12Fr.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FR_LIMBS).map(f => f.toString()) }),
116
+ );
117
+ }
118
+ }
119
+
120
+ /**
121
+ * See nr FinalBlobAccumulatorPublicInputs and ts BatchedBlobAccumulator for documentation.
122
+ */
123
+ export class FinalBlobAccumulatorPublicInputs {
124
+ constructor(
125
+ public blobCommitmentsHash: Fr,
126
+ public z: Fr,
127
+ public y: BLS12Fr,
128
+ public c: BLS12Point,
129
+ ) {}
130
+
131
+ static empty(): FinalBlobAccumulatorPublicInputs {
132
+ return new FinalBlobAccumulatorPublicInputs(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO);
133
+ }
134
+
135
+ static fromBuffer(buffer: Buffer | BufferReader): FinalBlobAccumulatorPublicInputs {
136
+ const reader = BufferReader.asReader(buffer);
137
+ return new FinalBlobAccumulatorPublicInputs(
138
+ Fr.fromBuffer(reader),
139
+ Fr.fromBuffer(reader),
140
+ BLS12Fr.fromBuffer(reader),
141
+ BLS12Point.fromBuffer(reader),
142
+ );
143
+ }
144
+
145
+ toBuffer() {
146
+ return serializeToBuffer(this.blobCommitmentsHash, this.z, this.y, this.c);
147
+ }
148
+
149
+ static fromBatchedBlob(blob: BatchedBlob) {
150
+ return new FinalBlobAccumulatorPublicInputs(blob.blobCommitmentsHash, blob.z, blob.y, blob.commitment);
151
+ }
152
+
153
+ toFields() {
154
+ return [
155
+ this.blobCommitmentsHash,
156
+ this.z,
157
+ ...this.y.toNoirBigNum().limbs.map(Fr.fromString),
158
+ // TODO(MW): add conversion when public inputs finalised
159
+ ...[new Fr(this.c.compress().subarray(0, 31)), new Fr(this.c.compress().subarray(31, 48))],
160
+ ];
161
+ }
162
+
163
+ // The below is used to send to L1 for proof verification
164
+ toString() {
165
+ // We prepend 32 bytes for the (unused) 'blobHash' slot. This is not read or required by getEpochProofPublicInputs() on L1, but
166
+ // is expected since we usually pass the full precompile inputs via verifyEpochRootProof() to getEpochProofPublicInputs() to ensure
167
+ // we use calldata rather than a slice in memory:
168
+ const buf = Buffer.concat([Buffer.alloc(32), this.z.toBuffer(), this.y.toBuffer(), this.c.compress()]);
169
+ return buf.toString('hex');
170
+ }
171
+
172
+ equals(other: FinalBlobAccumulatorPublicInputs) {
173
+ return (
174
+ this.blobCommitmentsHash.equals(other.blobCommitmentsHash) &&
175
+ this.z.equals(other.z) &&
176
+ this.y.equals(other.y) &&
177
+ this.c.equals(other.c)
178
+ );
179
+ }
180
+
181
+ // Creates a random instance. Used for testing only - will not prove/verify.
182
+ static random() {
183
+ return new FinalBlobAccumulatorPublicInputs(Fr.random(), Fr.random(), BLS12Fr.random(), BLS12Point.random());
184
+ }
185
+
186
+ [inspect.custom]() {
187
+ return `FinalBlobAccumulatorPublicInputs {
188
+ blobCommitmentsHash: ${inspect(this.blobCommitmentsHash)},
189
+ z: ${inspect(this.z)},
190
+ y: ${inspect(this.y)},
191
+ c: ${inspect(this.c)},
192
+ }`;
193
+ }
194
+ }
195
+
196
+ /**
197
+ * startBlobAccumulator: Accumulated opening proofs for all blobs before this block range.
198
+ * endBlobAccumulator: Accumulated opening proofs for all blobs after adding this block range.
199
+ * finalBlobChallenges: Final values z and gamma, shared across the epoch.
200
+ */
201
+ export class BlockBlobPublicInputs {
202
+ constructor(
203
+ public startBlobAccumulator: BlobAccumulatorPublicInputs,
204
+ public endBlobAccumulator: BlobAccumulatorPublicInputs,
205
+ public finalBlobChallenges: FinalBlobBatchingChallenges,
206
+ ) {}
207
+
208
+ static empty(): BlockBlobPublicInputs {
209
+ return new BlockBlobPublicInputs(
210
+ BlobAccumulatorPublicInputs.empty(),
211
+ BlobAccumulatorPublicInputs.empty(),
212
+ FinalBlobBatchingChallenges.empty(),
213
+ );
214
+ }
215
+
216
+ static fromBuffer(buffer: Buffer | BufferReader): BlockBlobPublicInputs {
217
+ const reader = BufferReader.asReader(buffer);
218
+ return new BlockBlobPublicInputs(
219
+ reader.readObject(BlobAccumulatorPublicInputs),
220
+ reader.readObject(BlobAccumulatorPublicInputs),
221
+ reader.readObject(FinalBlobBatchingChallenges),
222
+ );
223
+ }
224
+
225
+ toBuffer() {
226
+ return serializeToBuffer(this.startBlobAccumulator, this.endBlobAccumulator, this.finalBlobChallenges);
227
+ }
228
+
229
+ // Creates BlockBlobPublicInputs from the starting accumulator state and all blobs in the block.
230
+ // Assumes that startBlobAccumulator.finalChallenges have already been precomputed.
231
+ // Does not finalise challenge values (this is done in the final root rollup).
232
+ // TODO(MW): Integrate with BatchedBlob once old Blob classes removed
233
+ static async fromBlobs(startBlobAccumulator: BatchedBlobAccumulator, blobs: Blob[]): Promise<BlockBlobPublicInputs> {
234
+ const endBlobAccumulator = await startBlobAccumulator.accumulateBlobs(blobs);
235
+ return new BlockBlobPublicInputs(
236
+ startBlobAccumulator.toBlobAccumulatorPublicInputs(),
237
+ endBlobAccumulator.toBlobAccumulatorPublicInputs(),
238
+ startBlobAccumulator.finalBlobChallenges,
239
+ );
240
+ }
241
+ }
package/src/index.ts CHANGED
@@ -3,10 +3,11 @@ import cKzg from 'c-kzg';
3
3
  const { loadTrustedSetup } = cKzg;
4
4
 
5
5
  export * from './blob.js';
6
+ export * from './blob_batching.js';
6
7
  export * from './encoding.js';
7
8
  export * from './interface.js';
8
9
  export * from './errors.js';
9
- export * from './blob_public_inputs.js';
10
+ export * from './blob_batching_public_inputs.js';
10
11
  export * from './sponge_blob.js';
11
12
 
12
13
  try {
package/src/testing.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { BLOBS_PER_BLOCK } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
2
  import { toBufferBE } from '@aztec/foundation/bigint-buffer';
4
- import { Fr } from '@aztec/foundation/fields';
3
+ import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
5
4
 
6
5
  import { Blob } from './blob.js';
7
- import { BlobPublicInputs, BlockBlobPublicInputs } from './blob_public_inputs.js';
6
+ import { BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
7
+ import { BlockBlobPublicInputs } from './blob_batching_public_inputs.js';
8
8
  import { TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
9
9
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
10
10
 
@@ -28,16 +28,21 @@ export function makeSpongeBlob(seed = 1): SpongeBlob {
28
28
  }
29
29
 
30
30
  /**
31
- * Makes arbitrary blob public inputs.
31
+ * Makes arbitrary blob public accumulator.
32
32
  * Note: will not verify inside the circuit.
33
- * @param seed - The seed to use for generating the blob inputs.
34
- * @returns A blob public inputs instance.
33
+ * @param seed - The seed to use for generating the blob accumulator.
34
+ * @returns A blob accumulator instance.
35
35
  */
36
- export function makeBlobPublicInputs(seed = 1): BlobPublicInputs {
37
- return new BlobPublicInputs(
36
+ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
37
+ return new BatchedBlobAccumulator(
38
38
  new Fr(seed),
39
- BigInt(seed + 1),
40
- makeTuple(2, i => new Fr(i)),
39
+ new Fr(seed + 1),
40
+ new BLS12Fr(seed + 2),
41
+ BLS12Point.random(),
42
+ BLS12Point.random(),
43
+ new Fr(seed + 3),
44
+ new BLS12Fr(seed + 4),
45
+ new FinalBlobBatchingChallenges(new Fr(seed + 5), new BLS12Fr(seed + 6)),
41
46
  );
42
47
  }
43
48
 
@@ -48,7 +53,12 @@ export function makeBlobPublicInputs(seed = 1): BlobPublicInputs {
48
53
  * @returns A block blob public inputs instance.
49
54
  */
50
55
  export function makeBlockBlobPublicInputs(seed = 1): BlockBlobPublicInputs {
51
- return new BlockBlobPublicInputs(makeTuple(BLOBS_PER_BLOCK, () => makeBlobPublicInputs(seed)));
56
+ const startBlobAccumulator = makeBatchedBlobAccumulator(seed);
57
+ return new BlockBlobPublicInputs(
58
+ startBlobAccumulator.toBlobAccumulatorPublicInputs(),
59
+ makeBatchedBlobAccumulator(seed + 1).toBlobAccumulatorPublicInputs(),
60
+ startBlobAccumulator.finalBlobChallenges,
61
+ );
52
62
  }
53
63
 
54
64
  // TODO: copied form stdlib tx effect