@aztec/blob-lib 0.0.1-commit.b655e406 → 0.0.1-commit.c0b82b2

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 (101) hide show
  1. package/dest/batched_blob.d.ts +31 -0
  2. package/dest/batched_blob.d.ts.map +1 -0
  3. package/dest/batched_blob.js +20 -0
  4. package/dest/blob.d.ts +14 -14
  5. package/dest/blob.d.ts.map +1 -1
  6. package/dest/blob.js +21 -20
  7. package/dest/blob_batching.d.ts +35 -72
  8. package/dest/blob_batching.d.ts.map +1 -1
  9. package/dest/blob_batching.js +75 -110
  10. package/dest/blob_utils.d.ts +22 -12
  11. package/dest/blob_utils.d.ts.map +1 -1
  12. package/dest/blob_utils.js +32 -23
  13. package/dest/circuit_types/blob_accumulator.d.ts +4 -2
  14. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -1
  15. package/dest/circuit_types/blob_accumulator.js +5 -1
  16. package/dest/circuit_types/final_blob_accumulator.d.ts +3 -2
  17. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -1
  18. package/dest/circuit_types/final_blob_accumulator.js +5 -2
  19. package/dest/circuit_types/final_blob_batching_challenges.d.ts +3 -2
  20. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -1
  21. package/dest/circuit_types/final_blob_batching_challenges.js +2 -1
  22. package/dest/circuit_types/index.d.ts +1 -1
  23. package/dest/encoding/block_blob_data.d.ts +30 -0
  24. package/dest/encoding/block_blob_data.d.ts.map +1 -0
  25. package/dest/encoding/block_blob_data.js +75 -0
  26. package/dest/encoding/block_end_marker.d.ts +11 -0
  27. package/dest/encoding/block_end_marker.d.ts.map +1 -0
  28. package/dest/encoding/block_end_marker.js +41 -0
  29. package/dest/encoding/block_end_state_field.d.ts +12 -0
  30. package/dest/encoding/block_end_state_field.d.ts.map +1 -0
  31. package/dest/encoding/block_end_state_field.js +39 -0
  32. package/dest/encoding/checkpoint_blob_data.d.ts +15 -0
  33. package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
  34. package/dest/encoding/checkpoint_blob_data.js +67 -0
  35. package/dest/encoding/checkpoint_end_marker.d.ts +8 -0
  36. package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -0
  37. package/dest/encoding/checkpoint_end_marker.js +28 -0
  38. package/dest/encoding/fixtures.d.ts +41 -0
  39. package/dest/encoding/fixtures.d.ts.map +1 -0
  40. package/dest/encoding/fixtures.js +140 -0
  41. package/dest/encoding/index.d.ts +10 -0
  42. package/dest/encoding/index.d.ts.map +1 -0
  43. package/dest/encoding/index.js +9 -0
  44. package/dest/encoding/tx_blob_data.d.ts +19 -0
  45. package/dest/encoding/tx_blob_data.d.ts.map +1 -0
  46. package/dest/encoding/tx_blob_data.js +79 -0
  47. package/dest/encoding/tx_start_marker.d.ts +16 -0
  48. package/dest/encoding/tx_start_marker.d.ts.map +1 -0
  49. package/dest/{encoding.js → encoding/tx_start_marker.js} +14 -60
  50. package/dest/errors.d.ts +1 -1
  51. package/dest/errors.d.ts.map +1 -1
  52. package/dest/hash.d.ts +14 -6
  53. package/dest/hash.d.ts.map +1 -1
  54. package/dest/hash.js +25 -14
  55. package/dest/index.d.ts +4 -4
  56. package/dest/index.d.ts.map +1 -1
  57. package/dest/index.js +3 -3
  58. package/dest/interface.d.ts +1 -2
  59. package/dest/interface.d.ts.map +1 -1
  60. package/dest/kzg_context.d.ts +10 -4
  61. package/dest/kzg_context.d.ts.map +1 -1
  62. package/dest/kzg_context.js +29 -5
  63. package/dest/sponge_blob.d.ts +9 -13
  64. package/dest/sponge_blob.d.ts.map +1 -1
  65. package/dest/sponge_blob.js +21 -36
  66. package/dest/testing.d.ts +9 -17
  67. package/dest/testing.d.ts.map +1 -1
  68. package/dest/testing.js +35 -64
  69. package/dest/types.d.ts +2 -1
  70. package/dest/types.d.ts.map +1 -1
  71. package/dest/types.js +1 -0
  72. package/package.json +8 -7
  73. package/src/batched_blob.ts +26 -0
  74. package/src/blob.ts +21 -20
  75. package/src/blob_batching.ts +93 -128
  76. package/src/blob_utils.ts +38 -25
  77. package/src/circuit_types/blob_accumulator.ts +13 -1
  78. package/src/circuit_types/final_blob_accumulator.ts +2 -1
  79. package/src/circuit_types/final_blob_batching_challenges.ts +2 -1
  80. package/src/encoding/block_blob_data.ts +114 -0
  81. package/src/encoding/block_end_marker.ts +55 -0
  82. package/src/encoding/block_end_state_field.ts +59 -0
  83. package/src/encoding/checkpoint_blob_data.ts +102 -0
  84. package/src/encoding/checkpoint_end_marker.ts +40 -0
  85. package/src/encoding/fixtures.ts +210 -0
  86. package/src/encoding/index.ts +9 -0
  87. package/src/encoding/tx_blob_data.ts +116 -0
  88. package/src/{encoding.ts → encoding/tx_start_marker.ts} +20 -77
  89. package/src/hash.ts +26 -14
  90. package/src/index.ts +3 -3
  91. package/src/interface.ts +0 -1
  92. package/src/kzg_context.ts +39 -3
  93. package/src/sponge_blob.ts +23 -36
  94. package/src/testing.ts +48 -74
  95. package/src/types.ts +1 -0
  96. package/dest/deserialize.d.ts +0 -14
  97. package/dest/deserialize.d.ts.map +0 -1
  98. package/dest/deserialize.js +0 -33
  99. package/dest/encoding.d.ts +0 -26
  100. package/dest/encoding.d.ts.map +0 -1
  101. package/src/deserialize.ts +0 -38
