@aztec/blob-lib 3.0.0-nightly.20251113 → 3.0.0-nightly.20251115

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 (51) hide show
  1. package/dest/blob_utils.d.ts +5 -8
  2. package/dest/blob_utils.d.ts.map +1 -1
  3. package/dest/blob_utils.js +7 -10
  4. package/dest/encoding/block_blob_data.d.ts +22 -0
  5. package/dest/encoding/block_blob_data.d.ts.map +1 -0
  6. package/dest/encoding/block_blob_data.js +65 -0
  7. package/dest/encoding/block_end_marker.d.ts +10 -0
  8. package/dest/encoding/block_end_marker.d.ts.map +1 -0
  9. package/dest/encoding/block_end_marker.js +40 -0
  10. package/dest/encoding/block_end_state_field.d.ts +12 -0
  11. package/dest/encoding/block_end_state_field.d.ts.map +1 -0
  12. package/dest/encoding/block_end_state_field.js +39 -0
  13. package/dest/encoding/checkpoint_blob_data.d.ts +13 -0
  14. package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
  15. package/dest/encoding/checkpoint_blob_data.js +54 -0
  16. package/dest/encoding/fixtures.d.ts +41 -0
  17. package/dest/encoding/fixtures.d.ts.map +1 -0
  18. package/dest/encoding/fixtures.js +137 -0
  19. package/dest/encoding/index.d.ts +8 -0
  20. package/dest/encoding/index.d.ts.map +1 -0
  21. package/dest/encoding/index.js +7 -0
  22. package/dest/encoding/tx_blob_data.d.ts +19 -0
  23. package/dest/encoding/tx_blob_data.d.ts.map +1 -0
  24. package/dest/encoding/tx_blob_data.js +79 -0
  25. package/dest/encoding/tx_start_marker.d.ts +16 -0
  26. package/dest/encoding/tx_start_marker.d.ts.map +1 -0
  27. package/dest/{encoding.js → encoding/tx_start_marker.js} +12 -58
  28. package/dest/index.d.ts +1 -2
  29. package/dest/index.d.ts.map +1 -1
  30. package/dest/index.js +1 -2
  31. package/dest/testing.d.ts +1 -13
  32. package/dest/testing.d.ts.map +1 -1
  33. package/dest/testing.js +1 -60
  34. package/package.json +4 -4
  35. package/src/blob_utils.ts +7 -10
  36. package/src/encoding/block_blob_data.ts +102 -0
  37. package/src/encoding/block_end_marker.ts +54 -0
  38. package/src/encoding/block_end_state_field.ts +59 -0
  39. package/src/encoding/checkpoint_blob_data.ts +75 -0
  40. package/src/encoding/fixtures.ts +209 -0
  41. package/src/encoding/index.ts +7 -0
  42. package/src/encoding/tx_blob_data.ts +116 -0
  43. package/src/{encoding.ts → encoding/tx_start_marker.ts} +18 -75
  44. package/src/index.ts +1 -2
  45. package/src/testing.ts +2 -64
  46. package/dest/deserialize.d.ts +0 -14
  47. package/dest/deserialize.d.ts.map +0 -1
  48. package/dest/deserialize.js +0 -33
  49. package/dest/encoding.d.ts +0 -26
  50. package/dest/encoding.d.ts.map +0 -1
  51. package/src/deserialize.ts +0 -38
