@aztec/blob-lib 0.0.1-commit.b655e406 → 0.0.1-commit.c0b82b2

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 (101) 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 +14 -14
  5. package/dest/blob.d.ts.map +1 -1
  6. package/dest/blob.js +21 -20
  7. package/dest/blob_batching.d.ts +35 -72
  8. package/dest/blob_batching.d.ts.map +1 -1
  9. package/dest/blob_batching.js +75 -110
  10. package/dest/blob_utils.d.ts +22 -12
  11. package/dest/blob_utils.d.ts.map +1 -1
  12. package/dest/blob_utils.js +32 -23
  13. package/dest/circuit_types/blob_accumulator.d.ts +4 -2
  14. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -1
  15. package/dest/circuit_types/blob_accumulator.js +5 -1
  16. package/dest/circuit_types/final_blob_accumulator.d.ts +3 -2
  17. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -1
  18. package/dest/circuit_types/final_blob_accumulator.js +5 -2
  19. package/dest/circuit_types/final_blob_batching_challenges.d.ts +3 -2
  20. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -1
  21. package/dest/circuit_types/final_blob_batching_challenges.js +2 -1
  22. package/dest/circuit_types/index.d.ts +1 -1
  23. package/dest/encoding/block_blob_data.d.ts +30 -0
  24. package/dest/encoding/block_blob_data.d.ts.map +1 -0
  25. package/dest/encoding/block_blob_data.js +75 -0
  26. package/dest/encoding/block_end_marker.d.ts +11 -0
  27. package/dest/encoding/block_end_marker.d.ts.map +1 -0
  28. package/dest/encoding/block_end_marker.js +41 -0
  29. package/dest/encoding/block_end_state_field.d.ts +12 -0
  30. package/dest/encoding/block_end_state_field.d.ts.map +1 -0
  31. package/dest/encoding/block_end_state_field.js +39 -0
  32. package/dest/encoding/checkpoint_blob_data.d.ts +15 -0
  33. package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
  34. package/dest/encoding/checkpoint_blob_data.js +67 -0
  35. package/dest/encoding/checkpoint_end_marker.d.ts +8 -0
  36. package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -0
  37. package/dest/encoding/checkpoint_end_marker.js +28 -0
  38. package/dest/encoding/fixtures.d.ts +41 -0
  39. package/dest/encoding/fixtures.d.ts.map +1 -0
  40. package/dest/encoding/fixtures.js +140 -0
  41. package/dest/encoding/index.d.ts +10 -0
  42. package/dest/encoding/index.d.ts.map +1 -0
  43. package/dest/encoding/index.js +9 -0
  44. package/dest/encoding/tx_blob_data.d.ts +19 -0
  45. package/dest/encoding/tx_blob_data.d.ts.map +1 -0
  46. package/dest/encoding/tx_blob_data.js +79 -0
  47. package/dest/encoding/tx_start_marker.d.ts +16 -0
  48. package/dest/encoding/tx_start_marker.d.ts.map +1 -0
  49. package/dest/{encoding.js → encoding/tx_start_marker.js} +14 -60
  50. package/dest/errors.d.ts +1 -1
  51. package/dest/errors.d.ts.map +1 -1
  52. package/dest/hash.d.ts +14 -6
  53. package/dest/hash.d.ts.map +1 -1
  54. package/dest/hash.js +25 -14
  55. package/dest/index.d.ts +4 -4
  56. package/dest/index.d.ts.map +1 -1
  57. package/dest/index.js +3 -3
  58. package/dest/interface.d.ts +1 -2
  59. package/dest/interface.d.ts.map +1 -1
  60. package/dest/kzg_context.d.ts +10 -4
  61. package/dest/kzg_context.d.ts.map +1 -1
  62. package/dest/kzg_context.js +29 -5
  63. package/dest/sponge_blob.d.ts +9 -13
  64. package/dest/sponge_blob.d.ts.map +1 -1
  65. package/dest/sponge_blob.js +21 -36
  66. package/dest/testing.d.ts +9 -17
  67. package/dest/testing.d.ts.map +1 -1
  68. package/dest/testing.js +35 -64
  69. package/dest/types.d.ts +2 -1
  70. package/dest/types.d.ts.map +1 -1
  71. package/dest/types.js +1 -0
  72. package/package.json +8 -7
  73. package/src/batched_blob.ts +26 -0
  74. package/src/blob.ts +21 -20
  75. package/src/blob_batching.ts +93 -128
  76. package/src/blob_utils.ts +38 -25
  77. package/src/circuit_types/blob_accumulator.ts +13 -1
  78. package/src/circuit_types/final_blob_accumulator.ts +2 -1
  79. package/src/circuit_types/final_blob_batching_challenges.ts +2 -1
  80. package/src/encoding/block_blob_data.ts +114 -0
  81. package/src/encoding/block_end_marker.ts +55 -0
  82. package/src/encoding/block_end_state_field.ts +59 -0
  83. package/src/encoding/checkpoint_blob_data.ts +102 -0
  84. package/src/encoding/checkpoint_end_marker.ts +40 -0
  85. package/src/encoding/fixtures.ts +210 -0
  86. package/src/encoding/index.ts +9 -0
  87. package/src/encoding/tx_blob_data.ts +116 -0
  88. package/src/{encoding.ts → encoding/tx_start_marker.ts} +20 -77
  89. package/src/hash.ts +26 -14
  90. package/src/index.ts +3 -3
  91. package/src/interface.ts +0 -1
  92. package/src/kzg_context.ts +39 -3
  93. package/src/sponge_blob.ts +23 -36
  94. package/src/testing.ts +48 -74
  95. package/src/types.ts +1 -0
  96. package/dest/deserialize.d.ts +0 -14
  97. package/dest/deserialize.d.ts.map +0 -1
  98. package/dest/deserialize.js +0 -33
  99. package/dest/encoding.d.ts +0 -26
  100. package/dest/encoding.d.ts.map +0 -1
  101. package/src/deserialize.ts +0 -38