package/dest/testing.js CHANGED
@@ -1,84 +1,55 @@
1
- import { FIELDS_PER_BLOB } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
- import { randomInt } from '@aztec/foundation/crypto';
4
- import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
2
+ import { BLS12Fq, BLS12Fr, BLS12Point, BLSPointNotOnCurveError } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
4
  import { Blob } from './blob.js';
6
- import { BatchedBlobAccumulator } from './blob_batching.js';
7
- import { getBlobsPerL1Block } from './blob_utils.js';
8
- import { FinalBlobBatchingChallenges } from './circuit_types/index.js';
9
- import { createBlockEndMarker, encodeTxStartMarker } from './encoding.js';
5
+ import { BlobAccumulator } from './circuit_types/blob_accumulator.js';
6
+ import { FinalBlobAccumulator } from './circuit_types/final_blob_accumulator.js';
7
+ import { FinalBlobBatchingChallenges } from './circuit_types/final_blob_batching_challenges.js';
10
8
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
9
+ export * from './encoding/fixtures.js';
11
10
  /**
12
11
  * Makes arbitrary poseidon sponge for blob inputs.
13
12
  * Note: will not verify inside the circuit.
14
13
  * @param seed - The seed to use for generating the sponge.
15
14
  * @returns A sponge blob instance.
16
15
  */ export function makeSpongeBlob(seed = 1) {
17
- return new SpongeBlob(new Poseidon2Sponge(makeTuple(3, (i)=>new Fr(i)), makeTuple(4, (i)=>new Fr(i)), 1, false), seed, seed + 1);
16
+ return new SpongeBlob(new Poseidon2Sponge(makeTuple(3, (i)=>new Fr(i)), makeTuple(4, (i)=>new Fr(i)), 1, false), seed);
17
+ }
18
+ /**
19
+ * Makes an arbitrary but valid BLS12 point. The value is deterministic for a given seed.
20
+ * @param seed - The seed to use for generating the point.
21
+ * @returns A BLS12 point instance.
22
+ */ function makeBLS12Point(seed = 1) {
23
+ let accum = 0;
24
+ while(true){
25
+ try {
26
+ const x = new BLS12Fq(seed + accum);
27
+ const y = BLS12Point.YFromX(x);
28
+ if (y) {
29
+ return new BLS12Point(x, y, false);
30
+ }
31
+ accum++;
32
+ } catch (e) {
33
+ if (!(e instanceof BLSPointNotOnCurveError)) {
34
+ throw e;
35
+ }
36
+ // The point is not on the curve - try again
37
+ }
38
+ }
18
39
  }