@@ -0,0 +1,79 @@
1
+ import { chunk } from '@aztec/foundation/collection';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+ import { FieldReader } from '@aztec/foundation/serialize';
4
+ import { BlobDeserializationError } from '../errors.js';
5
+ import { decodeTxStartMarker, encodeTxStartMarker } from './tx_start_marker.js';
6
+ export function encodeTxBlobData(txBlobData) {
7
+ return [
8
+ encodeTxStartMarker(txBlobData.txStartMarker),
9
+ txBlobData.txHash,
10
+ txBlobData.transactionFee,
11
+ ...txBlobData.noteHashes,
12
+ ...txBlobData.nullifiers,
13
+ ...txBlobData.l2ToL1Msgs,
14
+ ...txBlobData.publicDataWrites.flat(),
15
+ ...txBlobData.privateLogs.map((log)=>[
16
+ new Fr(log.length),
17
+ ...log
18
+ ]).flat(),
19
+ ...txBlobData.publicLogs,
20
+ ...txBlobData.contractClassLog
21
+ ];
22
+ }
23
+ export function decodeTxBlobData(fields) {
24
+ const reader = FieldReader.asReader(fields);
25
+ if (reader.isFinished()) {
26
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: not enough fields for tx blob data.`);
27
+ }
28
+ const txStartMarker = decodeTxStartMarker(reader.readField());
29
+ const checkRemainingFields = (requiredFields, type)=>{
30
+ if (requiredFields > reader.remainingFields()) {
31
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: not enough fields for ${type}. Expected ${requiredFields} fields, only ${reader.remainingFields()} remaining.`);
32
+ }
33
+ };
34
+ const numTxEffectFields = txStartMarker.numBlobFields - 1; // -1 because we already read the tx start marker.
35
+ checkRemainingFields(numTxEffectFields, 'tx effect');
36
+ const txHash = reader.readField();
37
+ const transactionFee = reader.readField();
38
+ checkRemainingFields(txStartMarker.numNoteHashes, 'note hashes');
39
+ const noteHashes = reader.readFieldArray(txStartMarker.numNoteHashes);
40
+ checkRemainingFields(txStartMarker.numNullifiers, 'nullifiers');
41
+ const nullifiers = reader.readFieldArray(txStartMarker.numNullifiers);
42
+ checkRemainingFields(txStartMarker.numL2ToL1Msgs, 'l2-to-l1 messages');
43
+ const l2ToL1Msgs = reader.readFieldArray(txStartMarker.numL2ToL1Msgs);
44
+ checkRemainingFields(txStartMarker.numPublicDataWrites * 2, 'public data writes'); // *2 for leaf slot and value
45
+ const publicDataWrites = chunk(reader.readFieldArray(txStartMarker.numPublicDataWrites * 2), 2);
46
+ const privateLogs = Array.from({
47
+ length: txStartMarker.numPrivateLogs
48
+ }, ()=>{
49
+ const length = reader.readU32();
50
+ checkRemainingFields(length, 'private log');
51
+ return reader.readFieldArray(length);
52
+ });
53
+ checkRemainingFields(txStartMarker.publicLogsLength, 'public logs');
54
+ const publicLogs = reader.readFieldArray(txStartMarker.publicLogsLength);
55
+ const contractClassLogBlobDataLength = txStartMarker.contractClassLogLength > 0 ? txStartMarker.contractClassLogLength + 1 : 0; // If the log exists, +1 for the contract address
56
+ checkRemainingFields(contractClassLogBlobDataLength, 'contract class logs');
57
+ const contractClassLog = reader.readFieldArray(contractClassLogBlobDataLength);
58
+ return {
59
+ txStartMarker,
60
+ txHash,
61
+ transactionFee,
62
+ noteHashes,
63
+ nullifiers,
64
+ l2ToL1Msgs,
65
+ publicDataWrites,
66
+ privateLogs,
67
+ publicLogs,
68
+ contractClassLog
69
+ };
70
+ }
71
+ export function getNumTxBlobFields(txStartMarker) {
72
+ return 1 + // tx start marker
73
+ 1 + // tx hash
74
+ 1 + // transaction fee
75
+ txStartMarker.numNoteHashes + txStartMarker.numNullifiers + txStartMarker.numL2ToL1Msgs + txStartMarker.numPublicDataWrites * 2 + // *2 for leaf slot and value per public data write
76
+ txStartMarker.numPrivateLogs + // +1 length field for each private log
77
+ txStartMarker.privateLogsLength + txStartMarker.publicLogsLength + txStartMarker.contractClassLogLength + (txStartMarker.contractClassLogLength > 0 ? 1 : 0 // +1 for contract address of the contract class log
78
+ );
79
+ }
@@ -0,0 +1,16 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ export interface TxStartMarker {
3
+ numBlobFields: number;
4
+ revertCode: number;
5
+ numNoteHashes: number;
6
+ numNullifiers: number;
7
+ numL2ToL1Msgs: number;
8
+ numPublicDataWrites: number;
9
+ numPrivateLogs: number;
10
+ privateLogsLength: number;
11
+ publicLogsLength: number;
12
+ contractClassLogLength: number;
13
+ }
14
+ export declare function encodeTxStartMarker(txStartMarker: TxStartMarker): Fr;
15
+ export declare function decodeTxStartMarker(field: Fr): TxStartMarker;
16
+ //# sourceMappingURL=tx_start_marker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tx_start_marker.d.ts","sourceRoot":"","sources":["../../src/encoding/tx_start_marker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAiB9C,MAAM,WAAW,aAAa;IAC5B,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,MAAM,CAAC;IACtB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,cAAc,EAAE,MAAM,CAAC;IACvB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,gBAAgB,EAAE,MAAM,CAAC;IACzB,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAED,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,aAAa,GAAG,EAAE,CAuBpE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,EAAE,GAAG,aAAa,CAwC5D"}
@@ -1,6 +1,7 @@
1
- import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
1
+ import { TX_START_PREFIX } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
- import { FieldReader } from '@aztec/foundation/serialize';
3
+ import { BlobDeserializationError } from '../errors.js';
4
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
4
5
  const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
