@aztec/blob-lib 1.0.0-nightly.20250608 → 1.0.0-nightly.20250611
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 +1 -0
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob.js +20 -1
- package/dest/blob_batching.d.ts +205 -0
- package/dest/blob_batching.d.ts.map +1 -0
- package/dest/blob_batching.js +316 -0
- package/dest/blob_batching_public_inputs.d.ts +66 -0
- package/dest/blob_batching_public_inputs.d.ts.map +1 -0
- package/dest/blob_batching_public_inputs.js +170 -0
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/testing.d.ts +6 -5
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +10 -9
- package/package.json +5 -4
- package/src/blob.ts +15 -1
- package/src/blob_batching.ts +381 -0
- package/src/blob_batching_public_inputs.ts +241 -0
- package/src/index.ts +2 -1
- package/src/testing.ts +21 -11
- package/src/trusted_setup_bit_reversed.json +4100 -0
- package/dest/blob_public_inputs.d.ts +0 -48
- package/dest/blob_public_inputs.d.ts.map +0 -1
- package/dest/blob_public_inputs.js +0 -146
- package/src/blob_public_inputs.ts +0 -157
package/dest/blob.d.ts
CHANGED
|
@@ -151,6 +151,7 @@ export declare class Blob {
|
|
|
151
151
|
*/
|
|
152
152
|
getEthBlobEvaluationInputs(): `0x${string}`;
|
|
153
153
|
static getEthBlobEvaluationInputs(blobs: Blob[]): `0x${string}`;
|
|
154
|
+
static getPrefixedEthBlobCommitments(blobs: Blob[]): `0x${string}`;
|
|
154
155
|
static getViemKzgInstance(): {
|
|
155
156
|
blobToKzgCommitment: typeof cKzg.blobToKzgCommitment;
|
|
156
157
|
computeBlobKzgProof: typeof cKzg.computeBlobKzgProof;
|
package/dest/blob.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blob.d.ts","sourceRoot":"","sources":["../src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AC,GAAG,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI;IAYnD;;OAEG;IACH,OAAO;IAIP;;;;;;;;;;OAUG;IACH,0BAA0B,IAAI,KAAK,MAAM,EAAE;IAW3C,MAAM,CAAC,0BAA0B,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE;IAmB/D,MAAM,CAAC,6BAA6B,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE;IAYlE,MAAM,CAAC,kBAAkB;;;;WAWZ,QAAQ,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;CAUrD"}
|
package/dest/blob.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { poseidon2Hash, sha256 } from '@aztec/foundation/crypto';
|
|
2
2
|
import { Fr } from '@aztec/foundation/fields';
|
|
3
3
|
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
|
-
// Importing directly from 'c-kzg' does not work
|
|
4
|
+
// Importing directly from 'c-kzg' does not work:
|
|
5
5
|
import cKzg from 'c-kzg';
|
|
6
6
|
import { deserializeEncodedBlobToFields, extractBlobFieldsFromBuffer } from './encoding.js';
|
|
7
7
|
import { BlobDeserializationError } from './errors.js';
|
|
@@ -238,6 +238,23 @@ export const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
|
238
238
|
]);
|
|
239
239
|
return `0x${buf.toString('hex')}`;
|
|
240
240
|
}
|
|
241
|
+
static getPrefixedEthBlobCommitments(blobs) {
|
|
242
|
+
let buf = Buffer.alloc(0);
|
|
243
|
+
blobs.forEach((blob)=>{
|
|
244
|
+
buf = Buffer.concat([
|
|
245
|
+
buf,
|
|
246
|
+
blob.commitment
|
|
247
|
+
]);
|
|
248
|
+
});
|
|
249
|
+
// For multiple blobs, we prefix the number of blobs:
|
|
250
|
+
const lenBuf = Buffer.alloc(1);
|
|
251
|
+
lenBuf.writeUint8(blobs.length);
|
|
252
|
+
buf = Buffer.concat([
|
|
253
|
+
lenBuf,
|
|
254
|
+
buf
|
|
255
|
+
]);
|
|
256
|
+
return `0x${buf.toString('hex')}`;
|
|
257
|
+
}
|
|
241
258
|
static getViemKzgInstance() {
|
|
242
259
|
return {
|
|
243
260
|
blobToKzgCommitment: cKzg.blobToKzgCommitment,
|
|
@@ -246,6 +263,8 @@ export const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
|
246
263
|
}
|
|
247
264
|
// Returns as many blobs as we require to broadcast the given fields
|
|
248
265
|
// Assumes we share the fields hash between all blobs
|
|
266
|
+
// TODO(MW): Rename to more accurate getBlobsPerBlock() - the items here share a fields hash,
|
|
267
|
+
// which can only be done for one block because the hash is calculated in block root.
|
|
249
268
|
static async getBlobs(fields) {
|
|
250
269
|
const numBlobs = Math.max(Math.ceil(fields.length / FIELD_ELEMENTS_PER_BLOB), 1);
|
|
251
270
|
const multiBlobFieldsHash = await poseidon2Hash(fields);
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
+
import { Blob } from './blob.js';
|
|
4
|
+
import { BlobAccumulatorPublicInputs, FinalBlobAccumulatorPublicInputs } from './blob_batching_public_inputs.js';
|
|
5
|
+
/**
|
|
6
|
+
* A class to create, manage, and prove batched EVM blobs.
|
|
7
|
+
*/
|
|
8
|
+
export declare class BatchedBlob {
|
|
9
|
+
/** Hash of Cs (to link to L1 blob hashes). */
|
|
10
|
+
readonly blobCommitmentsHash: Fr;
|
|
11
|
+
/** Challenge point z such that p_i(z) = y_i. */
|
|
12
|
+
readonly z: Fr;
|
|
13
|
+
/** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
14
|
+
readonly y: BLS12Fr;
|
|
15
|
+
/** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
|
|
16
|
+
readonly commitment: BLS12Point;
|
|
17
|
+
/** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
18
|
+
readonly q: BLS12Point;
|
|
19
|
+
constructor(
|
|
20
|
+
/** Hash of Cs (to link to L1 blob hashes). */
|
|
21
|
+
blobCommitmentsHash: Fr,
|
|
22
|
+
/** Challenge point z such that p_i(z) = y_i. */
|
|
23
|
+
z: Fr,
|
|
24
|
+
/** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
25
|
+
y: BLS12Fr,
|
|
26
|
+
/** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
|
|
27
|
+
commitment: BLS12Point,
|
|
28
|
+
/** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
29
|
+
q: BLS12Point);
|
|
30
|
+
/**
|
|
31
|
+
* Get the final batched opening proof from multiple blobs.
|
|
32
|
+
*
|
|
33
|
+
* TODO(MW): Using the old Blob struct means there are ignored values (e.g. blob.evaluationY, because we now evaluate at shared z).
|
|
34
|
+
* When switching to batching, create new class w/o useless values.
|
|
35
|
+
*
|
|
36
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
37
|
+
* beforehand from ALL blobs.
|
|
38
|
+
*
|
|
39
|
+
* @returns A batched blob.
|
|
40
|
+
*/
|
|
41
|
+
static batch(blobs: Blob[]): Promise<BatchedBlob>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
44
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
45
|
+
* beforehand from ALL blobs.
|
|
46
|
+
*/
|
|
47
|
+
static newAccumulator(blobs: Blob[]): Promise<BatchedBlobAccumulator>;
|
|
48
|
+
/**
|
|
49
|
+
* Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
|
|
50
|
+
* Used in BatchedBlobAccumulator as 'finalZ' and finalGamma':
|
|
51
|
+
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
52
|
+
* - where z_i = H(H(fields of blob_i), C_i) = Blob.challengeZ,
|
|
53
|
+
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
54
|
+
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
55
|
+
* - 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.
|
|
56
|
+
* @returns Challenges z and gamma.
|
|
57
|
+
*/
|
|
58
|
+
static precomputeBatchedBlobChallenges(blobs: Blob[]): Promise<FinalBlobBatchingChallenges>;
|
|
59
|
+
static precomputeEmptyBatchedBlobChallenges(): Promise<FinalBlobBatchingChallenges>;
|
|
60
|
+
getEthVersionedBlobHash(): Buffer;
|
|
61
|
+
static getEthVersionedBlobHash(commitment: Buffer): Buffer;
|
|
62
|
+
/**
|
|
63
|
+
* Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
|
|
64
|
+
*
|
|
65
|
+
* input[:32] - versioned_hash
|
|
66
|
+
* input[32:64] - z
|
|
67
|
+
* input[64:96] - y
|
|
68
|
+
* input[96:144] - commitment C
|
|
69
|
+
* input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
|
|
70
|
+
*
|
|
71
|
+
* See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
|
|
72
|
+
*/
|
|
73
|
+
getEthBlobEvaluationInputs(): `0x${string}`;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
|
|
77
|
+
* - Checking equality in each block merge circuit and propagating up
|
|
78
|
+
* - Checking final z_acc == z in root circuit
|
|
79
|
+
* - Checking final gamma_acc == gamma in root circuit
|
|
80
|
+
*
|
|
81
|
+
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
82
|
+
* - where z_i = H(H(fields of blob_i), C_i),
|
|
83
|
+
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
84
|
+
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
85
|
+
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
|
|
86
|
+
* for all blob evaluations y_i (see above) and commitments C_i.
|
|
87
|
+
*
|
|
88
|
+
* Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
|
|
89
|
+
*/
|
|
90
|
+
export declare class FinalBlobBatchingChallenges {
|
|
91
|
+
readonly z: Fr;
|
|
92
|
+
readonly gamma: BLS12Fr;
|
|
93
|
+
constructor(z: Fr, gamma: BLS12Fr);
|
|
94
|
+
equals(other: FinalBlobBatchingChallenges): boolean;
|
|
95
|
+
static empty(): FinalBlobBatchingChallenges;
|
|
96
|
+
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobBatchingChallenges;
|
|
97
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* See noir-projects/noir-protocol-circuits/crates/blob/src/blob_batching_public_inputs.nr -> BlobAccumulatorPublicInputs
|
|
101
|
+
*/
|
|
102
|
+
export declare class BatchedBlobAccumulator {
|
|
103
|
+
/** Hash of Cs (to link to L1 blob hashes). */
|
|
104
|
+
readonly blobCommitmentsHashAcc: Fr;
|
|
105
|
+
/** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
|
|
106
|
+
readonly zAcc: Fr;
|
|
107
|
+
/** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
108
|
+
readonly yAcc: BLS12Fr;
|
|
109
|
+
/** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
|
|
110
|
+
readonly cAcc: BLS12Point;
|
|
111
|
+
/** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
112
|
+
readonly qAcc: BLS12Point;
|
|
113
|
+
/**
|
|
114
|
+
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
115
|
+
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
116
|
+
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
117
|
+
*/
|
|
118
|
+
readonly gammaAcc: Fr;
|
|
119
|
+
/** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
|
|
120
|
+
readonly gammaPow: BLS12Fr;
|
|
121
|
+
/** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
|
|
122
|
+
readonly finalBlobChallenges: FinalBlobBatchingChallenges;
|
|
123
|
+
constructor(
|
|
124
|
+
/** Hash of Cs (to link to L1 blob hashes). */
|
|
125
|
+
blobCommitmentsHashAcc: Fr,
|
|
126
|
+
/** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
|
|
127
|
+
zAcc: Fr,
|
|
128
|
+
/** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
|
|
129
|
+
yAcc: BLS12Fr,
|
|
130
|
+
/** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
|
|
131
|
+
cAcc: BLS12Point,
|
|
132
|
+
/** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
|
|
133
|
+
qAcc: BLS12Point,
|
|
134
|
+
/**
|
|
135
|
+
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
136
|
+
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
137
|
+
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
138
|
+
*/
|
|
139
|
+
gammaAcc: Fr,
|
|
140
|
+
/** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
|
|
141
|
+
gammaPow: BLS12Fr,
|
|
142
|
+
/** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
|
|
143
|
+
finalBlobChallenges: FinalBlobBatchingChallenges);
|
|
144
|
+
/**
|
|
145
|
+
* Init the first accumulation state of the epoch.
|
|
146
|
+
* We assume the input blob has not been evaluated at z.
|
|
147
|
+
*
|
|
148
|
+
* First state of the accumulator:
|
|
149
|
+
* - v_acc := sha256(C_0)
|
|
150
|
+
* - z_acc := z_0
|
|
151
|
+
* - y_acc := gamma^0 * y_0 = y_0
|
|
152
|
+
* - c_acc := gamma^0 * c_0 = c_0
|
|
153
|
+
* - gamma_acc := poseidon2(y_0.limbs)
|
|
154
|
+
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
155
|
+
*
|
|
156
|
+
* TODO(MW): When moved to batching, we should ONLY evaluate individual blobs at z => won't need finalZ input.
|
|
157
|
+
* @returns An initial blob accumulator.
|
|
158
|
+
*/
|
|
159
|
+
static initialize(blob: Blob, finalBlobChallenges: FinalBlobBatchingChallenges): Promise<BatchedBlobAccumulator>;
|
|
160
|
+
/**
|
|
161
|
+
* Create the empty accumulation state of the epoch.
|
|
162
|
+
* @returns An empty blob accumulator with challenges.
|
|
163
|
+
*/
|
|
164
|
+
static newWithChallenges(finalBlobChallenges: FinalBlobBatchingChallenges): BatchedBlobAccumulator;
|
|
165
|
+
/**
|
|
166
|
+
* Given blob i, accumulate all state.
|
|
167
|
+
* We assume the input blob has not been evaluated at z.
|
|
168
|
+
* TODO(MW): Currently returning new accumulator. May be better to mutate in future?
|
|
169
|
+
* @returns An updated blob accumulator.
|
|
170
|
+
*/
|
|
171
|
+
accumulate(blob: Blob): Promise<BatchedBlobAccumulator>;
|
|
172
|
+
/**
|
|
173
|
+
* Given blobs, accumulate all state.
|
|
174
|
+
* We assume the input blobs have not been evaluated at z.
|
|
175
|
+
* @returns An updated blob accumulator.
|
|
176
|
+
*/
|
|
177
|
+
accumulateBlobs(blobs: Blob[]): Promise<BatchedBlobAccumulator>;
|
|
178
|
+
/**
|
|
179
|
+
* Finalize accumulation state of the epoch.
|
|
180
|
+
* We assume ALL blobs in the epoch have been accumulated.
|
|
181
|
+
*
|
|
182
|
+
* Final accumulated values:
|
|
183
|
+
* - v := v_acc (hash of all commitments (C_i s) to be checked on L1)
|
|
184
|
+
* - z := z_acc (final challenge, at which all blobs are evaluated)
|
|
185
|
+
* - y := y_acc (final opening to be checked on L1)
|
|
186
|
+
* - c := c_acc (final commitment to be checked on L1)
|
|
187
|
+
* - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
|
|
188
|
+
*
|
|
189
|
+
* @returns A batched blob.
|
|
190
|
+
*/
|
|
191
|
+
finalize(): Promise<BatchedBlob>;
|
|
192
|
+
/**
|
|
193
|
+
* Converts to a struct for the public inputs of our rollup circuits.
|
|
194
|
+
* @returns A BlobAccumulatorPublicInputs instance.
|
|
195
|
+
*/
|
|
196
|
+
toBlobAccumulatorPublicInputs(): BlobAccumulatorPublicInputs;
|
|
197
|
+
/**
|
|
198
|
+
* Converts to a struct for the public inputs of our root rollup circuit.
|
|
199
|
+
* Warning: MUST be final accumulator state.
|
|
200
|
+
* @returns A FinalBlobAccumulatorPublicInputs instance.
|
|
201
|
+
*/
|
|
202
|
+
toFinalBlobAccumulatorPublicInputs(): FinalBlobAccumulatorPublicInputs;
|
|
203
|
+
isEmptyState(): boolean;
|
|
204
|
+
}
|
|
205
|
+
//# sourceMappingURL=blob_batching.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blob_batching.d.ts","sourceRoot":"","sources":["../src/blob_batching.ts"],"names":[],"mappings":"AAEA,OAAO,EAAc,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC/E,OAAO,EAAE,YAAY,EAAqB,MAAM,6BAA6B,CAAC;AAK9E,OAAO,EAAE,IAAI,EAA8B,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,2BAA2B,EAAE,gCAAgC,EAAE,MAAM,kCAAkC,CAAC;AAIj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ER_BLOCK } from '@aztec/constants';
|
|
2
|
+
import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
|
|
3
|
+
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
4
|
+
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
|
+
// Importing directly from 'c-kzg' does not work:
|
|
6
|
+
import cKzg from 'c-kzg';
|
|
7
|
+
import { Blob, VERSIONED_HASH_VERSION_KZG } from './blob.js';
|
|
8
|
+
import { BlobAccumulatorPublicInputs, FinalBlobAccumulatorPublicInputs } from './blob_batching_public_inputs.js';
|
|
9
|
+
const { computeKzgProof, verifyKzgProof } = cKzg;
|
|
10
|
+
/**
|
|
11
|
+
* A class to create, manage, and prove batched EVM blobs.
|
|
12
|
+
*/ export class BatchedBlob {
|
|
13
|
+
blobCommitmentsHash;
|
|
14
|
+
z;
|
|
15
|
+
y;
|
|
16
|
+
commitment;
|
|
17
|
+
q;
|
|
18
|
+
constructor(/** Hash of Cs (to link to L1 blob hashes). */ blobCommitmentsHash, /** Challenge point z such that p_i(z) = y_i. */ z, /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */ y, /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */ commitment, /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */ q){
|
|
19
|
+
this.blobCommitmentsHash = blobCommitmentsHash;
|
|
20
|
+
this.z = z;
|
|
21
|
+
this.y = y;
|
|
22
|
+
this.commitment = commitment;
|
|
23
|
+
this.q = q;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the final batched opening proof from multiple blobs.
|
|
27
|
+
*
|
|
28
|
+
* TODO(MW): Using the old Blob struct means there are ignored values (e.g. blob.evaluationY, because we now evaluate at shared z).
|
|
29
|
+
* When switching to batching, create new class w/o useless values.
|
|
30
|
+
*
|
|
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
|
+
*/ static async batch(blobs) {
|
|
36
|
+
const numBlobs = blobs.length;
|
|
37
|
+
if (numBlobs > BLOBS_PER_BLOCK * AZTEC_MAX_EPOCH_DURATION) {
|
|
38
|
+
throw new Error(`Too many blobs (${numBlobs}) sent to batch(). The maximum is ${BLOBS_PER_BLOCK * AZTEC_MAX_EPOCH_DURATION}.`);
|
|
39
|
+
}
|
|
40
|
+
// Precalculate the values (z and gamma) and initialize the accumulator:
|
|
41
|
+
let acc = await this.newAccumulator(blobs);
|
|
42
|
+
// Now we can create a multi opening proof of all input blobs:
|
|
43
|
+
acc = await acc.accumulateBlobs(blobs);
|
|
44
|
+
return await acc.finalize();
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
|
|
48
|
+
* @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
|
|
49
|
+
* beforehand from ALL blobs.
|
|
50
|
+
*/ static async newAccumulator(blobs) {
|
|
51
|
+
const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobs);
|
|
52
|
+
return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets the final challenges based on all blobs and their elements to perform a multi opening proof.
|
|
56
|
+
* Used in BatchedBlobAccumulator as 'finalZ' and finalGamma':
|
|
57
|
+
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
58
|
+
* - where z_i = H(H(fields of blob_i), C_i) = Blob.challengeZ,
|
|
59
|
+
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
60
|
+
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
61
|
+
* - 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.
|
|
62
|
+
* @returns Challenges z and gamma.
|
|
63
|
+
*/ static async precomputeBatchedBlobChallenges(blobs) {
|
|
64
|
+
// We need to precompute the final challenge values to evaluate the blobs.
|
|
65
|
+
let z = blobs[0].challengeZ;
|
|
66
|
+
// We start at i = 1, because z is initialised as the first blob's challenge.
|
|
67
|
+
for(let i = 1; i < blobs.length; i++){
|
|
68
|
+
z = await poseidon2Hash([
|
|
69
|
+
z,
|
|
70
|
+
blobs[i].challengeZ
|
|
71
|
+
]);
|
|
72
|
+
}
|
|
73
|
+
// Now we have a shared challenge for all blobs, evaluate them...
|
|
74
|
+
const proofObjects = blobs.map((b)=>computeKzgProof(b.data, z.toBuffer()));
|
|
75
|
+
const evaluations = proofObjects.map(([_, evaluation])=>BLS12Fr.fromBuffer(Buffer.from(evaluation)));
|
|
76
|
+
// ...and find the challenge for the linear combination of blobs.
|
|
77
|
+
let gamma = await hashNoirBigNumLimbs(evaluations[0]);
|
|
78
|
+
// We start at i = 1, because gamma is initialised as the first blob's evaluation.
|
|
79
|
+
for(let i = 1; i < blobs.length; i++){
|
|
80
|
+
gamma = await poseidon2Hash([
|
|
81
|
+
gamma,
|
|
82
|
+
await hashNoirBigNumLimbs(evaluations[i])
|
|
83
|
+
]);
|
|
84
|
+
}
|
|
85
|
+
gamma = await poseidon2Hash([
|
|
86
|
+
gamma,
|
|
87
|
+
z
|
|
88
|
+
]);
|
|
89
|
+
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
90
|
+
}
|
|
91
|
+
static async precomputeEmptyBatchedBlobChallenges() {
|
|
92
|
+
const blobs = [
|
|
93
|
+
await Blob.fromFields([])
|
|
94
|
+
];
|
|
95
|
+
// We need to precompute the final challenge values to evaluate the blobs.
|
|
96
|
+
const z = blobs[0].challengeZ;
|
|
97
|
+
// Now we have a shared challenge for all blobs, evaluate them...
|
|
98
|
+
const proofObjects = blobs.map((b)=>computeKzgProof(b.data, z.toBuffer()));
|
|
99
|
+
const evaluations = proofObjects.map(([_, evaluation])=>BLS12Fr.fromBuffer(Buffer.from(evaluation)));
|
|
100
|
+
// ...and find the challenge for the linear combination of blobs.
|
|
101
|
+
let gamma = await hashNoirBigNumLimbs(evaluations[0]);
|
|
102
|
+
gamma = await poseidon2Hash([
|
|
103
|
+
gamma,
|
|
104
|
+
z
|
|
105
|
+
]);
|
|
106
|
+
return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
|
|
107
|
+
}
|
|
108
|
+
// Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
|
|
109
|
+
getEthVersionedBlobHash() {
|
|
110
|
+
const hash = sha256(this.commitment.compress());
|
|
111
|
+
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
112
|
+
return hash;
|
|
113
|
+
}
|
|
114
|
+
static getEthVersionedBlobHash(commitment) {
|
|
115
|
+
const hash = sha256(commitment);
|
|
116
|
+
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
117
|
+
return hash;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
|
|
121
|
+
*
|
|
122
|
+
* input[:32] - versioned_hash
|
|
123
|
+
* input[32:64] - z
|
|
124
|
+
* input[64:96] - y
|
|
125
|
+
* input[96:144] - commitment C
|
|
126
|
+
* input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
|
|
127
|
+
*
|
|
128
|
+
* See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
|
|
129
|
+
*/ getEthBlobEvaluationInputs() {
|
|
130
|
+
const buf = Buffer.concat([
|
|
131
|
+
this.getEthVersionedBlobHash(),
|
|
132
|
+
this.z.toBuffer(),
|
|
133
|
+
this.y.toBuffer(),
|
|
134
|
+
this.commitment.compress(),
|
|
135
|
+
this.q.compress()
|
|
136
|
+
]);
|
|
137
|
+
return `0x${buf.toString('hex')}`;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Final values z and gamma are injected into each block root circuit. We ensure they are correct by:
|
|
142
|
+
* - Checking equality in each block merge circuit and propagating up
|
|
143
|
+
* - Checking final z_acc == z in root circuit
|
|
144
|
+
* - Checking final gamma_acc == gamma in root circuit
|
|
145
|
+
*
|
|
146
|
+
* - z = H(...H(H(z_0, z_1) z_2)..z_n)
|
|
147
|
+
* - where z_i = H(H(fields of blob_i), C_i),
|
|
148
|
+
* - used such that p_i(z) = y_i = Blob.evaluationY for all n blob polynomials p_i().
|
|
149
|
+
* - gamma = H(H(...H(H(y_0, y_1) y_2)..y_n), z)
|
|
150
|
+
* - used such that y = sum_i { gamma^i * y_i }, and C = sum_i { gamma^i * C_i }
|
|
151
|
+
* for all blob evaluations y_i (see above) and commitments C_i.
|
|
152
|
+
*
|
|
153
|
+
* Iteratively calculated by BlobAccumulatorPublicInputs.accumulate() in nr. See also precomputeBatchedBlobChallenges() above.
|
|
154
|
+
*/ export class FinalBlobBatchingChallenges {
|
|
155
|
+
z;
|
|
156
|
+
gamma;
|
|
157
|
+
constructor(z, gamma){
|
|
158
|
+
this.z = z;
|
|
159
|
+
this.gamma = gamma;
|
|
160
|
+
}
|
|
161
|
+
equals(other) {
|
|
162
|
+
return this.z.equals(other.z) && this.gamma.equals(other.gamma);
|
|
163
|
+
}
|
|
164
|
+
static empty() {
|
|
165
|
+
return new FinalBlobBatchingChallenges(Fr.ZERO, BLS12Fr.ZERO);
|
|
166
|
+
}
|
|
167
|
+
static fromBuffer(buffer) {
|
|
168
|
+
const reader = BufferReader.asReader(buffer);
|
|
169
|
+
return new FinalBlobBatchingChallenges(Fr.fromBuffer(reader), reader.readObject(BLS12Fr));
|
|
170
|
+
}
|
|
171
|
+
toBuffer() {
|
|
172
|
+
return serializeToBuffer(this.z, this.gamma);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* See noir-projects/noir-protocol-circuits/crates/blob/src/blob_batching_public_inputs.nr -> BlobAccumulatorPublicInputs
|
|
177
|
+
*/ export class BatchedBlobAccumulator {
|
|
178
|
+
blobCommitmentsHashAcc;
|
|
179
|
+
zAcc;
|
|
180
|
+
yAcc;
|
|
181
|
+
cAcc;
|
|
182
|
+
qAcc;
|
|
183
|
+
gammaAcc;
|
|
184
|
+
gammaPow;
|
|
185
|
+
finalBlobChallenges;
|
|
186
|
+
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, /**
|
|
187
|
+
* Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
|
|
188
|
+
* TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
|
|
189
|
+
* as a BLS12Fr field elt. Is this safe? Is there a skew?
|
|
190
|
+
*/ 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){
|
|
191
|
+
this.blobCommitmentsHashAcc = blobCommitmentsHashAcc;
|
|
192
|
+
this.zAcc = zAcc;
|
|
193
|
+
this.yAcc = yAcc;
|
|
194
|
+
this.cAcc = cAcc;
|
|
195
|
+
this.qAcc = qAcc;
|
|
196
|
+
this.gammaAcc = gammaAcc;
|
|
197
|
+
this.gammaPow = gammaPow;
|
|
198
|
+
this.finalBlobChallenges = finalBlobChallenges;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Init the first accumulation state of the epoch.
|
|
202
|
+
* We assume the input blob has not been evaluated at z.
|
|
203
|
+
*
|
|
204
|
+
* First state of the accumulator:
|
|
205
|
+
* - v_acc := sha256(C_0)
|
|
206
|
+
* - z_acc := z_0
|
|
207
|
+
* - y_acc := gamma^0 * y_0 = y_0
|
|
208
|
+
* - c_acc := gamma^0 * c_0 = c_0
|
|
209
|
+
* - gamma_acc := poseidon2(y_0.limbs)
|
|
210
|
+
* - gamma^(i + 1) = gamma^1 = gamma // denoted gamma_pow_acc
|
|
211
|
+
*
|
|
212
|
+
* TODO(MW): When moved to batching, we should ONLY evaluate individual blobs at z => won't need finalZ input.
|
|
213
|
+
* @returns An initial blob accumulator.
|
|
214
|
+
*/ static async initialize(blob, finalBlobChallenges) {
|
|
215
|
+
const [q, evaluation] = computeKzgProof(blob.data, finalBlobChallenges.z.toBuffer());
|
|
216
|
+
const firstY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
217
|
+
// Here, i = 0, so:
|
|
218
|
+
return new BatchedBlobAccumulator(sha256ToField([
|
|
219
|
+
blob.commitment
|
|
220
|
+
]), blob.challengeZ, firstY, BLS12Point.decompress(blob.commitment), BLS12Point.decompress(Buffer.from(q)), await hashNoirBigNumLimbs(firstY), finalBlobChallenges.gamma, finalBlobChallenges);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Create the empty accumulation state of the epoch.
|
|
224
|
+
* @returns An empty blob accumulator with challenges.
|
|
225
|
+
*/ static newWithChallenges(finalBlobChallenges) {
|
|
226
|
+
return new BatchedBlobAccumulator(Fr.ZERO, Fr.ZERO, BLS12Fr.ZERO, BLS12Point.ZERO, BLS12Point.ZERO, Fr.ZERO, BLS12Fr.ZERO, finalBlobChallenges);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Given blob i, accumulate all state.
|
|
230
|
+
* We assume the input blob has not been evaluated at z.
|
|
231
|
+
* TODO(MW): Currently returning new accumulator. May be better to mutate in future?
|
|
232
|
+
* @returns An updated blob accumulator.
|
|
233
|
+
*/ async accumulate(blob) {
|
|
234
|
+
if (this.isEmptyState()) {
|
|
235
|
+
return BatchedBlobAccumulator.initialize(blob, this.finalBlobChallenges);
|
|
236
|
+
} else {
|
|
237
|
+
const [q, evaluation] = computeKzgProof(blob.data, this.finalBlobChallenges.z.toBuffer());
|
|
238
|
+
const thisY = BLS12Fr.fromBuffer(Buffer.from(evaluation));
|
|
239
|
+
// Moving from i - 1 to i, so:
|
|
240
|
+
return new BatchedBlobAccumulator(sha256ToField([
|
|
241
|
+
this.blobCommitmentsHashAcc,
|
|
242
|
+
blob.commitment
|
|
243
|
+
]), await poseidon2Hash([
|
|
244
|
+
this.zAcc,
|
|
245
|
+
blob.challengeZ
|
|
246
|
+
]), this.yAcc.add(thisY.mul(this.gammaPow)), this.cAcc.add(BLS12Point.decompress(blob.commitment).mul(this.gammaPow)), this.qAcc.add(BLS12Point.decompress(Buffer.from(q)).mul(this.gammaPow)), await poseidon2Hash([
|
|
247
|
+
this.gammaAcc,
|
|
248
|
+
await hashNoirBigNumLimbs(thisY)
|
|
249
|
+
]), this.gammaPow.mul(this.finalBlobChallenges.gamma), this.finalBlobChallenges);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Given blobs, accumulate all state.
|
|
254
|
+
* We assume the input blobs have not been evaluated at z.
|
|
255
|
+
* @returns An updated blob accumulator.
|
|
256
|
+
*/ async accumulateBlobs(blobs) {
|
|
257
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
258
|
+
let acc = this; // TODO(MW): this.clone()
|
|
259
|
+
for(let i = 0; i < blobs.length; i++){
|
|
260
|
+
acc = await acc.accumulate(blobs[i]);
|
|
261
|
+
}
|
|
262
|
+
return acc;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Finalize accumulation state of the epoch.
|
|
266
|
+
* We assume ALL blobs in the epoch have been accumulated.
|
|
267
|
+
*
|
|
268
|
+
* Final accumulated values:
|
|
269
|
+
* - v := v_acc (hash of all commitments (C_i s) to be checked on L1)
|
|
270
|
+
* - z := z_acc (final challenge, at which all blobs are evaluated)
|
|
271
|
+
* - y := y_acc (final opening to be checked on L1)
|
|
272
|
+
* - c := c_acc (final commitment to be checked on L1)
|
|
273
|
+
* - gamma := poseidon2(gamma_acc, z) (challenge for linear combination of y and C, above)
|
|
274
|
+
*
|
|
275
|
+
* @returns A batched blob.
|
|
276
|
+
*/ async finalize() {
|
|
277
|
+
// All values in acc are final, apart from gamma := poseidon2(gammaAcc, z):
|
|
278
|
+
const calculatedGamma = await poseidon2Hash([
|
|
279
|
+
this.gammaAcc,
|
|
280
|
+
this.zAcc
|
|
281
|
+
]);
|
|
282
|
+
// Check final values:
|
|
283
|
+
if (!this.zAcc.equals(this.finalBlobChallenges.z)) {
|
|
284
|
+
throw new Error(`Blob batching mismatch: accumulated z ${this.zAcc} does not equal injected z ${this.finalBlobChallenges.z}`);
|
|
285
|
+
}
|
|
286
|
+
if (!calculatedGamma.equals(this.finalBlobChallenges.gamma.toBN254Fr())) {
|
|
287
|
+
throw new Error(`Blob batching mismatch: accumulated gamma ${calculatedGamma} does not equal injected gamma ${this.finalBlobChallenges.gamma.toBN254Fr()}`);
|
|
288
|
+
}
|
|
289
|
+
if (!verifyKzgProof(this.cAcc.compress(), this.zAcc.toBuffer(), this.yAcc.toBuffer(), this.qAcc.compress())) {
|
|
290
|
+
throw new Error(`KZG proof did not verify.`);
|
|
291
|
+
}
|
|
292
|
+
return new BatchedBlob(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.qAcc);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Converts to a struct for the public inputs of our rollup circuits.
|
|
296
|
+
* @returns A BlobAccumulatorPublicInputs instance.
|
|
297
|
+
*/ toBlobAccumulatorPublicInputs() {
|
|
298
|
+
return new BlobAccumulatorPublicInputs(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.gammaAcc, this.gammaPow);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Converts to a struct for the public inputs of our root rollup circuit.
|
|
302
|
+
* Warning: MUST be final accumulator state.
|
|
303
|
+
* @returns A FinalBlobAccumulatorPublicInputs instance.
|
|
304
|
+
*/ toFinalBlobAccumulatorPublicInputs() {
|
|
305
|
+
return new FinalBlobAccumulatorPublicInputs(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc);
|
|
306
|
+
}
|
|
307
|
+
isEmptyState() {
|
|
308
|
+
return this.blobCommitmentsHashAcc.isZero() && this.zAcc.isZero() && this.yAcc.isZero() && this.cAcc.isZero() && this.qAcc.isZero() && this.gammaAcc.isZero() && this.gammaPow.isZero();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
// To mimic the hash accumulation in the rollup circuits, here we hash
|
|
312
|
+
// each u128 limb of the noir bignum struct representing the BLS field.
|
|
313
|
+
async function hashNoirBigNumLimbs(field) {
|
|
314
|
+
const num = field.toNoirBigNum();
|
|
315
|
+
return await poseidon2Hash(num.limbs.map(Fr.fromHexString));
|
|
316
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
|
|
3
|
+
import { inspect } from 'util';
|
|
4
|
+
import { Blob } from './blob.js';
|
|
5
|
+
import { BatchedBlob, BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
|
|
6
|
+
/**
|
|
7
|
+
* See nr BlobAccumulatorPublicInputs and ts BatchedBlobAccumulator for documentation.
|
|
8
|
+
*/
|
|
9
|
+
export declare class BlobAccumulatorPublicInputs {
|
|
10
|
+
blobCommitmentsHashAcc: Fr;
|
|
11
|
+
zAcc: Fr;
|
|
12
|
+
yAcc: BLS12Fr;
|
|
13
|
+
cAcc: BLS12Point;
|
|
14
|
+
gammaAcc: Fr;
|
|
15
|
+
gammaPowAcc: BLS12Fr;
|
|
16
|
+
constructor(blobCommitmentsHashAcc: Fr, zAcc: Fr, yAcc: BLS12Fr, cAcc: BLS12Point, gammaAcc: Fr, gammaPowAcc: BLS12Fr);
|
|
17
|
+
static empty(): BlobAccumulatorPublicInputs;
|
|
18
|
+
equals(other: BlobAccumulatorPublicInputs): boolean;
|
|
19
|
+
static fromBuffer(buffer: Buffer | BufferReader): BlobAccumulatorPublicInputs;
|
|
20
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
21
|
+
/**
|
|
22
|
+
* Given blobs, accumulate all public inputs state.
|
|
23
|
+
* We assume the input blobs have not been evaluated at z.
|
|
24
|
+
* NOTE: Does NOT accumulate non circuit values including Q. This exists to simulate/check exactly what the circuit is doing
|
|
25
|
+
* and is unsafe for other use. For that reason, a toBatchedBlobAccumulator does not exist. See evaluateBlobs() oracle for usage.
|
|
26
|
+
* @returns An updated blob accumulator.
|
|
27
|
+
*/
|
|
28
|
+
accumulateBlobs(blobs: Blob[], finalBlobChallenges: FinalBlobBatchingChallenges): Promise<BlobAccumulatorPublicInputs>;
|
|
29
|
+
toFields(): Fr[];
|
|
30
|
+
static fromFields(fields: Fr[] | FieldReader): BlobAccumulatorPublicInputs;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* See nr FinalBlobAccumulatorPublicInputs and ts BatchedBlobAccumulator for documentation.
|
|
34
|
+
*/
|
|
35
|
+
export declare class FinalBlobAccumulatorPublicInputs {
|
|
36
|
+
blobCommitmentsHash: Fr;
|
|
37
|
+
z: Fr;
|
|
38
|
+
y: BLS12Fr;
|
|
39
|
+
c: BLS12Point;
|
|
40
|
+
constructor(blobCommitmentsHash: Fr, z: Fr, y: BLS12Fr, c: BLS12Point);
|
|
41
|
+
static empty(): FinalBlobAccumulatorPublicInputs;
|
|
42
|
+
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobAccumulatorPublicInputs;
|
|
43
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
44
|
+
static fromBatchedBlob(blob: BatchedBlob): FinalBlobAccumulatorPublicInputs;
|
|
45
|
+
toFields(): Fr[];
|
|
46
|
+
toString(): string;
|
|
47
|
+
equals(other: FinalBlobAccumulatorPublicInputs): boolean;
|
|
48
|
+
static random(): FinalBlobAccumulatorPublicInputs;
|
|
49
|
+
[inspect.custom](): string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* startBlobAccumulator: Accumulated opening proofs for all blobs before this block range.
|
|
53
|
+
* endBlobAccumulator: Accumulated opening proofs for all blobs after adding this block range.
|
|
54
|
+
* finalBlobChallenges: Final values z and gamma, shared across the epoch.
|
|
55
|
+
*/
|
|
56
|
+
export declare class BlockBlobPublicInputs {
|
|
57
|
+
startBlobAccumulator: BlobAccumulatorPublicInputs;
|
|
58
|
+
endBlobAccumulator: BlobAccumulatorPublicInputs;
|
|
59
|
+
finalBlobChallenges: FinalBlobBatchingChallenges;
|
|
60
|
+
constructor(startBlobAccumulator: BlobAccumulatorPublicInputs, endBlobAccumulator: BlobAccumulatorPublicInputs, finalBlobChallenges: FinalBlobBatchingChallenges);
|
|
61
|
+
static empty(): BlockBlobPublicInputs;
|
|
62
|
+
static fromBuffer(buffer: Buffer | BufferReader): BlockBlobPublicInputs;
|
|
63
|
+
toBuffer(): Buffer<ArrayBufferLike>;
|
|
64
|
+
static fromBlobs(startBlobAccumulator: BatchedBlobAccumulator, blobs: Blob[]): Promise<BlockBlobPublicInputs>;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=blob_batching_public_inputs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"blob_batching_public_inputs.d.ts","sourceRoot":"","sources":["../src/blob_batching_public_inputs.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,WAAW,EAAqB,MAAM,6BAA6B,CAAC;AAE3F,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,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