@aztec/blob-lib 0.0.1-fake-ceab37513c → 0.0.6-commit.a2d1860fe9

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 (106) 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 +56 -102
  5. package/dest/blob.d.ts.map +1 -1
  6. package/dest/blob.js +77 -170
  7. package/dest/blob_batching.d.ts +41 -111
  8. package/dest/blob_batching.d.ts.map +1 -1
  9. package/dest/blob_batching.js +129 -203
  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 +30 -0
  26. package/dest/encoding/block_blob_data.d.ts.map +1 -0
  27. package/dest/encoding/block_blob_data.js +75 -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 +67 -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 +41 -0
  41. package/dest/encoding/fixtures.d.ts.map +1 -0
  42. package/dest/encoding/fixtures.js +140 -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 +1 -1
  53. package/dest/errors.d.ts.map +1 -1
  54. package/dest/hash.d.ts +43 -0
  55. package/dest/hash.d.ts.map +1 -0
  56. package/dest/hash.js +80 -0
  57. package/dest/index.d.ts +7 -4
  58. package/dest/index.d.ts.map +1 -1
  59. package/dest/index.js +6 -16
  60. package/dest/interface.d.ts +1 -2
  61. package/dest/interface.d.ts.map +1 -1
  62. package/dest/kzg_context.d.ts +8 -0
  63. package/dest/kzg_context.d.ts.map +1 -0
  64. package/dest/kzg_context.js +14 -0
  65. package/dest/sponge_blob.d.ts +13 -13
  66. package/dest/sponge_blob.d.ts.map +1 -1
  67. package/dest/sponge_blob.js +26 -30
  68. package/dest/testing.d.ts +10 -23
  69. package/dest/testing.d.ts.map +1 -1
  70. package/dest/testing.js +37 -53
  71. package/dest/types.d.ts +4 -1
  72. package/dest/types.d.ts.map +1 -1
  73. package/dest/types.js +3 -0
  74. package/package.json +9 -7
  75. package/src/batched_blob.ts +26 -0
  76. package/src/blob.ts +80 -201
  77. package/src/blob_batching.ts +168 -231
  78. package/src/blob_utils.ts +84 -0
  79. package/src/circuit_types/blob_accumulator.ts +96 -0
  80. package/src/circuit_types/final_blob_accumulator.ts +76 -0
  81. package/src/circuit_types/final_blob_batching_challenges.ts +30 -0
  82. package/src/circuit_types/index.ts +4 -0
  83. package/src/encoding/block_blob_data.ts +114 -0
  84. package/src/encoding/block_end_marker.ts +55 -0
  85. package/src/encoding/block_end_state_field.ts +59 -0
  86. package/src/encoding/checkpoint_blob_data.ts +102 -0
  87. package/src/encoding/checkpoint_end_marker.ts +40 -0
  88. package/src/encoding/fixtures.ts +210 -0
  89. package/src/encoding/index.ts +9 -0
  90. package/src/encoding/tx_blob_data.ts +116 -0
  91. package/src/encoding/tx_start_marker.ts +97 -0
  92. package/src/hash.ts +89 -0
  93. package/src/index.ts +6 -19
  94. package/src/interface.ts +0 -1
  95. package/src/kzg_context.ts +16 -0
  96. package/src/sponge_blob.ts +28 -31
  97. package/src/testing.ts +48 -59
  98. package/src/types.ts +3 -2
  99. package/dest/blob_batching_public_inputs.d.ts +0 -71
  100. package/dest/blob_batching_public_inputs.d.ts.map +0 -1
  101. package/dest/blob_batching_public_inputs.js +0 -168
  102. package/dest/encoding.d.ts +0 -66
  103. package/dest/encoding.d.ts.map +0 -1
  104. package/dest/encoding.js +0 -113
  105. package/src/blob_batching_public_inputs.ts +0 -252
  106. package/src/encoding.ts +0 -138