package/src/blob_utils.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { FIELDS_PER_BLOB } from '@aztec/constants';
2
- import { BLS12Point, Fr } from '@aztec/foundation/fields';
2
+ import { BLS12Point } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
 
5
+ import type { BatchedBlob } from './batched_blob.js';
4
6
  import { Blob } from './blob.js';
5
- import { deserializeEncodedBlobToFields } from './deserialize.js';
6
- import { computeBlobFieldsHash, computeBlobsHash } from './hash.js';
7
+ import { type CheckpointBlobData, decodeCheckpointBlobDataFromBuffer } from './encoding/index.js';
8
+ import { computeBlobsHash, computeEthVersionedBlobHash } from './hash.js';
7
9
 
8
10
  /**
9
11
  * @param blobs - The blobs to emit.
@@ -28,38 +30,27 @@ export function getPrefixedEthBlobCommitments(blobs: Blob[]): `0x${string}` {
28
30
  *
29
31
  * @throws If the number of fields does not match what's indicated by the checkpoint prefix.
30
32
  */
31
- export function getBlobsPerL1Block(fields: Fr[]): Blob[] {
33
+ export async function getBlobsPerL1Block(fields: Fr[]): Promise<Blob[]> {
32
34
  if (!fields.length) {
33
35
  throw new Error('Cannot create blobs from empty fields.');
34
36
  }
35
37
 
36
38
  const numBlobs = Math.ceil(fields.length / FIELDS_PER_BLOB);
37
- return Array.from({ length: numBlobs }, (_, i) =>
38
- Blob.fromFields(fields.slice(i * FIELDS_PER_BLOB, (i + 1) * FIELDS_PER_BLOB)),
39
+ return await Promise.all(
40
+ Array.from({ length: numBlobs }, (_, i) =>
41
+ Blob.fromFields(fields.slice(i * FIELDS_PER_BLOB, (i + 1) * FIELDS_PER_BLOB)),
42
+ ),
39
43
  );
40
44
  }
41
45
 
42
46
  /**
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.
47
+ * Get the encoded data from all blobs in the checkpoint.
48
+ * @param blobs - The blobs to read data from. Should be all the blobs for the L1 block proposing the checkpoint.
49
+ * @returns The encoded data of the checkpoint.
50
50
  */
51
- export function getBlobFieldsInCheckpoint(blobs: Blob[], checkEncoding = false): Fr[] {
52
- return deserializeEncodedBlobToFields(Buffer.concat(blobs.map(b => b.data)), checkEncoding);
53
- }
54
-
55
- export async function computeBlobFieldsHashFromBlobs(blobs: Blob[]): Promise<Fr> {
56
- const fields = blobs.map(b => b.toFields()).flat();
57
- const numBlobFields = fields[0].toNumber();
58
- if (numBlobFields > fields.length) {
59
- throw new Error(`The prefix indicates ${numBlobFields} fields. Got ${fields.length}.`);
60
- }
61
-
62
- return await computeBlobFieldsHash(fields.slice(0, numBlobFields));
51
+ export function decodeCheckpointBlobDataFromBlobs(blobs: Blob[]): CheckpointBlobData {
52
+ const buf = Buffer.concat(blobs.map(b => b.data));
53
+ return decodeCheckpointBlobDataFromBuffer(buf);
63
54
  }
64
55
 
65
56
  export function computeBlobsHashFromBlobs(blobs: Blob[]): Fr {
@@ -69,3 +60,25 @@ export function computeBlobsHashFromBlobs(blobs: Blob[]): Fr {
69
60
  export function getBlobCommitmentsFromBlobs(blobs: Blob[]): BLS12Point[] {
70
61
  return blobs.map(b => BLS12Point.decompress(b.commitment));
71
62
  }
63
+
64
+ /**
65
+ * Returns a proof of opening of the blobs to verify on L1 using the point evaluation precompile:
66
+ *
67
+ * input[:32] - versioned_hash
68
+ * input[32:64] - z
69
+ * input[64:96] - y
70
+ * input[96:144] - commitment C
71
+ * input[144:192] - commitment Q (a 'proof' committing to the quotient polynomial q(X))
72
+ *
73
+ * See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
74
+ */
75
+ export function getEthBlobEvaluationInputs(batchedBlob: BatchedBlob): `0x${string}` {
76
+ const buf = Buffer.concat([
77
+ computeEthVersionedBlobHash(batchedBlob.commitment.compress()),
78
+ batchedBlob.z.toBuffer(),
79
+ batchedBlob.y.toBuffer(),
80
+ batchedBlob.commitment.compress(),
81
+ batchedBlob.q.compress(),
82
+ ]);
83
+ return `0x${buf.toString('hex')}`;
84
+ }
@@ -1,5 +1,6 @@
1
1
  import { BLS12_FQ_LIMBS, BLS12_FR_LIMBS } from '@aztec/constants';
2
- import { BLS12Fq, BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
2
+ import { BLS12Fq, BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
  import { BufferReader, FieldReader, serializeToBuffer } from '@aztec/foundation/serialize';
4
5
 
5
6
  /**
@@ -81,4 +82,15 @@ export class BlobAccumulator {
81
82
  BLS12Fr.fromNoirBigNum({ limbs: reader.readFieldArray(BLS12_FR_LIMBS).map(f => f.toString()) }),
82
83
  );
83
84
  }
85
+
86
+ static random() {
87
+ return new BlobAccumulator(
88
+ Fr.random(),
89
+ Fr.random(),
90
+ BLS12Fr.random(),
91
+ BLS12Point.random(),
92
+ Fr.random(),
93
+ BLS12Fr.random(),
94
+ );
95
+ }
84
96
  }
@@ -1,4 +1,5 @@
1
- import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
1
+ import { BLS12Fr, BLS12Point } from '@aztec/foundation/curves/bls12';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
3
4
 
4
5
  import { inspect } from 'util';
@@ -1,4 +1,5 @@
1
- import { BLS12Fr, Fr } from '@aztec/foundation/fields';
1
+ import { BLS12Fr } from '@aztec/foundation/curves/bls12';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
3
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
3
4
 
4
5
  /**
@@ -0,0 +1,114 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
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/types/src/blob_data/block_blob_data.nr`.
19
+
20
+ export const NUM_BLOCK_END_BLOB_FIELDS = 6;
21
+ export const NUM_FIRST_BLOCK_END_BLOB_FIELDS = 7;
22
+ export const NUM_CHECKPOINT_END_MARKER_FIELDS = 1;
23
+
24
+ /**
25
+ * Returns the number of blob fields used for block end data.
26
+ * @param isFirstBlockInCheckpoint - Whether this is the first block in a checkpoint.
27
+ */
28
+ export function getNumBlockEndBlobFields(isFirstBlockInCheckpoint: boolean): number {
29
+ return isFirstBlockInCheckpoint ? NUM_FIRST_BLOCK_END_BLOB_FIELDS : NUM_BLOCK_END_BLOB_FIELDS;
30
+ }
31
+
32
+ export interface BlockEndBlobData {
33
+ blockEndMarker: BlockEndMarker;
34
+ blockEndStateField: BlockEndStateField;
35
+ lastArchiveRoot: Fr;
36
+ noteHashRoot: Fr;
37
+ nullifierRoot: Fr;
38
+ publicDataRoot: Fr;
39
+ l1ToL2MessageRoot: Fr | undefined;
40
+ }
41
+
42
+ export interface BlockBlobData extends BlockEndBlobData {
43
+ txs: TxBlobData[];
44
+ }
45
+
46
+ export function encodeBlockEndBlobData(blockEndBlobData: BlockEndBlobData): Fr[] {
47
+ return [
48
+ encodeBlockEndMarker(blockEndBlobData.blockEndMarker),
49
+ encodeBlockEndStateField(blockEndBlobData.blockEndStateField),
50
+ blockEndBlobData.lastArchiveRoot,
51
+ blockEndBlobData.noteHashRoot,
52
+ blockEndBlobData.nullifierRoot,
53
+ blockEndBlobData.publicDataRoot,
54
+ ...(blockEndBlobData.l1ToL2MessageRoot ? [blockEndBlobData.l1ToL2MessageRoot] : []),
55
+ ];
56
+ }
57
+
58
+ export function decodeBlockEndBlobData(fields: Fr[] | FieldReader, isFirstBlock: boolean): BlockEndBlobData {
59
+ const reader = FieldReader.asReader(fields);
60
+
61
+ const numBlockEndData = getNumBlockEndBlobFields(isFirstBlock);
62
+ if (numBlockEndData > reader.remainingFields()) {
63
+ throw new BlobDeserializationError(
64
+ `Incorrect encoding of blob fields: not enough fields for block end data. Expected ${numBlockEndData} fields, only ${reader.remainingFields()} remaining.`,
65
+ );
66
+ }
67
+
68
+ return {
69
+ blockEndMarker: decodeBlockEndMarker(reader.readField()),
70
+ blockEndStateField: decodeBlockEndStateField(reader.readField()),
71
+ lastArchiveRoot: reader.readField(),
72
+ noteHashRoot: reader.readField(),
73
+ nullifierRoot: reader.readField(),
74
+ publicDataRoot: reader.readField(),
75
+ l1ToL2MessageRoot: isFirstBlock ? reader.readField() : undefined,
76
+ };
77
+ }
78
+
79
+ export function encodeBlockBlobData(blockBlobData: BlockBlobData): Fr[] {
80
+ return [...blockBlobData.txs.map(tx => encodeTxBlobData(tx)).flat(), ...encodeBlockEndBlobData(blockBlobData)];
81
+ }
82
+
83
+ export function decodeBlockBlobData(fields: Fr[] | FieldReader, isFirstBlock: boolean): BlockBlobData {
84
+ const reader = FieldReader.asReader(fields);
85
+
86
+ const txs: TxBlobData[] = [];
87
+ let hasReachedBlockEnd = false;
88
+ while (!hasReachedBlockEnd) {
89
+ if (reader.isFinished()) {
90
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: not enough fields for block end marker.`);
91
+ }
92
+
93
+ const currentField = reader.peekField();
94
+ if (isBlockEndMarker(currentField)) {
95
+ hasReachedBlockEnd = true;
96
+ } else {
97
+ txs.push(decodeTxBlobData(reader));
98
+ }
99
+ }
100
+
101
+ const blockEndBlobData = decodeBlockEndBlobData(reader, isFirstBlock);
102
+
103
+ const blockEndMarker = blockEndBlobData.blockEndMarker;
104
+ if (blockEndMarker.numTxs !== txs.length) {
105
+ throw new BlobDeserializationError(
106
+ `Incorrect encoding of blob fields: expected ${blockEndMarker.numTxs} txs, but got ${txs.length}.`,
107
+ );
108
+ }
109
+
110
+ return {
111
+ txs,
112
+ ...blockEndBlobData,
113
+ };
114
+ }
@@ -0,0 +1,55 @@
1
+ import { BLOCK_END_PREFIX } from '@aztec/constants';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
+
5
+ import { BlobDeserializationError } from '../errors.js';
6
+
7
+ // Must match the implementation in `noir-protocol-circuits/crates/types/src/blob_data/block_blob_data.nr`.
8
+
9
+ const BLOCK_NUMBER_BIT_SIZE = 32n;
10
+ const TIMESTAMP_BIT_SIZE = 64n;
11
+ const NUM_TXS_BIT_SIZE = 16n;
12
+
13
+ export interface BlockEndMarker {
14
+ timestamp: bigint;
15
+ blockNumber: BlockNumber;
16
+ numTxs: number;
17
+ }
18
+
19
+ export function encodeBlockEndMarker(blockEndMarker: BlockEndMarker) {
20
+ let value = BigInt(BLOCK_END_PREFIX);
21
+ value <<= TIMESTAMP_BIT_SIZE;
22
+ value += blockEndMarker.timestamp;
23
+ value <<= BLOCK_NUMBER_BIT_SIZE;
24
+ value += BigInt(blockEndMarker.blockNumber);
25
+ value <<= NUM_TXS_BIT_SIZE;
26
+ value += BigInt(blockEndMarker.numTxs);
27
+ return new Fr(value);
28
+ }
29
+
30
+ export function decodeBlockEndMarker(field: Fr): BlockEndMarker {
31
+ let value = field.toBigInt();
32
+ const numTxs = Number(value & (2n ** NUM_TXS_BIT_SIZE - 1n));
33
+ value >>= NUM_TXS_BIT_SIZE;
34
+ const blockNumber = BlockNumber(Number(value & (2n ** BLOCK_NUMBER_BIT_SIZE - 1n)));
35
+ value >>= BLOCK_NUMBER_BIT_SIZE;
36
+ const timestamp = value & (2n ** TIMESTAMP_BIT_SIZE - 1n);
37
+ value >>= TIMESTAMP_BIT_SIZE;
38
+
39
+ const prefix = value;
40
+ if (prefix !== BigInt(BLOCK_END_PREFIX)) {
41
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid block end marker.`);
42
+ }
43
+
44
+ return {
45
+ blockNumber,
46
+ timestamp,
47
+ numTxs,
48
+ };
49
+ }
50
+
51
+ // Check if a field is a block end marker. Used before decoding to check if it has reached the end of the block.
52
+ export function isBlockEndMarker(field: Fr): boolean {
53
+ const prefix = field.toBigInt() >> (NUM_TXS_BIT_SIZE + BLOCK_NUMBER_BIT_SIZE + TIMESTAMP_BIT_SIZE);
54
+ return prefix === BigInt(BLOCK_END_PREFIX);
55
+ }
@@ -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/curves/bn254';
8
+
9
+ import { BlobDeserializationError } from '../errors.js';
10
+
11
+ // Must match the implementation in `noir-protocol-circuits/crates/types/src/blob_data/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
+ }
@@ -0,0 +1,102 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
+
4
+ import { BlobDeserializationError } from '../errors.js';
5
+ import {
6
+ type BlockBlobData,
7
+ NUM_BLOCK_END_BLOB_FIELDS,
8
+ NUM_CHECKPOINT_END_MARKER_FIELDS,
9
+ NUM_FIRST_BLOCK_END_BLOB_FIELDS,
10
+ decodeBlockBlobData,
11
+ encodeBlockBlobData,
12
+ } from './block_blob_data.js';
13
+ import {
14
+ type CheckpointEndMarker,
15
+ decodeCheckpointEndMarker,
16
+ encodeCheckpointEndMarker,
17
+ isCheckpointEndMarker,
18
+ } from './checkpoint_end_marker.js';
19
+ import type { TxStartMarker } from './tx_start_marker.js';
20
+
21
+ export interface CheckpointBlobData {
22
+ checkpointEndMarker: CheckpointEndMarker;
23
+ blocks: BlockBlobData[];
24
+ }
25
+
26
+ export function encodeCheckpointBlobData(checkpointBlobData: CheckpointBlobData): Fr[] {
27
+ return [
28
+ ...checkpointBlobData.blocks.map(block => encodeBlockBlobData(block)).flat(),
29
+ encodeCheckpointEndMarker(checkpointBlobData.checkpointEndMarker),
30
+ ];
31
+ }
32
+
33
+ export function encodeCheckpointBlobDataFromBlocks(blocks: BlockBlobData[]): Fr[] {
34
+ const blocksBlobFields = blocks.map(block => encodeBlockBlobData(block)).flat();
35
+ const numBlobFields = blocksBlobFields.length + NUM_CHECKPOINT_END_MARKER_FIELDS;
36
+ return blocksBlobFields.concat(encodeCheckpointEndMarker({ numBlobFields }));
37
+ }
38
+
39
+ export function decodeCheckpointBlobData(fields: Fr[] | FieldReader): CheckpointBlobData {
40
+ const reader = FieldReader.asReader(fields);
41
+
42
+ if (reader.isFinished()) {
43
+ throw new BlobDeserializationError(`Cannot decode empty blob data.`);
44
+ }
45
+
46
+ const blocks = [];
47
+ let checkpointEndMarker: CheckpointEndMarker | undefined;
48
+ while (!reader.isFinished() && !checkpointEndMarker) {
49
+ blocks.push(decodeBlockBlobData(reader, blocks.length === 0 /* isFirstBlock */));
50
+
51
+ // After reading a block, the next item must be either a checkpoint end marker or another block.
52
+ // The first field of a block is always a tx start marker. So if the provided fields are valid, it's not possible to
53
+ // misinterpret a tx start marker as checkpoint end marker, or vice versa.
54
+ const nextField = reader.peekField();
55
+ if (isCheckpointEndMarker(nextField)) {
56
+ checkpointEndMarker = decodeCheckpointEndMarker(reader.readField());
57
+ const numFieldsRead = reader.cursor;
58
+ if (numFieldsRead !== checkpointEndMarker.numBlobFields) {
59
+ throw new BlobDeserializationError(
60
+ `Incorrect encoding of blob fields: mismatch number of blob fields. Expected ${checkpointEndMarker.numBlobFields} fields, got ${numFieldsRead}.`,
61
+ );
62
+ }
63
+ }
64
+ }
65
+
66
+ if (!checkpointEndMarker) {
67
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: checkpoint end marker does not exist.`);
68
+ }
69
+
70
+ const remainingFields = reader.readFieldArray(reader.remainingFields());
71
+ if (!remainingFields.every(f => f.isZero())) {
72
+ throw new BlobDeserializationError(
73
+ `Incorrect encoding of blob fields: unexpected non-zero field after checkpoint end marker.`,
74
+ );
75
+ }
76
+
77
+ return {
78
+ checkpointEndMarker,
79
+ blocks,
80
+ };
81
+ }
82
+
83
+ export function decodeCheckpointBlobDataFromBuffer(buf: Buffer): CheckpointBlobData {
84
+ const reader = BufferReader.asReader(buf);
85
+ const totalFieldsInBuffer = Math.floor(buf.length / Fr.SIZE_IN_BYTES);
86
+ const blobFields = reader.readArray(totalFieldsInBuffer, Fr);
87
+ return decodeCheckpointBlobData(blobFields);
88
+ }
89
+
90
+ export function getTotalNumBlobFieldsFromTxs(txsPerBlock: TxStartMarker[][]): number {
91
+ const numBlocks = txsPerBlock.length;
92
+ if (!numBlocks) {
93
+ return 0;
94
+ }
95
+
96
+ return (
97
+ (numBlocks ? NUM_FIRST_BLOCK_END_BLOB_FIELDS - NUM_BLOCK_END_BLOB_FIELDS : 0) + // l1ToL2Messages root in the first block
98
+ numBlocks * NUM_BLOCK_END_BLOB_FIELDS + // 6 fields for each block end blob data.
99
+ txsPerBlock.reduce((total, txs) => total + txs.reduce((total, tx) => total + tx.numBlobFields, 0), 0) +
100
+ NUM_CHECKPOINT_END_MARKER_FIELDS // checkpoint end marker
101
+ );
102
+ }
@@ -0,0 +1,40 @@
1
+ import { CHECKPOINT_END_PREFIX } from '@aztec/constants';
2
+ import { Fr } from '@aztec/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/checkpoint_blob_data.nr`.
7
+
8
+ const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
9
+
10
+ export interface CheckpointEndMarker {
11
+ numBlobFields: number;
12
+ }
13
+
14
+ export function encodeCheckpointEndMarker(checkpointEndMarker: CheckpointEndMarker) {
15
+ let value = BigInt(CHECKPOINT_END_PREFIX);
16
+ value <<= NUM_BLOB_FIELDS_BIT_SIZE;
17
+ value += BigInt(checkpointEndMarker.numBlobFields);
18
+ return new Fr(value);
19
+ }
20
+
21
+ export function decodeCheckpointEndMarker(field: Fr): CheckpointEndMarker {
22
+ let value = field.toBigInt();
23
+ const numBlobFields = Number(value & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
24
+ value >>= NUM_BLOB_FIELDS_BIT_SIZE;
25
+
26
+ const prefix = value;
27
+ if (prefix !== BigInt(CHECKPOINT_END_PREFIX)) {
28
+ throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid checkpoint end marker.`);
29
+ }
30
+
31
+ return {
32
+ numBlobFields,
33
+ };
34
+ }
35
+
36
+ // Check if a field is a checkpoint end marker. Used to check if it has reached the end of the blob fields.
37
+ export function isCheckpointEndMarker(field: Fr): boolean {
38
+ const prefix = field.toBigInt() >> NUM_BLOB_FIELDS_BIT_SIZE;
39
+ return prefix === BigInt(CHECKPOINT_END_PREFIX);
40
+ }