@aztec-labs/blob-lib 6.0.0-nightly.20260829

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 (102) 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 +121 -0
  5. package/dest/blob.d.ts.map +1 -0
  6. package/dest/blob.js +167 -0
  7. package/dest/blob_batching.d.ts +118 -0
  8. package/dest/blob_batching.d.ts.map +1 -0
  9. package/dest/blob_batching.js +225 -0
  10. package/dest/blob_utils.d.ts +40 -0
  11. package/dest/blob_utils.d.ts.map +1 -0
  12. package/dest/blob_utils.js +69 -0
  13. package/dest/circuit_types/blob_accumulator.d.ts +23 -0
  14. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
  15. package/dest/circuit_types/blob_accumulator.js +62 -0
  16. package/dest/circuit_types/final_blob_accumulator.d.ts +23 -0
  17. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
  18. package/dest/circuit_types/final_blob_accumulator.js +66 -0
  19. package/dest/circuit_types/final_blob_batching_challenges.d.ts +16 -0
  20. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
  21. package/dest/circuit_types/final_blob_batching_challenges.js +26 -0
  22. package/dest/circuit_types/index.d.ts +4 -0
  23. package/dest/circuit_types/index.d.ts.map +1 -0
  24. package/dest/circuit_types/index.js +4 -0
  25. package/dest/encoding/block_blob_data.d.ts +26 -0
  26. package/dest/encoding/block_blob_data.d.ts.map +1 -0
  27. package/dest/encoding/block_blob_data.js +72 -0
  28. package/dest/encoding/block_end_marker.d.ts +11 -0
  29. package/dest/encoding/block_end_marker.d.ts.map +1 -0
  30. package/dest/encoding/block_end_marker.js +41 -0
  31. package/dest/encoding/block_end_state_field.d.ts +12 -0
  32. package/dest/encoding/block_end_state_field.d.ts.map +1 -0
  33. package/dest/encoding/block_end_state_field.js +39 -0
  34. package/dest/encoding/checkpoint_blob_data.d.ts +15 -0
  35. package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
  36. package/dest/encoding/checkpoint_blob_data.js +66 -0
  37. package/dest/encoding/checkpoint_end_marker.d.ts +8 -0
  38. package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -0
  39. package/dest/encoding/checkpoint_end_marker.js +28 -0
  40. package/dest/encoding/fixtures.d.ts +39 -0
  41. package/dest/encoding/fixtures.d.ts.map +1 -0
  42. package/dest/encoding/fixtures.js +139 -0
  43. package/dest/encoding/index.d.ts +10 -0
  44. package/dest/encoding/index.d.ts.map +1 -0
  45. package/dest/encoding/index.js +9 -0
  46. package/dest/encoding/tx_blob_data.d.ts +19 -0
  47. package/dest/encoding/tx_blob_data.d.ts.map +1 -0
  48. package/dest/encoding/tx_blob_data.js +79 -0
  49. package/dest/encoding/tx_start_marker.d.ts +16 -0
  50. package/dest/encoding/tx_start_marker.d.ts.map +1 -0
  51. package/dest/encoding/tx_start_marker.js +77 -0
  52. package/dest/errors.d.ts +4 -0
  53. package/dest/errors.d.ts.map +1 -0
  54. package/dest/errors.js +6 -0
  55. package/dest/hash.d.ts +44 -0
  56. package/dest/hash.d.ts.map +1 -0
  57. package/dest/hash.js +82 -0
  58. package/dest/index.d.ts +11 -0
  59. package/dest/index.d.ts.map +1 -0
  60. package/dest/index.js +10 -0
  61. package/dest/interface.d.ts +8 -0
  62. package/dest/interface.d.ts.map +1 -0
  63. package/dest/interface.js +3 -0
  64. package/dest/kzg_context.d.ts +14 -0
  65. package/dest/kzg_context.d.ts.map +1 -0
  66. package/dest/kzg_context.js +37 -0
  67. package/dest/sponge_blob.d.ts +50 -0
  68. package/dest/sponge_blob.d.ts.map +1 -0
  69. package/dest/sponge_blob.js +144 -0
  70. package/dest/testing.d.ts +31 -0
  71. package/dest/testing.d.ts.map +1 -0
  72. package/dest/testing.js +66 -0
  73. package/dest/types.d.ts +17 -0
  74. package/dest/types.d.ts.map +1 -0
  75. package/dest/types.js +4 -0
  76. package/package.json +91 -0
  77. package/src/batched_blob.ts +26 -0
  78. package/src/blob.ts +191 -0
  79. package/src/blob_batching.ts +303 -0
  80. package/src/blob_utils.ts +84 -0
  81. package/src/circuit_types/blob_accumulator.ts +96 -0
  82. package/src/circuit_types/final_blob_accumulator.ts +75 -0
  83. package/src/circuit_types/final_blob_batching_challenges.ts +30 -0
  84. package/src/circuit_types/index.ts +4 -0
  85. package/src/encoding/block_blob_data.ts +113 -0
  86. package/src/encoding/block_end_marker.ts +55 -0
  87. package/src/encoding/block_end_state_field.ts +59 -0
  88. package/src/encoding/checkpoint_blob_data.ts +100 -0
  89. package/src/encoding/checkpoint_end_marker.ts +40 -0
  90. package/src/encoding/fixtures.ts +202 -0
  91. package/src/encoding/index.ts +9 -0
  92. package/src/encoding/tx_blob_data.ts +116 -0
  93. package/src/encoding/tx_start_marker.ts +97 -0
  94. package/src/errors.ts +6 -0
  95. package/src/hash.ts +94 -0
  96. package/src/index.ts +10 -0
  97. package/src/interface.ts +7 -0
  98. package/src/kzg_context.ts +48 -0
  99. package/src/sponge_blob.ts +175 -0
  100. package/src/testing.ts +94 -0
  101. package/src/trusted_setup_bit_reversed.json +4100 -0
  102. package/src/types.ts +17 -0
