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