@@ -0,0 +1,31 @@
1
+ import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { FinalBlobAccumulator } from './circuit_types/index.js';
4
+ /**
5
+ * A class to represent the result from accumulating blobs in an epoch using BatchedBlobAccumulator.
6
+ */
7
+ export declare class BatchedBlob {
8
+ /** Hash of Cs (to link to L1 blob hashes). */
9
+ readonly blobCommitmentsHash: Fr;
10
+ /** Challenge point z such that p_i(z) = y_i. */
11
+ readonly z: Fr;
12
+ /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
13
+ readonly y: BLS12Fr;
14
+ /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
15
+ readonly commitment: BLS12Point;
16
+ /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
17
+ readonly q: BLS12Point;
18
+ constructor(
19
+ /** Hash of Cs (to link to L1 blob hashes). */
20
+ blobCommitmentsHash: Fr,
21
+ /** Challenge point z such that p_i(z) = y_i. */
22
+ z: Fr,
23
+ /** Evaluation y, linear combination of all evaluations y_i = p_i(z) with gamma. */
24
+ y: BLS12Fr,
25
+ /** Commitment C, linear combination of all commitments C_i = [p_i] with gamma. */
26
+ commitment: BLS12Point,
27
+ /** KZG opening 'proof' Q (commitment to the quotient poly.), linear combination of all blob kzg 'proofs' Q_i with gamma. */
28
+ q: BLS12Point);
29
+ toFinalBlobAccumulator(): FinalBlobAccumulator;
30
+ }
31
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmF0Y2hlZF9ibG9iLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvYmF0Y2hlZF9ibG9iLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sZ0NBQWdDLENBQUM7QUFDckUsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLGdDQUFnQyxDQUFDO0FBRXBELE9BQU8sRUFBRSxvQkFBb0IsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBRWhFOztHQUVHO0FBQ0gscUJBQWEsV0FBVztJQUVwQiw4Q0FBOEM7YUFDOUIsbUJBQW1CLEVBQUUsRUFBRTtJQUN2QyxnREFBZ0Q7YUFDaEMsQ0FBQyxFQUFFLEVBQUU7SUF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@ -0,0 +1,20 @@
1
+ import { FinalBlobAccumulator } from './circuit_types/index.js';
2
+ /**
3
+ * A class to represent the result from accumulating blobs in an epoch using BatchedBlobAccumulator.
4
+ */ export class BatchedBlob {
5
+ blobCommitmentsHash;
6
+ z;
7
+ y;
8
+ commitment;
9
+ q;
10
+ 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){
11
+ this.blobCommitmentsHash = blobCommitmentsHash;
12
+ this.z = z;
13
+ this.y = y;
14
+ this.commitment = commitment;
15
+ this.q = q;
16
+ }
17
+ toFinalBlobAccumulator() {
18
+ return new FinalBlobAccumulator(this.blobCommitmentsHash, this.z, this.y, this.commitment);
19
+ }
20
+ }
package/dest/blob.d.ts CHANGED
@@ -1,63 +1,66 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { FIELDS_PER_BLOB } from '@aztec/constants';
2
+ import { BLS12Fr } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
4
  import { BufferReader } from '@aztec/foundation/serialize';
3
- import cKzg from 'c-kzg';
4
- import type { Blob as BlobBuffer } from 'c-kzg';
5
5
  import type { BlobJson } from './interface.js';
6
- export declare const VERSIONED_HASH_VERSION_KZG = 1;
7
- /** Versioned blob hash for an empty blob */
8
- export declare const EMPTY_BLOB_VERSIONED_HASH: Buffer<ArrayBuffer>;
6
+ export { FIELDS_PER_BLOB };
9
7
  /**
10
8
  * A class to create, manage, and prove EVM blobs.
9
+ *
10
+ * @dev Note: All methods in this class do not check the encoding of the given data. It's the responsibility of other
11
+ * components to ensure that the blob data (which might spread across multiple blobs) was created following the protocol
12
+ * and is correctly encoded.
11
13
  */
