@aztec/blob-lib 3.0.0-canary.a9708bd → 3.0.0-manual.20251030

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 (65) hide show
  1. package/dest/blob.d.ts +52 -95
  2. package/dest/blob.d.ts.map +1 -1
  3. package/dest/blob.js +73 -165
  4. package/dest/blob_batching.d.ts +15 -48
  5. package/dest/blob_batching.d.ts.map +1 -1
  6. package/dest/blob_batching.js +81 -120
  7. package/dest/blob_utils.d.ts +30 -0
  8. package/dest/blob_utils.d.ts.map +1 -0
  9. package/dest/blob_utils.js +60 -0
  10. package/dest/circuit_types/blob_accumulator.d.ts +21 -0
  11. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
  12. package/dest/circuit_types/blob_accumulator.js +58 -0
  13. package/dest/circuit_types/final_blob_accumulator.d.ts +22 -0
  14. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
  15. package/dest/circuit_types/final_blob_accumulator.js +63 -0
  16. package/dest/circuit_types/final_blob_batching_challenges.d.ts +15 -0
  17. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
  18. package/dest/circuit_types/final_blob_batching_challenges.js +25 -0
  19. package/dest/circuit_types/index.d.ts +4 -0
  20. package/dest/circuit_types/index.d.ts.map +1 -0
  21. package/dest/circuit_types/index.js +4 -0
  22. package/dest/deserialize.d.ts +14 -0
  23. package/dest/deserialize.d.ts.map +1 -0
  24. package/dest/deserialize.js +33 -0
  25. package/dest/encoding.d.ts +22 -62
  26. package/dest/encoding.d.ts.map +1 -1
  27. package/dest/encoding.js +114 -104
  28. package/dest/hash.d.ts +35 -0
  29. package/dest/hash.d.ts.map +1 -0
  30. package/dest/hash.js +69 -0
  31. package/dest/index.d.ts +5 -2
  32. package/dest/index.d.ts.map +1 -1
  33. package/dest/index.js +5 -15
  34. package/dest/kzg_context.d.ts +4 -0
  35. package/dest/kzg_context.d.ts.map +1 -0
  36. package/dest/kzg_context.js +5 -0
  37. package/dest/sponge_blob.d.ts +13 -9
  38. package/dest/sponge_blob.d.ts.map +1 -1
  39. package/dest/sponge_blob.js +28 -17
  40. package/dest/testing.d.ts +7 -12
  41. package/dest/testing.d.ts.map +1 -1
  42. package/dest/testing.js +54 -41
  43. package/dest/types.d.ts +16 -0
  44. package/dest/types.d.ts.map +1 -0
  45. package/dest/types.js +3 -0
  46. package/package.json +6 -4
  47. package/src/blob.ts +76 -191
  48. package/src/blob_batching.ts +109 -137
  49. package/src/blob_utils.ts +71 -0
  50. package/src/circuit_types/blob_accumulator.ts +84 -0
  51. package/src/circuit_types/final_blob_accumulator.ts +75 -0
  52. package/src/circuit_types/final_blob_batching_challenges.ts +29 -0
  53. package/src/circuit_types/index.ts +4 -0
  54. package/src/deserialize.ts +38 -0
  55. package/src/encoding.ts +136 -120
  56. package/src/hash.ts +77 -0
  57. package/src/index.ts +5 -18
  58. package/src/kzg_context.ts +5 -0
  59. package/src/sponge_blob.ts +24 -14
  60. package/src/testing.ts +55 -40
  61. package/src/types.ts +16 -0
  62. package/dest/blob_batching_public_inputs.d.ts +0 -71
  63. package/dest/blob_batching_public_inputs.d.ts.map +0 -1
  64. package/dest/blob_batching_public_inputs.js +0 -168
  65. package/src/blob_batching_public_inputs.ts +0 -252
package/src/encoding.ts CHANGED
@@ -1,138 +1,154 @@
1
+ import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
1
2
  import { Fr } from '@aztec/foundation/fields';
2
- import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
+ import { FieldReader } from '@aztec/foundation/serialize';
3
4
 
