@aztec/blob-lib 0.0.1-commit.ec5f612 → 0.0.1-commit.ef17749e1
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/blob.d.ts +1 -1
- package/dest/blob.js +7 -7
- package/dest/hash.js +6 -6
- package/dest/kzg_context.d.ts +5 -3
- package/dest/kzg_context.d.ts.map +1 -1
- package/dest/kzg_context.js +18 -3
- package/package.json +3 -3
- package/src/blob.ts +7 -7
- package/src/hash.ts +6 -6
- package/src/kzg_context.ts +28 -3
package/dest/blob.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ export declare class Blob {
|
|
|
34
34
|
* @param data - The buffer of the Blob.
|
|
35
35
|
* @returns A Blob created from the buffer.
|
|
36
36
|
*
|
|
37
|
-
* @throws If data does not match the expected length (
|
|
37
|
+
* @throws If data does not match the expected length (getBytesPerBlob()).
|
|
38
38
|
*/
|
|
39
39
|
static fromBlobBuffer(data: Uint8Array): Promise<Blob>;
|
|
40
40
|
/**
|
package/dest/blob.js
CHANGED
|
@@ -3,7 +3,7 @@ import { BLS12Fr } from '@aztec/foundation/curves/bls12';
|
|
|
3
3
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
4
|
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
5
5
|
import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
|
|
6
|
-
import {
|
|
6
|
+
import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
|
|
7
7
|
export { FIELDS_PER_BLOB };
|
|
8
8
|
/**
|
|
9
9
|
* A class to create, manage, and prove EVM blobs.
|
|
@@ -21,11 +21,11 @@ export { FIELDS_PER_BLOB };
|
|
|
21
21
|
*/ commitment){
|
|
22
22
|
this.data = data;
|
|
23
23
|
this.commitment = commitment;
|
|
24
|
-
if (data.length !==
|
|
25
|
-
throw new Error(`Blob data must be ${
|
|
24
|
+
if (data.length !== getBytesPerBlob()) {
|
|
25
|
+
throw new Error(`Blob data must be ${getBytesPerBlob()} bytes. Got ${data.length}.`);
|
|
26
26
|
}
|
|
27
|
-
if (commitment.length !==
|
|
28
|
-
throw new Error(`Blob commitment must be ${
|
|
27
|
+
if (commitment.length !== getBytesPerCommitment()) {
|
|
28
|
+
throw new Error(`Blob commitment must be ${getBytesPerCommitment()} bytes. Got ${commitment.length}.`);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
@@ -33,7 +33,7 @@ export { FIELDS_PER_BLOB };
|
|
|
33
33
|
* @param data - The buffer of the Blob.
|
|
34
34
|
* @returns A Blob created from the buffer.
|
|
35
35
|
*
|
|
36
|
-
* @throws If data does not match the expected length (
|
|
36
|
+
* @throws If data does not match the expected length (getBytesPerBlob()).
|
|
37
37
|
*/ static async fromBlobBuffer(data) {
|
|
38
38
|
const commitment = await computeBlobCommitment(data);
|
|
39
39
|
return new Blob(data, commitment);
|
|
@@ -51,7 +51,7 @@ export { FIELDS_PER_BLOB };
|
|
|
51
51
|
}
|
|
52
52
|
const data = Buffer.concat([
|
|
53
53
|
serializeToBuffer(fields)
|
|
54
|
-
],
|
|
54
|
+
], getBytesPerBlob());
|
|
55
55
|
const commitment = await computeBlobCommitment(data);
|
|
56
56
|
return new Blob(data, commitment);
|
|
57
57
|
}
|
package/dest/hash.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
|
|
2
2
|
import { sha256, sha256ToField } from '@aztec/foundation/crypto/sha256';
|
|
3
3
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
4
|
-
import {
|
|
4
|
+
import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
|
|
5
5
|
import { SpongeBlob } from './sponge_blob.js';
|
|
6
6
|
const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
7
7
|
/**
|
|
@@ -36,8 +36,8 @@ export function computeBlobsHash(evmVersionedBlobHashes) {
|
|
|
36
36
|
return sponge.squeeze();
|
|
37
37
|
}
|
|
38
38
|
export async function computeBlobCommitment(data) {
|
|
39
|
-
if (data.length !==
|
|
40
|
-
throw new Error(`Expected ${
|
|
39
|
+
if (data.length !== getBytesPerBlob()) {
|
|
40
|
+
throw new Error(`Expected ${getBytesPerBlob()} bytes per blob. Got ${data.length}.`);
|
|
41
41
|
}
|
|
42
42
|
return Buffer.from(await getKzg().asyncBlobToKzgCommitment(data));
|
|
43
43
|
}
|
|
@@ -55,12 +55,12 @@ export async function computeBlobCommitment(data) {
|
|
|
55
55
|
* @param commitment - The commitment to convert to fields. Computed from `computeBlobCommitment`.
|
|
56
56
|
* @returns The fields representing the commitment buffer.
|
|
57
57
|
*/ export function commitmentToFields(commitment) {
|
|
58
|
-
if (commitment.length !==
|
|
59
|
-
throw new Error(`Expected ${
|
|
58
|
+
if (commitment.length !== getBytesPerCommitment()) {
|
|
59
|
+
throw new Error(`Expected ${getBytesPerCommitment()} bytes for blob commitment. Got ${commitment.length}.`);
|
|
60
60
|
}
|
|
61
61
|
return [
|
|
62
62
|
new Fr(commitment.subarray(0, 31)),
|
|
63
|
-
new Fr(commitment.subarray(31,
|
|
63
|
+
new Fr(commitment.subarray(31, getBytesPerCommitment()))
|
|
64
64
|
];
|
|
65
65
|
}
|
|
66
66
|
export async function computeChallengeZ(blobFieldsHash, commitment) {
|
package/dest/kzg_context.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
2
|
-
export
|
|
1
|
+
import type { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
2
|
+
export type { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
3
|
+
export declare function getBytesPerBlob(): number;
|
|
4
|
+
export declare function getBytesPerCommitment(): number;
|
|
3
5
|
/**
|
|
4
6
|
* Returns the lazily-initialized KZG context.
|
|
5
7
|
* The first call takes ~3 seconds to initialize the precomputation tables.
|
|
6
8
|
*/
|
|
7
9
|
export declare function getKzg(): DasContextJs;
|
|
8
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
10
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoia3pnX2NvbnRleHQuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9remdfY29udGV4dC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxZQUFZLEVBQUUsTUFBTSw0QkFBNEIsQ0FBQztBQU0vRCxZQUFZLEVBQUUsWUFBWSxFQUFFLE1BQU0sNEJBQTRCLENBQUM7QUFlL0Qsd0JBQWdCLGVBQWUsSUFBSSxNQUFNLENBRXhDO0FBRUQsd0JBQWdCLHFCQUFxQixJQUFJLE1BQU0sQ0FFOUM7QUFJRDs7O0dBR0c7QUFDSCx3QkFBZ0IsTUFBTSxJQUFJLFlBQVksQ0FLckMifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kzg_context.d.ts","sourceRoot":"","sources":["../src/kzg_context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"kzg_context.d.ts","sourceRoot":"","sources":["../src/kzg_context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAM/D,YAAY,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAe/D,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AAID;;;GAGG;AACH,wBAAgB,MAAM,IAAI,YAAY,CAKrC"}
|
package/dest/kzg_context.js
CHANGED
|
@@ -1,12 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { createRequire } from 'module';
|
|
2
|
+
let nativeModule;
|
|
3
|
+
/** Lazily loads the @crate-crypto/node-eth-kzg native module. */ function loadNativeModule() {
|
|
4
|
+
if (!nativeModule) {
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
nativeModule = require('@crate-crypto/node-eth-kzg');
|
|
7
|
+
}
|
|
8
|
+
return nativeModule;
|
|
9
|
+
}
|
|
10
|
+
// Ethereum blob constants, loaded lazily from the native module.
|
|
11
|
+
// Values: BYTES_PER_BLOB=131072, BYTES_PER_COMMITMENT=48
|
|
12
|
+
export function getBytesPerBlob() {
|
|
13
|
+
return loadNativeModule().BYTES_PER_BLOB;
|
|
14
|
+
}
|
|
15
|
+
export function getBytesPerCommitment() {
|
|
16
|
+
return loadNativeModule().BYTES_PER_COMMITMENT;
|
|
17
|
+
}
|
|
3
18
|
let kzgInstance;
|
|
4
19
|
/**
|
|
5
20
|
* Returns the lazily-initialized KZG context.
|
|
6
21
|
* The first call takes ~3 seconds to initialize the precomputation tables.
|
|
7
22
|
*/ export function getKzg() {
|
|
8
23
|
if (!kzgInstance) {
|
|
9
|
-
kzgInstance = DasContextJs.create({
|
|
24
|
+
kzgInstance = loadNativeModule().DasContextJs.create({
|
|
10
25
|
usePrecomp: true
|
|
11
26
|
});
|
|
12
27
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/blob-lib",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.ef17749e1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"../package.common.json"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@aztec/constants": "0.0.1-commit.
|
|
31
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
30
|
+
"@aztec/constants": "0.0.1-commit.ef17749e1",
|
|
31
|
+
"@aztec/foundation": "0.0.1-commit.ef17749e1",
|
|
32
32
|
"@crate-crypto/node-eth-kzg": "^0.10.0",
|
|
33
33
|
"tslib": "^2.4.0"
|
|
34
34
|
},
|
package/src/blob.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
|
5
5
|
|
|
6
6
|
import { computeBlobCommitment, computeChallengeZ, computeEthVersionedBlobHash } from './hash.js';
|
|
7
7
|
import type { BlobJson } from './interface.js';
|
|
8
|
-
import {
|
|
8
|
+
import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
|
|
9
9
|
|
|
10
10
|
export { FIELDS_PER_BLOB };
|
|
11
11
|
|
|
@@ -27,11 +27,11 @@ export class Blob {
|
|
|
27
27
|
*/
|
|
28
28
|
public readonly commitment: Buffer,
|
|
29
29
|
) {
|
|
30
|
-
if (data.length !==
|
|
31
|
-
throw new Error(`Blob data must be ${
|
|
30
|
+
if (data.length !== getBytesPerBlob()) {
|
|
31
|
+
throw new Error(`Blob data must be ${getBytesPerBlob()} bytes. Got ${data.length}.`);
|
|
32
32
|
}
|
|
33
|
-
if (commitment.length !==
|
|
34
|
-
throw new Error(`Blob commitment must be ${
|
|
33
|
+
if (commitment.length !== getBytesPerCommitment()) {
|
|
34
|
+
throw new Error(`Blob commitment must be ${getBytesPerCommitment()} bytes. Got ${commitment.length}.`);
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
@@ -40,7 +40,7 @@ export class Blob {
|
|
|
40
40
|
* @param data - The buffer of the Blob.
|
|
41
41
|
* @returns A Blob created from the buffer.
|
|
42
42
|
*
|
|
43
|
-
* @throws If data does not match the expected length (
|
|
43
|
+
* @throws If data does not match the expected length (getBytesPerBlob()).
|
|
44
44
|
*/
|
|
45
45
|
static async fromBlobBuffer(data: Uint8Array): Promise<Blob> {
|
|
46
46
|
const commitment = await computeBlobCommitment(data);
|
|
@@ -60,7 +60,7 @@ export class Blob {
|
|
|
60
60
|
throw new Error(`Attempted to overfill blob with ${fields.length} fields. The maximum is ${FIELDS_PER_BLOB}.`);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
const data = Buffer.concat([serializeToBuffer(fields)],
|
|
63
|
+
const data = Buffer.concat([serializeToBuffer(fields)], getBytesPerBlob());
|
|
64
64
|
const commitment = await computeBlobCommitment(data);
|
|
65
65
|
return new Blob(data, commitment);
|
|
66
66
|
}
|
package/src/hash.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { sha256, sha256ToField } from '@aztec/foundation/crypto/sha256';
|
|
|
3
3
|
import { BLS12Fr } from '@aztec/foundation/curves/bls12';
|
|
4
4
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
|
|
7
7
|
import { SpongeBlob } from './sponge_blob.js';
|
|
8
8
|
|
|
9
9
|
const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
@@ -45,8 +45,8 @@ export async function computeBlobFieldsHash(fields: Fr[]): Promise<Fr> {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
export async function computeBlobCommitment(data: Uint8Array): Promise<Buffer> {
|
|
48
|
-
if (data.length !==
|
|
49
|
-
throw new Error(`Expected ${
|
|
48
|
+
if (data.length !== getBytesPerBlob()) {
|
|
49
|
+
throw new Error(`Expected ${getBytesPerBlob()} bytes per blob. Got ${data.length}.`);
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
return Buffer.from(await getKzg().asyncBlobToKzgCommitment(data));
|
|
@@ -67,11 +67,11 @@ export async function computeBlobCommitment(data: Uint8Array): Promise<Buffer> {
|
|
|
67
67
|
* @returns The fields representing the commitment buffer.
|
|
68
68
|
*/
|
|
69
69
|
export function commitmentToFields(commitment: Buffer): [Fr, Fr] {
|
|
70
|
-
if (commitment.length !==
|
|
71
|
-
throw new Error(`Expected ${
|
|
70
|
+
if (commitment.length !== getBytesPerCommitment()) {
|
|
71
|
+
throw new Error(`Expected ${getBytesPerCommitment()} bytes for blob commitment. Got ${commitment.length}.`);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31,
|
|
74
|
+
return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, getBytesPerCommitment()))];
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
export async function computeChallengeZ(blobFieldsHash: Fr, commitment: Buffer): Promise<Fr> {
|
package/src/kzg_context.ts
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
-
import { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
1
|
+
import type { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
2
|
+
import { createRequire } from 'module';
|
|
2
3
|
|
|
3
|
-
export
|
|
4
|
+
// Re-export the type only. The native module is loaded lazily to avoid
|
|
5
|
+
// creating a napi-rs CustomGC handle at import time, which keeps the
|
|
6
|
+
// Node.js event loop alive and can deadlock process.exit().
|
|
7
|
+
export type { DasContextJs } from '@crate-crypto/node-eth-kzg';
|
|
8
|
+
|
|
9
|
+
let nativeModule: typeof import('@crate-crypto/node-eth-kzg') | undefined;
|
|
10
|
+
|
|
11
|
+
/** Lazily loads the @crate-crypto/node-eth-kzg native module. */
|
|
12
|
+
function loadNativeModule(): typeof import('@crate-crypto/node-eth-kzg') {
|
|
13
|
+
if (!nativeModule) {
|
|
14
|
+
const require = createRequire(import.meta.url);
|
|
15
|
+
nativeModule = require('@crate-crypto/node-eth-kzg') as typeof import('@crate-crypto/node-eth-kzg');
|
|
16
|
+
}
|
|
17
|
+
return nativeModule!;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Ethereum blob constants, loaded lazily from the native module.
|
|
21
|
+
// Values: BYTES_PER_BLOB=131072, BYTES_PER_COMMITMENT=48
|
|
22
|
+
export function getBytesPerBlob(): number {
|
|
23
|
+
return loadNativeModule().BYTES_PER_BLOB;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getBytesPerCommitment(): number {
|
|
27
|
+
return loadNativeModule().BYTES_PER_COMMITMENT;
|
|
28
|
+
}
|
|
4
29
|
|
|
5
30
|
let kzgInstance: DasContextJs | undefined;
|
|
6
31
|
|
|
@@ -10,7 +35,7 @@ let kzgInstance: DasContextJs | undefined;
|
|
|
10
35
|
*/
|
|
11
36
|
export function getKzg(): DasContextJs {
|
|
12
37
|
if (!kzgInstance) {
|
|
13
|
-
kzgInstance = DasContextJs.create({ usePrecomp: true });
|
|
38
|
+
kzgInstance = loadNativeModule().DasContextJs.create({ usePrecomp: true });
|
|
14
39
|
}
|
|
15
40
|
return kzgInstance;
|
|
16
41
|
}
|