@aztec/blob-lib 3.0.0-nightly.20250926 → 3.0.0-nightly.20250927

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.
@@ -1,6 +1,6 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
2
  import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
- import { getLengthFromFirstField, isBlockEndMarker } from './encoding.js';
3
+ import { getNumBlobFieldsFromTxStartMarker, isBlockEndMarker } from './encoding.js';
4
4
  /**
5
5
  * Deserializes a blob buffer into an array of field elements.
6
6
  *
@@ -51,7 +51,7 @@ import { getLengthFromFirstField, isBlockEndMarker } from './encoding.js';
51
51
  break;
52
52
  }
53
53
  // Skip the remaining fields in this transaction
54
- const len = getLengthFromFirstField(currentField);
54
+ const len = getNumBlobFieldsFromTxStartMarker(currentField);
55
55
  fieldReader.skip(len);
56
56
  }
57
57
  // Return array up to last non-zero field
@@ -1,17 +1,21 @@
1
1
  import { Fr } from '@aztec/foundation/fields';
2
- export declare const TX_START_PREFIX_BYTES_LENGTH: number;
3
- export declare const TX_EFFECT_PREFIX_BYTE_LENGTH: number;
4
- export declare const REVERT_CODE_PREFIX = 1;
5
- /**
6
- * Get the length of the transaction from the first field.
7
- *
8
- * @param firstField - The first field of the transaction.
9
- * @returns The length of the transaction.
10
- *
11
- * @throws If the first field does not include the correct prefix - encoding invalid.
12
- */
13
- export declare function getLengthFromFirstField(firstField: Fr): number;
2
+ export interface TxStartMarker {
3
+ prefix: bigint;
4
+ numBlobFields: number;
5
+ revertCode: number;
6
+ numNoteHashes: number;
7
+ numNullifiers: number;
8
+ numL2ToL1Msgs: number;
9
+ numPublicDataWrites: number;
10
+ numPrivateLogs: number;
11
+ publicLogsLength: number;
12
+ contractClassLogLength: number;
13
+ }
14
+ export declare function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>): Fr;
15
+ export declare function decodeTxStartMarker(field: Fr): TxStartMarker;
16
+ export declare function getNumBlobFieldsFromTxStartMarker(field: Fr): number;
17
+ export declare function isValidTxStartMarker(txStartMarker: TxStartMarker): boolean;
14
18
  export declare function createBlockEndMarker(numTxs: number): Fr;
15
- export declare function isBlockEndMarker(field: Fr): boolean;
16
19
  export declare function getNumTxsFromBlockEndMarker(field: Fr): number;
20
+ export declare function isBlockEndMarker(field: Fr): boolean;
17
21
  //# sourceMappingURL=encoding.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAG9C,eAAO,MAAM,4BAA4B,QAA0C,CAAC;AAEpF,eAAO,MAAM,4BAA4B,QAAmC,CAAC;AAC7E,eAAO,MAAM,kBAAkB,IAAI,CAAC;AAEpC;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,EAAE,GAAG,MAAM,CAO9D;AA4BD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,MAGlD;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,EAAE,WAIzC;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,EAAE,UAEpD"}
1
+ {"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAY9C,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,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,gBAAgB,EAAE,MAAM,CAAC;IACzB,sBAAsB,EAAE,MAAM,CAAC;CAChC;AAGD,wBAAgB,mBAAmB,CAAC,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,MAqB/E;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,EAAE,GAAG,aAAa,CAmC5D;AAED,wBAAgB,iCAAiC,CAAC,KAAK,EAAE,EAAE,UAE1D;AAED,wBAAgB,oBAAoB,CAAC,aAAa,EAAE,aAAa,WAEhE;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,MAGlD;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,EAAE,UAEpD;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,EAAE,WAIzC"}
package/dest/encoding.js CHANGED
@@ -1,50 +1,88 @@
1
1
  import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
