@aztec/blob-lib 0.0.1-commit.21caa21 → 0.0.1-commit.21ecf947b

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 (83) hide show
  1. package/dest/batched_blob.d.ts +8 -2
  2. package/dest/batched_blob.d.ts.map +1 -1
  3. package/dest/blob.d.ts +11 -5
  4. package/dest/blob.d.ts.map +1 -1
  5. package/dest/blob.js +7 -6
  6. package/dest/blob_batching.d.ts +15 -2
  7. package/dest/blob_batching.d.ts.map +1 -1
  8. package/dest/blob_batching.js +9 -7
  9. package/dest/blob_utils.d.ts +3 -2
  10. package/dest/blob_utils.d.ts.map +1 -1
  11. package/dest/blob_utils.js +1 -1
  12. package/dest/circuit_types/blob_accumulator.d.ts +3 -2
  13. package/dest/circuit_types/blob_accumulator.d.ts.map +1 -1
  14. package/dest/circuit_types/blob_accumulator.js +2 -1
  15. package/dest/circuit_types/final_blob_accumulator.d.ts +3 -2
  16. package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -1
  17. package/dest/circuit_types/final_blob_accumulator.js +5 -2
  18. package/dest/circuit_types/final_blob_batching_challenges.d.ts +3 -2
  19. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -1
  20. package/dest/circuit_types/final_blob_batching_challenges.js +2 -1
  21. package/dest/encoding/block_blob_data.d.ts +10 -2
  22. package/dest/encoding/block_blob_data.d.ts.map +1 -1
  23. package/dest/encoding/block_blob_data.js +11 -1
  24. package/dest/encoding/block_end_marker.d.ts +4 -3
  25. package/dest/encoding/block_end_marker.d.ts.map +1 -1
  26. package/dest/encoding/block_end_marker.js +6 -5
  27. package/dest/encoding/block_end_state_field.d.ts +2 -2
  28. package/dest/encoding/block_end_state_field.d.ts.map +1 -1
  29. package/dest/encoding/block_end_state_field.js +1 -1
  30. package/dest/encoding/checkpoint_blob_data.d.ts +2 -2
  31. package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -1
  32. package/dest/encoding/checkpoint_blob_data.js +6 -6
  33. package/dest/encoding/checkpoint_end_marker.d.ts +2 -2
  34. package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -1
  35. package/dest/encoding/checkpoint_end_marker.js +4 -4
  36. package/dest/encoding/fixtures.d.ts +1 -1
  37. package/dest/encoding/fixtures.d.ts.map +1 -1
  38. package/dest/encoding/fixtures.js +3 -2
  39. package/dest/encoding/tx_blob_data.d.ts +2 -2
  40. package/dest/encoding/tx_blob_data.d.ts.map +1 -1
  41. package/dest/encoding/tx_blob_data.js +1 -1
  42. package/dest/encoding/tx_start_marker.d.ts +2 -2
  43. package/dest/encoding/tx_start_marker.d.ts.map +1 -1
  44. package/dest/encoding/tx_start_marker.js +3 -3
  45. package/dest/hash.d.ts +3 -2
  46. package/dest/hash.d.ts.map +1 -1
  47. package/dest/hash.js +5 -4
  48. package/dest/index.d.ts +2 -1
  49. package/dest/index.d.ts.map +1 -1
  50. package/dest/index.js +1 -0
  51. package/dest/interface.d.ts +1 -2
  52. package/dest/interface.d.ts.map +1 -1
  53. package/dest/kzg_context.d.ts +6 -2
  54. package/dest/kzg_context.d.ts.map +1 -1
  55. package/dest/kzg_context.js +12 -3
  56. package/dest/sponge_blob.d.ts +4 -2
  57. package/dest/sponge_blob.d.ts.map +1 -1
  58. package/dest/sponge_blob.js +2 -2
  59. package/dest/testing.d.ts +1 -1
  60. package/dest/testing.d.ts.map +1 -1
  61. package/dest/testing.js +2 -1
  62. package/package.json +7 -7
  63. package/src/batched_blob.ts +2 -1
  64. package/src/blob.ts +7 -6
  65. package/src/blob_batching.ts +14 -7
  66. package/src/blob_utils.ts +2 -1
  67. package/src/circuit_types/blob_accumulator.ts +2 -1
  68. package/src/circuit_types/final_blob_accumulator.ts +2 -1
  69. package/src/circuit_types/final_blob_batching_challenges.ts +2 -1
  70. package/src/encoding/block_blob_data.ts +14 -2
  71. package/src/encoding/block_end_marker.ts +7 -6
  72. package/src/encoding/block_end_state_field.ts +1 -1
  73. package/src/encoding/checkpoint_blob_data.ts +13 -6
  74. package/src/encoding/checkpoint_end_marker.ts +4 -4
  75. package/src/encoding/fixtures.ts +3 -2
  76. package/src/encoding/tx_blob_data.ts +1 -1
  77. package/src/encoding/tx_start_marker.ts +3 -3
  78. package/src/hash.ts +6 -4
  79. package/src/index.ts +1 -0
  80. package/src/interface.ts +0 -1
  81. package/src/kzg_context.ts +12 -1
  82. package/src/sponge_blob.ts +2 -2
  83. package/src/testing.ts +2 -1