19
40
  /**
20
41
  * Makes arbitrary blob public accumulator.
21
42
  * Note: will not verify inside the circuit.
22
43
  * @param seed - The seed to use for generating the blob accumulator.
23
44
  * @returns A blob accumulator instance.
24
- */ export function makeBatchedBlobAccumulator(seed = 1) {
25
- return new BatchedBlobAccumulator(new Fr(seed), new Fr(seed + 1), new BLS12Fr(seed + 2), BLS12Point.random(), BLS12Point.random(), new Fr(seed + 3), new BLS12Fr(seed + 4), new FinalBlobBatchingChallenges(new Fr(seed + 5), new BLS12Fr(seed + 6)));
45
+ */ export function makeBlobAccumulator(seed = 1) {
46
+ return new BlobAccumulator(new Fr(seed), new Fr(seed + 0x10), new BLS12Fr(seed + 0x20), makeBLS12Point(seed + 0x30), new Fr(seed + 0x50), new BLS12Fr(seed + 0x60));
26
47
  }
27
- export function makeEncodedTxBlobFields(length) {
28
- const txStartMarker = {
29
- numBlobFields: length,
30
- // The rest of the values don't matter. The test components using it do not try to deserialize everything.
31
- // Only `checkBlobFieldsEncoding` is used and it only looks at `numBlobFields`. This might change in the future
32
- // when we add more thorough checks to `checkBlobFieldsEncoding`.
33
- revertCode: 0,
34
- numNoteHashes: 0,
35
- numNullifiers: 0,
36
- numL2ToL1Msgs: 0,
37
- numPublicDataWrites: 0,
38
- numPrivateLogs: 0,
39
- publicLogsLength: 0,
40
- contractClassLogLength: 0
41
- };
42
- return [
43
- encodeTxStartMarker(txStartMarker),
44
- ...Array.from({
45
- length: length - 1
46
- }, ()=>new Fr(randomInt(Number.MAX_SAFE_INTEGER)))
47
- ];
48
- }
49
- export function makeEncodedBlockBlobFields(...lengths) {
50
- return [
51
- ...lengths.length > 0 ? makeEncodedTxBlobFields(lengths[0] - 1) : [],
52
- ...lengths.slice(1).flatMap((length)=>makeEncodedTxBlobFields(length)),
53
- createBlockEndMarker(lengths.length)
54
- ];
55
- }
56
- // Create blob fields for a checkpoint with a single block.
57
- export function makeEncodedBlobFields(length) {
58
- if (length <= 2) {
59
- throw new Error('Encoded blob fields length must be greater than 2');
60
- }
61
- const checkpointPrefix = new Fr(length);
62
- return [
63
- checkpointPrefix,
64
- ...makeEncodedBlockBlobFields(length - 1)
65
- ]; // -1 to account for the checkpoint prefix.
66
- }
67
- /**
68
- * Make an encoded blob with the given length
69
- *
70
- * This will deserialise correctly in the archiver
71
- * @param length
72
- * @returns
73
- */ export function makeEncodedBlob(length) {
74
- if (length > FIELDS_PER_BLOB) {
75
- throw new Error(`A single encoded blob must be less than ${FIELDS_PER_BLOB} fields`);
76
- }
77
- return Blob.fromFields(makeEncodedBlobFields(length));
48
+ export function makeFinalBlobAccumulator(seed = 1) {
49
+ return new FinalBlobAccumulator(new Fr(seed), new Fr(seed + 0x10), new BLS12Fr(seed + 0x20), makeBLS12Point(seed + 0x30));
78
50
  }
79
- export function makeEncodedBlobs(length) {
80
- const fields = makeEncodedBlobFields(length);
81
- return getBlobsPerL1Block(fields);
51
+ export function makeFinalBlobBatchingChallenges(seed = 1) {
52
+ return new FinalBlobBatchingChallenges(new Fr(seed), new BLS12Fr(seed + 0x10));
82
53
  }
83
54
  /**
84
55
  * Make a blob with random fields.
package/dest/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './batched_blob.js';
1
2
  export * from './circuit_types/index.js';
2
3
  export * from './interface.js';
3
4
  export * from './sponge_blob.js';
@@ -13,4 +14,4 @@ export interface BlobKzgInstance {
13
14
  /** Function to compute both blob data cells and their corresponding KZG proofs for EIP7594 */
14
15
  computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]];
15
16
  }
16
- //# sourceMappingURL=types.d.ts.map
17
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy90eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLG1CQUFtQixDQUFDO0FBQ2xDLGNBQWMsMEJBQTBCLENBQUM7QUFDekMsY0FBYyxnQkFBZ0IsQ0FBQztBQUMvQixjQUFjLGtCQUFrQixDQUFDO0FBRWpDOzs7R0FHRztBQUNILE1BQU0sV0FBVyxlQUFlO0lBQzlCLHdEQUF3RDtJQUN4RCxtQkFBbUIsQ0FBQyxJQUFJLEVBQUUsVUFBVSxHQUFHLFVBQVUsQ0FBQztJQUNsRCxrREFBa0Q7SUFDbEQsbUJBQW1CLENBQUMsSUFBSSxFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsVUFBVSxHQUFHLFVBQVUsQ0FBQztJQUMxRSw4RkFBOEY7SUFDOUYsd0JBQXdCLENBQUMsSUFBSSxFQUFFLFVBQVUsR0FBRyxDQUFDLFVBQVUsRUFBRSxFQUFFLFVBQVUsRUFBRSxDQUFDLENBQUM7Q0FDMUUifQ==
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AAEjC;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,wDAAwD;IACxD,mBAAmB,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAAC;IAClD,kDAAkD;IAClD,mBAAmB,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,GAAG,UAAU,CAAC;IAC1E,8FAA8F;IAC9F,wBAAwB,CAAC,IAAI,EAAE,UAAU,GAAG,CAAC,UAAU,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;CAC1E"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,0BAA0B,CAAC;AACzC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AAEjC;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,wDAAwD;IACxD,mBAAmB,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAAC;IAClD,kDAAkD;IAClD,mBAAmB,CAAC,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,GAAG,UAAU,CAAC;IAC1E,8FAA8F;IAC9F,wBAAwB,CAAC,IAAI,EAAE,UAAU,GAAG,CAAC,UAAU,EAAE,EAAE,UAAU,EAAE,CAAC,CAAC;CAC1E"}
package/dest/types.js CHANGED
@@ -1,3 +1,4 @@
1
+ export * from './batched_blob.js';
1
2
  export * from './circuit_types/index.js';
