@aztec/blob-lib 0.0.0-test.1 → 0.0.1-fake-c83136db25

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.
Files changed (69) hide show
  1. package/dest/blob.d.ts +58 -99
  2. package/dest/blob.d.ts.map +1 -1
  3. package/dest/blob.js +83 -183
  4. package/dest/blob_batching.d.ts +155 -0
  5. package/dest/blob_batching.d.ts.map +1 -0
  6. package/dest/blob_batching.js +260 -0
  7. package/dest/blob_utils.d.ts +30 -0
  8. package/dest/blob_utils.d.ts.map +1 -0
  9. package/dest/blob_utils.js +60 -0
  10. package/dest/circuit_types/blob_accumulator.d.ts +21 -0
  11. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
  12. package/dest/circuit_types/blob_accumulator.js +58 -0
  13. package/dest/circuit_types/final_blob_accumulator.d.ts +22 -0
  14. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
  15. package/dest/circuit_types/final_blob_accumulator.js +63 -0
  16. package/dest/circuit_types/final_blob_batching_challenges.d.ts +15 -0
  17. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
  18. package/dest/circuit_types/final_blob_batching_challenges.js +25 -0
  19. package/dest/circuit_types/index.d.ts +4 -0
  20. package/dest/circuit_types/index.d.ts.map +1 -0
  21. package/dest/circuit_types/index.js +4 -0
  22. package/dest/deserialize.d.ts +14 -0
  23. package/dest/deserialize.d.ts.map +1 -0
  24. package/dest/deserialize.js +33 -0
  25. package/dest/encoding.d.ts +22 -62
  26. package/dest/encoding.d.ts.map +1 -1
  27. package/dest/encoding.js +114 -104
  28. package/dest/hash.d.ts +35 -0
  29. package/dest/hash.d.ts.map +1 -0
  30. package/dest/hash.js +69 -0
  31. package/dest/index.d.ts +6 -2
  32. package/dest/index.d.ts.map +1 -1
  33. package/dest/index.js +6 -15
  34. package/dest/interface.d.ts +1 -2
  35. package/dest/interface.d.ts.map +1 -1
  36. package/dest/kzg_context.d.ts +4 -0
  37. package/dest/kzg_context.d.ts.map +1 -0
  38. package/dest/kzg_context.js +5 -0
  39. package/dest/sponge_blob.d.ts +15 -13
  40. package/dest/sponge_blob.d.ts.map +1 -1
  41. package/dest/sponge_blob.js +28 -17
  42. package/dest/testing.d.ts +12 -16
  43. package/dest/testing.d.ts.map +1 -1
  44. package/dest/testing.js +60 -46
  45. package/dest/types.d.ts +16 -0
  46. package/dest/types.d.ts.map +1 -0
  47. package/dest/types.js +3 -0
  48. package/package.json +16 -12
  49. package/src/blob.ts +82 -221
  50. package/src/blob_batching.ts +335 -0
  51. package/src/blob_utils.ts +71 -0
  52. package/src/circuit_types/blob_accumulator.ts +84 -0
  53. package/src/circuit_types/final_blob_accumulator.ts +75 -0
  54. package/src/circuit_types/final_blob_batching_challenges.ts +29 -0
  55. package/src/circuit_types/index.ts +4 -0
  56. package/src/deserialize.ts +38 -0
  57. package/src/encoding.ts +136 -120
  58. package/src/hash.ts +77 -0
  59. package/src/index.ts +6 -19
  60. package/src/interface.ts +1 -4
  61. package/src/kzg_context.ts +5 -0
  62. package/src/sponge_blob.ts +24 -14
  63. package/src/testing.ts +68 -43
  64. package/src/trusted_setup_bit_reversed.json +4100 -0
  65. package/src/types.ts +16 -0
  66. package/dest/blob_public_inputs.d.ts +0 -50
  67. package/dest/blob_public_inputs.d.ts.map +0 -1
  68. package/dest/blob_public_inputs.js +0 -146
  69. package/src/blob_public_inputs.ts +0 -157
package/src/blob.ts CHANGED
@@ -1,92 +1,80 @@
1
- import { poseidon2Hash, sha256 } from '@aztec/foundation/crypto';
2
- import { Fr } from '@aztec/foundation/fields';
1
+ import { FIELDS_PER_BLOB } from '@aztec/constants';
2
+ import { BLS12Fr, Fr } from '@aztec/foundation/fields';
3
3
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
4
 
