@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,116 @@
1
+ import { chunk } from '@aztec-labs/foundation/collection';
2
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
3
+ import { FieldReader } from '@aztec-labs/foundation/serialize';
4
+
5
+ import { BlobDeserializationError } from '../errors.js';
6
+ import { type TxStartMarker, decodeTxStartMarker, encodeTxStartMarker } from './tx_start_marker.js';
7
+
8
+ // Must match the implementation in noir-protocol-circuits/crates/types/src/blob_data/tx_blob_data.nr.
9
+
10
+ export interface TxBlobData {
11
+ txStartMarker: TxStartMarker;
12
+ txHash: Fr;
13
+ transactionFee: Fr;
14
+ noteHashes: Fr[];
15
+ nullifiers: Fr[];
16
+ l2ToL1Msgs: Fr[];
17
+ publicDataWrites: [Fr, Fr][];
18
+ privateLogs: Fr[][];
19
+ publicLogs: Fr[];
20
+ contractClassLog: Fr[];
21
+ }
22
+
23
+ export function encodeTxBlobData(txBlobData: TxBlobData): Fr[] {
24
+ return [
25
+ encodeTxStartMarker(txBlobData.txStartMarker),
26
+ txBlobData.txHash,
27
+ txBlobData.transactionFee,
28
+ ...txBlobData.noteHashes,
29
+ ...txBlobData.nullifiers,
30
+ ...txBlobData.l2ToL1Msgs,
31
+ ...txBlobData.publicDataWrites.flat(),
32
+ ...txBlobData.privateLogs.map(log => [new Fr(log.length), ...log]).flat(),
33
+ ...txBlobData.publicLogs,
34
+ ...txBlobData.contractClassLog,
35
+ ];
36
+ }
37
+
38
+ export function decodeTxBlobData(fields: Fr[] | FieldReader): TxBlobData {
39
+ const reader = FieldReader.asReader(fields);
40
+
41
+ if (reader.isFinished()) {
42
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: not enough fields for tx blob data.`);
43
+ }
44
+
45
+ const txStartMarker = decodeTxStartMarker(reader.readField());
46
+
47
+ const checkRemainingFields = (requiredFields: number, type: string) => {
48
+ if (requiredFields > reader.remainingFields()) {
49
+ throw new BlobDeserializationError(
50
+ `Incorrect encoding of blob fields: not enough fields for ${type}. Expected ${requiredFields} fields, only ${reader.remainingFields()} remaining.`,
51
+ );
52
+ }
53
+ };
54
+
55
+ const numTxEffectFields = txStartMarker.numBlobFields - 1; // -1 because we already read the tx start marker.
56
+ checkRemainingFields(numTxEffectFields, 'tx effect');
57
+
58
+ const txHash = reader.readField();
59
+ const transactionFee = reader.readField();
60
+
61
+ checkRemainingFields(txStartMarker.numNoteHashes, 'note hashes');
62
+ const noteHashes = reader.readFieldArray(txStartMarker.numNoteHashes);
63
+
64
+ checkRemainingFields(txStartMarker.numNullifiers, 'nullifiers');
65
+ const nullifiers = reader.readFieldArray(txStartMarker.numNullifiers);
66
+
67
+ checkRemainingFields(txStartMarker.numL2ToL1Msgs, 'l2-to-l1 messages');
68
+ const l2ToL1Msgs = reader.readFieldArray(txStartMarker.numL2ToL1Msgs);
69
+
70
+ checkRemainingFields(txStartMarker.numPublicDataWrites * 2, 'public data writes'); // *2 for leaf slot and value
71
+ const publicDataWrites = chunk(reader.readFieldArray(txStartMarker.numPublicDataWrites * 2), 2) as [Fr, Fr][];
72
+
73
+ const privateLogs = Array.from({ length: txStartMarker.numPrivateLogs }, () => {
74
+ const length = reader.readU32();
75
+ checkRemainingFields(length, 'private log');
76
+ return reader.readFieldArray(length);
77
+ });
78
+
79
+ checkRemainingFields(txStartMarker.publicLogsLength, 'public logs');
80
+ const publicLogs = reader.readFieldArray(txStartMarker.publicLogsLength);
81
+
82
+ const contractClassLogBlobDataLength =
83
+ txStartMarker.contractClassLogLength > 0 ? txStartMarker.contractClassLogLength + 1 : 0; // If the log exists, +1 for the contract address
84
+ checkRemainingFields(contractClassLogBlobDataLength, 'contract class logs');
85
+ const contractClassLog = reader.readFieldArray(contractClassLogBlobDataLength);
86
+
87
+ return {
88
+ txStartMarker,
89
+ txHash,
90
+ transactionFee,
91
+ noteHashes,
92
+ nullifiers,
93
+ l2ToL1Msgs,
94
+ publicDataWrites,
95
+ privateLogs,
96
+ publicLogs,
97
+ contractClassLog,
98
+ };
99
+ }
100
+
101
+ export function getNumTxBlobFields(txStartMarker: Omit<TxStartMarker, 'revertCode' | 'numBlobFields'>) {
102
+ return (
103
+ 1 + // tx start marker
104
+ 1 + // tx hash
105
+ 1 + // transaction fee
106
+ txStartMarker.numNoteHashes +
107
+ txStartMarker.numNullifiers +
108
+ txStartMarker.numL2ToL1Msgs +
109
+ txStartMarker.numPublicDataWrites * 2 + // *2 for leaf slot and value per public data write
110
+ txStartMarker.numPrivateLogs + // +1 length field for each private log
111
+ txStartMarker.privateLogsLength +
112
+ txStartMarker.publicLogsLength +
113
+ txStartMarker.contractClassLogLength +
114
+ (txStartMarker.contractClassLogLength > 0 ? 1 : 0) // +1 for contract address of the contract class log
115
+ );
116
+ }
@@ -0,0 +1,97 @@
1
+ import { TX_START_PREFIX } from '@aztec-labs/constants';
2
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
3
+
4
+ import { BlobDeserializationError } from '../errors.js';
5
+
6
+ // Must match the implementation in `noir-protocol-circuits/crates/types/src/blob_data/tx_blob_data.nr`.
7
+
8
+ const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
9
+ const REVERT_CODE_BIT_SIZE = 8n;
10
+ const NUM_NOTE_HASH_BIT_SIZE = 16n;
11
+ const NUM_NULLIFIER_BIT_SIZE = 16n;
12
+ const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
13
+ const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
14
+ const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
15
+ const PRIVATE_LOGS_LENGTH_BIT_SIZE = 16n;
16
+ const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
17
+ const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
18
+
19
+ export interface TxStartMarker {
20
+ numBlobFields: number;
21
+ revertCode: number;
22
+ numNoteHashes: number;
23
+ numNullifiers: number;
24
+ numL2ToL1Msgs: number;
25
+ numPublicDataWrites: number;
26
+ numPrivateLogs: number;
27
+ privateLogsLength: number;
28
+ publicLogsLength: number;
29
+ contractClassLogLength: number;
30
+ }
31
+
32
+ export function encodeTxStartMarker(txStartMarker: TxStartMarker): Fr {
33
+ let value = BigInt(TX_START_PREFIX);
34
+ value <<= NUM_NOTE_HASH_BIT_SIZE;
35
+ value += BigInt(txStartMarker.numNoteHashes);
36
+ value <<= NUM_NULLIFIER_BIT_SIZE;
37
+ value += BigInt(txStartMarker.numNullifiers);
38
+ value <<= NUM_L2_TO_L1_MSG_BIT_SIZE;
39
+ value += BigInt(txStartMarker.numL2ToL1Msgs);
40
+ value <<= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
41
+ value += BigInt(txStartMarker.numPublicDataWrites);
42
+ value <<= NUM_PRIVATE_LOG_BIT_SIZE;
43
+ value += BigInt(txStartMarker.numPrivateLogs);
44
+ value <<= PRIVATE_LOGS_LENGTH_BIT_SIZE;
45
+ value += BigInt(txStartMarker.privateLogsLength);
46
+ value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
47
+ value += BigInt(txStartMarker.publicLogsLength);
48
+ value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
49
+ value += BigInt(txStartMarker.contractClassLogLength);
50
+ value <<= REVERT_CODE_BIT_SIZE;
51
+ value += BigInt(txStartMarker.revertCode);
52
+ value <<= NUM_BLOB_FIELDS_BIT_SIZE;
53
+ value += BigInt(txStartMarker.numBlobFields);
54
+ return new Fr(value);
55
+ }
56
+
57
+ export function decodeTxStartMarker(field: Fr): TxStartMarker {
58
+ let value = field.toBigInt();
59
+ const numBlobFields = Number(value & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
60
+ value >>= NUM_BLOB_FIELDS_BIT_SIZE;
61
+ const revertCode = Number(value & (2n ** REVERT_CODE_BIT_SIZE - 1n));
62
+ value >>= REVERT_CODE_BIT_SIZE;
63
+ const contractClassLogLength = Number(value & (2n ** CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE - 1n));
64
+ value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
65
+ const publicLogsLength = Number(value & (2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n));
66
+ value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
67
+ const privateLogsLength = Number(value & (2n ** PRIVATE_LOGS_LENGTH_BIT_SIZE - 1n));
68
+ value >>= PRIVATE_LOGS_LENGTH_BIT_SIZE;
69
+ const numPrivateLogs = Number(value & (2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n));
70
+ value >>= NUM_PRIVATE_LOG_BIT_SIZE;
71
+ const numPublicDataWrites = Number(value & (2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n));
72
+ value >>= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
73
+ const numL2ToL1Msgs = Number(value & (2n ** NUM_L2_TO_L1_MSG_BIT_SIZE - 1n));
74
+ value >>= NUM_L2_TO_L1_MSG_BIT_SIZE;
75
+ const numNullifiers = Number(value & (2n ** NUM_NULLIFIER_BIT_SIZE - 1n));
76
+ value >>= NUM_NULLIFIER_BIT_SIZE;
77
+ const numNoteHashes = Number(value & (2n ** NUM_NOTE_HASH_BIT_SIZE - 1n));
78
+ value >>= NUM_NOTE_HASH_BIT_SIZE;
79
+
80
+ const prefix = value;
81
+ if (prefix !== BigInt(TX_START_PREFIX)) {
82
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid tx start marker.`);
83
+ }
84
+
85
+ return {
86
+ numBlobFields,
87
+ revertCode,
88
+ numNoteHashes,
89
+ numNullifiers,
90
+ numL2ToL1Msgs,
91
+ numPublicDataWrites,
92
+ numPrivateLogs,
93
+ privateLogsLength,
94
+ publicLogsLength,
95
+ contractClassLogLength,
96
+ };
97
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,6 @@
1
+ export class BlobDeserializationError extends Error {
2
+ constructor(message: string) {
3
+ super(message);
4
+ this.name = 'BlobDeserializationError';
5
+ }
6
+ }
package/src/hash.ts ADDED
@@ -0,0 +1,94 @@
1
+ import { DomainSeparator } from '@aztec-labs/constants';
2
+ import { poseidon2HashWithSeparator } from '@aztec-labs/foundation/crypto/poseidon';
3
+ import { sha256, sha256ToField } from '@aztec-labs/foundation/crypto/sha256';
4
+ import { BLS12Fr } from '@aztec-labs/foundation/curves/bls12';
5
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
6
+
7
+ import { getBytesPerBlob, getBytesPerCommitment, getKzg } from './kzg_context.js';
8
+ import { SpongeBlob } from './sponge_blob.js';
9
+
10
+ const VERSIONED_HASH_VERSION_KZG = 0x01;
11
+
12
+ /**
13
+ * Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
14
+ */
15
+ export function computeEthVersionedBlobHash(commitment: Buffer): Buffer {
16
+ const hash = sha256(commitment);
17
+ hash[0] = VERSIONED_HASH_VERSION_KZG;
18
+ return hash;
19
+ }
20
+
21
+ // TODO(#13430): The blobsHash is confusingly similar to blobCommitmentsHash, calculated from below blobCommitments:
22
+ // - blobsHash := sha256([blobhash_0, ..., blobhash_m]) = a hash of all blob hashes in a block with m+1 blobs inserted into the header, exists so a user can cross check blobs.
23
+ // - blobCommitmentsHash := sha256( ...sha256(sha256(C_0), C_1) ... C_n) = iteratively calculated hash of all blob commitments in an epoch with n+1 blobs (see calculateBlobCommitmentsHash()),
24
+ // exists so we can validate injected commitments to the rollup circuits correspond to the correct real blobs.
25
+ // We may be able to combine these values e.g. blobCommitmentsHash := sha256( ...sha256(sha256(blobshash_0), blobshash_1) ... blobshash_l) for an epoch with l+1 blocks.
26
+ export function computeBlobsHash(evmVersionedBlobHashes: Buffer[]): Fr {
27
+ return sha256ToField(evmVersionedBlobHashes);
28
+ }
29
+
30
+ /**
31
+ * Computes a non-standard Poseidon2 hash over the provided fields.
32
+ *
33
+ * This function is used to compute:
34
+ * - `blobFieldsHash` of a checkpoint:
35
+ * Verified in the circuit against all fields absorbed into the blob sponge over the entire checkpoint.
36
+ * The exact number of fields is encoded in the checkpoint end marker (the last field).
37
+ * This hash is used when generating the challenge `z` for all blobs in the checkpoint.
38
+ * - `spongeBlobHash` of a block:
39
+ * Computed from the block's tx effects, its end-state, and the blob fields of all prior blocks in the same checkpoint.
40
+ * This hash is included in the block header.
41
+ */
42
+ export async function computeBlobFieldsHash(fields: Fr[]): Promise<Fr> {
43
+ const sponge = SpongeBlob.init();
44
+ await sponge.absorb(fields);
45
+ return sponge.squeeze();
46
+ }
47
+
48
+ export async function computeBlobCommitment(data: Uint8Array): Promise<Buffer> {
49
+ if (data.length !== getBytesPerBlob()) {
50
+ throw new Error(`Expected ${getBytesPerBlob()} bytes per blob. Got ${data.length}.`);
51
+ }
52
+
53
+ return Buffer.from(await getKzg().asyncBlobToKzgCommitment(data));
54
+ }
55
+
56
+ /**
57
+ * Get the commitment fields of the blob, to compute the challenge z.
58
+ *
59
+ * The 48-byte commitment is encoded into two field elements:
60
+ * +-------------------+------------------------+
61
+ * | 31 bytes | 17 bytes |
62
+ * +-------------------+------------------------+
63
+ * | Field Element 1 | Field Element 2 |
64
+ * | [0][bytes 0-30] | [0...0][bytes 31-47] |
65
+ * +-------------------+------------------------+
66
+ *
67
+ * @param commitment - The commitment to convert to fields. Computed from `computeBlobCommitment`.
68
+ * @returns The fields representing the commitment buffer.
69
+ */
70
+ export function commitmentToFields(commitment: Buffer): [Fr, Fr] {
71
+ if (commitment.length !== getBytesPerCommitment()) {
72
+ throw new Error(`Expected ${getBytesPerCommitment()} bytes for blob commitment. Got ${commitment.length}.`);
73
+ }
74
+
75
+ return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, getBytesPerCommitment()))];
76
+ }
77
+
78
+ export async function computeChallengeZ(blobFieldsHash: Fr, commitment: Buffer): Promise<Fr> {
79
+ const commitmentFields = commitmentToFields(commitment);
80
+ return await poseidon2HashWithSeparator(
81
+ [blobFieldsHash, commitmentFields[0], commitmentFields[1]],
82
+ DomainSeparator.BLOB_CHALLENGE_Z,
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Hash the u128 limbs of a BLS field's noir bignum representation under the `BLOB_HASHED_Y_LIMBS` separator.
88
+ * Used to commit to blob evaluation values `y_i` before folding them into the gamma accumulator; mirrors the
89
+ * hash accumulation performed in the rollup circuits.
90
+ */
91
+ export async function hashBlobYLimbs(field: BLS12Fr): Promise<Fr> {
92
+ const num = field.toNoirBigNum();
93
+ return await poseidon2HashWithSeparator(num.limbs.map(Fr.fromHexString), DomainSeparator.BLOB_HASHED_Y_LIMBS);
94
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from './batched_blob.js';
2
+ export * from './blob.js';
3
+ export * from './blob_batching.js';
4
+ export * from './blob_utils.js';
5
+ export * from './circuit_types/index.js';
6
+ export * from './encoding/index.js';
7
+ export * from './hash.js';
8
+ export * from './interface.js';
9
+ export * from './sponge_blob.js';
10
+ export * from './kzg_context.js';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * The relevant parts of a response from https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
3
+ */
4
+ export interface BlobJson {
5
+ blob: string;
6
+ kzg_commitment: string;
7
+ }
@@ -0,0 +1,48 @@
1
+ import type { Logger } from '@aztec-labs/foundation/log';
2
+ import { elapsedSync } from '@aztec-labs/foundation/timer';
3
+ import type { DasContextJs } from '@crate-crypto/node-eth-kzg';
4
+ import { createRequire } from 'module';
5
+
6
+ // Re-export the type only. The native module is loaded lazily to avoid
7
+ // creating a napi-rs CustomGC handle at import time, which keeps the
8
+ // Node.js event loop alive and can deadlock process.exit().
9
+ export type { DasContextJs } from '@crate-crypto/node-eth-kzg';
10
+
11
+ let nativeModule: typeof import('@crate-crypto/node-eth-kzg') | undefined;
12
+
13
+ /** Lazily loads the @crate-crypto/node-eth-kzg native module. */
14
+ function loadNativeModule(): typeof import('@crate-crypto/node-eth-kzg') {
15
+ if (!nativeModule) {
16
+ const require = createRequire(import.meta.url);
17
+ nativeModule = require('@crate-crypto/node-eth-kzg') as typeof import('@crate-crypto/node-eth-kzg');
18
+ }
19
+ return nativeModule!;
20
+ }
21
+
22
+ // Ethereum blob constants, loaded lazily from the native module.
23
+ // Values: BYTES_PER_BLOB=131072, BYTES_PER_COMMITMENT=48
24
+ export function getBytesPerBlob(): number {
25
+ return loadNativeModule().BYTES_PER_BLOB;
26
+ }
27
+
28
+ export function getBytesPerCommitment(): number {
29
+ return loadNativeModule().BYTES_PER_COMMITMENT;
30
+ }
31
+
32
+ let kzgInstance: DasContextJs | undefined;
33
+
34
+ /**
35
+ * Returns the lazily-initialized KZG context. The first call synchronously builds the
36
+ * precomputation tables (~2s locally, blocking the event loop; longer under constrained CPU), so
37
+ * callers on a latency-sensitive path should warm it ahead of time. Pass a logger to record how
38
+ * long that first build took.
39
+ * @param logger - Optional logger; when provided, the initial table build is timed and logged.
40
+ */
41
+ export function getKzg(logger?: Logger): DasContextJs {
42
+ if (!kzgInstance) {
43
+ const [durationMs, instance] = elapsedSync(() => loadNativeModule().DasContextJs.create({ usePrecomp: true }));
44
+ kzgInstance = instance;
45
+ logger?.verbose(`Loaded KZG trusted setup`, { durationMs });
46
+ }
47
+ return kzgInstance;
48
+ }
@@ -0,0 +1,175 @@
1
+ import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, TWO_POW_64 } from '@aztec-labs/constants';
2
+ import { type FieldsOf, makeTuple } from '@aztec-labs/foundation/array';
3
+ import { poseidon2Permutation } from '@aztec-labs/foundation/crypto/poseidon';
4
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
5
+ import {
6
+ BufferReader,
7
+ FieldReader,
8
+ type Tuple,
9
+ serializeToBuffer,
10
+ serializeToFields,
11
+ } from '@aztec-labs/foundation/serialize';
12
+
13
+ /**
14
+ * A Poseidon2 sponge used to accumulate data that will be added to blobs.
15
+ * See noir-projects/fnd/noir-protocol-circuits/crates/types/src/blob_data/sponge_blob.nr.
16
+ */
17
+ export class SpongeBlob {
18
+ static MAX_FIELDS = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB;
19
+
20
+ constructor(
21
+ /** Sponge with absorbed fields that will go into one or more blobs. */
22
+ public readonly sponge: Poseidon2Sponge,
23
+ /** Number of effects absorbed so far. */
24
+ public numAbsorbedFields: number,
25
+ ) {}
26
+
27
+ /**
28
+ * Initialize the sponge blob to absorb data for a checkpoint.
29
+ */
30
+ static init(): SpongeBlob {
31
+ // This must match the implementation in noir-projects/fnd/noir-protocol-circuits/crates/types/src/blob_data/sponge_blob.nr
32
+ const iv = new Fr(BigInt(SpongeBlob.MAX_FIELDS) * TWO_POW_64);
33
+ const sponge = Poseidon2Sponge.init(iv);
34
+ return new SpongeBlob(sponge, 0);
35
+ }
36
+
37
+ static fromBuffer(buffer: Buffer | BufferReader): SpongeBlob {
38
+ const reader = BufferReader.asReader(buffer);
39
+ return new SpongeBlob(reader.readObject(Poseidon2Sponge), reader.readNumber());
40
+ }
41
+
42
+ toBuffer() {
43
+ return serializeToBuffer(...SpongeBlob.getFields(this));
44
+ }
45
+
46
+ static getFields(fields: FieldsOf<SpongeBlob>) {
47
+ return [fields.sponge, fields.numAbsorbedFields];
48
+ }
49
+
50
+ toFields(): Fr[] {
51
+ return serializeToFields(...SpongeBlob.getFields(this));
52
+ }
53
+
54
+ static fromFields(fields: Fr[] | FieldReader): SpongeBlob {
55
+ const reader = FieldReader.asReader(fields);
56
+ return new SpongeBlob(reader.readObject(Poseidon2Sponge), reader.readField().toNumber());
57
+ }
58
+
59
+ clone() {
60
+ return SpongeBlob.fromBuffer(this.toBuffer());
61
+ }
62
+
63
+ async absorb(fields: Fr[]) {
64
+ if (this.numAbsorbedFields + fields.length > SpongeBlob.MAX_FIELDS) {
65
+ throw new Error(
66
+ `Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${SpongeBlob.MAX_FIELDS}`,
67
+ );
68
+ }
69
+ await this.sponge.absorb(fields);
70
+ this.numAbsorbedFields += fields.length;
71
+ }
72
+
73
+ async squeeze(): Promise<Fr> {
74
+ return await this.sponge.squeeze();
75
+ }
76
+
77
+ static empty(): SpongeBlob {
78
+ return new SpongeBlob(Poseidon2Sponge.empty(), 0);
79
+ }
80
+ }
81
+
82
+ // This is just noir's stdlib version of the poseidon2 sponge. We use it for a blob-specific implmentation of the hasher.
83
+ export class Poseidon2Sponge {
84
+ constructor(
85
+ public cache: Tuple<Fr, 3>,
86
+ public state: Tuple<Fr, 4>,
87
+ public cacheSize: number,
88
+ public squeezeMode: boolean,
89
+ ) {}
90
+
91
+ static fromBuffer(buffer: Buffer | BufferReader): Poseidon2Sponge {
92
+ const reader = BufferReader.asReader(buffer);
93
+ return new Poseidon2Sponge(
94
+ reader.readArray(3, Fr),
95
+ reader.readArray(4, Fr),
96
+ reader.readNumber(),
97
+ reader.readBoolean(),
98
+ );
99
+ }
100
+
101
+ toBuffer() {
102
+ return serializeToBuffer(this.cache, this.state, this.cacheSize, this.squeezeMode);
103
+ }
104
+
105
+ static getFields(fields: FieldsOf<Poseidon2Sponge>) {
106
+ return [fields.cache, fields.state, fields.cacheSize, fields.squeezeMode];
107
+ }
108
+
109
+ toFields(): Fr[] {
110
+ return serializeToFields(...Poseidon2Sponge.getFields(this));
111
+ }
112
+
113
+ static fromFields(fields: Fr[] | FieldReader): Poseidon2Sponge {
114
+ const reader = FieldReader.asReader(fields);
115
+ return new Poseidon2Sponge(
116
+ reader.readFieldArray(3),
117
+ reader.readFieldArray(4),
118
+ reader.readField().toNumber(),
119
+ reader.readBoolean(),
120
+ );
121
+ }
122
+
123
+ static empty(): Poseidon2Sponge {
124
+ return new Poseidon2Sponge(
125
+ makeTuple(3, () => Fr.ZERO),
126
+ makeTuple(4, () => Fr.ZERO),
127
+ 0,
128
+ false,
129
+ );
130
+ }
131
+
132
+ static init(iv: Fr): Poseidon2Sponge {
133
+ const sponge = Poseidon2Sponge.empty();
134
+ sponge.state[3] = iv;
135
+ return sponge;
136
+ }
137
+
138
+ // Note: there isn't currently an impl in ts that allows for a custom aborption via an
139
+ // existing sponge.
140
+ // A custom blob-based impl of noir/noir-repo/noir_stdlib/src/hash/poseidon2.nr
141
+ async performDuplex() {
142
+ for (let i = 0; i < this.cache.length; i++) {
143
+ if (i < this.cacheSize) {
144
+ this.state[i] = this.state[i].add(this.cache[i]);
145
+ }
146
+ }
147
+ const perm = await poseidon2Permutation(this.state);
148
+ // ts doesn't understand that the above always gives 4
149
+ this.state = [perm[0], perm[1], perm[2], perm[3]];
150
+ }
151
+
152
+ async absorb(fields: Fr[]) {
153
+ if (this.squeezeMode) {
154
+ throw new Error(`Poseidon sponge is not able to absorb more inputs.`);
155
+ }
156
+ for (const field of fields) {
157
+ if (this.cacheSize == this.cache.length) {
158
+ await this.performDuplex();
159
+ this.cache[0] = field;
160
+ this.cacheSize = 1;
161
+ } else {
162
+ this.cache[this.cacheSize++] = field;
163
+ }
164
+ }
165
+ }
166
+
167
+ async squeeze(): Promise<Fr> {
168
+ if (this.squeezeMode) {
169
+ throw new Error(`Poseidon sponge has already been squeezed.`);
170
+ }
171
+ await this.performDuplex();
172
+ this.squeezeMode = true;
173
+ return this.state[0];
174
+ }
175
+ }
package/src/testing.ts ADDED
@@ -0,0 +1,94 @@
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
+
5
+ import { Blob } from './blob.js';
6
+ import { BlobAccumulator } from './circuit_types/blob_accumulator.js';
7
+ import { FinalBlobAccumulator } from './circuit_types/final_blob_accumulator.js';
8
+ import { FinalBlobBatchingChallenges } from './circuit_types/final_blob_batching_challenges.js';
9
+ import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
10
+
11
+ export * from './encoding/fixtures.js';
12
+
13
+ /**
14
+ * Makes arbitrary poseidon sponge for blob inputs.
15
+ * Note: will not verify inside the circuit.
16
+ * @param seed - The seed to use for generating the sponge.
17
+ * @returns A sponge blob instance.
18
+ */
19
+ export function makeSpongeBlob(seed = 1): SpongeBlob {
20
+ return new SpongeBlob(
21
+ new Poseidon2Sponge(
22
+ makeTuple(3, i => new Fr(i)),
23
+ makeTuple(4, i => new Fr(i)),
24
+ 1,
25
+ false,
26
+ ),
27
+ seed,
28
+ );
29
+ }
30
+
31
+ /**
32
+ * Makes an arbitrary but valid BLS12 point. The value is deterministic for a given seed.
33
+ * @param seed - The seed to use for generating the point.
34
+ * @returns A BLS12 point instance.
35
+ */
36
+ function makeBLS12Point(seed = 1): BLS12Point {
37
+ let accum = 0;
38
+ while (true) {
39
+ try {
40
+ const x = new BLS12Fq(seed + accum);
41
+ const y = BLS12Point.YFromX(x);
42
+ if (y) {
43
+ return new BLS12Point(x, y, false);
44
+ }
45
+ accum++;
46
+ } catch (e: any) {
47
+ if (!(e instanceof BLSPointNotOnCurveError)) {
48
+ throw e;
49
+ }
50
+ // The point is not on the curve - try again
51
+ }
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Makes arbitrary blob public accumulator.
57
+ * Note: will not verify inside the circuit.
58
+ * @param seed - The seed to use for generating the blob accumulator.
59
+ * @returns A blob accumulator instance.
60
+ */
61
+ export function makeBlobAccumulator(seed = 1): BlobAccumulator {
62
+ return new BlobAccumulator(
63
+ new Fr(seed),
64
+ new Fr(seed + 0x10),
65
+ new BLS12Fr(seed + 0x20),
66
+ makeBLS12Point(seed + 0x30),
67
+ new Fr(seed + 0x50),
68
+ new BLS12Fr(seed + 0x60),
69
+ );
70
+ }
71
+
72
+ export function makeFinalBlobAccumulator(seed = 1) {
73
+ return new FinalBlobAccumulator(
74
+ new Fr(seed),
75
+ new Fr(seed + 0x10),
76
+ new BLS12Fr(seed + 0x20),
77
+ makeBLS12Point(seed + 0x30),
78
+ );
79
+ }
80
+
81
+ export function makeFinalBlobBatchingChallenges(seed = 1) {
82
+ return new FinalBlobBatchingChallenges(new Fr(seed), new BLS12Fr(seed + 0x10));
83
+ }
84
+
85
+ /**
86
+ * Make a blob with random fields.
87
+ *
88
+ * This will fail deserialisation in the archiver
89
+ * @param length
90
+ * @returns
91
+ */
92
+ export function makeRandomBlob(length: number): Promise<Blob> {
93
+ return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
94
+ }