- // These are helper constants to decode tx effects from blob encoded fields
4
- export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX.toString(16).length / 2;
5
- // 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
6
- export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7;
7
- export const REVERT_CODE_PREFIX = 1;
8
- /**
9
- * Get the length of the transaction from the first field.
10
- *
11
- * @param firstField - The first field of the transaction.
12
- * @returns The length of the transaction.
13
- *
14
- * @throws If the first field does not include the correct prefix - encoding invalid.
15
- */ export function getLengthFromFirstField(firstField) {
16
- // Check that the first field includes the correct prefix
17
- if (!isValidFirstField(firstField)) {
18
- throw new Error('Invalid prefix');
19
- }
20
- const buf = firstField.toBuffer().subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
21
- return new Fr(buf.subarray(TX_START_PREFIX_BYTES_LENGTH + 1, TX_START_PREFIX_BYTES_LENGTH + 3)).toNumber();
3
+ const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
4
+ const REVERT_CODE_BIT_SIZE = 8n;
5
+ const NUM_NOTE_HASH_BIT_SIZE = 16n;
6
+ const NUM_NULLIFIER_BIT_SIZE = 16n;
7
+ const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
8
+ const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
9
+ const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
10
+ const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
11
+ const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
12
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
13
+ export function encodeTxStartMarker(txStartMarker) {
14
+ let value = TX_START_PREFIX;
15
+ value <<= NUM_NOTE_HASH_BIT_SIZE;
16
+ value += BigInt(txStartMarker.numNoteHashes);
17
+ value <<= NUM_NULLIFIER_BIT_SIZE;
18
+ value += BigInt(txStartMarker.numNullifiers);
19
+ value <<= NUM_L2_TO_L1_MSG_BIT_SIZE;
20
+ value += BigInt(txStartMarker.numL2ToL1Msgs);
21
+ value <<= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
22
+ value += BigInt(txStartMarker.numPublicDataWrites);
23
+ value <<= NUM_PRIVATE_LOG_BIT_SIZE;
24
+ value += BigInt(txStartMarker.numPrivateLogs);
25
+ value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
26
+ value += BigInt(txStartMarker.publicLogsLength);
27
+ value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
28
+ value += BigInt(txStartMarker.contractClassLogLength);
29
+ value <<= REVERT_CODE_BIT_SIZE;
30
+ value += BigInt(txStartMarker.revertCode);
31
+ value <<= NUM_BLOB_FIELDS_BIT_SIZE;
32
+ value += BigInt(txStartMarker.numBlobFields);
33
+ return new Fr(value);
22
34
  }
23
- /**
24
- * Determines whether a field is the first field of a tx effect
25
- */ function isValidFirstField(field) {
26
- const buf = field.toBuffer();
27
- if (!buf.subarray(0, field.size - TX_EFFECT_PREFIX_BYTE_LENGTH).equals(Buffer.alloc(field.size - TX_EFFECT_PREFIX_BYTE_LENGTH))) {
28
- return false;
29
- }
30
- const sliced = buf.subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
31
- if (// Checking we start with the correct prefix...
32
- !new Fr(sliced.subarray(0, TX_START_PREFIX_BYTES_LENGTH)).equals(new Fr(TX_START_PREFIX)) || // ...and include the revert code prefix..
33
- sliced[sliced.length - 3] !== REVERT_CODE_PREFIX || // ...and the following revert code is valid.
34
- sliced[sliced.length - 1] > 4) {
35
- return false;
36
- }
37
- return true;
35
+ export function decodeTxStartMarker(field) {
36
+ let value = field.toBigInt();
37
+ const numBlobFields = Number(value & 2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n);
38
+ value >>= NUM_BLOB_FIELDS_BIT_SIZE;
39
+ const revertCode = Number(value & 2n ** REVERT_CODE_BIT_SIZE - 1n);
40
+ value >>= REVERT_CODE_BIT_SIZE;
41
+ const contractClassLogLength = Number(value & 2n ** CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE - 1n);
42
+ value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
43
+ const publicLogsLength = Number(value & 2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n);
44
+ value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
45
+ const numPrivateLogs = Number(value & 2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n);
46
+ value >>= NUM_PRIVATE_LOG_BIT_SIZE;
47
+ const numPublicDataWrites = Number(value & 2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n);
48
+ value >>= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
49
+ const numL2ToL1Msgs = Number(value & 2n ** NUM_L2_TO_L1_MSG_BIT_SIZE - 1n);
50
+ value >>= NUM_L2_TO_L1_MSG_BIT_SIZE;
51
+ const numNullifiers = Number(value & 2n ** NUM_NULLIFIER_BIT_SIZE - 1n);
52
+ value >>= NUM_NULLIFIER_BIT_SIZE;
53
+ const numNoteHashes = Number(value & 2n ** NUM_NOTE_HASH_BIT_SIZE - 1n);
54
+ value >>= NUM_NOTE_HASH_BIT_SIZE;
55
+ // Do not throw if the prefix doesn't match.
56
+ // The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
57
+ const prefix = value;
58
+ return {
59
+ prefix,
60
+ numBlobFields,
61
+ revertCode,
62
+ numNoteHashes,
63
+ numNullifiers,
64
+ numL2ToL1Msgs,
65
+ numPublicDataWrites,
66
+ numPrivateLogs,
67
+ publicLogsLength,
68
+ contractClassLogLength
69
+ };
70
+ }
71
+ export function getNumBlobFieldsFromTxStartMarker(field) {
72
+ return Number(field.toBigInt() & 2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n);
73
+ }
74
+ export function isValidTxStartMarker(txStartMarker) {
75
+ return txStartMarker.prefix === TX_START_PREFIX;
38
76
  }
39
77
  export function createBlockEndMarker(numTxs) {
40
- // Should match the implementation in block_rollup_public_inputs_composer.nr > create_block_end_marker
78
+ // Must match the implementation in `block_rollup_public_inputs_composer.nr > create_block_end_marker`.
41
79
  return new Fr(BLOCK_END_PREFIX * 256n * 256n + BigInt(numTxs));
42
80
  }
81
+ export function getNumTxsFromBlockEndMarker(field) {
82
+ return Number(field.toBigInt() & 0xffffn);
83
+ }
43
84
  export function isBlockEndMarker(field) {
44
85
  const value = field.toBigInt();
45
86
  const numTxs = value & 0xffffn;
46
87
  return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
47
88
  }
48
- export function getNumTxsFromBlockEndMarker(field) {
49
- return Number(field.toBigInt() & 0xffffn);
50
- }
package/dest/testing.d.ts CHANGED
@@ -1,4 +1,3 @@
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';
@@ -25,12 +24,11 @@ export declare function makeBatchedBlobAccumulator(seed?: number): BatchedBlobAc
25
24
  */
26
25
  export declare function makeEncodedBlob(length: number): Promise<Blob>;
27
26
  /**
28
- * Make an unencoded blob with the given length
27
+ * Make a blob with random fields.
29
28
  *
30
29
  * This will fail deserialisation in the archiver
31
30
  * @param length
32
31
  * @returns
33
32
  */
34
- export declare function makeUnencodedBlob(length: number): Promise<Blob>;
35
- export declare function makeEncodedBlobFields(fields: Fr[]): Promise<Blob>;
33
+ export declare function makeRandomBlob(length: number): Promise<Blob>;
36
34
  //# sourceMappingURL=testing.d.ts.map
@@ -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,EAA+B,MAAM,oBAAoB,CAAC;AAEzF,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;AAmBD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/D;AAED,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAEjE"}
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,EAA+B,MAAM,oBAAoB,CAAC;AAEzF,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;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAkB7D;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5D"}
package/dest/testing.js CHANGED
@@ -1,10 +1,8 @@
1
- import { TX_START_PREFIX } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
4
2
  import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
5
3
  import { Blob } from './blob.js';
6
4
  import { BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
7
- import { TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
5
+ import { encodeTxStartMarker } from './encoding.js';
8
6
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
9
7
  /**
10
8
  * Makes arbitrary poseidon sponge for blob inputs.
@@ -22,22 +20,6 @@ import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
22
20
  */ export function makeBatchedBlobAccumulator(seed = 1) {
23
21
  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)));
24
22
  }
25
- // TODO: copied form stdlib tx effect
26
- function encodeFirstField(length) {
27
- const lengthBuf = Buffer.alloc(2);
28
- lengthBuf.writeUInt16BE(length, 0);
29
- return new Fr(Buffer.concat([
30
- toBufferBE(TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH),
31
- Buffer.alloc(1),
32
- lengthBuf,
33
- Buffer.alloc(1),
34
- Buffer.from([
35
- 1
36
- ]),
37
- Buffer.alloc(1),
38
- Buffer.alloc(1)
39
- ]));
40
- }
41
23
  /**
42
24
  * Make an encoded blob with the given length
43
25
  *
@@ -45,29 +27,36 @@ function encodeFirstField(length) {
45
27
  * @param length
46
28
  * @returns
47
29
  */ export function makeEncodedBlob(length) {
30
+ const txStartMarker = {
31
+ numBlobFields: length,
32
+ // The rest of the values don't matter. The test components using it only look at `numBlobFields` to split the blobs
33
+ // into fields for tx effects.
34
+ revertCode: 0,
35
+ numNoteHashes: 0,
36
+ numNullifiers: 0,
37
+ numL2ToL1Msgs: 0,
38
+ numPublicDataWrites: 0,
39
+ numPrivateLogs: 0,
40
+ publicLogsLength: 0,
41
+ contractClassLogLength: 0
42
+ };
48
43
  return Blob.fromFields([
49
- encodeFirstField(length + 1),
44
+ encodeTxStartMarker(txStartMarker),
50
45
  ...Array.from({
51
- length: length
46
+ length: length - 1
52
47
  }, ()=>Fr.random())
53
48
  ]);
54
49
  }
55
50
  /**
56
- * Make an unencoded blob with the given length
51
+ * Make a blob with random fields.
57
52
  *
58
53
  * This will fail deserialisation in the archiver
59
54
  * @param length
60
55
  * @returns
61
- */ export function makeUnencodedBlob(length) {
56
+ */ export function makeRandomBlob(length) {
62
57
  return Blob.fromFields([
63
58
  ...Array.from({
64
59
  length: length
65
60
  }, ()=>Fr.random())
66
61
  ]);
67
62
  }
68
- export function makeEncodedBlobFields(fields) {
69
- return Blob.fromFields([
70
- encodeFirstField(fields.length + 1),
71
- ...fields
72
- ]);
73
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/blob-lib",
3
- "version": "3.0.0-nightly.20250926",
3
+ "version": "3.0.0-nightly.20250927",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/index.js",
@@ -27,8 +27,8 @@
27
27
  "../package.common.json"
28
28
  ],
29
29
  "dependencies": {
30
- "@aztec/constants": "3.0.0-nightly.20250926",
31
- "@aztec/foundation": "3.0.0-nightly.20250926",
30
+ "@aztec/constants": "3.0.0-nightly.20250927",
31
+ "@aztec/foundation": "3.0.0-nightly.20250927",
32
32
  "c-kzg": "4.0.0-alpha.1",
33
33
  "tslib": "^2.4.0"
34
34
  },
@@ -3,7 +3,7 @@ import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
3
 
4
4
  import type { Blob as BlobBuffer } from 'c-kzg';
5
5
 
6
- import { getLengthFromFirstField, isBlockEndMarker } from './encoding.js';
6
+ import { getNumBlobFieldsFromTxStartMarker, isBlockEndMarker } from './encoding.js';
7
7
 
8
8
  /**
9
9
  * Deserializes a blob buffer into an array of field elements.
@@ -60,7 +60,7 @@ export function deserializeEncodedBlobToFields(blob: BlobBuffer): Fr[] {
60
60
  }
61
61
 
62
62
  // Skip the remaining fields in this transaction
63
- const len = getLengthFromFirstField(currentField);
63
+ const len = getNumBlobFieldsFromTxStartMarker(currentField);
64
64
  fieldReader.skip(len);
65
65
  }
66
66
 
package/src/encoding.ts CHANGED
@@ -1,66 +1,109 @@
1
1
  import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
2
2
  import { Fr } from '@aztec/foundation/fields';
3
3
 
4
- // These are helper constants to decode tx effects from blob encoded fields
5
- export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX.toString(16).length / 2;
6
- // 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
7
- export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7;
8
- export const REVERT_CODE_PREFIX = 1;
4
+ const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
5
+ const REVERT_CODE_BIT_SIZE = 8n;
6
+ const NUM_NOTE_HASH_BIT_SIZE = 16n;
7
+ const NUM_NULLIFIER_BIT_SIZE = 16n;
8
+ const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
9
+ const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
10
+ const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
11
+ const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
12
+ const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
9
13
 
10
- /**
11
- * Get the length of the transaction from the first field.
12
- *
13
- * @param firstField - The first field of the transaction.
14
- * @returns The length of the transaction.
15
- *
16
- * @throws If the first field does not include the correct prefix - encoding invalid.
17
- */
18
- export function getLengthFromFirstField(firstField: Fr): number {
19
- // Check that the first field includes the correct prefix
20
- if (!isValidFirstField(firstField)) {
21
- throw new Error('Invalid prefix');
22
- }
23
- const buf = firstField.toBuffer().subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
24
- return new Fr(buf.subarray(TX_START_PREFIX_BYTES_LENGTH + 1, TX_START_PREFIX_BYTES_LENGTH + 3)).toNumber();
14
+ export interface TxStartMarker {
15
+ prefix: bigint;
16
+ numBlobFields: number;
17
+ revertCode: number;
18
+ numNoteHashes: number;
19
+ numNullifiers: number;
20
+ numL2ToL1Msgs: number;
21
+ numPublicDataWrites: number;
22
+ numPrivateLogs: number;
23
+ publicLogsLength: number;
24
+ contractClassLogLength: number;
25
25
  }
26
26
 
27
- /**
28
- * Determines whether a field is the first field of a tx effect
29
- */
30
- function isValidFirstField(field: Fr): boolean {
31
- const buf = field.toBuffer();
32
- if (
33
- !buf
34
- .subarray(0, field.size - TX_EFFECT_PREFIX_BYTE_LENGTH)
35
- .equals(Buffer.alloc(field.size - TX_EFFECT_PREFIX_BYTE_LENGTH))
36
- ) {
37
- return false;
38
- }
39
- const sliced = buf.subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
40
- if (
41
- // Checking we start with the correct prefix...
42
- !new Fr(sliced.subarray(0, TX_START_PREFIX_BYTES_LENGTH)).equals(new Fr(TX_START_PREFIX)) ||
43
- // ...and include the revert code prefix..
44
- sliced[sliced.length - 3] !== REVERT_CODE_PREFIX ||
45
- // ...and the following revert code is valid.
46
- sliced[sliced.length - 1] > 4
47
- ) {
48
- return false;
49
- }
50
- return true;
27
+ // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
28
+ export function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>) {
29
+ let value = TX_START_PREFIX;
30
+ value <<= NUM_NOTE_HASH_BIT_SIZE;
31
+ value += BigInt(txStartMarker.numNoteHashes);
32
+ value <<= NUM_NULLIFIER_BIT_SIZE;
33
+ value += BigInt(txStartMarker.numNullifiers);
34
+ value <<= NUM_L2_TO_L1_MSG_BIT_SIZE;
35
+ value += BigInt(txStartMarker.numL2ToL1Msgs);
36
+ value <<= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
37
+ value += BigInt(txStartMarker.numPublicDataWrites);
38
+ value <<= NUM_PRIVATE_LOG_BIT_SIZE;
39
+ value += BigInt(txStartMarker.numPrivateLogs);
40
+ value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
41
+ value += BigInt(txStartMarker.publicLogsLength);
42
+ value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
43
+ value += BigInt(txStartMarker.contractClassLogLength);
44
+ value <<= REVERT_CODE_BIT_SIZE;
45
+ value += BigInt(txStartMarker.revertCode);
46
+ value <<= NUM_BLOB_FIELDS_BIT_SIZE;
47
+ value += BigInt(txStartMarker.numBlobFields);
48
+ return new Fr(value);
49
+ }
50
+
51
+ export function decodeTxStartMarker(field: Fr): TxStartMarker {
52
+ let value = field.toBigInt();
53
+ const numBlobFields = Number(value & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
54
+ value >>= NUM_BLOB_FIELDS_BIT_SIZE;
55
+ const revertCode = Number(value & (2n ** REVERT_CODE_BIT_SIZE - 1n));
56
+ value >>= REVERT_CODE_BIT_SIZE;
57
+ const contractClassLogLength = Number(value & (2n ** CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE - 1n));
58
+ value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
59
+ const publicLogsLength = Number(value & (2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n));
60
+ value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
61
+ const numPrivateLogs = Number(value & (2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n));
62
+ value >>= NUM_PRIVATE_LOG_BIT_SIZE;
63
+ const numPublicDataWrites = Number(value & (2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n));
64
+ value >>= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
65
+ const numL2ToL1Msgs = Number(value & (2n ** NUM_L2_TO_L1_MSG_BIT_SIZE - 1n));
66
+ value >>= NUM_L2_TO_L1_MSG_BIT_SIZE;
67
+ const numNullifiers = Number(value & (2n ** NUM_NULLIFIER_BIT_SIZE - 1n));
68
+ value >>= NUM_NULLIFIER_BIT_SIZE;
69
+ const numNoteHashes = Number(value & (2n ** NUM_NOTE_HASH_BIT_SIZE - 1n));
70
+ value >>= NUM_NOTE_HASH_BIT_SIZE;
71
+ // Do not throw if the prefix doesn't match.
72
+ // The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
73
+ const prefix = value;
74
+ return {
75
+ prefix,
76
+ numBlobFields,
77
+ revertCode,
78
+ numNoteHashes,
79
+ numNullifiers,
80
+ numL2ToL1Msgs,
81
+ numPublicDataWrites,
82
+ numPrivateLogs,
83
+ publicLogsLength,
84
+ contractClassLogLength,
85
+ };
86
+ }
87
+
88
+ export function getNumBlobFieldsFromTxStartMarker(field: Fr) {
89
+ return Number(field.toBigInt() & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
90
+ }
91
+
92
+ export function isValidTxStartMarker(txStartMarker: TxStartMarker) {
93
+ return txStartMarker.prefix === TX_START_PREFIX;
51
94
  }
52
95
 
53
96
  export function createBlockEndMarker(numTxs: number) {
54
- // Should match the implementation in block_rollup_public_inputs_composer.nr > create_block_end_marker
97
+ // Must match the implementation in `block_rollup_public_inputs_composer.nr > create_block_end_marker`.
55
98
  return new Fr(BLOCK_END_PREFIX * 256n * 256n + BigInt(numTxs));
56
99
  }
57
100
 
101
+ export function getNumTxsFromBlockEndMarker(field: Fr) {
102
+ return Number(field.toBigInt() & 0xffffn);
103
+ }
104
+
58
105
  export function isBlockEndMarker(field: Fr) {
59
106
  const value = field.toBigInt();
60
107
  const numTxs = value & 0xffffn;
61
108
  return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
62
109
  }
63
-
64
- export function getNumTxsFromBlockEndMarker(field: Fr) {
65
- return Number(field.toBigInt() & 0xffffn);
66
- }
package/src/testing.ts CHANGED
@@ -1,11 +1,9 @@
1
- import { TX_START_PREFIX } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
- import { toBufferBE } from '@aztec/foundation/bigint-buffer';
4
2
  import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
5
3
 
6
4
  import { Blob } from './blob.js';
7
5
  import { BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
8
- import { TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
6
+ import { encodeTxStartMarker } from './encoding.js';
9
7
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
10
8
 
11
9
  /**
@@ -46,23 +44,6 @@ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
46
44
  );
47
45
  }
48
46
 
49
- // TODO: copied form stdlib tx effect
50
- function encodeFirstField(length: number): Fr {
51
- const lengthBuf = Buffer.alloc(2);
52
- lengthBuf.writeUInt16BE(length, 0);
53
- return new Fr(
54
- Buffer.concat([
55
- toBufferBE(TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH),
56
- Buffer.alloc(1),
57
- lengthBuf,
58
- Buffer.alloc(1),
59
- Buffer.from([1]),
60
- Buffer.alloc(1),
61
- Buffer.alloc(1),
62
- ]),
63
- );
64
- }
65
-
66
47
  /**
67
48
  * Make an encoded blob with the given length
68
49
  *
@@ -71,20 +52,32 @@ function encodeFirstField(length: number): Fr {
71
52
  * @returns
72
53
  */
73
54
  export function makeEncodedBlob(length: number): Promise<Blob> {
74
- return Blob.fromFields([encodeFirstField(length + 1), ...Array.from({ length: length }, () => Fr.random())]);
55
+ const txStartMarker = {
56
+ numBlobFields: length,
57
+ // The rest of the values don't matter. The test components using it only look at `numBlobFields` to split the blobs
58
+ // into fields for tx effects.
59
+ revertCode: 0,
60
+ numNoteHashes: 0,
61
+ numNullifiers: 0,
62
+ numL2ToL1Msgs: 0,
63
+ numPublicDataWrites: 0,
64
+ numPrivateLogs: 0,
65
+ publicLogsLength: 0,
66
+ contractClassLogLength: 0,
67
+ };
68
+ return Blob.fromFields([
69
+ encodeTxStartMarker(txStartMarker),
70
+ ...Array.from({ length: length - 1 }, () => Fr.random()),
71
+ ]);
75
72
  }
76
73
 
77
74
  /**
78
- * Make an unencoded blob with the given length
75
+ * Make a blob with random fields.
79
76
  *
80
77
  * This will fail deserialisation in the archiver
81
78
  * @param length
82
79
  * @returns
83
80
  */
84
- export function makeUnencodedBlob(length: number): Promise<Blob> {
81
+ export function makeRandomBlob(length: number): Promise<Blob> {
85
82
  return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
86
83
  }
87
-
88
- export function makeEncodedBlobFields(fields: Fr[]): Promise<Blob> {
89
- return Blob.fromFields([encodeFirstField(fields.length + 1), ...fields]);
90
- }