5
- // Importing directly from 'c-kzg' does not work, ignoring import/no-named-as-default-member err:
6
- import cKzg from 'c-kzg';
7
- import type { Blob as BlobBuffer } from 'c-kzg';
8
-
9
- import { deserializeEncodedBlobToFields, extractBlobFieldsFromBuffer } from './encoding.js';
10
- import { BlobDeserializationError } from './errors.js';
5
+ import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
11
6
  import type { BlobJson } from './interface.js';
7
+ import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
12
8
 
13
- /* eslint-disable import/no-named-as-default-member */
14
- const { BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, blobToKzgCommitment, computeKzgProof, verifyKzgProof } = cKzg;
15
-
16
- // The prefix to the EVM blobHash, defined here: https://eips.ethereum.org/EIPS/eip-4844#specification
17
- export const VERSIONED_HASH_VERSION_KZG = 0x01;
9
+ export { FIELDS_PER_BLOB };
18
10
 
19
11
  /**
20
12
  * A class to create, manage, and prove EVM blobs.
13
+ *
14
+ * @dev Note: All methods in this class do not check the encoding of the given data. It's the responsibility of other
15
+ * components to ensure that the blob data (which might spread across multiple blobs) was created following the protocol
16
+ * and is correctly encoded.
21
17
  */
22
18
  export class Blob {
23
19
  constructor(
24
- /** The blob to be broadcast on L1 in bytes form. */
25
- public readonly data: BlobBuffer,
26
- /** The hash of all tx effects inside the blob. Used in generating the challenge z and proving that we have included all required effects. */
27
- public readonly fieldsHash: Fr,
28
- /** Challenge point z (= H(H(tx_effects), kzgCommmitment). Used such that p(z) = y. */
29
- public readonly challengeZ: Fr,
30
- /** Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts. */
31
- public readonly evaluationY: Buffer,
32
- /** Commitment to the blob C. Used in compressed BLS12 point format (48 bytes). */
20
+ /**
21
+ * The data to be broadcast on L1 in bytes form.
22
+ */
23
+ public readonly data: Uint8Array,
24
+ /**
25
+ * Commitment to the blob data. Used in compressed BLS12 point format (48 bytes).
26
+ */
33
27
  public readonly commitment: Buffer,
34
- /** KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes). */
35
- public readonly proof: Buffer,
36
- ) {}
28
+ ) {
29
+ if (data.length !== BYTES_PER_BLOB) {
30
+ throw new Error(`Blob data must be ${BYTES_PER_BLOB} bytes. Got ${data.length}.`);
31
+ }
32
+ if (commitment.length !== BYTES_PER_COMMITMENT) {
33
+ throw new Error(`Blob commitment must be ${BYTES_PER_COMMITMENT} bytes. Got ${commitment.length}.`);
34
+ }
35
+ }
37
36
 
38
37
  /**
39
- * The encoded version of the blob will determine the end of the blob based on the transaction encoding.
40
- * This is required when the fieldsHash of a blob will contain trailing zeros.
41
- *
42
- * See `./encoding.ts` for more details.
43
- *
44
- * This method is used to create a Blob from a buffer.
45
- * @param blob - The buffer to create the Blob from.
46
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
38
+ * Create a Blob from a buffer.
39
+ * @param data - The buffer of the Blob.
47
40
  * @returns A Blob created from the buffer.
48
41
  *
49
- * @throws If unable to deserialize the blob.
42
+ * @throws If data does not match the expected length (BYTES_PER_BLOB).
50
43
  */