5
6
  const REVERT_CODE_BIT_SIZE = 8n;
6
7
  const NUM_NOTE_HASH_BIT_SIZE = 16n;
@@ -8,9 +9,9 @@ const NUM_NULLIFIER_BIT_SIZE = 16n;
8
9
  const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
9
10
  const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
10
11
  const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
12
+ const PRIVATE_LOGS_LENGTH_BIT_SIZE = 16n;
11
13
  const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
12
14
  const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
13
- // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
14
15
  export function encodeTxStartMarker(txStartMarker) {
15
16
  let value = TX_START_PREFIX;
16
17
  value <<= NUM_NOTE_HASH_BIT_SIZE;
@@ -23,6 +24,8 @@ export function encodeTxStartMarker(txStartMarker) {
23
24
  value += BigInt(txStartMarker.numPublicDataWrites);
24
25
  value <<= NUM_PRIVATE_LOG_BIT_SIZE;
25
26
  value += BigInt(txStartMarker.numPrivateLogs);
27
+ value <<= PRIVATE_LOGS_LENGTH_BIT_SIZE;
28
+ value += BigInt(txStartMarker.privateLogsLength);
26
29
  value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
27
30
  value += BigInt(txStartMarker.publicLogsLength);
28
31
  value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
@@ -43,6 +46,8 @@ export function decodeTxStartMarker(field) {
43
46
  value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
44
47
  const publicLogsLength = Number(value & 2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n);
45
48
  value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
49
+ const privateLogsLength = Number(value & 2n ** PRIVATE_LOGS_LENGTH_BIT_SIZE - 1n);
50
+ value >>= PRIVATE_LOGS_LENGTH_BIT_SIZE;
46
51
  const numPrivateLogs = Number(value & 2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n);
47
52
  value >>= NUM_PRIVATE_LOG_BIT_SIZE;
48
53
  const numPublicDataWrites = Number(value & 2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n);
@@ -53,11 +58,11 @@ export function decodeTxStartMarker(field) {
53
58
  value >>= NUM_NULLIFIER_BIT_SIZE;
54
59
  const numNoteHashes = Number(value & 2n ** NUM_NOTE_HASH_BIT_SIZE - 1n);
55
60
  value >>= NUM_NOTE_HASH_BIT_SIZE;
56
- // Do not throw if the prefix doesn't match.
57
- // The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
58
61
  const prefix = value;
62
+ if (prefix !== TX_START_PREFIX) {
63
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid tx start marker.`);
64
+ }
59
65
  return {
60
- prefix,
61
66
  numBlobFields,
62
67
  revertCode,
63
68
  numNoteHashes,
@@ -65,59 +70,8 @@ export function decodeTxStartMarker(field) {
65
70
  numL2ToL1Msgs,
66
71
  numPublicDataWrites,
67
72
  numPrivateLogs,
73
+ privateLogsLength,
68
74
  publicLogsLength,
69
75
  contractClassLogLength
70
76
  };
71
77
  }
72
- export function getNumBlobFieldsFromTxStartMarker(field) {
73
- return Number(field.toBigInt() & 2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n);
74
- }
75
- export function isValidTxStartMarker(txStartMarker) {
76
- return txStartMarker.prefix === TX_START_PREFIX;
77
- }
78
- export function createBlockEndMarker(numTxs) {
79
- // Must match the implementation in `block_rollup_public_inputs_composer.nr > create_block_end_marker`.
80
- return new Fr(BLOCK_END_PREFIX * 256n * 256n + BigInt(numTxs));
81
- }
82
- export function getNumTxsFromBlockEndMarker(field) {
83
- return Number(field.toBigInt() & 0xffffn);
84
- }
85
- export function isBlockEndMarker(field) {
86
- const value = field.toBigInt();
87
- const numTxs = value & 0xffffn;
88
- return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
89
- }
90
- /**
91
- * Check that the fields are emitted from the circuits and conform to the encoding.
92
- * @param blobFields - The concatenated fields from all blobs of an L1 block.
93
- */ export function checkBlobFieldsEncoding(blobFields) {
94
- const reader = FieldReader.asReader(blobFields);
95
- const checkpointPrefix = reader.readField();
96
- if (checkpointPrefix.toBigInt() !== BigInt(blobFields.length)) {
97
- return false;
98
- }
99
- const numFieldsInCheckpoint = checkpointPrefix.toNumber();
100
- let seenNumTxs = 0;
101
- while(reader.cursor < numFieldsInCheckpoint){
102
- const currentField = reader.readField();
103
- if (isBlockEndMarker(currentField)) {
104
- // Found a block end marker. Confirm that the number of txs in this block is correct.
105
- const numTxs = getNumTxsFromBlockEndMarker(currentField);
106
- if (numTxs !== seenNumTxs) {
107
- return false;
108
- }
109
- seenNumTxs = 0;
110
- continue;
111
- }
112
- // If the field is not a block end marker, it must be a tx start marker.
113
- const txStartMarker = decodeTxStartMarker(currentField);
114
- if (!isValidTxStartMarker(txStartMarker)) {
115
- return false;
116
- }
117
- seenNumTxs += 1;
118
- // Skip the remaining fields in this tx. -1 because we already read the tx start marker.
119
- reader.skip(txStartMarker.numBlobFields - 1);
120
- // TODO: Check the encoding of the tx if we want to be more strict.
121
- }
122
- return true;
123
- }
package/dest/index.d.ts CHANGED
@@ -2,8 +2,7 @@ export * from './blob.js';
2
2
  export * from './blob_batching.js';
3
3
  export * from './blob_utils.js';
4
4
  export * from './circuit_types/index.js';
5
- export * from './deserialize.js';
6
- export * from './encoding.js';
5
+ export * from './encoding/index.js';
7
6
  export * from './errors.js';
8
7
  export * from './hash.js';
9
8
  export * from './interface.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,eAAe,CAAC;AAC9B,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,qBAAqB,CAAC;AACpC,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
package/dest/index.js CHANGED
@@ -2,8 +2,7 @@ export * from './blob.js';
2
2
  export * from './blob_batching.js';
3
3
  export * from './blob_utils.js';
4
4
  export * from './circuit_types/index.js';
5
- export * from './deserialize.js';
6
- export * from './encoding.js';
5
+ export * from './encoding/index.js';
7
6
  export * from './errors.js';
8
7
  export * from './hash.js';
9
8
  export * from './interface.js';
package/dest/testing.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { Fr } from '@aztec/foundation/fields';
2
1
  import { Blob } from './blob.js';
3
2
  import { BatchedBlobAccumulator } from './blob_batching.js';
4
3
  import { SpongeBlob } from './sponge_blob.js';
4
+ export * from './encoding/fixtures.js';
5
5
  /**
6
6
  * Makes arbitrary poseidon sponge for blob inputs.
7
7
  * Note: will not verify inside the circuit.
@@ -16,18 +16,6 @@ export declare function makeSpongeBlob(seed?: number): SpongeBlob;
16
16
  * @returns A blob accumulator instance.
17
17
  */
18
18
  export declare function makeBatchedBlobAccumulator(seed?: number): BatchedBlobAccumulator;
19
- export declare function makeEncodedTxBlobFields(length: number): Fr[];
20
- export declare function makeEncodedBlockBlobFields(...lengths: number[]): Fr[];
21
- export declare function makeEncodedBlobFields(length: number): Fr[];
22
- /**
23
- * Make an encoded blob with the given length
24
- *
25
- * This will deserialise correctly in the archiver
26
- * @param length
27
- * @returns
28
- */
29
- export declare function makeEncodedBlob(length: number): Blob;
30
- export declare function makeEncodedBlobs(length: number): Blob[];
31
19
  /**
32
20
  * Make a blob with random fields.
33
21
  *
@@ -1 +1 @@
1
- {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAGA,OAAO,EAAuB,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAEnE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAI5D,OAAO,EAAmB,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE/D;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,SAAI,GAAG,UAAU,CAWnD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,SAAI,GAAG,sBAAsB,CAW3E;AAED,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,CAoB5D;AAED,wBAAgB,0BAA0B,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,CAMrE;AAGD,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,EAAE,EAAE,CAO1D;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAMpD;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,EAAE,CAGvD;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAEnD"}
1
+ {"version":3,"file":"testing.d.ts","sourceRoot":"","sources":["../src/testing.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAE5D,OAAO,EAAmB,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE/D,cAAc,wBAAwB,CAAC;AAEvC;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,SAAI,GAAG,UAAU,CAWnD;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,SAAI,GAAG,sBAAsB,CAW3E;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAEnD"}
package/dest/testing.js CHANGED
@@ -1,13 +1,10 @@
1
- import { FIELDS_PER_BLOB } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
- import { randomInt } from '@aztec/foundation/crypto';
4
2
  import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
5
3
  import { Blob } from './blob.js';
6
4
  import { BatchedBlobAccumulator } from './blob_batching.js';
7
- import { getBlobsPerL1Block } from './blob_utils.js';
8
5
  import { FinalBlobBatchingChallenges } from './circuit_types/index.js';
9
- import { createBlockEndMarker, encodeTxStartMarker } from './encoding.js';
10
6
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
7
+ export * from './encoding/fixtures.js';
11
8
  /**
12
9
  * Makes arbitrary poseidon sponge for blob inputs.
13
10
  * Note: will not verify inside the circuit.
@@ -24,62 +21,6 @@ import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
24
21
  */ export function makeBatchedBlobAccumulator(seed = 1) {
25
22
  return new BatchedBlobAccumulator(new Fr(seed), new Fr(seed + 1), new BLS12Fr(seed + 2), BLS12Point.random(), BLS12Point.random(), new Fr(seed + 3), new BLS12Fr(seed + 4), new FinalBlobBatchingChallenges(new Fr(seed + 5), new BLS12Fr(seed + 6)));
26
23
  }
27
- export function makeEncodedTxBlobFields(length) {
28
- const txStartMarker = {
29
- numBlobFields: length,
30
- // The rest of the values don't matter. The test components using it do not try to deserialize everything.
31
- // Only `checkBlobFieldsEncoding` is used and it only looks at `numBlobFields`. This might change in the future
32
- // when we add more thorough checks to `checkBlobFieldsEncoding`.
33
- revertCode: 0,
34
- numNoteHashes: 0,
35
- numNullifiers: 0,
36
- numL2ToL1Msgs: 0,
37
- numPublicDataWrites: 0,
38
- numPrivateLogs: 0,
39
- publicLogsLength: 0,
40
- contractClassLogLength: 0
41
- };
42
- return [
43
- encodeTxStartMarker(txStartMarker),
44
- ...Array.from({
45
- length: length - 1
46
- }, ()=>new Fr(randomInt(Number.MAX_SAFE_INTEGER)))
47
- ];
48
- }
49
- export function makeEncodedBlockBlobFields(...lengths) {
50
- return [
51
- ...lengths.length > 0 ? makeEncodedTxBlobFields(lengths[0] - 1) : [],
52
- ...lengths.slice(1).flatMap((length)=>makeEncodedTxBlobFields(length)),
53
- createBlockEndMarker(lengths.length)
54
- ];
55
- }
56
- // Create blob fields for a checkpoint with a single block.
57
- export function makeEncodedBlobFields(length) {
58
- if (length <= 2) {
59
- throw new Error('Encoded blob fields length must be greater than 2');
60
- }
61
- const checkpointPrefix = new Fr(length);
62
- return [
63
- checkpointPrefix,
64
- ...makeEncodedBlockBlobFields(length - 1)
65
- ]; // -1 to account for the checkpoint prefix.
66
- }
67
- /**
68
- * Make an encoded blob with the given length
69
- *
70
- * This will deserialise correctly in the archiver
71
- * @param length
72
- * @returns
73
- */ export function makeEncodedBlob(length) {
74
- if (length > FIELDS_PER_BLOB) {
75
- throw new Error(`A single encoded blob must be less than ${FIELDS_PER_BLOB} fields`);
76
- }
77
- return Blob.fromFields(makeEncodedBlobFields(length));
78
- }
79
- export function makeEncodedBlobs(length) {
80
- const fields = makeEncodedBlobFields(length);
81
- return getBlobsPerL1Block(fields);
82
- }
83
24
  /**
84
25
  * Make a blob with random fields.
85
26
  *
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@aztec/blob-lib",
3
- "version": "3.0.0-nightly.20251113",
3
+ "version": "3.0.0-nightly.20251115",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
7
- "./encoding": "./dest/encoding.js",
7
+ "./encoding": "./dest/encoding/index.js",
8
8
  "./types": "./dest/types.js",
9
9
  "./testing": "./dest/testing.js"
10
10
  },
@@ -27,8 +27,8 @@
27
27
  "../package.common.json"
28
28
  ],
29
29
  "dependencies": {
30
- "@aztec/constants": "3.0.0-nightly.20251113",
31
- "@aztec/foundation": "3.0.0-nightly.20251113",
30
+ "@aztec/constants": "3.0.0-nightly.20251115",
31
+ "@aztec/foundation": "3.0.0-nightly.20251115",
32
32
  "@crate-crypto/node-eth-kzg": "^0.10.0",
33
33
  "tslib": "^2.4.0"
34
34
  },
package/src/blob_utils.ts CHANGED
@@ -2,7 +2,7 @@ import { FIELDS_PER_BLOB } from '@aztec/constants';
2
2
  import { BLS12Point, Fr } from '@aztec/foundation/fields';
3
3
 
4
4
  import { Blob } from './blob.js';
5
- import { deserializeEncodedBlobToFields } from './deserialize.js';
5
+ import { type CheckpointBlobData, decodeCheckpointBlobDataFromBuffer } from './encoding/index.js';
6
6
  import { computeBlobFieldsHash, computeBlobsHash } from './hash.js';
7
7
 
8
8
  /**
@@ -40,16 +40,13 @@ export function getBlobsPerL1Block(fields: Fr[]): Blob[] {
40
40
  }
41
41
 
42
42
  /**
43
- * Get the fields from all blobs in the checkpoint. Ignoring the fields beyond the length specified by the
44
- * checkpoint prefix (the first field).
45
- *
46
- * @param blobs - The blobs to read fields from. Should be all the blobs in the L1 block proposing the checkpoint.
47
- * @param checkEncoding - Whether to check if the entire encoded blob fields are valid. If false, it will still check
48
- * the checkpoint prefix and throw if there's not enough fields.
49
- * @returns The fields added throughout the checkpoint.
43
+ * Get the encoded data from all blobs in the checkpoint.
44
+ * @param blobs - The blobs to read data from. Should be all the blobs for the L1 block proposing the checkpoint.
45
+ * @returns The encoded data of the checkpoint.
50
46
  */
51
- export function getBlobFieldsInCheckpoint(blobs: Blob[], checkEncoding = false): Fr[] {
52
- return deserializeEncodedBlobToFields(Buffer.concat(blobs.map(b => b.data)), checkEncoding);
47
+ export function decodeCheckpointBlobDataFromBlobs(blobs: Blob[]): CheckpointBlobData {
48
+ const buf = Buffer.concat(blobs.map(b => b.data));
49
+ return decodeCheckpointBlobDataFromBuffer(buf);
53
50
  }
54
51
 
55
52
  export async function computeBlobFieldsHashFromBlobs(blobs: Blob[]): Promise<Fr> {
@@ -0,0 +1,102 @@
1
+ import { Fr } from '@aztec/foundation/fields';
2
+ import { FieldReader } from '@aztec/foundation/serialize';
3
+
4
+ import { BlobDeserializationError } from '../errors.js';
5
+ import {
6
+ type BlockEndMarker,
7
+ decodeBlockEndMarker,
8
+ encodeBlockEndMarker,
9
+ isBlockEndMarker,
10
+ } from './block_end_marker.js';
11
+ import {
12
+ type BlockEndStateField,
13
+ decodeBlockEndStateField,
14
+ encodeBlockEndStateField,
15
+ } from './block_end_state_field.js';
16
+ import { type TxBlobData, decodeTxBlobData, encodeTxBlobData } from './tx_blob_data.js';
17
+
18
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/block_merge/components/block_blob_data.nr`.
19
+
20
+ export interface BlockEndBlobData {
21
+ blockEndMarker: BlockEndMarker;
22
+ blockEndStateField: BlockEndStateField;
23
+ lastArchiveRoot: Fr;
24
+ noteHashRoot: Fr;
25
+ nullifierRoot: Fr;
26
+ publicDataRoot: Fr;
27
+ l1ToL2MessageRoot: Fr | undefined;
28
+ }
29
+
30
+ export interface BlockBlobData extends BlockEndBlobData {
31
+ txs: TxBlobData[];
32
+ }
33
+
34
+ export function encodeBlockEndBlobData(blockEndBlobData: BlockEndBlobData): Fr[] {
35
+ return [
36
+ encodeBlockEndMarker(blockEndBlobData.blockEndMarker),
37
+ encodeBlockEndStateField(blockEndBlobData.blockEndStateField),
38
+ blockEndBlobData.lastArchiveRoot,
39
+ blockEndBlobData.noteHashRoot,
40
+ blockEndBlobData.nullifierRoot,
41
+ blockEndBlobData.publicDataRoot,
42
+ ...(blockEndBlobData.l1ToL2MessageRoot ? [blockEndBlobData.l1ToL2MessageRoot] : []),
43
+ ];
44
+ }
45
+
46
+ export function decodeBlockEndBlobData(fields: Fr[] | FieldReader, isFirstBlock: boolean): BlockEndBlobData {
47
+ const reader = FieldReader.asReader(fields);
48
+
49
+ const numBlockEndData = isFirstBlock ? 7 : 6;
50
+ if (numBlockEndData > reader.remainingFields()) {
51
+ throw new BlobDeserializationError(
52
+ `Incorrect encoding of blob fields: not enough fields for block end data. Expected ${numBlockEndData} fields, only ${reader.remainingFields()} remaining.`,
53
+ );
54
+ }
55
+
56
+ return {
57
+ blockEndMarker: decodeBlockEndMarker(reader.readField()),
58
+ blockEndStateField: decodeBlockEndStateField(reader.readField()),
59
+ lastArchiveRoot: reader.readField(),
60
+ noteHashRoot: reader.readField(),
61
+ nullifierRoot: reader.readField(),
62
+ publicDataRoot: reader.readField(),
63
+ l1ToL2MessageRoot: isFirstBlock ? reader.readField() : undefined,
64
+ };
65
+ }
66
+
67
+ export function encodeBlockBlobData(blockBlobData: BlockBlobData): Fr[] {
68
+ return [...blockBlobData.txs.map(tx => encodeTxBlobData(tx)).flat(), ...encodeBlockEndBlobData(blockBlobData)];
69
+ }
70
+
71
+ export function decodeBlockBlobData(fields: Fr[] | FieldReader, isFirstBlock: boolean): BlockBlobData {
72
+ const reader = FieldReader.asReader(fields);
73
+
74
+ const txs: TxBlobData[] = [];
75
+ let hasReachedBlockEnd = false;
76
+ while (!hasReachedBlockEnd) {
77
+ if (reader.isFinished()) {
78
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: not enough fields for block end marker.`);
79
+ }
80
+
81
+ const currentField = reader.peekField();
82
+ if (isBlockEndMarker(currentField)) {
83
+ hasReachedBlockEnd = true;
84
+ } else {
85
+ txs.push(decodeTxBlobData(reader));
86
+ }
87
+ }
88
+
89
+ const blockEndBlobData = decodeBlockEndBlobData(reader, isFirstBlock);
90
+
91
+ const blockEndMarker = blockEndBlobData.blockEndMarker;
92
+ if (blockEndMarker.numTxs !== txs.length) {
93
+ throw new BlobDeserializationError(
94
+ `Incorrect encoding of blob fields: expected ${blockEndMarker.numTxs} txs, but got ${txs.length}.`,
95
+ );
96
+ }
97
+
98
+ return {
99
+ txs,
100
+ ...blockEndBlobData,
101
+ };
102
+ }
@@ -0,0 +1,54 @@
1
+ import { BLOCK_END_PREFIX } from '@aztec/constants';
2
+ import { Fr } from '@aztec/foundation/fields';
3
+
4
+ import { BlobDeserializationError } from '../errors.js';
5
+
6
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/block_merge/components/block_blob_data.nr`.
7
+
8
+ const BLOCK_NUMBER_BIT_SIZE = 32n;
9
+ const TIMESTAMP_BIT_SIZE = 64n;
10
+ const NUM_TXS_BIT_SIZE = 16n;
11
+
12
+ export interface BlockEndMarker {
13
+ timestamp: bigint;
14
+ blockNumber: number;
15
+ numTxs: number;
16
+ }
17
+
18
+ export function encodeBlockEndMarker(blockEndMarker: BlockEndMarker) {
19
+ let value = BLOCK_END_PREFIX;
20
+ value <<= TIMESTAMP_BIT_SIZE;
21
+ value += blockEndMarker.timestamp;
22
+ value <<= BLOCK_NUMBER_BIT_SIZE;
23
+ value += BigInt(blockEndMarker.blockNumber);
24
+ value <<= NUM_TXS_BIT_SIZE;
25
+ value += BigInt(blockEndMarker.numTxs);
26
+ return new Fr(value);
27
+ }
28
+
29
+ export function decodeBlockEndMarker(field: Fr): BlockEndMarker {
30
+ let value = field.toBigInt();
31
+ const numTxs = Number(value & (2n ** NUM_TXS_BIT_SIZE - 1n));
32
+ value >>= NUM_TXS_BIT_SIZE;
33
+ const blockNumber = Number(value & (2n ** BLOCK_NUMBER_BIT_SIZE - 1n));
34
+ value >>= BLOCK_NUMBER_BIT_SIZE;
35
+ const timestamp = value & (2n ** TIMESTAMP_BIT_SIZE - 1n);
36
+ value >>= TIMESTAMP_BIT_SIZE;
37
+
38
+ const prefix = value;
39
+ if (prefix !== BLOCK_END_PREFIX) {
40
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid block end marker.`);
41
+ }
42
+
43
+ return {
44
+ blockNumber,
45
+ timestamp,
46
+ numTxs,
47
+ };
48
+ }
49
+
50
+ // Check if a field is a block end marker. Used before decoding to check if it has reached the end of the block.
51
+ export function isBlockEndMarker(field: Fr): boolean {
52
+ const prefix = field.toBigInt() >> (NUM_TXS_BIT_SIZE + BLOCK_NUMBER_BIT_SIZE + TIMESTAMP_BIT_SIZE);
53
+ return prefix === BLOCK_END_PREFIX;
54
+ }
@@ -0,0 +1,59 @@
1
+ import {
2
+ L1_TO_L2_MSG_TREE_HEIGHT,
3
+ NOTE_HASH_TREE_HEIGHT,
4
+ NULLIFIER_TREE_HEIGHT,
5
+ PUBLIC_DATA_TREE_HEIGHT,
6
+ } from '@aztec/constants';
7
+ import { Fr } from '@aztec/foundation/fields';
8
+
9
+ import { BlobDeserializationError } from '../errors.js';
10
+
11
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/block_merge/components/block_blob_data.nr`.
12
+
13
+ export const TOTAL_MANA_USED_BIT_SIZE = 48n;
14
+
15
+ export interface BlockEndStateField {
16
+ l1ToL2MessageNextAvailableLeafIndex: number;
17
+ noteHashNextAvailableLeafIndex: number;
18
+ nullifierNextAvailableLeafIndex: number;
19
+ publicDataNextAvailableLeafIndex: number;
20
+ totalManaUsed: bigint;
21
+ }
22
+
23
+ export function encodeBlockEndStateField(blockEndStateField: BlockEndStateField) {
24
+ let value = BigInt(blockEndStateField.l1ToL2MessageNextAvailableLeafIndex);
25
+ value <<= BigInt(NOTE_HASH_TREE_HEIGHT);
26
+ value += BigInt(blockEndStateField.noteHashNextAvailableLeafIndex);
27
+ value <<= BigInt(NULLIFIER_TREE_HEIGHT);
28
+ value += BigInt(blockEndStateField.nullifierNextAvailableLeafIndex);
29
+ value <<= BigInt(PUBLIC_DATA_TREE_HEIGHT);
30
+ value += BigInt(blockEndStateField.publicDataNextAvailableLeafIndex);
31
+ value <<= BigInt(TOTAL_MANA_USED_BIT_SIZE);
32
+ value += BigInt(blockEndStateField.totalManaUsed);
33
+ return new Fr(value);
34
+ }
35
+
36
+ export function decodeBlockEndStateField(field: Fr): BlockEndStateField {
37
+ let value = field.toBigInt();
38
+ const totalManaUsed = value & (2n ** TOTAL_MANA_USED_BIT_SIZE - 1n);
39
+ value >>= TOTAL_MANA_USED_BIT_SIZE;
40
+ const publicDataNextAvailableLeafIndex = Number(value & (2n ** BigInt(PUBLIC_DATA_TREE_HEIGHT) - 1n));
41
+ value >>= BigInt(PUBLIC_DATA_TREE_HEIGHT);
42
+ const nullifierNextAvailableLeafIndex = Number(value & (2n ** BigInt(NULLIFIER_TREE_HEIGHT) - 1n));
43
+ value >>= BigInt(NULLIFIER_TREE_HEIGHT);
44
+ const noteHashNextAvailableLeafIndex = Number(value & (2n ** BigInt(NOTE_HASH_TREE_HEIGHT) - 1n));
45
+ value >>= BigInt(NOTE_HASH_TREE_HEIGHT);
46
+
47
+ if (value > 2n ** BigInt(L1_TO_L2_MSG_TREE_HEIGHT) - 1n) {
48
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid block end state field.`);
49
+ }
50
+ const l1ToL2MessageNextAvailableLeafIndex = Number(value);
51
+
52
+ return {
53
+ l1ToL2MessageNextAvailableLeafIndex,
54
+ noteHashNextAvailableLeafIndex,
55
+ nullifierNextAvailableLeafIndex,
56
+ publicDataNextAvailableLeafIndex,
57
+ totalManaUsed,
58
+ };
59
+ }