12
14
  export declare class Blob {
13
- /** The blob to be broadcast on L1 in bytes form. */
14
- readonly data: BlobBuffer;
15
- /** The hash of all tx effects inside the blob. Used in generating the challenge z and proving that we have included all required effects. */
16
- readonly fieldsHash: Fr;
17
- /** Challenge point z (= H(H(tx_effects), kzgCommmitment). Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts). */
18
- readonly challengeZ: Fr;
19
- /** Commitment to the blob C. Used in compressed BLS12 point format (48 bytes). */
15
+ /**
16
+ * The data to be broadcast on L1 in bytes form.
17
+ */
18
+ readonly data: Uint8Array;
19
+ /**
20
+ * Commitment to the blob data. Used in compressed BLS12 point format (48 bytes).
21
+ */
20
22
  readonly commitment: Buffer;
21
23
  constructor(
22
- /** The blob to be broadcast on L1 in bytes form. */
23
- data: BlobBuffer,
24
- /** The hash of all tx effects inside the blob. Used in generating the challenge z and proving that we have included all required effects. */
25
- fieldsHash: Fr,
26
- /** Challenge point z (= H(H(tx_effects), kzgCommmitment). Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts). */
27
- challengeZ: Fr,
28
- /** Commitment to the blob C. Used in compressed BLS12 point format (48 bytes). */
24
+ /**
25
+ * The data to be broadcast on L1 in bytes form.
26
+ */
27
+ data: Uint8Array,
28
+ /**
29
+ * Commitment to the blob data. Used in compressed BLS12 point format (48 bytes).
30
+ */
29
31
  commitment: Buffer);
30
32
  /**
31
- * The encoded version of the blob will determine the end of the blob based on the transaction encoding.
32
- * This is required when the fieldsHash of a blob will contain trailing zeros.
33
- *
34
- * See `./encoding.ts` for more details.
35
- *
36
- * This method is used to create a Blob from a buffer.
37
- * @param blob - The buffer to create the Blob from.
38
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
33
+ * Create a Blob from a buffer.
34
+ * @param data - The buffer of the Blob.
39
35
  * @returns A Blob created from the buffer.
40
36
  *
41
- * @throws If unable to deserialize the blob.
37
+ * @throws If data does not match the expected length (BYTES_PER_BLOB).
42
38
  */
43
- static fromEncodedBlobBuffer(blob: BlobBuffer, multiBlobFieldsHash?: Fr): Promise<Blob>;
39
+ static fromBlobBuffer(data: Uint8Array): Promise<Blob>;
44
40
  /**
45
41
  * Create a Blob from an array of fields.
46
42
  *
43
+ * @dev This method pads 0s to the data, extending it to the size of a full blob.
44
+ *
47
45
  * @param fields - The array of fields to create the Blob from.
48
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
49
46
  * @returns A Blob created from the array of fields.
50
47
  */
51
- static fromFields(fields: Fr[], multiBlobFieldsHash?: Fr): Promise<Blob>;
48
+ static fromFields(fields: Fr[]): Promise<Blob>;
49
+ /**
50
+ * Get the fields from the blob data.
51
+ *
52
+ * @dev WARNING: this method returns all fields
53
+ *
54
+ * @returns The fields from the blob.
55
+ */
56
+ toFields(): Fr[];
52
57
  /**
53
58
  * Create a Blob from a JSON object.
54
59
  *
55
- * Blobs will be in this form when requested from the blob sink, or from
60
+ * Blobs will be in this form when requested from the blob client, or from
56
61
  * the beacon chain via `getBlobSidecars`
57
62
  * https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
58
63
  *
59
- * @dev WARNING: by default json deals with encoded buffers
60
- *
61
64
  * @param json - The JSON object to create the Blob from.
62
65
  * @returns A Blob created from the JSON object.
63
66
  */
@@ -65,69 +68,33 @@ export declare class Blob {
65
68
  /**
66
69
  * Get the JSON representation of the blob.
67
70
  *
68
- * @dev WARNING: by default json deals with encoded buffers
69
- * @param index - optional - The index of the blob in the block.
70
71
  * @returns The JSON representation of the blob.
71
72
  */
72
- toJson(index: number): BlobJson;
73
- /**
74
- * Get the fields from the blob.
75
- *
76
- * @dev WARNING: this method does not take into account trailing zeros
77
- *
78
- * @returns The fields from the blob.
79
- */
80
- toFields(): Fr[];
81
- /**
82
- * Get the encoded fields from the blob.
83
- *
84
- * @dev This method takes into account trailing zeros
85
- *
86
- * @returns The encoded fields from the blob.
87
- *
88
- * @throws If unable to deserialize the blob.
89
- */
90
- toEncodedFields(): Fr[];
91
- /**
92
- * Get the encoded fields from multiple blobs.
93
- *
94
- * @dev This method takes into account trailing zeros
95
- *
96
- * @returns The encoded fields from the blobs.
97
- */
98
- static toEncodedFields(blobs: Blob[]): Fr[];
73
+ toJSON(): BlobJson;
74
+ getEthVersionedBlobHash(): Buffer;
99
75
  /**
100
- * Get the commitment fields from the blob.
101
- *
102
- * The 48-byte commitment is encoded into two field elements:
103
- * +------------------+------------------+
104
- * | Field Element 1 | Field Element 2 |
105
- * | [bytes 0-31] | [bytes 32-47] |
106
- * +------------------+------------------+
107
- * | 32 bytes | 16 bytes |
108
- * +------------------+------------------+
109
- * @returns The commitment fields from the blob.
76
+ * Challenge point z (= H(H(tx_effects), kzgCommitment)).
77
+ * Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts).
110
78
  */
111
- commitmentToFields(): [Fr, Fr];
112
- getEthVersionedBlobHash(): Buffer;
113
- static getEthVersionedBlobHash(commitment: Buffer): Buffer;
79
+ computeChallengeZ(blobFieldsHash: Fr): Promise<Fr>;
114
80
  /**
115
81
  * Evaluate the blob at a given challenge and return the evaluation and KZG proof.
116
82
  *
117
- * @param challengeZ - The challenge z at which to evaluate the blob. If not given, assume we want to evaluate at the individual blob's z.
83
+ * @param challengeZ - The challenge z at which to evaluate the blob.
84
+ * @param verifyProof - Whether to verify the KZG proof.
118
85
  *
119
- * @returns -
120
- * y: Buffer - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts
86
+ * @returns
87
+ * y: BLS12Fr - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts.
121
88
  * proof: Buffer - KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes).
122
89
  */
123
- evaluate(challengeZ?: Fr): {
124
- y: Buffer<ArrayBuffer>;
90
+ evaluate(challengeZ: Fr, verifyProof?: boolean): Promise<{
91
+ y: BLS12Fr;
125
92
  proof: Buffer<ArrayBuffer>;
126
- };
93
+ }>;
127
94
  /**
128
95
  * Get the buffer representation of the ENTIRE blob.
129
96
  *
130
- * @dev WARNING: this buffer contains all metadata aswell as the data itself
97
+ * @dev WARNING: this buffer contains all metadata as well as the data itself.
131
98
  *
132
99
  * @returns The buffer representation of the blob.
133
100
  */
@@ -135,7 +102,7 @@ export declare class Blob {
135
102
  /**
136
103
  * Create a Blob from a buffer.
137
104
  *
138
- * @dev WARNING: this method contains all metadata aswell as the data itself
105
+ * @dev WARNING: this method contains all metadata as well as the data itself.
139
106
  *
140
107
  * @param buf - The buffer to create the Blob from.
141
108
  * @returns A Blob created from the buffer.
@@ -145,23 +112,10 @@ export declare class Blob {
145
112
  * Get the size of the blob in bytes
146
113
  */
147
114
  getSize(): number;
148
- /**
149
- * @param blobs - The blobs to emit
150
- * @returns The blobs' compressed commitments in hex prefixed by the number of blobs
151
- * @dev Used for proposing blocks to validate injected blob commitments match real broadcast blobs:
152
- * One byte for the number blobs + 48 bytes per blob commitment
153
- */
154
- static getPrefixedEthBlobCommitments(blobs: Blob[]): `0x${string}`;
155
115
  static getViemKzgInstance(): {
156
- blobToKzgCommitment: typeof cKzg.blobToKzgCommitment;
157
- computeBlobKzgProof: typeof cKzg.computeBlobKzgProof;
158
- computeCellsAndKzgProofs: typeof cKzg.computeCellsAndKzgProofs;
116
+ blobToKzgCommitment: (blob: Uint8Array<ArrayBufferLike>) => Uint8Array<ArrayBufferLike>;
117
+ computeBlobKzgProof: (blob: Uint8Array<ArrayBufferLike>, commitment: Uint8Array<ArrayBufferLike>) => Uint8Array<ArrayBufferLike>;
118
+ computeCellsAndKzgProofs: (b: Uint8Array<ArrayBufferLike>) => [Uint8Array<ArrayBufferLike>[], Uint8Array<ArrayBufferLike>[]];
159
119
  };
160
- /**
161
- * @param fields - Fields to broadcast in the blob(s)
162
- * @returns As many blobs as we require to broadcast the given fields for a block
163
- * @dev Assumes we share the fields hash between all blobs which can only be done for ONE BLOCK because the hash is calculated in block root.
164
- */
165
- static getBlobsP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,GAAG,OAAO,CAAC,IAAI,CAAC,CASnD;IAED;;;;OAIG;IACH,MAAM,IAAI,QAAQ,CAMjB;IAED,uBAAuB,IAAI,MAAM,CAEhC;IAED;;;OAGG;IACG,iBAAiB,CAAC,cAAc,EAAE,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,CAEvD;IAED;;;;;;;;;OASG;IACG,QAAQ,CAAC,UAAU,EAAE,EAAE,EAAE,WAAW,UAAQ;;;OAUjD;IAED;;;;;;OAMG;IACH,QAAQ,IAAI,MAAM,CAEjB;IAED;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,GAAG,IAAI,CAGlD;IAED;;OAEG;IACH,OAAO,WAEN;IAED,MAAM,CAAC,kBAAkB;;;;MAUxB;CACF"}
package/dest/blob.js CHANGED
@@ -1,182 +1,125 @@
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 } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
- // Importing directly from 'c-kzg' does not work:
5
- import cKzg from 'c-kzg';
6
- import { deserializeEncodedBlobToFields, extractBlobFieldsFromBuffer } from './encoding.js';
7
- import { BlobDeserializationError } from './errors.js';
8
- const { BYTES_PER_BLOB, FIELD_ELEMENTS_PER_BLOB, blobToKzgCommitment, computeKzgProof, verifyKzgProof } = cKzg;
9
- // The prefix to the EVM blobHash, defined here: https://eips.ethereum.org/EIPS/eip-4844#specification
10
- export const VERSIONED_HASH_VERSION_KZG = 0x01;
11
- /** Versioned blob hash for an empty blob */ export const EMPTY_BLOB_VERSIONED_HASH = Buffer.from(`010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c444014`, 'hex');
5
+ import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
6
+ import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, getKzg } from './kzg_context.js';
7
+ export { FIELDS_PER_BLOB };
12
8
  /**
13
9
  * A class to create, manage, and prove EVM blobs.
10
+ *
11
+ * @dev Note: All methods in this class do not check the encoding of the given data. It's the responsibility of other
12
+ * components to ensure that the blob data (which might spread across multiple blobs) was created following the protocol
13
+ * and is correctly encoded.
14
14
  */ export class Blob {
15
15
  data;
16
- fieldsHash;
17
- challengeZ;
18
16
  commitment;
19
- constructor(/** The blob to be broadcast on L1 in bytes form. */ data, /** The hash of all tx effects inside the blob. Used in generating the challenge z and proving that we have included all required effects. */ fieldsHash, /** Challenge point z (= H(H(tx_effects), kzgCommmitment). Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts). */ challengeZ, /** Commitment to the blob C. Used in compressed BLS12 point format (48 bytes). */ commitment){
17
+ constructor(/**
18
+ * The data to be broadcast on L1 in bytes form.
19
+ */ data, /**
20
+ * Commitment to the blob data. Used in compressed BLS12 point format (48 bytes).
21
+ */ commitment){
20
22
  this.data = data;
21
- this.fieldsHash = fieldsHash;
22
- this.challengeZ = challengeZ;
23
23
  this.commitment = commitment;
24
+ if (data.length !== BYTES_PER_BLOB) {
25
+ throw new Error(`Blob data must be ${BYTES_PER_BLOB} bytes. Got ${data.length}.`);
26
+ }
27
+ if (commitment.length !== BYTES_PER_COMMITMENT) {
28
+ throw new Error(`Blob commitment must be ${BYTES_PER_COMMITMENT} bytes. Got ${commitment.length}.`);
29
+ }
24
30
  }
25
31
  /**
26
- * The encoded version of the blob will determine the end of the blob based on the transaction encoding.
27
- * This is required when the fieldsHash of a blob will contain trailing zeros.
28
- *
29
- * See `./encoding.ts` for more details.
30
- *
31
- * This method is used to create a Blob from a buffer.
32
- * @param blob - The buffer to create the Blob from.
33
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
32
+ * Create a Blob from a buffer.
33
+ * @param data - The buffer of the Blob.
34
34
  * @returns A Blob created from the buffer.
35
35
  *
36
- * @throws If unable to deserialize the blob.
37
- */ static fromEncodedBlobBuffer(blob, multiBlobFieldsHash) {
38
- try {
39
- const fields = deserializeEncodedBlobToFields(blob);
40
- return Blob.fromFields(fields, multiBlobFieldsHash);
41
- } catch {
42
- throw new BlobDeserializationError(`Failed to create Blob from encoded blob buffer, this blob was likely not created by us`);
43
- }
36
+ * @throws If data does not match the expected length (BYTES_PER_BLOB).
37
+ */ static async fromBlobBuffer(data) {
38
+ const commitment = await computeBlobCommitment(data);
39
+ return new Blob(data, commitment);
44
40
  }
45
41
  /**
46
42
  * Create a Blob from an array of fields.
47
43
  *
44
+ * @dev This method pads 0s to the data, extending it to the size of a full blob.
45
+ *
48
46
  * @param fields - The array of fields to create the Blob from.
49
- * @param multiBlobFieldsHash - The fields hash to use for the Blob.
50
47
  * @returns A Blob created from the array of fields.
51
- */ static async fromFields(fields, multiBlobFieldsHash) {
52
- if (fields.length > FIELD_ELEMENTS_PER_BLOB) {
53
- throw new Error(`Attempted to overfill blob with ${fields.length} elements. The maximum is ${FIELD_ELEMENTS_PER_BLOB}`);
48
+ */ static async fromFields(fields) {
49
+ if (fields.length > FIELDS_PER_BLOB) {
50
+ throw new Error(`Attempted to overfill blob with ${fields.length} fields. The maximum is ${FIELDS_PER_BLOB}.`);
54
51
  }
55
52
  const data = Buffer.concat([
56
53
  serializeToBuffer(fields)
57
54
  ], BYTES_PER_BLOB);
58
- // This matches the output of SpongeBlob.squeeze() in the blob circuit
59
- const fieldsHash = multiBlobFieldsHash ? multiBlobFieldsHash : await poseidon2Hash(fields);
60
- const commitment = Buffer.from(blobToKzgCommitment(data));
61
- const challengeZ = await poseidon2Hash([
62
- fieldsHash,
63
- ...commitmentToFields(commitment)
64
- ]);
65
- return new Blob(data, fieldsHash, challengeZ, commitment);
55
+ const commitment = await computeBlobCommitment(data);
56
+ return new Blob(data, commitment);
57
+ }
58
+ /**
59
+ * Get the fields from the blob data.
60
+ *
61
+ * @dev WARNING: this method returns all fields
62
+ *
63
+ * @returns The fields from the blob.
64
+ */ toFields() {
65
+ const reader = BufferReader.asReader(this.data);
66
+ const numTotalFields = this.data.length / Fr.SIZE_IN_BYTES;
67
+ return reader.readArray(numTotalFields, Fr);
66
68
  }
67
69
  /**
68
70
  * Create a Blob from a JSON object.
69
71
  *
70
- * Blobs will be in this form when requested from the blob sink, or from
72
+ * Blobs will be in this form when requested from the blob client, or from
71
73
  * the beacon chain via `getBlobSidecars`
72
74
  * https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
73
75
  *
74
- * @dev WARNING: by default json deals with encoded buffers
75
- *
76
76
  * @param json - The JSON object to create the Blob from.
77
77
  * @returns A Blob created from the JSON object.
78
78
  */ static async fromJson(json) {
79
79
  const blobBuffer = Buffer.from(json.blob.slice(2), 'hex');
80
- const blob = await Blob.fromEncodedBlobBuffer(blobBuffer);
80
+ const blob = await Blob.fromBlobBuffer(blobBuffer);
81
81
  if (blob.commitment.toString('hex') !== json.kzg_commitment.slice(2)) {
82
82
  throw new Error('KZG commitment does not match');
83
83
  }
84
- // We do not check the proof, as it will be different if the challenge is shared
85
- // across multiple blobs
86
84
  return blob;
87
85
  }
88
86
  /**
89
87
  * Get the JSON representation of the blob.
90
88
  *
91
- * @dev WARNING: by default json deals with encoded buffers
92
- * @param index - optional - The index of the blob in the block.
93
89
  * @returns The JSON representation of the blob.
94
- */ toJson(index) {
90
+ */ toJSON() {
95
91
  return {
96
92
  blob: `0x${Buffer.from(this.data).toString('hex')}`,
97
- index: index.toString(),
98
93
  // eslint-disable-next-line camelcase
99
94
  kzg_commitment: `0x${this.commitment.toString('hex')}`
100
95
  };
101
96
  }
102
- /**
103
- * Get the fields from the blob.
104
- *
105
- * @dev WARNING: this method does not take into account trailing zeros
106
- *
107
- * @returns The fields from the blob.
108
- */ toFields() {
109
- return extractBlobFieldsFromBuffer(this.data);
110
- }
111
- /**
112
- * Get the encoded fields from the blob.
113
- *
114
- * @dev This method takes into account trailing zeros
115
- *
116
- * @returns The encoded fields from the blob.
117
- *
118
- * @throws If unable to deserialize the blob.
119
- */ toEncodedFields() {
120
- try {
121
- return deserializeEncodedBlobToFields(this.data);
122
- } catch {
123
- throw new BlobDeserializationError(`Failed to deserialize encoded blob fields, this blob was likely not created by us`);
124
- }
125
- }
126
- /**
127
- * Get the encoded fields from multiple blobs.
128
- *
129
- * @dev This method takes into account trailing zeros
130
- *
131
- * @returns The encoded fields from the blobs.
132
- */ static toEncodedFields(blobs) {
133
- try {
134
- return deserializeEncodedBlobToFields(Buffer.concat(blobs.map((b)=>b.data)));
135
- } catch {
136
- throw new BlobDeserializationError(`Failed to deserialize encoded blob fields, this blob was likely not created by us`);
137
- }
138
- }
139
- /**
140
- * Get the commitment fields from the blob.
141
- *
142
- * The 48-byte commitment is encoded into two field elements:
143
- * +------------------+------------------+
144
- * | Field Element 1 | Field Element 2 |
145
- * | [bytes 0-31] | [bytes 32-47] |
146
- * +------------------+------------------+
147
- * | 32 bytes | 16 bytes |
148
- * +------------------+------------------+
149
- * @returns The commitment fields from the blob.
150
- */ commitmentToFields() {
151
- return commitmentToFields(this.commitment);
152
- }
153
- // Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
154
97
  getEthVersionedBlobHash() {
155
- const hash = sha256(this.commitment);
156
- hash[0] = VERSIONED_HASH_VERSION_KZG;
157
- return hash;
98
+ return computeEthVersionedBlobHash(this.commitment);
158
99
  }
159
- static getEthVersionedBlobHash(commitment) {
160
- const hash = sha256(commitment);
161
- hash[0] = VERSIONED_HASH_VERSION_KZG;
162
- return hash;
100
+ /**
101
+ * Challenge point z (= H(H(tx_effects), kzgCommitment)).
102
+ * Used such that p(z) = y for a single blob, used as z_i in batching (see ./blob_batching.ts).
103
+ */ async computeChallengeZ(blobFieldsHash) {
104
+ return await computeChallengeZ(blobFieldsHash, this.commitment);
163
105
  }
164
106
  /**
165
107
  * Evaluate the blob at a given challenge and return the evaluation and KZG proof.
166
108
  *
167
- * @param challengeZ - The challenge z at which to evaluate the blob. If not given, assume we want to evaluate at the individual blob's z.
109
+ * @param challengeZ - The challenge z at which to evaluate the blob.
110
+ * @param verifyProof - Whether to verify the KZG proof.
168
111
  *
169
- * @returns -
170
- * y: Buffer - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts
112
+ * @returns
113
+ * y: BLS12Fr - Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts.
171
114
  * proof: Buffer - KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes).
172
- */ evaluate(challengeZ) {
173
- const z = challengeZ || this.challengeZ;
174
- const res = computeKzgProof(this.data, z.toBuffer());
175
- if (!verifyKzgProof(this.commitment, z.toBuffer(), res[1], res[0])) {
115
+ */ async evaluate(challengeZ, verifyProof = false) {
116
+ const kzg = getKzg();
117
+ const res = await kzg.asyncComputeKzgProof(this.data, challengeZ.toBuffer());
118
+ if (verifyProof && !kzg.verifyKzgProof(this.commitment, challengeZ.toBuffer(), res[1], res[0])) {
176
119
  throw new Error(`KZG proof did not verify.`);
177
120
  }
178
121
  const proof = Buffer.from(res[0]);
179
- const y = Buffer.from(res[1]);
122
+ const y = BLS12Fr.fromBuffer(Buffer.from(res[1]));
180
123
  return {
181
124
  y,
182
125
  proof
@@ -185,76 +128,40 @@ export const VERSIONED_HASH_VERSION_KZG = 0x01;
185
128
  /**
186
129
  * Get the buffer representation of the ENTIRE blob.
187
130
  *
188
- * @dev WARNING: this buffer contains all metadata aswell as the data itself
131
+ * @dev WARNING: this buffer contains all metadata as well as the data itself.
189
132
  *
190
133
  * @returns The buffer representation of the blob.
191
134
  */ toBuffer() {
192
- return Buffer.from(serializeToBuffer(this.data.length, this.data, this.fieldsHash, this.challengeZ, this.commitment.length, this.commitment));
135
+ return Buffer.from(serializeToBuffer(this.data.length, this.data, this.commitment.length, this.commitment));
193
136
  }
194
137
  /**
195
138
  * Create a Blob from a buffer.
196
139
  *
197
- * @dev WARNING: this method contains all metadata aswell as the data itself
140
+ * @dev WARNING: this method contains all metadata as well as the data itself.
198
141
  *
199
142
  * @param buf - The buffer to create the Blob from.
200
143
  * @returns A Blob created from the buffer.
201
144
  */ static fromBuffer(buf) {
202
145
  const reader = BufferReader.asReader(buf);
203
- return new Blob(reader.readUint8Array(), reader.readObject(Fr), reader.readObject(Fr), reader.readBuffer());
146
+ return new Blob(reader.readUint8Array(), reader.readBuffer());
204
147
  }
205
148
  /**
206
149
  * Get the size of the blob in bytes
207
150
  */ getSize() {
208
151
  return this.data.length;
209
152
  }
210
- /**
211
- * @param blobs - The blobs to emit
212
- * @returns The blobs' compressed commitments in hex prefixed by the number of blobs
213
- * @dev Used for proposing blocks to validate injected blob commitments match real broadcast blobs:
214
- * One byte for the number blobs + 48 bytes per blob commitment
215
- */ static getPrefixedEthBlobCommitments(blobs) {
216
- let buf = Buffer.alloc(0);
217
- blobs.forEach((blob)=>{
218
- buf = Buffer.concat([
219
- buf,
220
- blob.commitment
221
- ]);
222
- });
223
- // We prefix the number of blobs:
224
- const lenBuf = Buffer.alloc(1);
225
- lenBuf.writeUint8(blobs.length);
226
- buf = Buffer.concat([
227
- lenBuf,
228
- buf
229
- ]);
230
- return `0x${buf.toString('hex')}`;
231
- }
232
153
  static getViemKzgInstance() {
154
+ const kzg = getKzg();
233
155
  return {
234
- blobToKzgCommitment: cKzg.blobToKzgCommitment,
235
- computeBlobKzgProof: cKzg.computeBlobKzgProof,
236
- computeCellsAndKzgProofs: cKzg.computeCellsAndKzgProofs
156
+ blobToKzgCommitment: kzg.blobToKzgCommitment.bind(kzg),
157
+ computeBlobKzgProof: kzg.computeBlobKzgProof.bind(kzg),
158
+ computeCellsAndKzgProofs: (b)=>{
159
+ const result = kzg.computeCellsAndKzgProofs(b);
160
+ return [
161
+ result.cells,
162
+ result.proofs
163
+ ];
164
+ }
237
165
  };
238
166
  }
239
- /**
240
- * @param fields - Fields to broadcast in the blob(s)
241
- * @returns As many blobs as we require to broadcast the given fields for a block
242
- * @dev Assumes we share the fields hash between all blobs which can only be done for ONE BLOCK because the hash is calculated in block root.
243
- */ static async getBlobsPerBlock(fields) {
244
- const numBlobs = Math.max(Math.ceil(fields.length / FIELD_ELEMENTS_PER_BLOB), 1);
245
- const multiBlobFieldsHash = await poseidon2Hash(fields);
246
- const res = [];
247
- for(let i = 0; i < numBlobs; i++){
248
- const end = fields.length < (i + 1) * FIELD_ELEMENTS_PER_BLOB ? fields.length : (i + 1) * FIELD_ELEMENTS_PER_BLOB;
249
- res.push(await Blob.fromFields(fields.slice(i * FIELD_ELEMENTS_PER_BLOB, end), multiBlobFieldsHash));
250
- }
251
- return res;
252
- }
253
- }
254
- // 48 bytes encoded in fields as [Fr, Fr] = [0->31, 31->48]
255
- function commitmentToFields(commitment) {
256
- return [
257
- new Fr(commitment.subarray(0, 31)),
258
- new Fr(commitment.subarray(31, 48))
259
- ];
260
167
  }