@aztec/blob-lib 2.1.0-rc.9 → 3.0.0-devnet.2
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/blob.d.ts +52 -95
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob.js +73 -165
- package/dest/blob_batching.d.ts +15 -48
- package/dest/blob_batching.d.ts.map +1 -1
- package/dest/blob_batching.js +81 -120
- package/dest/blob_utils.d.ts +30 -0
- package/dest/blob_utils.d.ts.map +1 -0
- package/dest/blob_utils.js +60 -0
- package/dest/circuit_types/blob_accumulator.d.ts +21 -0
- package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/blob_accumulator.js +58 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts +22 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_accumulator.js +63 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts +15 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_batching_challenges.js +25 -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/deserialize.d.ts +14 -0
- package/dest/deserialize.d.ts.map +1 -0
- package/dest/deserialize.js +33 -0
- package/dest/encoding.d.ts +22 -62
- package/dest/encoding.d.ts.map +1 -1
- package/dest/encoding.js +114 -104
- package/dest/hash.d.ts +35 -0
- package/dest/hash.d.ts.map +1 -0
- package/dest/hash.js +69 -0
- package/dest/index.d.ts +5 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +5 -15
- package/dest/kzg_context.d.ts +4 -0
- package/dest/kzg_context.d.ts.map +1 -0
- package/dest/kzg_context.js +5 -0
- package/dest/sponge_blob.d.ts +13 -9
- package/dest/sponge_blob.d.ts.map +1 -1
- package/dest/sponge_blob.js +28 -17
- package/dest/testing.d.ts +7 -12
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +54 -41
- package/dest/types.d.ts +4 -0
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -0
- package/package.json +5 -4
- package/src/blob.ts +76 -191
- package/src/blob_batching.ts +109 -137
- package/src/blob_utils.ts +71 -0
- package/src/circuit_types/blob_accumulator.ts +84 -0
- package/src/circuit_types/final_blob_accumulator.ts +75 -0
- package/src/circuit_types/final_blob_batching_challenges.ts +29 -0
- package/src/circuit_types/index.ts +4 -0
- package/src/deserialize.ts +38 -0
- package/src/encoding.ts +136 -120
- package/src/hash.ts +77 -0
- package/src/index.ts +5 -18
- package/src/kzg_context.ts +5 -0
- package/src/sponge_blob.ts +24 -14
- package/src/testing.ts +55 -40
- package/src/types.ts +4 -2
- 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/src/blob_batching_public_inputs.ts +0 -252
package/src/blob_batching.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { AZTEC_MAX_EPOCH_DURATION, BLOBS_PER_BLOCK } from '@aztec/constants';
|
|
2
|
-
import { poseidon2Hash,
|
|
3
|
-
import {
|
|
4
|
-
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
2
|
+
import { poseidon2Hash, sha256ToField } from '@aztec/foundation/crypto';
|
|
3
|
+
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
5
|
+
import { Blob } from './blob.js';
|
|
6
|
+
import { computeBlobFieldsHashFromBlobs } from './blob_utils.js';
|
|
7
|
+
import { BlobAccumulator, FinalBlobAccumulator, FinalBlobBatchingChallenges } from './circuit_types/index.js';
|
|
8
|
+
import { computeEthVersionedBlobHash, hashNoirBigNumLimbs } from './hash.js';
|
|
9
|
+
import { kzg } from './kzg_context.js';
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
12
|
* A class to create, manage, and prove batched EVM blobs.
|
|
@@ -34,17 +32,19 @@ export class BatchedBlob {
|
|
|
34
32
|
*
|
|
35
33
|
* @returns A batched blob.
|
|
36
34
|
*/
|
|
37
|
-
static async batch(blobs: Blob[]): Promise<BatchedBlob> {
|
|
38
|
-
|
|
39
|
-
if (numBlobs > BLOBS_PER_BLOCK * AZTEC_MAX_EPOCH_DURATION) {
|
|
35
|
+
static async batch(blobs: Blob[][]): Promise<BatchedBlob> {
|
|
36
|
+
if (blobs.length > AZTEC_MAX_EPOCH_DURATION) {
|
|
40
37
|
throw new Error(
|
|
41
|
-
`Too many
|
|
38
|
+
`Too many blocks sent to batch(). The maximum is ${AZTEC_MAX_EPOCH_DURATION}. Got ${blobs.length}.`,
|
|
42
39
|
);
|
|
43
40
|
}
|
|
41
|
+
|
|
44
42
|
// Precalculate the values (z and gamma) and initialize the accumulator:
|
|
45
43
|
let acc = await this.newAccumulator(blobs);
|
|
46
44
|
// Now we can create a multi opening proof of all input blobs:
|
|
47
|
-
|
|
45
|
+
for (const blockBlobs of blobs) {
|
|
46
|
+
acc = await acc.accumulateBlobs(blockBlobs);
|
|
47
|
+
}
|
|
48
48
|
return await acc.finalize();
|
|
49
49
|
}
|
|
50
50
|
|
|
@@ -53,7 +53,7 @@ export class BatchedBlob {
|
|
|
53
53
|
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
54
54
|
* beforehand from ALL blobs.
|
|
55
55
|
*/
|
|
56
|
-
static async newAccumulator(blobs: Blob[]): Promise<BatchedBlobAccumulator> {
|
|
56
|
+
static async newAccumulator(blobs: Blob[][]): Promise<BatchedBlobAccumulator> {
|
|
57
57
|
const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobs);
|
|
58
58
|
return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
|
|
59
59
|
}
|
|
@@ -66,54 +66,52 @@ export class BatchedBlob {
|
|
|
66
66
|
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
67
67
|
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
68
68
|
* - 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.
|
|
69
|
+
*
|
|
70
|
+
* @param blobs - The blobs to precompute the challenges for. Each sub-array is the blobs for an L1 block.
|
|
69
71
|
* @returns Challenges z and gamma.
|
|
70
72
|
*/
|
|
71
|
-
static async precomputeBatchedBlobChallenges(blobs: Blob[]): Promise<FinalBlobBatchingChallenges> {
|
|
72
|
-
//
|
|
73
|
-
let z
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
static async precomputeBatchedBlobChallenges(blobs: Blob[][]): Promise<FinalBlobBatchingChallenges> {
|
|
74
|
+
// Compute the final challenge z to evaluate the blobs.
|
|
75
|
+
let z: Fr | undefined;
|
|
76
|
+
for (const blockBlobs of blobs) {
|
|
77
|
+
// Compute the hash of all the fields in the block.
|
|
78
|
+
const blobFieldsHash = await computeBlobFieldsHashFromBlobs(blockBlobs);
|
|
79
|
+
for (const blob of blockBlobs) {
|
|
80
|
+
// Compute the challenge z for each blob and accumulate it.
|
|
81
|
+
const challengeZ = await blob.computeChallengeZ(blobFieldsHash);
|
|
82
|
+
if (!z) {
|
|
83
|
+
z = challengeZ;
|
|
84
|
+
} else {
|
|
85
|
+
z = await poseidon2Hash([z, challengeZ]);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (!z) {
|
|
90
|
+
throw new Error('No blobs to precompute challenges for.');
|
|
77
91
|
}
|
|
92
|
+
|
|
78
93
|
// Now we have a shared challenge for all blobs, evaluate them...
|
|
79
|
-
const
|
|
80
|
-
const
|
|
94
|
+
const allBlobs = blobs.flat();
|
|
95
|
+
const proofObjects = allBlobs.map(b => b.evaluate(z));
|
|
96
|
+
const evaluations = await Promise.all(proofObjects.map(({ y }) => hashNoirBigNumLimbs(y)));
|
|
81
97
|
// ...and find the challenge for the linear combination of blobs.
|
|
82
|
-
let gamma =
|
|
98
|
+
let gamma = evaluations[0];
|
|
83
99
|
// We start at i = 1, because gamma is initialized as the first blob's evaluation.
|
|
84
|
-
for (let i = 1; i <
|
|
85
|
-
gamma = await poseidon2Hash([gamma,
|
|
100
|
+
for (let i = 1; i < allBlobs.length; i++) {
|
|
101
|
+
gamma = await poseidon2Hash([gamma, evaluations[i]]);
|
|
86
102
|
}
|
|
87
103
|
gamma = await poseidon2Hash([gamma, z]);
|
|
88
104
|
|
|
89
105
|
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
90
106
|
}
|
|
91
107
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
// We need to precompute the final challenge values to evaluate the blobs.
|
|
95
|
-
const z = blobs[0].challengeZ;
|
|
96
|
-
// Now we have a shared challenge for all blobs, evaluate them...
|
|
97
|
-
const proofObjects = blobs.map(b => computeKzgProof(b.data, z.toBuffer()));
|
|
98
|
-
const evaluations = proofObjects.map(([_, evaluation]) => BLS12Fr.fromBuffer(Buffer.from(evaluation)));
|
|
99
|
-
// ...and find the challenge for the linear combination of blobs.
|
|
100
|
-
let gamma = await hashNoirBigNumLimbs(evaluations[0]);
|
|
101
|
-
gamma = await poseidon2Hash([gamma, z]);
|
|
102
|
-
|
|
103
|
-
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
108
|
+
verify() {
|
|
109
|
+
return kzg.verifyKzgProof(this.commitment.compress(), this.z.toBuffer(), this.y.toBuffer(), this.q.compress());
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
// Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
|
|
107
113
|
getEthVersionedBlobHash(): Buffer {
|
|
108
|
-
|
|
109
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
110
|
-
return hash;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
static getEthVersionedBlobHash(commitment: Buffer): Buffer {
|
|
114
|
-
const hash = sha256(commitment);
|
|
115
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
116
|
-
return hash;
|
|
114
|
+
return computeEthVersionedBlobHash(this.commitment.compress());
|
|
117
115
|
}
|
|
118
116
|
|
|
119
117
|
/**
|
|
@@ -137,49 +135,14 @@ export class BatchedBlob {
|
|
|
137
135
|
]);
|
|
138
136
|
return `0x${buf.toString('hex')}`;
|
|
139
137
|
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
|
|
144
|
-
* - Checking equality in each block merge circuit and propagating up
|
|
145
|
-
* - Checking final z_acc == z in root circuit
|
|
146
|
-
* - Checking final gamma_acc == gamma in root circuit
|
|
147
|
-
*
|
|
148
|
-
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
149
|
-
* - where z_i = H(H(fields of blob_i), C_i),
|
|
150
|
-
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
151
|
-
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
152
|
-
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
|
|
153
|
-
* for all blob evaluations y_i (see above) and commitments C_i.
|
|
154
|
-
*
|
|
155
|
-
* Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
|
|
156
|
-
*/
|
|
157
|
-
export class FinalBlobBatchingChallenges {
|
|
158
|
-
constructor(
|
|
159
|
-
public readonly z: Fr,
|
|
160
|
-
public readonly gamma: BLS12Fr,
|
|
161
|
-
) {}
|
|
162
|
-
|
|
163
|
-
equals(other: FinalBlobBatchingChallenges) {
|
|
164
|
-
return this.z.equals(other.z) && this.gamma.equals(other.gamma);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
static empty(): FinalBlobBatchingChallenges {
|
|
168
|
-
return new FinalBlobBatchingChallenges(Fr.ZERO, BLS12Fr.ZERO);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobBatchingChallenges {
|
|
172
|
-
const reader = BufferReader.asReader(buffer);
|
|
173
|
-
return new FinalBlobBatchingChallenges(Fr.fromBuffer(reader), reader.readObject(BLS12Fr));
|
|
174
|
-
}
|
|
175
138
|
|
|
176
|
-
|
|
177
|
-
return
|
|
139
|
+
toFinalBlobAccumulator() {
|
|
140
|
+
return new FinalBlobAccumulator(this.blobCommitmentsHash, this.z, this.y, this.commitment);
|
|
178
141
|
}
|
|
179
142
|
}
|
|
180
143
|
|
|
181
144
|
/**
|
|
182
|
-
* See noir-projects/noir-protocol-circuits/crates/blob/src/
|
|
145
|
+
* See noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr
|
|
183
146
|
*/
|
|
184
147
|
export class BatchedBlobAccumulator {
|
|
185
148
|
constructor(
|
|
@@ -205,39 +168,6 @@ export class BatchedBlobAccumulator {
|
|
|
205
168
|
public readonly finalBlobChallenges: FinalBlobBatchingChallenges,
|
|
206
169
|
) {}
|
|
207
170
|
|
|
208
|
-
/**
|
|
209
|
-
* Init the first accumulation state of the epoch.
|
|
210
|
-
* We assume the input blob has not been evaluated at z.
|
|
211
|
-
*
|
|
212
|
-
* First state of the accumulator:
|
|
213
|
-
* - v_acc := sha256(C_0)
|
|
214
|
-
* - z_acc := z_0
|
|
215
|
-
* - y_acc := gamma^0 * y_0 = y_0
|
|
216
|
-
* - c_acc := gamma^0 * c_0 = c_0
|
|
217
|
-
* - gamma_acc := poseidon2(y_0.limbs)
|
|
218
|
-
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
219
|
-
*
|
|
220
|
-
* @returns An initial blob accumulator.
|
|
221
|
-
*/
|
|
222
|
-
static async initialize(
|
|
223
|
-
blob: Blob,
|
|
224
|
-
finalBlobChallenges: FinalBlobBatchingChallenges,
|
|
225
|
-
): Promise<BatchedBlobAccumulator> {
|
|
226
|
-
const [q, evaluation] = computeKzgProof(blob.data, finalBlobChallenges.z.toBuffer());
|
|
227
|
-
const firstY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
228
|
-
// Here, i = 0, so:
|
|
229
|
-
return new BatchedBlobAccumulator(
|
|
230
|
-
sha256ToField([blob.commitment]), // blobCommitmentsHashAcc = sha256(C_0)
|
|
231
|
-
blob.challengeZ, // zAcc = z_0
|
|
232
|
-
firstY, // yAcc = gamma^0 * y_0 = 1 * y_0
|
|
233
|
-
BLS12Point.decompress(blob.commitment), // cAcc = gamma^0 * C_0 = 1 * C_0
|
|
234
|
-
BLS12Point.decompress(Buffer.from(q)), // qAcc = gamma^0 * Q_0 = 1 * Q_0
|
|
235
|
-
await hashNoirBigNumLimbs(firstY), // gammaAcc = poseidon2(y_0.limbs)
|
|
236
|
-
finalBlobChallenges.gamma, // gammaPow = gamma^(i + 1) = gamma^1 = gamma
|
|
237
|
-
finalBlobChallenges,
|
|
238
|
-
);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
171
|
/**
|
|
242
172
|
* Create the empty accumulation state of the epoch.
|
|
243
173
|
* @returns An empty blob accumulator with challenges.
|
|
@@ -260,20 +190,40 @@ export class BatchedBlobAccumulator {
|
|
|
260
190
|
* We assume the input blob has not been evaluated at z.
|
|
261
191
|
* @returns An updated blob accumulator.
|
|
262
192
|
*/
|
|
263
|
-
async accumulate(blob: Blob) {
|
|
193
|
+
private async accumulate(blob: Blob, blobFieldsHash: Fr) {
|
|
194
|
+
const { proof, y: thisY } = blob.evaluate(this.finalBlobChallenges.z);
|
|
195
|
+
const thisC = BLS12Point.decompress(blob.commitment);
|
|
196
|
+
const thisQ = BLS12Point.decompress(proof);
|
|
197
|
+
const blobChallengeZ = await blob.computeChallengeZ(blobFieldsHash);
|
|
198
|
+
|
|
264
199
|
if (this.isEmptyState()) {
|
|
265
|
-
|
|
200
|
+
/**
|
|
201
|
+
* Init the first accumulation state of the epoch.
|
|
202
|
+
* - v_acc := sha256(C_0)
|
|
203
|
+
* - z_acc := z_0
|
|
204
|
+
* - y_acc := gamma^0 * y_0 = y_0
|
|
205
|
+
* - c_acc := gamma^0 * c_0 = c_0
|
|
206
|
+
* - gamma_acc := poseidon2(y_0.limbs)
|
|
207
|
+
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
208
|
+
*/
|
|
209
|
+
return new BatchedBlobAccumulator(
|
|
210
|
+
sha256ToField([blob.commitment]), // blobCommitmentsHashAcc = sha256(C_0)
|
|
211
|
+
blobChallengeZ, // zAcc = z_0
|
|
212
|
+
thisY, // yAcc = gamma^0 * y_0 = 1 * y_0
|
|
213
|
+
thisC, // cAcc = gamma^0 * C_0 = 1 * C_0
|
|
214
|
+
thisQ, // qAcc = gamma^0 * Q_0 = 1 * Q_0
|
|
215
|
+
await hashNoirBigNumLimbs(thisY), // gammaAcc = poseidon2(y_0.limbs)
|
|
216
|
+
this.finalBlobChallenges.gamma, // gammaPow = gamma^(i + 1) = gamma^1 = gamma
|
|
217
|
+
this.finalBlobChallenges,
|
|
218
|
+
);
|
|
266
219
|
} else {
|
|
267
|
-
const [q, evaluation] = computeKzgProof(blob.data, this.finalBlobChallenges.z.toBuffer());
|
|
268
|
-
const thisY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
269
|
-
|
|
270
220
|
// Moving from i - 1 to i, so:
|
|
271
221
|
return new BatchedBlobAccumulator(
|
|
272
222
|
sha256ToField([this.blobCommitmentsHashAcc, blob.commitment]), // blobCommitmentsHashAcc := sha256(blobCommitmentsHashAcc, C_i)
|
|
273
|
-
await poseidon2Hash([this.zAcc,
|
|
223
|
+
await poseidon2Hash([this.zAcc, blobChallengeZ]), // zAcc := poseidon2(zAcc, z_i)
|
|
274
224
|
this.yAcc.add(thisY.mul(this.gammaPow)), // yAcc := yAcc + (gamma^i * y_i)
|
|
275
|
-
this.cAcc.add(
|
|
276
|
-
this.qAcc.add(
|
|
225
|
+
this.cAcc.add(thisC.mul(this.gammaPow)), // cAcc := cAcc + (gamma^i * C_i)
|
|
226
|
+
this.qAcc.add(thisQ.mul(this.gammaPow)), // qAcc := qAcc + (gamma^i * C_i)
|
|
277
227
|
await poseidon2Hash([this.gammaAcc, await hashNoirBigNumLimbs(thisY)]), // gammaAcc := poseidon2(gammaAcc, poseidon2(y_i.limbs))
|
|
278
228
|
this.gammaPow.mul(this.finalBlobChallenges.gamma), // gammaPow = gamma^(i + 1) = gamma^i * final_gamma
|
|
279
229
|
this.finalBlobChallenges,
|
|
@@ -284,13 +234,23 @@ export class BatchedBlobAccumulator {
|
|
|
284
234
|
/**
|
|
285
235
|
* Given blobs, accumulate all state.
|
|
286
236
|
* We assume the input blobs have not been evaluated at z.
|
|
237
|
+
* @param blobs - The blobs to accumulate. They should be in the same L1 block.
|
|
287
238
|
* @returns An updated blob accumulator.
|
|
288
239
|
*/
|
|
289
240
|
async accumulateBlobs(blobs: Blob[]) {
|
|
241
|
+
if (blobs.length > BLOBS_PER_BLOCK) {
|
|
242
|
+
throw new Error(
|
|
243
|
+
`Too many blobs to accumulate. The maximum is ${BLOBS_PER_BLOCK} per block. Got ${blobs.length}.`,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Compute the hash of all the fields in the block.
|
|
248
|
+
const blobFieldsHash = await computeBlobFieldsHashFromBlobs(blobs);
|
|
249
|
+
|
|
290
250
|
// Initialize the acc to iterate over:
|
|
291
251
|
let acc: BatchedBlobAccumulator = this.clone();
|
|
292
|
-
for (
|
|
293
|
-
acc = await acc.accumulate(
|
|
252
|
+
for (const blob of blobs) {
|
|
253
|
+
acc = await acc.accumulate(blob, blobFieldsHash);
|
|
294
254
|
}
|
|
295
255
|
return acc;
|
|
296
256
|
}
|
|
@@ -306,9 +266,10 @@ export class BatchedBlobAccumulator {
|
|
|
306
266
|
* - c := c_acc (final commitment to be checked on L1)
|
|
307
267
|
* - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
|
|
308
268
|
*
|
|
269
|
+
* @param verifyProof - Whether to verify the KZG proof.
|
|
309
270
|
* @returns A batched blob.
|
|
310
271
|
*/
|
|
311
|
-
async finalize(): Promise<BatchedBlob> {
|
|
272
|
+
async finalize(verifyProof = false): Promise<BatchedBlob> {
|
|
312
273
|
// All values in acc are final, apart from gamma := poseidon2(gammaAcc, z):
|
|
313
274
|
const calculatedGamma = await poseidon2Hash([this.gammaAcc, this.zAcc]);
|
|
314
275
|
// Check final values:
|
|
@@ -322,11 +283,14 @@ export class BatchedBlobAccumulator {
|
|
|
322
283
|
`Blob batching mismatch: accumulated gamma ${calculatedGamma} does not equal injected gamma ${this.finalBlobChallenges.gamma.toBN254Fr()}`,
|
|
323
284
|
);
|
|
324
285
|
}
|
|
325
|
-
|
|
286
|
+
|
|
287
|
+
const batchedBlob = new BatchedBlob(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.qAcc);
|
|
288
|
+
|
|
289
|
+
if (verifyProof && !batchedBlob.verify()) {
|
|
326
290
|
throw new Error(`KZG proof did not verify.`);
|
|
327
291
|
}
|
|
328
292
|
|
|
329
|
-
return
|
|
293
|
+
return batchedBlob;
|
|
330
294
|
}
|
|
331
295
|
|
|
332
296
|
isEmptyState() {
|
|
@@ -353,11 +317,19 @@ export class BatchedBlobAccumulator {
|
|
|
353
317
|
FinalBlobBatchingChallenges.fromBuffer(this.finalBlobChallenges.toBuffer()),
|
|
354
318
|
);
|
|
355
319
|
}
|
|
356
|
-
}
|
|
357
320
|
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
321
|
+
toBlobAccumulator() {
|
|
322
|
+
return new BlobAccumulator(
|
|
323
|
+
this.blobCommitmentsHashAcc,
|
|
324
|
+
this.zAcc,
|
|
325
|
+
this.yAcc,
|
|
326
|
+
this.cAcc,
|
|
327
|
+
this.gammaAcc,
|
|
328
|
+
this.gammaPow,
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
toFinalBlobAccumulator() {
|
|
333
|
+
return new FinalBlobAccumulator(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc);
|
|
334
|
+
}
|
|
363
335
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { FIELDS_PER_BLOB } from '@aztec/constants';
|
|
2
|
+
import { BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
3
|
+
|
|
4
|
+
import { Blob } from './blob.js';
|
|
5
|
+
import { deserializeEncodedBlobToFields } from './deserialize.js';
|
|
6
|
+
import { computeBlobFieldsHash, computeBlobsHash } from './hash.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @param blobs - The blobs to emit.
|
|
10
|
+
* @returns The blobs' compressed commitments in hex prefixed by the number of blobs. 1 byte for the prefix, 48 bytes
|
|
11
|
+
* per blob commitment.
|
|
12
|
+
* @dev Used for proposing blocks to validate injected blob commitments match real broadcast blobs.
|
|
13
|
+
*/
|
|
14
|
+
export function getPrefixedEthBlobCommitments(blobs: Blob[]): `0x${string}` {
|
|
15
|
+
// Prefix the number of blobs.
|
|
16
|
+
const lenBuf = Buffer.alloc(1);
|
|
17
|
+
lenBuf.writeUint8(blobs.length);
|
|
18
|
+
|
|
19
|
+
const blobBuf = Buffer.concat(blobs.map(blob => blob.commitment));
|
|
20
|
+
|
|
21
|
+
const buf = Buffer.concat([lenBuf, blobBuf]);
|
|
22
|
+
return `0x${buf.toString('hex')}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param fields - Fields to broadcast in the blob(s)
|
|
27
|
+
* @returns As many blobs as required to broadcast the given fields to an L1 block.
|
|
28
|
+
*
|
|
29
|
+
* @throws If the number of fields does not match what's indicated by the checkpoint prefix.
|
|
30
|
+
*/
|
|
31
|
+
export function getBlobsPerL1Block(fields: Fr[]): Blob[] {
|
|
32
|
+
if (!fields.length) {
|
|
33
|
+
throw new Error('Cannot create blobs from empty fields.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const numBlobs = Math.ceil(fields.length / FIELDS_PER_BLOB);
|
|
37
|
+
return Array.from({ length: numBlobs }, (_, i) =>
|
|
38
|
+
Blob.fromFields(fields.slice(i * FIELDS_PER_BLOB, (i + 1) * FIELDS_PER_BLOB)),
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get the fields from all blobs in the checkpoint. Ignoring the fields beyond the length specified by the
|
|
44
|
+
* checkpoint prefix (the first field).
|
|
45
|
+
*
|
|
46
|
+
* @param blobs - The blobs to read fields from. Should be all the blobs in the L1 block proposing the checkpoint.
|
|
47
|
+
* @param checkEncoding - Whether to check if the entire encoded blob fields are valid. If false, it will still check
|
|
48
|
+
* the checkpoint prefix and throw if there's not enough fields.
|
|
49
|
+
* @returns The fields added throughout the checkpoint.
|
|
50
|
+
*/
|
|
51
|
+
export function getBlobFieldsInCheckpoint(blobs: Blob[], checkEncoding = false): Fr[] {
|
|
52
|
+
return deserializeEncodedBlobToFields(Buffer.concat(blobs.map(b => b.data)), checkEncoding);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function computeBlobFieldsHashFromBlobs(blobs: Blob[]): Promise<Fr> {
|
|
56
|
+
const fields = blobs.map(b => b.toFields()).flat();
|
|
57
|
+
const numBlobFields = fields[0].toNumber();
|
|
58
|
+
if (numBlobFields > fields.length) {
|
|
59
|
+
throw new Error(`The prefix indicates ${numBlobFields} fields. Got ${fields.length}.`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return await computeBlobFieldsHash(fields.slice(0, numBlobFields));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function computeBlobsHashFromBlobs(blobs: Blob[]): Fr {
|
|
66
|
+
return computeBlobsHash(blobs.map(b => b.getEthVersionedBlobHash()));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getBlobCommitmentsFromBlobs(blobs: Blob[]): BLS12Point[] {
|
|
70
|
+
return blobs.map(b => BLS12Point.decompress(b.commitment));
|
|
71
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
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
|
+
/**
|
|
6
|
+
* See `noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr` for documentation.
|
|
7
|
+
*/
|
|
8
|
+
export class BlobAccumulator {
|
|
9
|
+
constructor(
|
|
10
|
+
public blobCommitmentsHashAcc: Fr,
|
|
11
|
+
public zAcc: Fr,
|
|
12
|
+
public yAcc: BLS12Fr,
|
|
13
|
+
public cAcc: BLS12Point,
|
|
14
|
+
public gammaAcc: Fr,
|
|
15
|
+
public gammaPowAcc: BLS12Fr,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
static empty(): BlobAccumulator {
|
|
19
|
+
return new BlobAccumulator(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO, Fr.ZERO, BLS12Fr.ZERO);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
equals(other: BlobAccumulator) {
|
|
23
|
+
return (
|
|
24
|
+
this.blobCommitmentsHashAcc.equals(other.blobCommitmentsHashAcc) &&
|
|
25
|
+
this.zAcc.equals(other.zAcc) &&
|
|
26
|
+
this.yAcc.equals(other.yAcc) &&
|
|
27
|
+
this.cAcc.equals(other.cAcc) &&
|
|
28
|
+
this.gammaAcc.equals(other.gammaAcc) &&
|
|
29
|
+
this.gammaPowAcc.equals(other.gammaPowAcc)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static fromBuffer(buffer: Buffer | BufferReader): BlobAccumulator {
|
|
34
|
+
const reader = BufferReader.asReader(buffer);
|
|
35
|
+
return new BlobAccumulator(
|
|
36
|
+
Fr.fromBuffer(reader),
|
|
37
|
+
Fr.fromBuffer(reader),
|
|
38
|
+
BLS12Fr.fromBuffer(reader),
|
|
39
|
+
BLS12Point.fromBuffer(reader),
|
|
40
|
+
Fr.fromBuffer(reader),
|
|
41
|
+
BLS12Fr.fromBuffer(reader),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
toBuffer() {
|
|
46
|
+
return serializeToBuffer(
|
|
47
|
+
this.blobCommitmentsHashAcc,
|
|
48
|
+
this.zAcc,
|
|
49
|
+
this.yAcc,
|
|
50
|
+
this.cAcc,
|
|
51
|
+
this.gammaAcc,
|
|
52
|
+
this.gammaPowAcc,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toFields() {
|
|
57
|
+
return [
|
|
58
|
+
this.blobCommitmentsHashAcc,
|
|
59
|
+
this.zAcc,
|
|
60
|
+
...this.yAcc.toNoirBigNum().limbs.map(Fr.fromString),
|
|
61
|
+
...this.cAcc.x.toNoirBigNum().limbs.map(Fr.fromString),
|
|
62
|
+
...this.cAcc.y.toNoirBigNum().limbs.map(Fr.fromString),
|
|
63
|
+
new Fr(this.cAcc.isInfinite),
|
|
64
|
+
this.gammaAcc,
|
|
65
|
+
...this.gammaPowAcc.toNoirBigNum().limbs.map(Fr.fromString),
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static fromFields(fields: Fr[] | FieldReader): BlobAccumulator {
|
|
70
|
+
const reader = FieldReader.asReader(fields);
|
|
71
|
+
return new BlobAccumulator(
|
|
72
|
+
reader.readField(),
|
|
73
|
+
reader.readField(),
|
|
74
|
+
BLS12Fr.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FR_LIMBS).map(f => f.toString()) }),
|
|
75
|
+
new BLS12Point(
|
|
76
|
+
BLS12Fq.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FQ_LIMBS).map(f => f.toString()) }),
|
|
77
|
+
BLS12Fq.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FQ_LIMBS).map(f => f.toString()) }),
|
|
78
|
+
reader.readBoolean(),
|
|
79
|
+
),
|
|
80
|
+
reader.readField(),
|
|
81
|
+
BLS12Fr.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FR_LIMBS).map(f => f.toString()) }),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
3
|
+
|
|
4
|
+
import { inspect } from 'util';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* See `noir-projects/noir-protocol-circuits/crates/blob/src/abis/final_blob_accumulator.nr` for documentation.
|
|
8
|
+
*/
|
|
9
|
+
export class FinalBlobAccumulator {
|
|
10
|
+
constructor(
|
|
11
|
+
public blobCommitmentsHash: Fr,
|
|
12
|
+
public z: Fr,
|
|
13
|
+
public y: BLS12Fr,
|
|
14
|
+
public c: BLS12Point,
|
|
15
|
+
) {}
|
|
16
|
+
|
|
17
|
+
static empty(): FinalBlobAccumulator {
|
|
18
|
+
return new FinalBlobAccumulator(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobAccumulator {
|
|
22
|
+
const reader = BufferReader.asReader(buffer);
|
|
23
|
+
return new FinalBlobAccumulator(
|
|
24
|
+
Fr.fromBuffer(reader),
|
|
25
|
+
Fr.fromBuffer(reader),
|
|
26
|
+
BLS12Fr.fromBuffer(reader),
|
|
27
|
+
BLS12Point.fromBuffer(reader),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
toBuffer() {
|
|
32
|
+
return serializeToBuffer(this.blobCommitmentsHash, this.z, this.y, this.c);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
toFields() {
|
|
36
|
+
return [
|
|
37
|
+
this.blobCommitmentsHash,
|
|
38
|
+
this.z,
|
|
39
|
+
...this.y.toNoirBigNum().limbs.map(Fr.fromString),
|
|
40
|
+
...this.c.toBN254Fields(),
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The below is used to send to L1 for proof verification
|
|
45
|
+
toString() {
|
|
46
|
+
// We prepend 32 bytes for the (unused) 'blobHash' slot. This is not read or required by getEpochProofPublicInputs() on L1, but
|
|
47
|
+
// is expected since we usually pass the full precompile inputs via verifyEpochRootProof() to getEpochProofPublicInputs() to ensure
|
|
48
|
+
// we use calldata rather than a slice in memory:
|
|
49
|
+
const buf = Buffer.concat([Buffer.alloc(32), this.z.toBuffer(), this.y.toBuffer(), this.c.compress()]);
|
|
50
|
+
return buf.toString('hex');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
equals(other: FinalBlobAccumulator) {
|
|
54
|
+
return (
|
|
55
|
+
this.blobCommitmentsHash.equals(other.blobCommitmentsHash) &&
|
|
56
|
+
this.z.equals(other.z) &&
|
|
57
|
+
this.y.equals(other.y) &&
|
|
58
|
+
this.c.equals(other.c)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Creates a random instance. Used for testing only - will not prove/verify.
|
|
63
|
+
static random() {
|
|
64
|
+
return new FinalBlobAccumulator(Fr.random(), Fr.random(), BLS12Fr.random(), BLS12Point.random());
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
[inspect.custom]() {
|
|
68
|
+
return `FinalBlobAccumulator {
|
|
69
|
+
blobCommitmentsHash: ${inspect(this.blobCommitmentsHash)},
|
|
70
|
+
z: ${inspect(this.z)},
|
|
71
|
+
y: ${inspect(this.y)},
|
|
72
|
+
c: ${inspect(this.c)},
|
|
73
|
+
}`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { BLS12Fr, Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* See `noir-projects/noir-protocol-circuits/crates/blob/src/abis/final_blob_batching_challenges.nr` for documentation.
|
|
6
|
+
*/
|
|
7
|
+
export class FinalBlobBatchingChallenges {
|
|
8
|
+
constructor(
|
|
9
|
+
public readonly z: Fr,
|
|
10
|
+
public readonly gamma: BLS12Fr,
|
|
11
|
+
) {}
|
|
12
|
+
|
|
13
|
+
equals(other: FinalBlobBatchingChallenges) {
|
|
14
|
+
return this.z.equals(other.z) && this.gamma.equals(other.gamma);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static empty(): FinalBlobBatchingChallenges {
|
|
18
|
+
return new FinalBlobBatchingChallenges(Fr.ZERO, BLS12Fr.ZERO);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobBatchingChallenges {
|
|
22
|
+
const reader = BufferReader.asReader(buffer);
|
|
23
|
+
return new FinalBlobBatchingChallenges(Fr.fromBuffer(reader), reader.readObject(BLS12Fr));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
toBuffer() {
|
|
27
|
+
return serializeToBuffer(this.z, this.gamma);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
+
|
|
4
|
+
import { checkBlobFieldsEncoding } from './encoding.js';
|
|
5
|
+
import { BlobDeserializationError } from './errors.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Deserializes a buffer into an array of field elements.
|
|
9
|
+
*
|
|
10
|
+
* This function returns the fields that were actually added in a checkpoint. The number of fields is specified by the
|
|
11
|
+
* first field.
|
|
12
|
+
*
|
|
13
|
+
* @param buf - The buffer to deserialize.
|
|
14
|
+
* @param checkEncoding - Whether to check if the encoding is correct. If false, it will still check the checkpoint
|
|
15
|
+
* prefix and throw if there's not enough fields.
|
|
16
|
+
* @returns An array of field elements.
|
|
17
|
+
*/
|
|
18
|
+
export function deserializeEncodedBlobToFields(buf: Uint8Array, checkEncoding = false): Fr[] {
|
|
19
|
+
const reader = BufferReader.asReader(buf);
|
|
20
|
+
const firstField = reader.readObject(Fr);
|
|
21
|
+
|
|
22
|
+
// Use toBigInt instead of toNumber so that we can catch it and throw a more descriptive error below if the first
|
|
23
|
+
// field is larger than a javascript integer.
|
|
24
|
+
const numFields = firstField.toBigInt();
|
|
25
|
+
const totalFieldsInBuffer = BigInt(buf.length / Fr.SIZE_IN_BYTES);
|
|
26
|
+
if (numFields > totalFieldsInBuffer) {
|
|
27
|
+
throw new BlobDeserializationError(`Failed to deserialize blob fields, this blob was likely not created by us`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const numFieldsWithoutPrefix = Number(numFields) - 1;
|
|
31
|
+
const blobFields = [firstField].concat(reader.readArray(numFieldsWithoutPrefix, Fr));
|
|
32
|
+
|
|
33
|
+
if (checkEncoding && !checkBlobFieldsEncoding(blobFields)) {
|
|
34
|
+
throw new BlobDeserializationError(`Incorrect encoding of blob fields, this blob was likely not created by us`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return blobFields;
|
|
38
|
+
}
|