2
3
  export * from './interface.js';
3
4
  export * from './sponge_blob.js';
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@aztec/blob-lib",
3
- "version": "0.0.1-commit.b655e406",
3
+ "version": "0.0.1-commit.c0b82b2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
7
- "./encoding": "./dest/encoding.js",
7
+ "./encoding": "./dest/encoding/index.js",
8
8
  "./types": "./dest/types.js",
9
9
  "./testing": "./dest/testing.js"
10
10
  },
@@ -16,10 +16,10 @@
16
16
  "tsconfig": "./tsconfig.json"
17
17
  },
18
18
  "scripts": {
19
- "build": "yarn clean && tsc -b",
20
- "build:dev": "tsc -b --watch",
19
+ "build": "yarn clean && ../scripts/tsc.sh",
20
+ "build:dev": "../scripts/tsc.sh --watch",
21
21
  "clean": "rm -rf ./dest .tsbuildinfo",
22
- "start:dev": "tsc-watch -p tsconfig.json --onSuccess 'yarn start'",
22
+ "start:dev": "concurrently -k \"../scripts/tsc.sh --watch\" \"nodemon --watch dest --exec yarn start\"",
23
23
  "start": "node ./dest/index.js",
24
24
  "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
25
25
  },
@@ -27,8 +27,8 @@
27
27
  "../package.common.json"
28
28
  ],
29
29
  "dependencies": {
30
- "@aztec/constants": "0.0.1-commit.b655e406",
31
- "@aztec/foundation": "0.0.1-commit.b655e406",
30
+ "@aztec/constants": "0.0.1-commit.c0b82b2",
31
+ "@aztec/foundation": "0.0.1-commit.c0b82b2",
32
32
  "@crate-crypto/node-eth-kzg": "^0.10.0",
33
33
  "tslib": "^2.4.0"
34
34
  },
@@ -36,6 +36,7 @@
36
36
  "@jest/globals": "^30.0.0",
37
37
  "@types/jest": "^30.0.0",
38
38
  "@types/node": "^22.15.17",
39
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
39
40
  "get-port": "^7.1.0",
40
41
  "jest": "^30.0.0",
41
42
  "ts-node": "^10.9.1",
@@ -0,0 +1,26 @@
1
+ import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+
4
+ import { FinalBlobAccumulator } from './circuit_types/index.js';
5
+
6
+ /**
7
+ * A class to represent the result from accumulating blobs in an epoch using BatchedBlobAccumulator.
8
+ */
9
+ export class BatchedBlob {
10
+ constructor(
11
+ /** Hash of Cs (to link to L1 blob hashes). */
12
+ public readonly blobCommitmentsHash: Fr,
13
+ /** Challenge point z such that p_i(z) = y_i. */
14
+ public readonly z: Fr,
15
+ /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
16
+ public readonly y: BLS12Fr,
17
+ /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
18
+ public readonly commitment: BLS12Point,
19
+ /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
20
+ public readonly q: BLS12Point,
21
+ ) {}
22
+
23
+ toFinalBlobAccumulator() {
24
+ return new FinalBlobAccumulator(this.blobCommitmentsHash, this.z, this.y, this.commitment);
25
+ }
26
+ }
package/src/blob.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import { FIELDS_PER_BLOB } from '@aztec/constants';
2
- import { BLS12Fr, Fr } from '@aztec/foundation/fields';
2
+ import { BLS12Fr } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
5
 
5
6
  import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
6
7
  import type { BlobJson } from './interface.js';
7
- import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
8
+ import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
8
9
 
9
10
  export { FIELDS_PER_BLOB };
10
11
 