51
- static fromEncodedBlobBuffer(blob: BlobBuffer, multiBlobFieldsHash?: Fr): Promise<Blob> {
52
- try {
53
- const fields: Fr[] = deserializeEncodedBlobToFields(blob);
54
- return Blob.fromFields(fields, multiBlobFieldsHash);
55
- } catch (err) {
56
- throw new BlobDeserializationError(
57
- `Failed to create Blob from encoded blob buffer, this blob was likely not created by us`,
58
- );
59
- }
44
+ static fromBlobBuffer(data: Uint8Array): Blob {
45
+ const commitment = computeBlobCommitment(data);
46
+ return new Blob(data, commitment);
60
47
  }
61
48
 
62
49
  /**
63
50
  * Create a Blob from an array of fields.
64
51
  *
52
+ * @dev This method pads 0s to the data, extending it to the size of a full blob.
53
+ *
65
54
  * @param fields - The array of fields to create the Blob from.
66
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
67
55
  * @returns A Blob created from the array of fields.
68
56
  */
69
- static async fromFields(fields: Fr[], multiBlobFieldsHash?: Fr): Promise<Blob> {
70
- if (fields.length > FIELD_ELEMENTS_PER_BLOB) {
71
- throw new Error(
72
- `Attempted to overfill blob with ${fields.length} elements. The maximum is ${FIELD_ELEMENTS_PER_BLOB}`,
73
- );
57
+ static fromFields(fields: Fr[]): Blob {
58
+ if (fields.length > FIELDS_PER_BLOB) {
59
+ throw new Error(`Attempted to overfill blob with ${fields.length} fields. The maximum is ${FIELDS_PER_BLOB}.`);
74
60
  }
75
61
 
76
62
  const data = Buffer.concat([serializeToBuffer(fields)], BYTES_PER_BLOB);
63
+ const commitment = computeBlobCommitment(data);
64
+ return new Blob(data, commitment);
65
+ }
77
66
 
78
- // This matches the output of SpongeBlob.squeeze() in the blob circuit
79
- const fieldsHash = multiBlobFieldsHash ? multiBlobFieldsHash : await poseidon2Hash(fields);
80
- const commitment = Buffer.from(blobToKzgCommitment(data));
81
- const challengeZ = await poseidon2Hash([fieldsHash, ...commitmentToFields(commitment)]);
82
- const res = computeKzgProof(data, challengeZ.toBuffer());
83
- if (!verifyKzgProof(commitment, challengeZ.toBuffer(), res[1], res[0])) {
84
- throw new Error(`KZG proof did not verify.`);
85
- }
86
- const proof = Buffer.from(res[0]);
87
- const evaluationY = Buffer.from(res[1]);
88
-
89
- return new Blob(data, fieldsHash, challengeZ, evaluationY, commitment, proof);
67
+ /**
68
+ * Get the fields from the blob data.
69
+ *
70
+ * @dev WARNING: this method returns all fields
71
+ *
72
+ * @returns The fields from the blob.
73
+ */
74
+ toFields(): Fr[] {
75
+ const reader = BufferReader.asReader(this.data);
76
+ const numTotalFields = this.data.length / Fr.SIZE_IN_BYTES;
77
+ return reader.readArray(numTotalFields, Fr);
90
78
  }
91
79
 
92
80
  /**
@@ -96,162 +84,90 @@ export class Blob {
96
84
  * the beacon chain via `getBlobSidecars`
97
85
  * https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
98
86
  *
99
- * @dev WARNING: by default json deals with encoded buffers
100
- *
101
87
  * @param json - The JSON object to create the Blob from.
102
88
  * @returns A Blob created from the JSON object.
103
89
  */
104
- static async fromJson(json: BlobJson): Promise<Blob> {
90
+ static fromJson(json: BlobJson): Blob {
105
91
  const blobBuffer = Buffer.from(json.blob.slice(2), 'hex');
106
-
107
- const blob = await Blob.fromEncodedBlobBuffer(blobBuffer);
92
+ const blob = Blob.fromBlobBuffer(blobBuffer);
108
93
 
109
94
  if (blob.commitment.toString('hex') !== json.kzg_commitment.slice(2)) {
110
95
  throw new Error('KZG commitment does not match');
111
96
  }
112
97
 
113
- // We do not check the proof, as it will be different if the challenge is shared
114
- // across multiple blobs
115
-
116
98
  return blob;
117
99
  }
118
100
 
119
101
  /**
120
102
  * Get the JSON representation of the blob.
121
103
  *
122
- * @dev WARNING: by default json deals with encoded buffers
123
104
  * @param index - optional - The index of the blob in the block.
124
105
  * @returns The JSON representation of the blob.
125
106
  */
126
- toJson(index?: number): BlobJson {
107
+ toJson(index: number): BlobJson {
127
108
  return {
128
109
  blob: `0x${Buffer.from(this.data).toString('hex')}`,
129
- index,
110
+ index: index.toString(),
130
111
  // eslint-disable-next-line camelcase
131
112
  kzg_commitment: `0x${this.commitment.toString('hex')}`,
132
- // eslint-disable-next-line camelcase
133
- kzg_proof: `0x${this.proof.toString('hex')}`,
134
113
  };
135
114
  }
136
115
 
137
- /**
138
- * Get the fields from the blob.
139
- *
140
- * @dev WARNING: this method does not take into account trailing zeros
141
- *
142
- * @returns The fields from the blob.
143
- */
144
- toFields(): Fr[] {
145
- return extractBlobFieldsFromBuffer(this.data);
116
+ getEthVersionedBlobHash(): Buffer {
117
+ return computeEthVersionedBlobHash(this.commitment);
146
118
  }
147
119
 
148
120
  /**
149
- * Get the encoded fields from the blob.
150
- *
151
- * @dev This method takes into account trailing zeros
152
- *
153
- * @returns The encoded fields from the blob.
154
- *
155
- * @throws If unable to deserialize the blob.
121
+ * Challenge point z (= H(H(tx_effects), kzgCommitment)).
122
+ * Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts).
156
123
  */
157
- toEncodedFields(): Fr[] {
158
- try {
159
- return deserializeEncodedBlobToFields(this.data);
160
- } catch (err) {
161
- throw new BlobDeserializationError(
162
- `Failed to deserialize encoded blob fields, this blob was likely not created by us`,
163
- );
164
- }
124
+ async computeChallengeZ(blobFieldsHash: Fr): Promise<Fr> {
125
+ return await computeChallengeZ(blobFieldsHash, this.commitment);
165
126
  }
166
127
 
167
128
  /**
168
- * Get the encoded fields from multiple blobs.
129
+ * Evaluate the blob at a given challenge and return the evaluation and KZG proof.
169
130
  *
170
- * @dev This method takes into account trailing zeros
131
+ * @param challengeZ - The challenge z at which to evaluate the blob.
132
+ * @param verifyProof - Whether to verify the KZG proof.
171
133
  *
172
- * @returns The encoded fields from the blobs.
134
+ * @returns
135
+ * y: BLS12Fr - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts.
136
+ * proof: Buffer - KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes).
173
137
  */
174
- static toEncodedFields(blobs: Blob[]): Fr[] {
175
- try {
176
- return deserializeEncodedBlobToFields(Buffer.concat(blobs.map(b => b.data)));
177
- } catch (err) {
178
- throw new BlobDeserializationError(
179
- `Failed to deserialize encoded blob fields, this blob was likely not created by us`,
180
- );
138
+ evaluate(challengeZ: Fr, verifyProof = false) {
139
+ const res = kzg.computeKzgProof(this.data, challengeZ.toBuffer());
140
+ if (verifyProof && !kzg.verifyKzgProof(this.commitment, challengeZ.toBuffer(), res[1], res[0])) {
141
+ throw new Error(`KZG proof did not verify.`);
181
142
  }
182
- }
183
-
184
- /**
185
- * Get the commitment fields from the blob.
186
- *
187
- * The 48-byte commitment is encoded into two field elements:
188
- * +------------------+------------------+
189
- * | Field Element 1 | Field Element 2 |
190
- * | [bytes 0-31] | [bytes 32-47] |
191
- * +------------------+------------------+
192
- * | 32 bytes | 16 bytes |
193
- * +------------------+------------------+
194
- * @returns The commitment fields from the blob.
195
- */
196
- commitmentToFields(): [Fr, Fr] {
197
- return commitmentToFields(this.commitment);
198
- }
199
-
200
- // Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
201
- getEthVersionedBlobHash(): Buffer {
202
- const hash = sha256(this.commitment);
203
- hash[0] = VERSIONED_HASH_VERSION_KZG;
204
- return hash;
205
- }
206
143
 
207
- static getEthVersionedBlobHash(commitment: Buffer): Buffer {
208
- const hash = sha256(commitment);
209
- hash[0] = VERSIONED_HASH_VERSION_KZG;
210
- return hash;
144
+ const proof = Buffer.from(res[0]);
145
+ const y = BLS12Fr.fromBuffer(Buffer.from(res[1]));
146
+ return { y, proof };
211
147
  }
212
148
 
213
149
  /**
214
150
  * Get the buffer representation of the ENTIRE blob.
215
151
  *
216
- * @dev WARNING: this buffer contains all metadata aswell as the data itself
152
+ * @dev WARNING: this buffer contains all metadata as well as the data itself.
217
153
  *
218
154
  * @returns The buffer representation of the blob.
219
155
  */
220
156
  toBuffer(): Buffer {
221
- return Buffer.from(
222
- serializeToBuffer(
223
- this.data.length,
224
- this.data,
225
- this.fieldsHash,
226
- this.challengeZ,
227
- this.evaluationY.length,
228
- this.evaluationY,
229
- this.commitment.length,
230
- this.commitment,
231
- this.proof.length,
232
- this.proof,
233
- ),
234
- );
157
+ return Buffer.from(serializeToBuffer(this.data.length, this.data, this.commitment.length, this.commitment));
235
158
  }
236
159
 
237
160
  /**
238
161
  * Create a Blob from a buffer.
239
162
  *
240
- * @dev WARNING: this method contains all metadata aswell as the data itself
163
+ * @dev WARNING: this method contains all metadata as well as the data itself.
241
164
  *
242
165
  * @param buf - The buffer to create the Blob from.
243
166
  * @returns A Blob created from the buffer.
244
167
  */
245
168
  static fromBuffer(buf: Buffer | BufferReader): Blob {
246
169
  const reader = BufferReader.asReader(buf);
247
- return new Blob(
248
- reader.readUint8Array(),
249
- reader.readObject(Fr),
250
- reader.readObject(Fr),
251
- reader.readBuffer(),
252
- reader.readBuffer(),
253
- reader.readBuffer(),
254
- );
170
+ return new Blob(reader.readUint8Array(), reader.readBuffer());
255
171
  }
256
172
 
257
173
  /**
@@ -261,69 +177,14 @@ export class Blob {
261
177
  return this.data.length;
262
178
  }
263
179
 
264
- /**
265
- * Returns a proof of opening of the blob to verify on L1 using the point evaluation precompile:
266
- *
267
- * input[:32] - versioned_hash
268
- * input[32:64] - z
269
- * input[64:96] - y
270
- * input[96:144] - commitment C
271
- * input[144:192] - proof (a commitment to the quotient polynomial q(X))
272
- *
273
- * See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
274
- */
275
- getEthBlobEvaluationInputs(): `0x${string}` {
276
- const buf = Buffer.concat([
277
- this.getEthVersionedBlobHash(),
278
- this.challengeZ.toBuffer(),
279
- this.evaluationY,
280
- this.commitment,
281
- this.proof,
282
- ]);
283
- return `0x${buf.toString('hex')}`;
284
- }
285
-
286
- static getEthBlobEvaluationInputs(blobs: Blob[]): `0x${string}` {
287
- let buf = Buffer.alloc(0);
288
- blobs.forEach(blob => {
289
- buf = Buffer.concat([
290
- buf,
291
- blob.getEthVersionedBlobHash(),
292
- blob.challengeZ.toBuffer(),
293
- blob.evaluationY,
294
- blob.commitment,
295
- blob.proof,
296
- ]);
297
- });
298
- // For multiple blobs, we prefix the number of blobs:
299
- const lenBuf = Buffer.alloc(1);
300
- lenBuf.writeUint8(blobs.length);
301
- buf = Buffer.concat([lenBuf, buf]);
302
- return `0x${buf.toString('hex')}`;
303
- }
304
-
305
180
  static getViemKzgInstance() {
306
181
  return {
307
- blobToKzgCommitment: cKzg.blobToKzgCommitment,
308
- computeBlobKzgProof: cKzg.computeBlobKzgProof,
182
+ blobToKzgCommitment: kzg.blobToKzgCommitment.bind(kzg),
183
+ computeBlobKzgProof: kzg.computeBlobKzgProof.bind(kzg),
184
+ computeCellsAndKzgProofs: (b: Uint8Array): [Uint8Array[], Uint8Array[]] => {
185
+ const result = kzg.computeCellsAndKzgProofs(b);
186
+ return [result.cells, result.proofs];
187
+ },
309
188
  };
310
189
  }
311
-
312
- // Returns as many blobs as we require to broadcast the given fields
313
- // Assumes we share the fields hash between all blobs
314
- static async getBlobs(fields: Fr[]): Promise<Blob[]> {
315
- const numBlobs = Math.max(Math.ceil(fields.length / FIELD_ELEMENTS_PER_BLOB), 1);
316
- const multiBlobFieldsHash = await poseidon2Hash(fields);
317
- const res = [];
318
- for (let i = 0; i < numBlobs; i++) {
319
- const end = fields.length < (i + 1) * FIELD_ELEMENTS_PER_BLOB ? fields.length : (i + 1) * FIELD_ELEMENTS_PER_BLOB;
320
- res.push(await Blob.fromFields(fields.slice(i * FIELD_ELEMENTS_PER_BLOB, end), multiBlobFieldsHash));
321
- }
322
- return res;
323
- }
324
- }
325
-
326
- // 48 bytes encoded in fields as [Fr, Fr] = [0->31, 31->48]
327
- function commitmentToFields(commitment: Buffer): [Fr, Fr] {
328
- return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, 48))];
329
190
  }