@@ -1,4 +1,4 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { FieldReader } from '@aztec/foundation/serialize';
3
3
 
4
4
  import { BlobDeserializationError } from '../errors.js';
@@ -17,6 +17,18 @@ import { type TxBlobData, decodeTxBlobData, encodeTxBlobData } from './tx_blob_d
17
17
 
18
18
  // Must match the implementation in `noir-protocol-circuits/crates/types/src/blob_data/block_blob_data.nr`.
19
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
+
20
32
  export interface BlockEndBlobData {
21
33
  blockEndMarker: BlockEndMarker;
22
34
  blockEndStateField: BlockEndStateField;
@@ -46,7 +58,7 @@ export function encodeBlockEndBlobData(blockEndBlobData: BlockEndBlobData): Fr[]
46
58
  export function decodeBlockEndBlobData(fields: Fr[] | FieldReader, isFirstBlock: boolean): BlockEndBlobData {
47
59
  const reader = FieldReader.asReader(fields);
48
60
 
49
- const numBlockEndData = isFirstBlock ? 7 : 6;
61
+ const numBlockEndData = getNumBlockEndBlobFields(isFirstBlock);
50
62
  if (numBlockEndData > reader.remainingFields()) {
51
63
  throw new BlobDeserializationError(
52
64
  `Incorrect encoding of blob fields: not enough fields for block end data. Expected ${numBlockEndData} fields, only ${reader.remainingFields()} remaining.`,
@@ -1,5 +1,6 @@
1
1
  import { BLOCK_END_PREFIX } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
 
4
5
  import { BlobDeserializationError } from '../errors.js';
5
6
 
@@ -11,12 +12,12 @@ const NUM_TXS_BIT_SIZE = 16n;
11
12
 
12
13
  export interface BlockEndMarker {
13
14
  timestamp: bigint;
14
- blockNumber: number;
15
+ blockNumber: BlockNumber;
15
16
  numTxs: number;
16
17
  }
17
18
 
18
19
  export function encodeBlockEndMarker(blockEndMarker: BlockEndMarker) {
19
- let value = BLOCK_END_PREFIX;
20
+ let value = BigInt(BLOCK_END_PREFIX);
20
21
  value <<= TIMESTAMP_BIT_SIZE;
21
22
  value += blockEndMarker.timestamp;
22
23
  value <<= BLOCK_NUMBER_BIT_SIZE;
@@ -30,13 +31,13 @@ export function decodeBlockEndMarker(field: Fr): BlockEndMarker {
30
31
  let value = field.toBigInt();
31
32
  const numTxs = Number(value & (2n ** NUM_TXS_BIT_SIZE - 1n));
32
33
  value >>= NUM_TXS_BIT_SIZE;
33
- const blockNumber = Number(value & (2n ** BLOCK_NUMBER_BIT_SIZE - 1n));
34
+ const blockNumber = BlockNumber(Number(value & (2n ** BLOCK_NUMBER_BIT_SIZE - 1n)));
34
35
  value >>= BLOCK_NUMBER_BIT_SIZE;
35
36
  const timestamp = value & (2n ** TIMESTAMP_BIT_SIZE - 1n);
36
37
  value >>= TIMESTAMP_BIT_SIZE;
37
38
 
38
39
  const prefix = value;
39
- if (prefix !== BLOCK_END_PREFIX) {
40
+ if (prefix !== BigInt(BLOCK_END_PREFIX)) {
40
41
  throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid block end marker.`);
41
42
  }
42
43
 
@@ -50,5 +51,5 @@ export function decodeBlockEndMarker(field: Fr): BlockEndMarker {
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.
51
52
  export function isBlockEndMarker(field: Fr): boolean {
52
53
  const prefix = field.toBigInt() >> (NUM_TXS_BIT_SIZE + BLOCK_NUMBER_BIT_SIZE + TIMESTAMP_BIT_SIZE);
53
- return prefix === BLOCK_END_PREFIX;
54
+ return prefix === BigInt(BLOCK_END_PREFIX);
54
55
  }
@@ -4,7 +4,7 @@ import {
4
4
  NULLIFIER_TREE_HEIGHT,
5
5
  PUBLIC_DATA_TREE_HEIGHT,
6
6
  } from '@aztec/constants';
7
- import { Fr } from '@aztec/foundation/fields';
7
+ import { Fr } from '@aztec/foundation/curves/bn254';
8
8
 
9
9
  import { BlobDeserializationError } from '../errors.js';
10
10
 
@@ -1,8 +1,15 @@
1
- import { Fr } from '@aztec/foundation/fields';
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
3
 
4
4
  import { BlobDeserializationError } from '../errors.js';
5
- import { type BlockBlobData, decodeBlockBlobData, encodeBlockBlobData } from './block_blob_data.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';
6
13
  import {
7
14
  type CheckpointEndMarker,
8
15
  decodeCheckpointEndMarker,
@@ -25,7 +32,7 @@ export function encodeCheckpointBlobData(checkpointBlobData: CheckpointBlobData)
25
32
 
26
33
  export function encodeCheckpointBlobDataFromBlocks(blocks: BlockBlobData[]): Fr[] {
27
34
  const blocksBlobFields = blocks.map(block => encodeBlockBlobData(block)).flat();
28
- const numBlobFields = blocksBlobFields.length + 1; // +1 for the checkpoint end marker.
35
+ const numBlobFields = blocksBlobFields.length + NUM_CHECKPOINT_END_MARKER_FIELDS;
29
36
  return blocksBlobFields.concat(encodeCheckpointEndMarker({ numBlobFields }));
30
37
  }
31
38
 
@@ -87,9 +94,9 @@ export function getTotalNumBlobFieldsFromTxs(txsPerBlock: TxStartMarker[][]): nu
87
94
  }
88
95
 
89
96
  return (
90
- (numBlocks ? 1 : 0) + // l1ToL2Messages root in the first block
91
- numBlocks * 6 + // 6 fields for each block end blob data.
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.
92
99
  txsPerBlock.reduce((total, txs) => total + txs.reduce((total, tx) => total + tx.numBlobFields, 0), 0) +
93
- 1 // checkpoint end marker
100
+ NUM_CHECKPOINT_END_MARKER_FIELDS // checkpoint end marker
94
101
  );
95
102
  }
@@ -1,5 +1,5 @@
1
1
  import { CHECKPOINT_END_PREFIX } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
 
4
4
  import { BlobDeserializationError } from '../errors.js';
5
5
 
@@ -12,7 +12,7 @@ export interface CheckpointEndMarker {
12
12
  }
13
13
 
14
14
  export function encodeCheckpointEndMarker(checkpointEndMarker: CheckpointEndMarker) {
15
- let value = CHECKPOINT_END_PREFIX;
15
+ let value = BigInt(CHECKPOINT_END_PREFIX);
16
16
  value <<= NUM_BLOB_FIELDS_BIT_SIZE;
17
17
  value += BigInt(checkpointEndMarker.numBlobFields);
18
18
  return new Fr(value);
@@ -24,7 +24,7 @@ export function decodeCheckpointEndMarker(field: Fr): CheckpointEndMarker {
24
24
  value >>= NUM_BLOB_FIELDS_BIT_SIZE;
25
25
 
26
26
  const prefix = value;
27
- if (prefix !== CHECKPOINT_END_PREFIX) {
27
+ if (prefix !== BigInt(CHECKPOINT_END_PREFIX)) {
28
28
  throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid checkpoint end marker.`);
29
29
  }
30
30
 
@@ -36,5 +36,5 @@ export function decodeCheckpointEndMarker(field: Fr): CheckpointEndMarker {
36
36
  // Check if a field is a checkpoint end marker. Used to check if it has reached the end of the blob fields.
37
37
  export function isCheckpointEndMarker(field: Fr): boolean {
38
38
  const prefix = field.toBigInt() >> NUM_BLOB_FIELDS_BIT_SIZE;
39
- return prefix === CHECKPOINT_END_PREFIX;
39
+ return prefix === BigInt(CHECKPOINT_END_PREFIX);
40
40
  }
@@ -9,7 +9,8 @@ import {
9
9
  PRIVATE_LOG_SIZE_IN_FIELDS,
10
10
  } from '@aztec/constants';
11
11
  import { makeTuple } from '@aztec/foundation/array';
12
- import { Fr } from '@aztec/foundation/fields';
12
+ import { BlockNumber } from '@aztec/foundation/branded-types';
13
+ import { Fr } from '@aztec/foundation/curves/bn254';
13
14
 
14
15
  import type { BlockBlobData, BlockEndBlobData } from './block_blob_data.js';
15
16
  import type { BlockEndMarker } from './block_end_marker.js';
@@ -109,7 +110,7 @@ export function makeBlockEndMarker({
109
110
  }: { seed?: number } & Partial<BlockEndMarker> = {}): BlockEndMarker {
110
111
  return {
111
112
  numTxs: seed,
112
- blockNumber: seed + 1,
113
+ blockNumber: BlockNumber(seed + 1),
113
114
  timestamp: BigInt(seed + 2),
114
115
  ...overrides,
115
116
  };
@@ -1,5 +1,5 @@
1
1
  import { chunk } from '@aztec/foundation/collection';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
  import { FieldReader } from '@aztec/foundation/serialize';
4
4
 
5
5
  import { BlobDeserializationError } from '../errors.js';
@@ -1,5 +1,5 @@
1
1
  import { TX_START_PREFIX } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
3
 
4
4
  import { BlobDeserializationError } from '../errors.js';
5
5
 
@@ -30,7 +30,7 @@ export interface TxStartMarker {
30
30
  }
31
31
 
32
32
  export function encodeTxStartMarker(txStartMarker: TxStartMarker): Fr {
33
- let value = TX_START_PREFIX;
33
+ let value = BigInt(TX_START_PREFIX);
34
34
  value <<= NUM_NOTE_HASH_BIT_SIZE;
35
35
  value += BigInt(txStartMarker.numNoteHashes);
36
36
  value <<= NUM_NULLIFIER_BIT_SIZE;
@@ -78,7 +78,7 @@ export function decodeTxStartMarker(field: Fr): TxStartMarker {
78
78
  value >>= NUM_NOTE_HASH_BIT_SIZE;
79
79
 
80
80
  const prefix = value;
81
- if (prefix !== TX_START_PREFIX) {
81
+ if (prefix !== BigInt(TX_START_PREFIX)) {
82
82
  throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid tx start marker.`);
83
83
  }
84
84
 
package/src/hash.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
2
- import { BLS12Fr, Fr } from '@aztec/foundation/fields';
1
+ import { poseidon2Hash } from '@aztec/foundation/crypto/poseidon';
2
+ import { sha256, sha256ToField } from '@aztec/foundation/crypto/sha256';
3
+ import { BLS12Fr } from '@aztec/foundation/curves/bls12';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
5
 
4
- import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
6
+ import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, getKzg } from './kzg_context.js';
5
7
  import { SpongeBlob } from './sponge_blob.js';
6
8
 
7
9
  const VERSIONED_HASH_VERSION_KZG = 0x01;
@@ -47,7 +49,7 @@ export function computeBlobCommitment(data: Uint8Array): Buffer {
47
49
  throw new Error(`Expected ${BYTES_PER_BLOB} bytes per blob. Got ${data.length}.`);
48
50
  }
49
51
 
50
- return Buffer.from(kzg.blobToKzgCommitment(data));
52
+ return Buffer.from(getKzg().blobToKzgCommitment(data));
51
53
  }
52
54
 
53
55
  /**
package/src/index.ts CHANGED
@@ -7,3 +7,4 @@ export * from './encoding/index.js';
7
7
  export * from './hash.js';
8
8
  export * from './interface.js';
9
9
  export * from './sponge_blob.js';
10
+ export * from './kzg_context.js';
package/src/interface.ts CHANGED
@@ -3,6 +3,5 @@
3
3
  */
4
4
  export interface BlobJson {
5
5
  blob: string;
6
- index: string;
7
6
  kzg_commitment: string;
8
7
  }
@@ -2,4 +2,15 @@ import { DasContextJs } from '@crate-crypto/node-eth-kzg';
2
2
 
3
3
  export * from '@crate-crypto/node-eth-kzg';
4
4
 
5
- export const kzg = DasContextJs.create({ usePrecomp: true });
5
+ let kzgInstance: DasContextJs | undefined;
6
+
7
+ /**
8
+ * Returns the lazily-initialized KZG context.
9
+ * The first call takes ~3 seconds to initialize the precomputation tables.
10
+ */
11
+ export function getKzg(): DasContextJs {
12
+ if (!kzgInstance) {
13
+ kzgInstance = DasContextJs.create({ usePrecomp: true });
14
+ }
15
+ return kzgInstance;
16
+ }
@@ -1,7 +1,7 @@
1
1
  import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, TWO_POW_64 } from '@aztec/constants';
2
2
  import { type FieldsOf, makeTuple } from '@aztec/foundation/array';
3
- import { poseidon2Permutation } from '@aztec/foundation/crypto';
4
- import { Fr } from '@aztec/foundation/fields';
3
+ import { poseidon2Permutation } from '@aztec/foundation/crypto/poseidon';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import {
6
6
  BufferReader,
7
7
  FieldReader,
package/src/testing.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { makeTuple } from '@aztec/foundation/array';
2
- import { BLS12Fq, BLS12Fr, BLS12Point, BLSPointNotOnCurveError, Fr } from '@aztec/foundation/fields';
2
+ import { BLS12Fq, BLS12Fr, BLS12Point, BLSPointNotOnCurveError } from '@aztec/foundation/curves/bls12';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
4
 
4
5
  import { Blob } from './blob.js';
5
6
  import { BlobAccumulator } from './circuit_types/blob_accumulator.js';