4
- import type { Blob as BlobBuffer } from 'c-kzg';
5
+ const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
6
+ const REVERT_CODE_BIT_SIZE = 8n;
7
+ const NUM_NOTE_HASH_BIT_SIZE = 16n;
8
+ const NUM_NULLIFIER_BIT_SIZE = 16n;
9
+ const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
10
+ const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
11
+ const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
12
+ const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
13
+ const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
5
14
 
6
- // Note duplicated from stdlib !
7
- // This will appear as 0x74785f7374617274 in logs
8
- export const TX_START_PREFIX = 8392562855083340404n;
9
- // These are helper constants to decode tx effects from blob encoded fields
10
- export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX.toString(16).length / 2;
11
- // 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
12
- export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7;
13
- export const REVERT_CODE_PREFIX = 1;
15
+ export interface TxStartMarker {
16
+ prefix: bigint;
17
+ numBlobFields: number;
18
+ revertCode: number;
19
+ numNoteHashes: number;
20
+ numNullifiers: number;
21
+ numL2ToL1Msgs: number;
22
+ numPublicDataWrites: number;
23
+ numPrivateLogs: number;
24
+ publicLogsLength: number;
25
+ contractClassLogLength: number;
26
+ }
14
27
 
15
- /**
16
- * Deserializes a blob buffer into an array of field elements.
17
- *
18
- * Blobs are converted into BN254 fields to perform a poseidon2 hash on them (fieldHash).
19
- * This method is sparse, meaning it does not include trailing zeros at the end of the blob.
20
- *
21
- * However, we cannot simply trim the zero's from the end of the blob, as some logs may include zero's
22
- * within them.
23
- * If we end on a set of zeros, such as the log below:
24
- * length 7: [ a, b, c, d, e, 0, 0]
25
- *
26
- * we will end up with the incorrect hash if we trim the zeros from the end.
27
- *
28
- * Each transactions logs contains a TX start prefix, which includes a string followed
29
- * by the length ( in field elements ) of the transaction's log.
30
- *
31
- * This function finds the end of the last transaction's logs, and returns the array up to this point.
32
- *
33
- * We search for a series of Tx Prefixes progressing the cursor in the field reader until we hit
34
- * a field that is not a Tx Prefix, this indicates that we have reached the end of the last transaction's logs.
35
- *
36
- * +------------------+------------------+------------------+------------------+
37
- * | TX1 Start Prefix | TX1 Log Fields | TX2 Start Prefix | Padded zeros |
38
- * | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
39
- * +------------------+------------------+------------------+------------------+
40
- * ^
41
- * |
42
- * Function reads until here --------------------------------
43
- *
44
- * @param blob - The blob buffer to deserialize.
45
- * @returns An array of field elements.
46
- */
47
- export function deserializeEncodedBlobToFields(blob: BlobBuffer): Fr[] {
48
- // Convert blob buffer to array of field elements
49
- const reader = BufferReader.asReader(blob);
50
- const array = reader.readArray(blob.length >> 5, Fr); // >> 5 = / 32 (bytes per field)
51
- const fieldReader = FieldReader.asReader(array);
52
-
53
- // Read fields until we hit zeros at the end
54
- while (!fieldReader.isFinished()) {
55
- const currentField = fieldReader.peekField();
56
-
57
- // Stop when we hit a zero field
58
- if (!currentField || currentField.isZero()) {
59
- break;
60
- }
28
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
29
+ export function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>) {
30
+ let value = TX_START_PREFIX;
31
+ value <<= NUM_NOTE_HASH_BIT_SIZE;
32
+ value += BigInt(txStartMarker.numNoteHashes);
33
+ value <<= NUM_NULLIFIER_BIT_SIZE;
34
+ value += BigInt(txStartMarker.numNullifiers);
35
+ value <<= NUM_L2_TO_L1_MSG_BIT_SIZE;
36
+ value += BigInt(txStartMarker.numL2ToL1Msgs);
37
+ value <<= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
38
+ value += BigInt(txStartMarker.numPublicDataWrites);
39
+ value <<= NUM_PRIVATE_LOG_BIT_SIZE;
40
+ value += BigInt(txStartMarker.numPrivateLogs);
41
+ value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
42
+ value += BigInt(txStartMarker.publicLogsLength);
43
+ value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
44
+ value += BigInt(txStartMarker.contractClassLogLength);
45
+ value <<= REVERT_CODE_BIT_SIZE;
46
+ value += BigInt(txStartMarker.revertCode);
47
+ value <<= NUM_BLOB_FIELDS_BIT_SIZE;
48
+ value += BigInt(txStartMarker.numBlobFields);
49
+ return new Fr(value);
50
+ }
61
51
 
62
- // Skip the remaining fields in this transaction
63
- const len = getLengthFromFirstField(currentField);
64
- fieldReader.skip(len);
65
- }
52
+ export function decodeTxStartMarker(field: Fr): TxStartMarker {
53
+ let value = field.toBigInt();
54
+ const numBlobFields = Number(value & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
55
+ value >>= NUM_BLOB_FIELDS_BIT_SIZE;
56
+ const revertCode = Number(value & (2n ** REVERT_CODE_BIT_SIZE - 1n));
57
+ value >>= REVERT_CODE_BIT_SIZE;
58
+ const contractClassLogLength = Number(value & (2n ** CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE - 1n));
59
+ value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
60
+ const publicLogsLength = Number(value & (2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n));
61
+ value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
62
+ const numPrivateLogs = Number(value & (2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n));
63
+ value >>= NUM_PRIVATE_LOG_BIT_SIZE;
64
+ const numPublicDataWrites = Number(value & (2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n));
65
+ value >>= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
66
+ const numL2ToL1Msgs = Number(value & (2n ** NUM_L2_TO_L1_MSG_BIT_SIZE - 1n));
67
+ value >>= NUM_L2_TO_L1_MSG_BIT_SIZE;
68
+ const numNullifiers = Number(value & (2n ** NUM_NULLIFIER_BIT_SIZE - 1n));
69
+ value >>= NUM_NULLIFIER_BIT_SIZE;
70
+ const numNoteHashes = Number(value & (2n ** NUM_NOTE_HASH_BIT_SIZE - 1n));
71
+ value >>= NUM_NOTE_HASH_BIT_SIZE;
72
+ // Do not throw if the prefix doesn't match.
73
+ // The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
74
+ const prefix = value;
75
+ return {
76
+ prefix,
77
+ numBlobFields,
78
+ revertCode,
79
+ numNoteHashes,
80
+ numNullifiers,
81
+ numL2ToL1Msgs,
82
+ numPublicDataWrites,
83
+ numPrivateLogs,
84
+ publicLogsLength,
85
+ contractClassLogLength,
86
+ };
87
+ }
66
88
 
67
- // Return array up to last non-zero field
68
- return array.slice(0, fieldReader.cursor);
89
+ export function getNumBlobFieldsFromTxStartMarker(field: Fr) {
90
+ return Number(field.toBigInt() & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
69
91
  }
70
92
 
71
- /**
72
- * Get the length of the transaction from the first field.
73
- *
74
- * @param firstField - The first field of the transaction.
75
- * @returns The length of the transaction.
76
- *
77
- * @throws If the first field does not include the correct prefix - encoding invalid.
78
- */
79
- export function getLengthFromFirstField(firstField: Fr): number {
80
- // Check that the first field includes the correct prefix
81
- if (!isValidFirstField(firstField)) {
82
- throw new Error('Invalid prefix');
83
- }
84
- const buf = firstField.toBuffer().subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
85
- return new Fr(buf.subarray(TX_START_PREFIX_BYTES_LENGTH + 1, TX_START_PREFIX_BYTES_LENGTH + 3)).toNumber();
93
+ export function isValidTxStartMarker(txStartMarker: TxStartMarker) {
94
+ return txStartMarker.prefix === TX_START_PREFIX;
95
+ }
96
+
97
+ export function createBlockEndMarker(numTxs: number) {
98
+ // Must match the implementation in `block_rollup_public_inputs_composer.nr > create_block_end_marker`.
99
+ return new Fr(BLOCK_END_PREFIX * 256n * 256n + BigInt(numTxs));
100
+ }
101
+
102
+ export function getNumTxsFromBlockEndMarker(field: Fr) {
103
+ return Number(field.toBigInt() & 0xffffn);
104
+ }
105
+
106
+ export function isBlockEndMarker(field: Fr) {
107
+ const value = field.toBigInt();
108
+ const numTxs = value & 0xffffn;
109
+ return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
86
110
  }
87
111
 
88
112
  /**
89
- * Determines whether a field is the first field of a tx effect
113
+ * Check that the fields are emitted from the circuits and conform to the encoding.
114
+ * @param blobFields - The concatenated fields from all blobs of an L1 block.
90
115
  */
91
- export function isValidFirstField(field: Fr): boolean {
92
- const buf = field.toBuffer();
93
- if (
94
- !buf
95
- .subarray(0, field.size - TX_EFFECT_PREFIX_BYTE_LENGTH)
96
- .equals(Buffer.alloc(field.size - TX_EFFECT_PREFIX_BYTE_LENGTH))
97
- ) {
98
- return false;
99
- }
100
- const sliced = buf.subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
101
- if (
102
- // Checking we start with the correct prefix...
103
- !new Fr(sliced.subarray(0, TX_START_PREFIX_BYTES_LENGTH)).equals(new Fr(TX_START_PREFIX)) ||
104
- // ...and include the revert code prefix..
105
- sliced[sliced.length - 3] !== REVERT_CODE_PREFIX ||
106
- // ...and the following revert code is valid.
107
- sliced[sliced.length - 1] > 4
108
- ) {
116
+ export function checkBlobFieldsEncoding(blobFields: Fr[]) {
117
+ const reader = FieldReader.asReader(blobFields);
118
+
119
+ const checkpointPrefix = reader.readField();
120
+ if (checkpointPrefix.toBigInt() !== BigInt(blobFields.length)) {
109
121
  return false;
110
122
  }
111
- return true;
112
- }
113
123
 
114
- /**
115
- * Extract the fields from a blob buffer, but do not take into account encoding
116
- * that will include trailing zeros.
117
- *
118
- * +------------------+------------------+------------------+------------------+
119
- * | | | | Padded zeros |
120
- * | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
121
- * +------------------+------------------+------------------+------------------+
122
- * ^
123
- * |
124
- * Function reads until here ----------------------
125
- */
126
- export function extractBlobFieldsFromBuffer(blob: BlobBuffer): Fr[] {
127
- const reader = BufferReader.asReader(blob);
128
- const array = reader.readArray(blob.length >> 5, Fr);
129
-
130
- // Find the index of the last non-zero field
131
- let lastNonZeroIndex = array.length - 1;
132
- while (lastNonZeroIndex >= 0 && array[lastNonZeroIndex].isZero()) {
133
- lastNonZeroIndex--;
124
+ const numFieldsInCheckpoint = checkpointPrefix.toNumber();
125
+ let seenNumTxs = 0;
126
+ while (reader.cursor < numFieldsInCheckpoint) {
127
+ const currentField = reader.readField();
128
+
129
+ if (isBlockEndMarker(currentField)) {
130
+ // Found a block end marker. Confirm that the number of txs in this block is correct.
131
+ const numTxs = getNumTxsFromBlockEndMarker(currentField);
132
+ if (numTxs !== seenNumTxs) {
133
+ return false;
134
+ }
135
+ seenNumTxs = 0;
136
+ // Continue the loop to process the next field.
137
+ continue;
138
+ }
139
+
140
+ // If the field is not a block end marker, it must be a tx start marker.
141
+ const txStartMarker = decodeTxStartMarker(currentField);
142
+ if (!isValidTxStartMarker(txStartMarker)) {
143
+ return false;
144
+ }
145
+
146
+ seenNumTxs += 1;
147
+
148
+ // Skip the remaining fields in this tx. -1 because we already read the tx start marker.
149
+ reader.skip(txStartMarker.numBlobFields - 1);
150
+ // TODO: Check the encoding of the tx if we want to be more strict.
134
151
  }
135
152
 
136
- // Return the trimmed array
137
- return array.slice(0, lastNonZeroIndex + 1);
153
+ return true;
138
154
  }
package/src/hash.ts ADDED
@@ -0,0 +1,77 @@
1
+ import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
2
+ import { BLS12Fr, Fr } from '@aztec/foundation/fields';
3
+
4
+ import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
5
+
6
+ const VERSIONED_HASH_VERSION_KZG = 0x01;
7
+
8
+ /**
9
+ * Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
10
+ */
11
+ export function computeEthVersionedBlobHash(commitment: Buffer): Buffer {
12
+ const hash = sha256(commitment);
13
+ hash[0] = VERSIONED_HASH_VERSION_KZG;
14
+ return hash;
15
+ }
16
+
17
+ // TODO(#13430): The blobsHash is confusingly similar to blobCommitmentsHash, calculated from below blobCommitments:
18
+ // - 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.
19
+ // - 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()),
20
+ // exists so we can validate injected commitments to the rollup circuits correspond to the correct real blobs.
21
+ // 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.
22
+ export function computeBlobsHash(evmVersionedBlobHashes: Buffer[]): Fr {
23
+ return sha256ToField(evmVersionedBlobHashes);
24
+ }
25
+
26
+ /**
27
+ * The hash of the fields added throughout the checkpoint. The exact number of fields is specified by the checkpoint
28
+ * prefix (the first field). It's verified in the circuit against the fields absorbed into the sponge blob.
29
+ * This hash is used in generating the challenge z for all blobs in the same checkpoint.
30
+ */
31
+ export async function computeBlobFieldsHash(fields: Fr[]): Promise<Fr> {
32
+ return await poseidon2Hash(fields);
33
+ }
34
+
35
+ export function computeBlobCommitment(data: Uint8Array): Buffer {
36
+ if (data.length !== BYTES_PER_BLOB) {
37
+ throw new Error(`Expected ${BYTES_PER_BLOB} bytes per blob. Got ${data.length}.`);
38
+ }
39
+
40
+ return Buffer.from(kzg.blobToKzgCommitment(data));
41
+ }
42
+
43
+ /**
44
+ * Get the commitment fields of the blob, to compute the challenge z.
45
+ *
46
+ * The 48-byte commitment is encoded into two field elements:
47
+ * +-------------------+------------------------+
48
+ * | 31 bytes | 17 bytes |
49
+ * +-------------------+------------------------+
50
+ * | Field Element 1 | Field Element 2 |
51
+ * | [0][bytes 0-30] | [0...0][bytes 31-47] |
52
+ * +-------------------+------------------------+
53
+ *
54
+ * @param commitment - The commitment to convert to fields. Computed from `computeBlobCommitment`.
55
+ * @returns The fields representing the commitment buffer.
56
+ */
57
+ export function commitmentToFields(commitment: Buffer): [Fr, Fr] {
58
+ if (commitment.length !== BYTES_PER_COMMITMENT) {
59
+ throw new Error(`Expected ${BYTES_PER_COMMITMENT} bytes for blob commitment. Got ${commitment.length}.`);
60
+ }
61
+
62
+ return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, BYTES_PER_COMMITMENT))];
63
+ }
64
+
65
+ export async function computeChallengeZ(blobFieldsHash: Fr, commitment: Buffer): Promise<Fr> {
66
+ const commitmentFields = commitmentToFields(commitment);
67
+ return await poseidon2Hash([blobFieldsHash, commitmentFields[0], commitmentFields[1]]);
68
+ }
69
+
70
+ /**
71
+ * Hash each u128 limb of the noir bignum struct representing the BLS field, to mimic the hash accumulation in the
72
+ * rollup circuits.
73
+ */
74
+ export async function hashNoirBigNumLimbs(field: BLS12Fr): Promise<Fr> {
75
+ const num = field.toNoirBigNum();
76
+ return await poseidon2Hash(num.limbs.map(Fr.fromHexString));
77
+ }
package/src/index.ts CHANGED
@@ -1,23 +1,10 @@
1
- import cKzg from 'c-kzg';
2
-
3
- const { loadTrustedSetup } = cKzg;
4
-
5
1
  export * from './blob.js';
6
2
  export * from './blob_batching.js';
3
+ export * from './blob_utils.js';
4
+ export * from './circuit_types/index.js';
5
+ export * from './deserialize.js';
7
6
  export * from './encoding.js';
8
- export * from './interface.js';
9
7
  export * from './errors.js';
10
- export * from './blob_batching_public_inputs.js';
8
+ export * from './hash.js';
9
+ export * from './interface.js';
11
10
  export * from './sponge_blob.js';
12
-
13
- try {
14
- loadTrustedSetup();
15
- } catch (error: any) {
16
- if (error.message.includes('trusted setup is already loaded')) {
17
- // NB: The c-kzg lib has no way of checking whether the setup is loaded or not,
18
- // and it throws an error if it's already loaded, even though nothing is wrong.
19
- // This is a rudimentary way of ensuring we load the trusted setup if we need it.
20
- } else {
21
- throw new Error(error);
22
- }
23
- }
@@ -0,0 +1,5 @@
1
+ import { DasContextJs } from '@crate-crypto/node-eth-kzg';
2
+
3
+ export * from '@crate-crypto/node-eth-kzg';
4
+
5
+ export const kzg = DasContextJs.create({ usePrecomp: true });
@@ -1,3 +1,4 @@
1
+ import { TWO_POW_64 } from '@aztec/constants';
1
2
  import { type FieldsOf, makeTuple } from '@aztec/foundation/array';
2
3
  import { poseidon2Permutation } from '@aztec/foundation/crypto';
3
4
  import { Fr } from '@aztec/foundation/fields';
@@ -10,17 +11,17 @@ import {
10
11
  } from '@aztec/foundation/serialize';
11
12
 
12
13
  /**
13
- * A Poseidon2 sponge used to accumulate data that will be added to a blob.
14
+ * A Poseidon2 sponge used to accumulate data that will be added to blobs.
14
15
  * See noir-projects/noir-protocol-circuits/crates/types/src/abis/sponge_blob.nr.
15
16
  */
16
17
  export class SpongeBlob {
17
18
  constructor(
18
- /** Sponge with absorbed tx effects that will go into a blob. */
19
+ /** Sponge with absorbed fields that will go into one or more blobs. */
19
20
  public readonly sponge: Poseidon2Sponge,
20
21
  /** Number of effects absorbed so far. */
21
- public fields: number,
22
+ public numAbsorbedFields: number,
22
23
  /** Number of effects that will be absorbed. */
23
- public readonly expectedFields: number,
24
+ public readonly numExpectedFields: number,
24
25
  ) {}
25
26
 
26
27
  static fromBuffer(buffer: Buffer | BufferReader): SpongeBlob {
@@ -29,11 +30,11 @@ export class SpongeBlob {
29
30
  }
30
31
 
31
32
  toBuffer() {
32
- return serializeToBuffer(this.sponge, this.fields, this.expectedFields);
33
+ return serializeToBuffer(...SpongeBlob.getFields(this));
33
34
  }
34
35
 
35
36
  static getFields(fields: FieldsOf<SpongeBlob>) {
36
- return [fields.sponge, fields.fields, fields.expectedFields];
37
+ return [fields.sponge, fields.numAbsorbedFields, fields.numExpectedFields];
37
38
  }
38
39
 
39
40
  toFields(): Fr[] {
@@ -54,19 +55,19 @@ export class SpongeBlob {
54
55
  }
55
56
 
56
57
  async absorb(fields: Fr[]) {
57
- if (this.fields + fields.length > this.expectedFields) {
58
+ if (this.numAbsorbedFields + fields.length > this.numExpectedFields) {
58
59
  throw new Error(
59
- `Attempted to fill spongeblob with ${this.fields + fields.length}, but it has a max of ${this.expectedFields}`,
60
+ `Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${this.numExpectedFields}`,
60
61
  );
61
62
  }
62
63
  await this.sponge.absorb(fields);
63
- this.fields += fields.length;
64
+ this.numAbsorbedFields += fields.length;
64
65
  }
65
66
 
66
67
  async squeeze(): Promise<Fr> {
67
68
  // If the blob sponge is not 'full', we append 1 to match Poseidon2::hash_internal()
68
69
  // NB: There is currently no use case in which we don't 'fill' a blob sponge, but adding for completeness
69
- if (this.fields != this.expectedFields) {
70
+ if (this.numAbsorbedFields != this.numExpectedFields) {
70
71
  await this.sponge.absorb([Fr.ONE]);
71
72
  }
72
73
  return this.sponge.squeeze();
@@ -76,8 +77,17 @@ export class SpongeBlob {
76
77
  return new SpongeBlob(Poseidon2Sponge.empty(), 0, 0);
77
78
  }
78
79
 
79
- static init(expectedFields: number): SpongeBlob {
80
- return new SpongeBlob(Poseidon2Sponge.init(expectedFields), 0, expectedFields);
80
+ /**
81
+ * Initialize the sponge blob with the number of expected fields in the checkpoint and absorb it as the first field.
82
+ * Note: `numExpectedFields` includes the first field absorbed in this method.
83
+ */
84
+ static async init(numExpectedFields: number): Promise<SpongeBlob> {
85
+ // This must match what the checkpoint root rollup circuit expects.
86
+ // See noir-projects/noir-protocol-circuits/types/src/abis/sponge_blob.nr -> init_for_checkpoint.
87
+ const sponge = Poseidon2Sponge.init(numExpectedFields);
88
+ await sponge.absorb([new Fr(numExpectedFields)]);
89
+ const numAbsorbedFields = 1;
90
+ return new SpongeBlob(sponge, numAbsorbedFields, numExpectedFields);
81
91
  }
82
92
  }
83
93
 
@@ -131,8 +141,8 @@ export class Poseidon2Sponge {
131
141
  );
132
142
  }
133
143
 
134
- static init(expectedFields: number): Poseidon2Sponge {
135
- const iv = new Fr(expectedFields).mul(new Fr(BigInt('18446744073709551616')));
144
+ static init(numExpectedFields: number): Poseidon2Sponge {
145
+ const iv = new Fr(numExpectedFields).mul(new Fr(TWO_POW_64));
136
146
  const sponge = Poseidon2Sponge.empty();
137
147
  sponge.state[3] = iv;
138
148
  return sponge;
package/src/testing.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import { FIELDS_PER_BLOB } from '@aztec/constants';
1
2
  import { makeTuple } from '@aztec/foundation/array';
2
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
3
+ import { randomInt } from '@aztec/foundation/crypto';
3
4
  import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
4
5
 
5
6
  import { Blob } from './blob.js';
6
- import { BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
7
- import { BlobAccumulatorPublicInputs, BlockBlobPublicInputs } from './blob_batching_public_inputs.js';
8
- import { TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
7
+ import { BatchedBlobAccumulator } from './blob_batching.js';
8
+ import { getBlobsPerL1Block } from './blob_utils.js';
9
+ import { FinalBlobBatchingChallenges } from './circuit_types/index.js';
10
+ import { createBlockEndMarker, encodeTxStartMarker } from './encoding.js';
9
11
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
10
12
 
11
13
  /**
@@ -46,36 +48,44 @@ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
46
48
  );
47
49
  }
48
50
 
49
- /**
50
- * Makes arbitrary block blob public inputs.
51
- * Note: will not verify inside the circuit.
52
- * @param seed - The seed to use for generating the blob inputs.
53
- * @returns A block blob public inputs instance.
54
- */
55
- export function makeBlockBlobPublicInputs(seed = 1): BlockBlobPublicInputs {
56
- const startBlobAccumulator = makeBatchedBlobAccumulator(seed);
57
- return new BlockBlobPublicInputs(
58
- BlobAccumulatorPublicInputs.fromBatchedBlobAccumulator(startBlobAccumulator),
59
- BlobAccumulatorPublicInputs.fromBatchedBlobAccumulator(makeBatchedBlobAccumulator(seed + 1)),
60
- startBlobAccumulator.finalBlobChallenges,
61
- );
51
+ export function makeEncodedTxBlobFields(length: number): Fr[] {
52
+ const txStartMarker = {
53
+ numBlobFields: length,
54
+ // The rest of the values don't matter. The test components using it do not try to deserialize everything.
55
+ // Only `checkBlobFieldsEncoding` is used and it only looks at `numBlobFields`. This might change in the future
56
+ // when we add more thorough checks to `checkBlobFieldsEncoding`.
57
+ revertCode: 0,
58
+ numNoteHashes: 0,
59
+ numNullifiers: 0,
60
+ numL2ToL1Msgs: 0,
61
+ numPublicDataWrites: 0,
62
+ numPrivateLogs: 0,
63
+ publicLogsLength: 0,
64
+ contractClassLogLength: 0,
65
+ };
66
+
67
+ return [
68
+ encodeTxStartMarker(txStartMarker),
69
+ ...Array.from({ length: length - 1 }, () => new Fr(randomInt(Number.MAX_SAFE_INTEGER))), // -1 to account for the tx start marker.
70
+ ];
62
71
  }
63
72
 
64
- // TODO: copied form stdlib tx effect
65
- function encodeFirstField(length: number): Fr {
66
- const lengthBuf = Buffer.alloc(2);
67
- lengthBuf.writeUInt16BE(length, 0);
68
- return new Fr(
69
- Buffer.concat([
70
- toBufferBE(TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH),
71
- Buffer.alloc(1),
72
- lengthBuf,
73
- Buffer.alloc(1),
74
- Buffer.from([1]),
75
- Buffer.alloc(1),
76
- Buffer.alloc(1),
77
- ]),
78
- );
73
+ export function makeEncodedBlockBlobFields(...lengths: number[]): Fr[] {
74
+ return [
75
+ ...(lengths.length > 0 ? makeEncodedTxBlobFields(lengths[0] - 1) : []), // -1 to account for the block end marker.
76
+ ...lengths.slice(1).flatMap(length => makeEncodedTxBlobFields(length)),
77
+ createBlockEndMarker(lengths.length),
78
+ ];
79
+ }
80
+
81
+ // Create blob fields for a checkpoint with a single block.
82
+ export function makeEncodedBlobFields(length: number): Fr[] {
83
+ if (length <= 2) {
84
+ throw new Error('Encoded blob fields length must be greater than 2');
85
+ }
86
+
87
+ const checkpointPrefix = new Fr(length);
88
+ return [checkpointPrefix, ...makeEncodedBlockBlobFields(length - 1)]; // -1 to account for the checkpoint prefix.
79
89
  }
80
90
 
81
91
  /**
@@ -85,21 +95,26 @@ function encodeFirstField(length: number): Fr {
85
95
  * @param length
86
96
  * @returns
87
97
  */
88
- export function makeEncodedBlob(length: number): Promise<Blob> {
89
- return Blob.fromFields([encodeFirstField(length + 1), ...Array.from({ length: length }, () => Fr.random())]);
98
+ export function makeEncodedBlob(length: number): Blob {
99
+ if (length > FIELDS_PER_BLOB) {
100
+ throw new Error(`A single encoded blob must be less than ${FIELDS_PER_BLOB} fields`);
101
+ }
102
+
103
+ return Blob.fromFields(makeEncodedBlobFields(length));
104
+ }
105
+
106
+ export function makeEncodedBlobs(length: number): Blob[] {
107
+ const fields = makeEncodedBlobFields(length);
108
+ return getBlobsPerL1Block(fields);
90
109
  }
91
110
 
92
111
  /**
93
- * Make an unencoded blob with the given length
112
+ * Make a blob with random fields.
94
113
  *
95
114
  * This will fail deserialisation in the archiver
96
115
  * @param length
97
116
  * @returns
98
117
  */
99
- export function makeUnencodedBlob(length: number): Promise<Blob> {
118
+ export function makeRandomBlob(length: number): Blob {
100
119
  return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
101
120
  }
102
-
103
- export function makeEncodedBlobFields(fields: Fr[]): Promise<Blob> {
104
- return Blob.fromFields([encodeFirstField(fields.length + 1), ...fields]);
105
- }
package/src/types.ts ADDED
@@ -0,0 +1,16 @@
1
+ export * from './circuit_types/index.js';
2
+ export * from './interface.js';
3
+ export * from './sponge_blob.js';
4
+
5
+ /**
6
+ * Type definition for the KZG instance returned by Blob.getViemKzgInstance().
7
+ * Contains the cryptographic functions needed for blob commitment and proof generation.
8
+ */
9
+ export interface BlobKzgInstance {
10
+ /** Function to compute KZG commitment from blob data */
11
+ blobToKzgCommitment(blob: Uint8Array): Uint8Array;
12
+ /** Function to compute KZG proof for blob data */
13
+ computeBlobKzgProof(blob: Uint8Array, commitment: Uint8Array): Uint8Array;
14
+ /** Function to compute both blob data cells and their corresponding KZG proofs for EIP7594 */
15
+ computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]];
16
+ }