@@ -0,0 +1,31 @@
1
+ import { Blob } from './blob.js';
2
+ import { BlobAccumulator } from './circuit_types/blob_accumulator.js';
3
+ import { FinalBlobAccumulator } from './circuit_types/final_blob_accumulator.js';
4
+ import { FinalBlobBatchingChallenges } from './circuit_types/final_blob_batching_challenges.js';
5
+ import { SpongeBlob } from './sponge_blob.js';
6
+ export * from './encoding/fixtures.js';
7
+ /**
8
+ * Makes arbitrary poseidon sponge for blob inputs.
9
+ * Note: will not verify inside the circuit.
10
+ * @param seed - The seed to use for generating the sponge.
11
+ * @returns A sponge blob instance.
12
+ */
13
+ export declare function makeSpongeBlob(seed?: number): SpongeBlob;
14
+ /**
15
+ * Makes arbitrary blob public accumulator.
16
+ * Note: will not verify inside the circuit.
17
+ * @param seed - The seed to use for generating the blob accumulator.
18
+ * @returns A blob accumulator instance.
19
+ */
20
+ export declare function makeBlobAccumulator(seed?: number): BlobAccumulator;
21
+ export declare function makeFinalBlobAccumulator(seed?: number): FinalBlobAccumulator;
22
+ export declare function makeFinalBlobBatchingChallenges(seed?: number): FinalBlobBatchingChallenges;
23
+ /**
24
+ * Make a blob with random fields.
25
+ *
26
+ * This will fail deserialisation in the archiver
27
+ * @param length
28
+ * @returns
29
+ */
30
+ export declare function makeRandomBlob(length: number): Promise<Blob>;
31
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdGluZy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL3Rlc3RpbmcudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBSUEsT0FBTyxFQUFFLElBQUksRUFBRSxNQUFNLFdBQVcsQ0FBQztBQUNqQyxPQUFPLEVBQUUsZUFBZSxFQUFFLE1BQU0scUNBQXFDLENBQUM7QUFDdEUsT0FBTyxFQUFFLG9CQUFvQixFQUFFLE1BQU0sMkNBQTJDLENBQUM7QUFDakYsT0FBTyxFQUFFLDJCQUEyQixFQUFFLE1BQU0sbURBQW1ELENBQUM7QUFDaEcsT0FBTyxFQUFtQixVQUFVLEVBQUUsTUFBTSxrQkFBa0IsQ0FBQztBQUUvRCxjQUFjLHdCQUF3QixDQUFDO0FBRXZDOzs7OztHQUtHO0FBQ0gsd0JBQWdCLGNBQWMsQ0FBQyxJQUFJLFNBQUksR0FBRyxVQUFVLENBVW5EO0FBMEJEOzs7OztHQUtHO0FBQ0gsd0JBQWdCLG1CQUFtQixDQUFDLElBQUksU0FBSSxHQUFHLGVBQWUsQ0FTN0Q7QUFFRCx3QkFBZ0Isd0JBQXdCLENBQUMsSUFBSSxTQUFJLHdCQU9oRDtBQUVELHdCQUFnQiwrQkFBK0IsQ0FBQyxJQUFJLFNBQUksK0JBRXZEO0FBRUQ7Ozs7OztHQU1HO0FBQ0gsd0JBQWdCLGNBQWMsQ0FBQyxNQUFNLEVBQUUsTUFBTSxHQUFHLE9BQU8sQ0FBQyxJQUFJLENBQUMsQ0FFNUQifQ==
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAC;AAChG,OAAO,EAAmB,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE/D,cAAc,wBAAwB,CAAC;AAEvC;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,SAAI,GAAG,UAAU,CAUnD;AA0BD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,SAAI,GAAG,eAAe,CAS7D;AAED,wBAAgB,wBAAwB,CAAC,IAAI,SAAI,wBAOhD;AAED,wBAAgB,+BAA+B,CAAC,IAAI,SAAI,+BAEvD;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5D"}
@@ -0,0 +1,66 @@
1
+ import { makeTuple } from '@aztec-labs/foundation/array';
2
+ import { BLS12Fq, BLS12Fr, BLS12Point, BLSPointNotOnCurveError } from '@aztec-labs/foundation/curves/bls12';
3
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
4
+ import { Blob } from './blob.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';
8
+ import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
9
+ export * from './encoding/fixtures.js';
10
+ /**
11
+ * Makes arbitrary poseidon sponge for blob inputs.
12
+ * Note: will not verify inside the circuit.
13
+ * @param seed - The seed to use for generating the sponge.
14
+ * @returns A sponge blob instance.
15
+ */ export function makeSpongeBlob(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
+ }
39
+ }
40
+ /**
41
+ * Makes arbitrary blob public accumulator.
42
+ * Note: will not verify inside the circuit.
43
+ * @param seed - The seed to use for generating the blob accumulator.
44
+ * @returns A blob accumulator instance.
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));
47
+ }
48
+ export function makeFinalBlobAccumulator(seed = 1) {
49
+ return new FinalBlobAccumulator(new Fr(seed), new Fr(seed + 0x10), new BLS12Fr(seed + 0x20), makeBLS12Point(seed + 0x30));
50
+ }
51
+ export function makeFinalBlobBatchingChallenges(seed = 1) {
52
+ return new FinalBlobBatchingChallenges(new Fr(seed), new BLS12Fr(seed + 0x10));
53
+ }
54
+ /**
55
+ * Make a blob with random fields.
56
+ *
57
+ * This will fail deserialisation in the archiver
58
+ * @param length
59
+ * @returns
60
+ */ export function makeRandomBlob(length) {
61
+ return Blob.fromFields([
62
+ ...Array.from({
63
+ length: length
64
+ }, ()=>Fr.random())
65
+ ]);
66
+ }
@@ -0,0 +1,17 @@
1
+ export * from './batched_blob.js';
2
+ export * from './circuit_types/index.js';
3
+ export * from './interface.js';
4
+ export * from './sponge_blob.js';
5
+ /**
6
+ * Type definition for the KZG instance returned by Blob.getViemKzgInstance().
7
+ * Contains the cryptographic functions needed for blob commitment and proof generation.
8
+ */
9
+ export interface BlobKzgInstance {
10
+ /** Function to compute KZG commitment from blob data */
11
+ blobToKzgCommitment(blob: Uint8Array): Uint8Array;
12
+ /** Function to compute KZG proof for blob data */
13
+ computeBlobKzgProof(blob: Uint8Array, commitment: Uint8Array): Uint8Array;
14
+ /** Function to compute both blob data cells and their corresponding KZG proofs for EIP7594 */
15
+ computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]];
16
+ }
17
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidHlwZXMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy90eXBlcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLG1CQUFtQixDQUFDO0FBQ2xDLGNBQWMsMEJBQTBCLENBQUM7QUFDekMsY0FBYyxnQkFBZ0IsQ0FBQztBQUMvQixjQUFjLGtCQUFrQixDQUFDO0FBRWpDOzs7R0FHRztBQUNILE1BQU0sV0FBVyxlQUFlO0lBQzlCLHdEQUF3RDtJQUN4RCxtQkFBbUIsQ0FBQyxJQUFJLEVBQUUsVUFBVSxHQUFHLFVBQVUsQ0FBQztJQUNsRCxrREFBa0Q7SUFDbEQsbUJBQW1CLENBQUMsSUFBSSxFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsVUFBVSxHQUFHLFVBQVUsQ0FBQztJQUMxRSw4RkFBOEY7SUFDOUYsd0JBQXdCLENBQUMsSUFBSSxFQUFFLFVBQVUsR0FBRyxDQUFDLFVBQVUsRUFBRSxFQUFFLFVBQVUsRUFBRSxDQUFDLENBQUM7Q0FDMUUifQ==
@@ -0,0 +1 @@
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 ADDED
@@ -0,0 +1,4 @@
1
+ export * from './batched_blob.js';
2
+ export * from './circuit_types/index.js';
3
+ export * from './interface.js';
4
+ export * from './sponge_blob.js';
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@aztec-labs/blob-lib",
3
+ "version": "6.0.0-nightly.20260829",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./dest/index.js",
7
+ "./encoding": "./dest/encoding/index.js",
8
+ "./types": "./dest/types.js",
9
+ "./testing": "./dest/testing.js"
10
+ },
11
+ "typedocOptions": {
12
+ "entryPoints": [
13
+ "./src/index.ts"
14
+ ],
15
+ "name": "Blob Lib",
16
+ "tsconfig": "./tsconfig.json"
17
+ },
18
+ "scripts": {
19
+ "build": "yarn clean && ../scripts/tsc.sh",
20
+ "build:dev": "../scripts/tsc.sh --watch",
21
+ "clean": "rm -rf ./dest .tsbuildinfo",
22
+ "start:dev": "concurrently -k \"../scripts/tsc.sh --watch\" \"nodemon --watch dest --exec yarn start\"",
23
+ "start": "node ./dest/index.js",
24
+ "test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
25
+ },
26
+ "inherits": [
27
+ "../package.common.json"
28
+ ],
29
+ "dependencies": {
30
+ "@aztec-labs/constants": "6.0.0-nightly.20260829",
31
+ "@aztec-labs/foundation": "6.0.0-nightly.20260829",
32
+ "@crate-crypto/node-eth-kzg": "^0.10.0",
33
+ "tslib": "^2.4.0"
34
+ },
35
+ "devDependencies": {
36
+ "@jest/globals": "^30.0.0",
37
+ "@types/jest": "^30.0.0",
38
+ "@types/node": "^22.15.17",
39
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
40
+ "get-port": "^7.1.0",
41
+ "jest": "^30.0.0",
42
+ "ts-node": "^10.9.1",
43
+ "typescript": "^5.3.3"
44
+ },
45
+ "files": [
46
+ "dest",
47
+ "src",
48
+ "!*.test.*"
49
+ ],
50
+ "types": "./dest/index.d.ts",
51
+ "jest": {
52
+ "moduleNameMapper": {
53
+ "^(\\.{1,2}/.*)\\.[cm]?js$": "$1"
54
+ },
55
+ "testRegex": "./src/.*\\.test\\.(js|mjs|ts)$",
56
+ "rootDir": "./src",
57
+ "transform": {
58
+ "^.+\\.tsx?$": [
59
+ "@swc/jest",
60
+ {
61
+ "jsc": {
62
+ "parser": {
63
+ "syntax": "typescript",
64
+ "decorators": true
65
+ },
66
+ "transform": {
67
+ "decoratorVersion": "2022-03"
68
+ }
69
+ }
70
+ }
71
+ ]
72
+ },
73
+ "extensionsToTreatAsEsm": [
74
+ ".ts"
75
+ ],
76
+ "reporters": [
77
+ "default"
78
+ ],
79
+ "testTimeout": 120000,
80
+ "setupFiles": [
81
+ "../../foundation/src/jest/setup.mjs"
82
+ ],
83
+ "testEnvironment": "../../foundation/src/jest/env.mjs",
84
+ "setupFilesAfterEnv": [
85
+ "../../foundation/src/jest/setupAfterEnv.mjs"
86
+ ]
87
+ },
88
+ "engines": {
89
+ "node": ">=20.10"
90
+ }
91
+ }
@@ -0,0 +1,26 @@
1
+ import { BLS12Fr, BLS12Point } from '@aztec-labs/foundation/curves/bls12';
2
+ import { Fr } from '@aztec-labs/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 ADDED
@@ -0,0 +1,191 @@
1
+ import { FIELDS_PER_BLOB } from '@aztec-labs/constants';
2
+ import { BLS12Fr } from '@aztec-labs/foundation/curves/bls12';
3
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
4
+ import { BufferReader, serializeToBuffer } from '@aztec-labs/foundation/serialize';
5
+
6
+ import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
7
+ import type { BlobJson } from './interface.js';
8
+ import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
9
+
10
+ export { FIELDS_PER_BLOB };
11
+
12
+ /**
13
+ * A class to create, manage, and prove EVM blobs.
14
+ *
15
+ * @dev Note: All methods in this class do not check the encoding of the given data. It's the responsibility of other
16
+ * components to ensure that the blob data (which might spread across multiple blobs) was created following the protocol
17
+ * and is correctly encoded.
18
+ */
19
+ export class Blob {
20
+ constructor(
21
+ /**
22
+ * The data to be broadcast on L1 in bytes form.
23
+ */
24
+ public readonly data: Uint8Array,
25
+ /**
26
+ * Commitment to the blob data. Used in compressed BLS12 point format (48 bytes).
27
+ */
28
+ public readonly commitment: Buffer,
29
+ ) {
30
+ if (data.length !== getBytesPerBlob()) {
31
+ throw new Error(`Blob data must be ${getBytesPerBlob()} bytes. Got ${data.length}.`);
32
+ }
33
+ if (commitment.length !== getBytesPerCommitment()) {
34
+ throw new Error(`Blob commitment must be ${getBytesPerCommitment()} bytes. Got ${commitment.length}.`);
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Create a Blob from a buffer.
40
+ * @param data - The buffer of the Blob.
41
+ * @returns A Blob created from the buffer.
42
+ *
43
+ * @throws If data does not match the expected length (getBytesPerBlob()).
44
+ */
45
+ static async fromBlobBuffer(data: Uint8Array): Promise<Blob> {
46
+ const commitment = await computeBlobCommitment(data);
47
+ return new Blob(data, commitment);
48
+ }
49
+
50
+ /**
51
+ * Create a Blob from an array of fields.
52
+ *
53
+ * @dev This method pads 0s to the data, extending it to the size of a full blob.
54
+ *
55
+ * @param fields - The array of fields to create the Blob from.
56
+ * @returns A Blob created from the array of fields.
57
+ */
58
+ static async fromFields(fields: Fr[]): Promise<Blob> {
59
+ if (fields.length > FIELDS_PER_BLOB) {
60
+ throw new Error(`Attempted to overfill blob with ${fields.length} fields. The maximum is ${FIELDS_PER_BLOB}.`);
61
+ }
62
+
63
+ const data = Buffer.concat([serializeToBuffer(fields)], getBytesPerBlob());
64
+ const commitment = await computeBlobCommitment(data);
65
+ return new Blob(data, commitment);
66
+ }
67
+
68
+ /**
69
+ * Get the fields from the blob data.
70
+ *
71
+ * @dev WARNING: this method returns all fields
72
+ *
73
+ * @returns The fields from the blob.
74
+ */
75
+ toFields(): Fr[] {
76
+ const reader = BufferReader.asReader(this.data);
77
+ const numTotalFields = this.data.length / Fr.SIZE_IN_BYTES;
78
+ return reader.readArray(numTotalFields, Fr);
79
+ }
80
+
81
+ /**
82
+ * Create a Blob from a JSON object.
83
+ *
84
+ * Blobs will be in this form when requested from the blob client, or from
85
+ * the beacon chain via `getBlobSidecars`
86
+ * https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
87
+ *
88
+ * @param json - The JSON object to create the Blob from.
89
+ * @returns A Blob created from the JSON object.
90
+ */
91
+ static async fromJson(json: BlobJson): Promise<Blob> {
92
+ const blobBuffer = Buffer.from(json.blob.slice(2), 'hex');
93
+ const blob = await Blob.fromBlobBuffer(blobBuffer);
94
+
95
+ if (blob.commitment.toString('hex') !== json.kzg_commitment.slice(2)) {
96
+ throw new Error('KZG commitment does not match');
97
+ }
98
+
99
+ return blob;
100
+ }
101
+
102
+ /**
103
+ * Get the JSON representation of the blob.
104
+ *
105
+ * @returns The JSON representation of the blob.
106
+ */
107
+ toJSON(): BlobJson {
108
+ return {
109
+ blob: `0x${Buffer.from(this.data).toString('hex')}`,
110
+ // eslint-disable-next-line camelcase
111
+ kzg_commitment: `0x${this.commitment.toString('hex')}`,
112
+ };
113
+ }
114
+
115
+ getEthVersionedBlobHash(): Buffer {
116
+ return computeEthVersionedBlobHash(this.commitment);
117
+ }
118
+
119
+ /**
120
+ * Challenge point z (= H(H(tx_effects), kzgCommitment)).
121
+ * Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts).
122
+ */
123
+ async computeChallengeZ(blobFieldsHash: Fr): Promise<Fr> {
124
+ return await computeChallengeZ(blobFieldsHash, this.commitment);
125
+ }
126
+
127
+ /**
128
+ * Evaluate the blob at a given challenge and return the evaluation and KZG proof.
129
+ *
130
+ * @param challengeZ - The challenge z at which to evaluate the blob.
131
+ * @param verifyProof - Whether to verify the KZG proof.
132
+ *
133
+ * @returns
134
+ * y: BLS12Fr - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts.
135
+ * proof: Buffer - KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes).
136
+ */
137
+ async evaluate(challengeZ: Fr, verifyProof = false) {
138
+ const kzg = getKzg();
139
+ const res = await kzg.asyncComputeKzgProof(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.`);
142
+ }
143
+
144
+ const proof = Buffer.from(res[0]);
145
+ const y = BLS12Fr.fromBuffer(Buffer.from(res[1]));
146
+ return { y, proof };
147
+ }
148
+
149
+ /**
150
+ * Get the buffer representation of the ENTIRE blob.
151
+ *
152
+ * @dev WARNING: this buffer contains all metadata as well as the data itself.
153
+ *
154
+ * @returns The buffer representation of the blob.
155
+ */
156
+ toBuffer(): Buffer {
157
+ return Buffer.from(serializeToBuffer(this.data.length, this.data, this.commitment.length, this.commitment));
158
+ }
159
+
160
+ /**
161
+ * Create a Blob from a buffer.
162
+ *
163
+ * @dev WARNING: this method contains all metadata as well as the data itself.
164
+ *
165
+ * @param buf - The buffer to create the Blob from.
166
+ * @returns A Blob created from the buffer.
167
+ */
168
+ static fromBuffer(buf: Buffer | BufferReader): Blob {
169
+ const reader = BufferReader.asReader(buf);
170
+ return new Blob(reader.readUint8Array(), reader.readBuffer());
171
+ }
172
+
173
+ /**
174
+ * Get the size of the blob in bytes
175
+ */
176
+ getSize() {
177
+ return this.data.length;
178
+ }
179
+
180
+ static getViemKzgInstance() {
181
+ const kzg = getKzg();
182
+ return {
183
+ blobToKzgCommitment: kzg.blobToKzgCommitment.bind(kzg),
184
+ computeBlobKzgProof: kzg.computeBlobKzgProof.bind(kzg),
185
+ computeCellsAndKzgProofs: (b: Uint8Array): [Uint8Array[], Uint8Array[]] => {
186
+ const result = kzg.computeCellsAndKzgProofs(b);
187
+ return [result.cells, result.proofs];
188
+ },
189
+ };
190
+ }
191
+ }