@@ -26,11 +27,11 @@ export class Blob {
26
27
  */
27
28
  public readonly commitment: Buffer,
28
29
  ) {
29
- if (data.length !== BYTES_PER_BLOB) {
30
- throw new Error(`Blob data must be ${BYTES_PER_BLOB} bytes. Got ${data.length}.`);
30
+ if (data.length !== getBytesPerBlob()) {
31
+ throw new Error(`Blob data must be ${getBytesPerBlob()} bytes. Got ${data.length}.`);
31
32
  }
32
- if (commitment.length !== BYTES_PER_COMMITMENT) {
33
- throw new Error(`Blob commitment must be ${BYTES_PER_COMMITMENT} bytes. Got ${commitment.length}.`);
33
+ if (commitment.length !== getBytesPerCommitment()) {
34
+ throw new Error(`Blob commitment must be ${getBytesPerCommitment()} bytes. Got ${commitment.length}.`);
34
35
  }
35
36
  }
36
37
 
@@ -39,10 +40,10 @@ export class Blob {
39
40
  * @param data - The buffer of the Blob.
40
41
  * @returns A Blob created from the buffer.
41
42
  *
42
- * @throws If data does not match the expected length (BYTES_PER_BLOB).
43
+ * @throws If data does not match the expected length (getBytesPerBlob()).
43
44
  */
44
- static fromBlobBuffer(data: Uint8Array): Blob {
45
- const commitment = computeBlobCommitment(data);
45
+ static async fromBlobBuffer(data: Uint8Array): Promise<Blob> {
46
+ const commitment = await computeBlobCommitment(data);
46
47
  return new Blob(data, commitment);
47
48
  }
48
49
 
@@ -54,13 +55,13 @@ export class Blob {
54
55
  * @param fields - The array of fields to create the Blob from.
55
56
  * @returns A Blob created from the array of fields.
56
57
  */
57
- static fromFields(fields: Fr[]): Blob {
58
+ static async fromFields(fields: Fr[]): Promise<Blob> {
58
59
  if (fields.length > FIELDS_PER_BLOB) {
59
60
  throw new Error(`Attempted to overfill blob with ${fields.length} fields. The maximum is ${FIELDS_PER_BLOB}.`);
60
61
  }
61
62
 
62
- const data = Buffer.concat([serializeToBuffer(fields)], BYTES_PER_BLOB);
63
- const commitment = computeBlobCommitment(data);
63
+ const data = Buffer.concat([serializeToBuffer(fields)], getBytesPerBlob());
64
+ const commitment = await computeBlobCommitment(data);
64
65
  return new Blob(data, commitment);
65
66
  }
66
67
 
@@ -80,16 +81,16 @@ export class Blob {
80
81
  /**
81
82
  * Create a Blob from a JSON object.
82
83
  *
83
- * Blobs will be in this form when requested from the blob sink, or from
84
+ * Blobs will be in this form when requested from the blob client, or from
84
85
  * the beacon chain via `getBlobSidecars`
85
86
  * https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
86
87
  *
87
88
  * @param json - The JSON object to create the Blob from.
88
89
  * @returns A Blob created from the JSON object.
89
90
  */
90
- static fromJson(json: BlobJson): Blob {
91
+ static async fromJson(json: BlobJson): Promise<Blob> {
91
92
  const blobBuffer = Buffer.from(json.blob.slice(2), 'hex');
92
- const blob = Blob.fromBlobBuffer(blobBuffer);
93
+ const blob = await Blob.fromBlobBuffer(blobBuffer);
93
94
 
94
95
  if (blob.commitment.toString('hex') !== json.kzg_commitment.slice(2)) {
95
96
  throw new Error('KZG commitment does not match');
@@ -101,13 +102,11 @@ export class Blob {
101
102
  /**
102
103
  * Get the JSON representation of the blob.
103
104
  *
104
- * @param index - optional - The index of the blob in the block.
105
105
  * @returns The JSON representation of the blob.
106
106
  */
107
- toJson(index: number): BlobJson {
107
+ toJSON(): BlobJson {
108
108
  return {
109
109
  blob: `0x${Buffer.from(this.data).toString('hex')}`,
110
- index: index.toString(),
111
110
  // eslint-disable-next-line camelcase
112
111
  kzg_commitment: `0x${this.commitment.toString('hex')}`,
113
112
  };
@@ -135,8 +134,9 @@ export class Blob {
135
134
  * y: BLS12Fr - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts.
136
135
  * proof: Buffer - KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes).
137
136
  */
138
- evaluate(challengeZ: Fr, verifyProof = false) {
139
- const res = kzg.computeKzgProof(this.data, challengeZ.toBuffer());
137
+ async evaluate(challengeZ: Fr, verifyProof = false) {
138
+ const kzg = getKzg();
139
+ const res = await kzg.asyncComputeKzgProof(this.data, challengeZ.toBuffer());
140
140
  if (verifyProof && !kzg.verifyKzgProof(this.commitment, challengeZ.toBuffer(), res[1], res[0])) {
141
141
  throw new Error(`KZG proof did not verify.`);
142
142
  }
@@ -178,6 +178,7 @@ export class Blob {
178
178
  }
179
179
 
180
180
  static getViemKzgInstance() {
181
+ const kzg = getKzg();
181
182
  return {
182
183
  blobToKzgCommitment: kzg.blobToKzgCommitment.bind(kzg),
183
184
  computeBlobKzgProof: kzg.computeBlobKzgProof.bind(kzg),
@@ -1,30 +1,71 @@
1
- import { AZTEC_MAX_EPOCH_DURATION, BLOBS_PER_BLOCK } from '@aztec/constants';
2
- import { poseidon2Hash, sha256ToField } from '@aztec/foundation/crypto';
3
- import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
1
+ import { BLOBS_PER_CHECKPOINT, MAX_CHECKPOINTS_PER_EPOCH } from '@aztec/constants';
2
+ import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
3
+ import { sha256ToField } from '@aztec/foundation/crypto/sha256';
4
+ import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
5
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
6
 
7
+ import { BatchedBlob } from './batched_blob.js';
5
8
  import { Blob } from './blob.js';
6
- import { computeBlobFieldsHashFromBlobs } from './blob_utils.js';
9
+ import { getBlobsPerL1Block } from './blob_utils.js';
7
10
  import { BlobAccumulator, FinalBlobAccumulator, FinalBlobBatchingChallenges } from './circuit_types/index.js';
8
- import { computeEthVersionedBlobHash, hashNoirBigNumLimbs } from './hash.js';
9
- import { kzg } from './kzg_context.js';
11
+ import { computeBlobFieldsHash, hashNoirBigNumLimbs } from './hash.js';
12
+ import { getKzg } from './kzg_context.js';
10
13
 
11
14
  /**
12
15
  * A class to create, manage, and prove batched EVM blobs.
16
+ * See noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr
13
17
  */
14
- export class BatchedBlob {
18
+ export class BatchedBlobAccumulator {
15
19
  constructor(
16
20
  /** Hash of Cs (to link to L1 blob hashes). */
17
- public readonly blobCommitmentsHash: Fr,
18
- /** Challenge point z such that p_i(z) = y_i. */
19
- public readonly z: Fr,
20
- /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
21
- public readonly y: BLS12Fr,
22
- /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
23
- public readonly commitment: BLS12Point,
24
- /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
25
- public readonly q: BLS12Point,
21
+ public readonly blobCommitmentsHashAcc: Fr,
22
+ /** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
23
+ public readonly zAcc: Fr,
24
+ /** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
25
+ public readonly yAcc: BLS12Fr,
26
+ /** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
27
+ public readonly cAcc: BLS12Point,
28
+ /** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
29
+ public readonly qAcc: BLS12Point,
30
+ /**
31
+ * Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
32
+ * TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
33
+ * as a BLS12Fr field elt. Is this safe? Is there a skew?
34
+ */
35
+ public readonly gammaAcc: Fr,
36
+ /** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
37
+ public readonly gammaPow: BLS12Fr,
38
+ /** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
39
+ public readonly finalBlobChallenges: FinalBlobBatchingChallenges,
26
40
  ) {}
27
41
 
42
+ /**
43
+ * Create the empty accumulation state of the epoch.
44
+ * @returns An empty blob accumulator with challenges.
45
+ */
46
+ static newWithChallenges(finalBlobChallenges: FinalBlobBatchingChallenges): BatchedBlobAccumulator {
47
+ return new BatchedBlobAccumulator(
48
+ Fr.ZERO,
49
+ Fr.ZERO,
50
+ BLS12Fr.ZERO,
51
+ BLS12Point.ZERO,
52
+ BLS12Point.ZERO,
53
+ Fr.ZERO,
54
+ BLS12Fr.ZERO,
55
+ finalBlobChallenges,
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
61
+ * @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
62
+ * beforehand from ALL blobs.
63
+ */
64
+ static async fromBlobFields(blobFieldsPerCheckpoint: Fr[][]): Promise<BatchedBlobAccumulator> {
65
+ const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint);
66
+ return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
67
+ }
68
+
28
69
  /**
29
70
  * Get the final batched opening proof from multiple blobs.
30
71
  * @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
@@ -32,30 +73,21 @@ export class BatchedBlob {
32
73
  *
33
74
  * @returns A batched blob.
34
75
  */
35
- static async batch(blobs: Blob[][]): Promise<BatchedBlob> {
36
- if (blobs.length > AZTEC_MAX_EPOCH_DURATION) {
76
+ static async batch(blobFieldsPerCheckpoint: Fr[][], verifyProof = false): Promise<BatchedBlob> {
77
+ const numCheckpoints = blobFieldsPerCheckpoint.length;
78
+ if (numCheckpoints > MAX_CHECKPOINTS_PER_EPOCH) {
37
79
  throw new Error(
38
- `Too many blocks sent to batch(). The maximum is ${AZTEC_MAX_EPOCH_DURATION}. Got ${blobs.length}.`,
80
+ `Too many checkpoints sent to batch(). The maximum is ${MAX_CHECKPOINTS_PER_EPOCH}. Got ${numCheckpoints}.`,
39
81
  );
40
82
  }
41
83
 
42
84
  // Precalculate the values (z and gamma) and initialize the accumulator:
43
- let acc = await this.newAccumulator(blobs);
85
+ let acc = await this.fromBlobFields(blobFieldsPerCheckpoint);
44
86
  // Now we can create a multi opening proof of all input blobs:
45
- for (const blockBlobs of blobs) {
46
- acc = await acc.accumulateBlobs(blockBlobs);
87
+ for (const blobFields of blobFieldsPerCheckpoint) {
88
+ acc = await acc.accumulateFields(blobFields);
47
89
  }
48
- return await acc.finalize();
49
- }
50
-
51
- /**
52
- * Returns an empty BatchedBlobAccumulator with precomputed challenges from all blobs in the epoch.
53
- * @dev MUST input all blobs to be broadcast. Does not work in multiple calls because z and gamma are calculated
54
- * beforehand from ALL blobs.
55
- */
56
- static async newAccumulator(blobs: Blob[][]): Promise<BatchedBlobAccumulator> {
57
- const finalBlobChallenges = await this.precomputeBatchedBlobChallenges(blobs);
58
- return BatchedBlobAccumulator.newWithChallenges(finalBlobChallenges);
90
+ return await acc.finalize(verifyProof);
59
91
  }
60
92
 
61
93
  /**
@@ -70,13 +102,15 @@ export class BatchedBlob {
70
102
  * @param blobs - The blobs to precompute the challenges for. Each sub-array is the blobs for an L1 block.
71
103
  * @returns Challenges z and gamma.
72
104
  */
73
- static async precomputeBatchedBlobChallenges(blobs: Blob[][]): Promise<FinalBlobBatchingChallenges> {
105
+ static async precomputeBatchedBlobChallenges(blobFieldsPerCheckpoint: Fr[][]): Promise<FinalBlobBatchingChallenges> {
74
106
  // Compute the final challenge z to evaluate the blobs.
75
107
  let z: Fr | undefined;
76
- for (const blockBlobs of blobs) {
108
+ const allBlobs = [];
109
+ for (const blobFields of blobFieldsPerCheckpoint) {
77
110
  // Compute the hash of all the fields in the block.
78
- const blobFieldsHash = await computeBlobFieldsHashFromBlobs(blockBlobs);
79
- for (const blob of blockBlobs) {
111
+ const blobFieldsHash = await computeBlobFieldsHash(blobFields);
112
+ const blobs = await getBlobsPerL1Block(blobFields);
113
+ for (const blob of blobs) {
80
114
  // Compute the challenge z for each blob and accumulate it.
81
115
  const challengeZ = await blob.computeChallengeZ(blobFieldsHash);
82
116
  if (!z) {
@@ -85,14 +119,14 @@ export class BatchedBlob {
85
119
  z = await poseidon2Hash([z, challengeZ]);
86
120
  }
87
121
  }
122
+ allBlobs.push(...blobs);
88
123
  }
89
124
  if (!z) {
90
125
  throw new Error('No blobs to precompute challenges for.');
91
126
  }
92
127
 
93
128
  // Now we have a shared challenge for all blobs, evaluate them...
94
- const allBlobs = blobs.flat();
95
- const proofObjects = allBlobs.map(b => b.evaluate(z));
129
+ const proofObjects = await Promise.all(allBlobs.map(b => b.evaluate(z)));
96
130
  const evaluations = await Promise.all(proofObjects.map(({ y }) => hashNoirBigNumLimbs(y)));
97
131
  // ...and find the challenge for the linear combination of blobs.
98
132
  let gamma = evaluations[0];
@@ -105,93 +139,13 @@ export class BatchedBlob {
105
139
  return new FinalBlobBatchingChallenges(z, BLS12Fr.fromBN254Fr(gamma));
106
140
  }
107
141
 
108
- verify() {
109
- return kzg.verifyKzgProof(this.commitment.compress(), this.z.toBuffer(), this.y.toBuffer(), this.q.compress());
110
- }
111
-
112
- // Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
113
- getEthVersionedBlobHash(): Buffer {
114
- return computeEthVersionedBlobHash(this.commitment.compress());
115
- }
116
-
117
- /**
118
- * Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
119
- *
120
- * input[:32] - versioned_hash
121
- * input[32:64] - z
122
- * input[64:96] - y
123
- * input[96:144] - commitment C
124
- * input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
125
- *
126
- * See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
127
- */
128
- getEthBlobEvaluationInputs(): `0x${string}` {
129
- const buf = Buffer.concat([
130
- this.getEthVersionedBlobHash(),
131
- this.z.toBuffer(),
132
- this.y.toBuffer(),
133
- this.commitment.compress(),
134
- this.q.compress(),
135
- ]);
136
- return `0x${buf.toString('hex')}`;
137
- }
138
-
139
- toFinalBlobAccumulator() {
140
- return new FinalBlobAccumulator(this.blobCommitmentsHash, this.z, this.y, this.commitment);
141
- }
142
- }
143
-
144
- /**
145
- * See noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr
146
- */
147
- export class BatchedBlobAccumulator {
148
- constructor(
149
- /** Hash of Cs (to link to L1 blob hashes). */
150
- public readonly blobCommitmentsHashAcc: Fr,
151
- /** Challenge point z_acc. Final value used such that p_i(z) = y_i. */
152
- public readonly zAcc: Fr,
153
- /** Evaluation y_acc. Final value is is linear combination of all evaluations y_i = p_i(z) with gamma. */
154
- public readonly yAcc: BLS12Fr,
155
- /** Commitment c_acc. Final value is linear combination of all commitments C_i = [p_i] with gamma. */
156
- public readonly cAcc: BLS12Point,
157
- /** KZG opening q_acc. Final value is linear combination of all blob kzg 'proofs' Q_i with gamma. */
158
- public readonly qAcc: BLS12Point,
159
- /**
160
- * Challenge point gamma_acc for multi opening. Used with y, C, and kzg 'proof' Q above.
161
- * TODO(#13608): We calculate this by hashing natively in the circuit (hence Fr representation), but it's actually used
162
- * as a BLS12Fr field elt. Is this safe? Is there a skew?
163
- */
164
- public readonly gammaAcc: Fr,
165
- /** Simply gamma^(i + 1) at blob i. Used for calculating the i'th element of the above linear comb.s */
166
- public readonly gammaPow: BLS12Fr,
167
- /** Final challenge values used in evaluation. Optimistically input and checked in the final acc. */
168
- public readonly finalBlobChallenges: FinalBlobBatchingChallenges,
169
- ) {}
170
-
171
- /**
172
- * Create the empty accumulation state of the epoch.
173
- * @returns An empty blob accumulator with challenges.
174
- */
175
- static newWithChallenges(finalBlobChallenges: FinalBlobBatchingChallenges): BatchedBlobAccumulator {
176
- return new BatchedBlobAccumulator(
177
- Fr.ZERO,
178
- Fr.ZERO,
179
- BLS12Fr.ZERO,
180
- BLS12Point.ZERO,
181
- BLS12Point.ZERO,
182
- Fr.ZERO,
183
- BLS12Fr.ZERO,
184
- finalBlobChallenges,
185
- );
186
- }
187
-
188
142
  /**
189
143
  * Given blob i, accumulate all state.
190
144
  * We assume the input blob has not been evaluated at z.
191
145
  * @returns An updated blob accumulator.
192
146
  */
193
- private async accumulate(blob: Blob, blobFieldsHash: Fr) {
194
- const { proof, y: thisY } = blob.evaluate(this.finalBlobChallenges.z);
147
+ async accumulateBlob(blob: Blob, blobFieldsHash: Fr) {
148
+ const { proof, y: thisY } = await blob.evaluate(this.finalBlobChallenges.z);
195
149
  const thisC = BLS12Point.decompress(blob.commitment);
196
150
  const thisQ = BLS12Point.decompress(proof);
197
151
  const blobChallengeZ = await blob.computeChallengeZ(blobFieldsHash);
@@ -234,23 +188,25 @@ export class BatchedBlobAccumulator {
234
188
  /**
235
189
  * Given blobs, accumulate all state.
236
190
  * 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.
191
+ * @param blobFields - The blob fields of a checkpoint to accumulate.
238
192
  * @returns An updated blob accumulator.
239
193
  */
240
- async accumulateBlobs(blobs: Blob[]) {
241
- if (blobs.length > BLOBS_PER_BLOCK) {
194
+ async accumulateFields(blobFields: Fr[]) {
195
+ const blobs = await getBlobsPerL1Block(blobFields);
196
+
197
+ if (blobs.length > BLOBS_PER_CHECKPOINT) {
242
198
  throw new Error(
243
- `Too many blobs to accumulate. The maximum is ${BLOBS_PER_BLOCK} per block. Got ${blobs.length}.`,
199
+ `Too many blobs to accumulate. The maximum is ${BLOBS_PER_CHECKPOINT} per checkpoint. Got ${blobs.length}.`,
244
200
  );
245
201
  }
246
202
 
247
203
  // Compute the hash of all the fields in the block.
248
- const blobFieldsHash = await computeBlobFieldsHashFromBlobs(blobs);
204
+ const blobFieldsHash = await computeBlobFieldsHash(blobFields);
249
205
 
250
206
  // Initialize the acc to iterate over:
251
207
  let acc: BatchedBlobAccumulator = this.clone();
252
208
  for (const blob of blobs) {
253
- acc = await acc.accumulate(blob, blobFieldsHash);
209
+ acc = await acc.accumulateBlob(blob, blobFieldsHash);
254
210
  }
255
211
  return acc;
256
212
  }
@@ -286,13 +242,22 @@ export class BatchedBlobAccumulator {
286
242
 
287
243
  const batchedBlob = new BatchedBlob(this.blobCommitmentsHashAcc, this.zAcc, this.yAcc, this.cAcc, this.qAcc);
288
244
 
289
- if (verifyProof && !batchedBlob.verify()) {
245
+ if (verifyProof && !this.verify()) {
290
246
  throw new Error(`KZG proof did not verify.`);
291
247
  }
292
248
 
293
249
  return batchedBlob;
294
250
  }
295
251
 
252
+ verify() {
253
+ return getKzg().verifyKzgProof(
254
+ this.cAcc.compress(),
255
+ this.zAcc.toBuffer(),
256
+ this.yAcc.toBuffer(),
257
+ this.qAcc.compress(),
258
+ );
259
+ }
260
+
296
261
  isEmptyState() {
297
262
  return (
298
263
  this.blobCommitmentsHashAcc.isZero() &&