@aztec/blob-lib 0.0.1-fake-c83136db25 → 0.0.1-fake-ceab37513c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dest/blob.d.ts +98 -52
  2. package/dest/blob.d.ts.map +1 -1
  3. package/dest/blob.js +167 -73
  4. package/dest/blob_batching.d.ts +48 -15
  5. package/dest/blob_batching.d.ts.map +1 -1
  6. package/dest/blob_batching.js +120 -81
  7. package/dest/blob_batching_public_inputs.d.ts +71 -0
  8. package/dest/blob_batching_public_inputs.d.ts.map +1 -0
  9. package/dest/blob_batching_public_inputs.js +168 -0
  10. package/dest/encoding.d.ts +62 -22
  11. package/dest/encoding.d.ts.map +1 -1
  12. package/dest/encoding.js +104 -114
  13. package/dest/index.d.ts +2 -5
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +15 -5
  16. package/dest/sponge_blob.d.ts +9 -13
  17. package/dest/sponge_blob.d.ts.map +1 -1
  18. package/dest/sponge_blob.js +17 -28
  19. package/dest/testing.d.ts +12 -7
  20. package/dest/testing.d.ts.map +1 -1
  21. package/dest/testing.js +41 -54
  22. package/dest/types.d.ts +0 -2
  23. package/dest/types.d.ts.map +1 -1
  24. package/dest/types.js +0 -2
  25. package/package.json +4 -5
  26. package/src/blob.ts +198 -76
  27. package/src/blob_batching.ts +137 -109
  28. package/src/blob_batching_public_inputs.ts +252 -0
  29. package/src/encoding.ts +120 -136
  30. package/src/index.ts +18 -5
  31. package/src/sponge_blob.ts +14 -24
  32. package/src/testing.ts +40 -55
  33. package/src/types.ts +2 -2
  34. package/dest/blob_utils.d.ts +0 -30
  35. package/dest/blob_utils.d.ts.map +0 -1
  36. package/dest/blob_utils.js +0 -60
  37. package/dest/circuit_types/blob_accumulator.d.ts +0 -21
  38. package/dest/circuit_types/blob_accumulator.d.ts.map +0 -1
  39. package/dest/circuit_types/blob_accumulator.js +0 -58
  40. package/dest/circuit_types/final_blob_accumulator.d.ts +0 -22
  41. package/dest/circuit_types/final_blob_accumulator.d.ts.map +0 -1
  42. package/dest/circuit_types/final_blob_accumulator.js +0 -63
  43. package/dest/circuit_types/final_blob_batching_challenges.d.ts +0 -15
  44. package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +0 -1
  45. package/dest/circuit_types/final_blob_batching_challenges.js +0 -25
  46. package/dest/circuit_types/index.d.ts +0 -4
  47. package/dest/circuit_types/index.d.ts.map +0 -1
  48. package/dest/circuit_types/index.js +0 -4
  49. package/dest/deserialize.d.ts +0 -14
  50. package/dest/deserialize.d.ts.map +0 -1
  51. package/dest/deserialize.js +0 -33
  52. package/dest/hash.d.ts +0 -35
  53. package/dest/hash.d.ts.map +0 -1
  54. package/dest/hash.js +0 -69
  55. package/dest/kzg_context.d.ts +0 -4
  56. package/dest/kzg_context.d.ts.map +0 -1
  57. package/dest/kzg_context.js +0 -5
  58. package/src/blob_utils.ts +0 -71
  59. package/src/circuit_types/blob_accumulator.ts +0 -84
  60. package/src/circuit_types/final_blob_accumulator.ts +0 -75
  61. package/src/circuit_types/final_blob_batching_challenges.ts +0 -29
  62. package/src/circuit_types/index.ts +0 -4
  63. package/src/deserialize.ts +0 -38
  64. package/src/hash.ts +0 -77
  65. package/src/kzg_context.ts +0 -5
package/src/encoding.ts CHANGED
@@ -1,154 +1,138 @@
1
- import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
2
1
  import { Fr } from '@aztec/foundation/fields';
3
- import { FieldReader } from '@aztec/foundation/serialize';
2
+ import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
4
3
 
5
- const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
6
- const REVERT_CODE_BIT_SIZE = 8n;
7
- const NUM_NOTE_HASH_BIT_SIZE = 16n;
8
- const NUM_NULLIFIER_BIT_SIZE = 16n;
9
- const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
10
- const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
11
- const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
12
- const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
13
- const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
4
+ import type { Blob as BlobBuffer } from 'c-kzg';
14
5
 
15
- export interface TxStartMarker {
16
- prefix: bigint;
17
- numBlobFields: number;
18
- revertCode: number;
19
- numNoteHashes: number;
20
- numNullifiers: number;
21
- numL2ToL1Msgs: number;
22
- numPublicDataWrites: number;
23
- numPrivateLogs: number;
24
- publicLogsLength: number;
25
- contractClassLogLength: number;
26
- }
27
-
28
- // Must match the implementation in `noir-protocol-circuits/crates/rollup-lib/src/tx_base/components/tx_blob_data.nr`.
29
- export function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>) {
30
- let value = TX_START_PREFIX;
31
- value <<= NUM_NOTE_HASH_BIT_SIZE;
32
- value += BigInt(txStartMarker.numNoteHashes);
33
- value <<= NUM_NULLIFIER_BIT_SIZE;
34
- value += BigInt(txStartMarker.numNullifiers);
35
- value <<= NUM_L2_TO_L1_MSG_BIT_SIZE;
36
- value += BigInt(txStartMarker.numL2ToL1Msgs);
37
- value <<= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
38
- value += BigInt(txStartMarker.numPublicDataWrites);
39
- value <<= NUM_PRIVATE_LOG_BIT_SIZE;
40
- value += BigInt(txStartMarker.numPrivateLogs);
41
- value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
42
- value += BigInt(txStartMarker.publicLogsLength);
43
- value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
44
- value += BigInt(txStartMarker.contractClassLogLength);
45
- value <<= REVERT_CODE_BIT_SIZE;
46
- value += BigInt(txStartMarker.revertCode);
47
- value <<= NUM_BLOB_FIELDS_BIT_SIZE;
48
- value += BigInt(txStartMarker.numBlobFields);
49
- return new Fr(value);
50
- }
51
-
52
- export function decodeTxStartMarker(field: Fr): TxStartMarker {
53
- let value = field.toBigInt();
54
- const numBlobFields = Number(value & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
55
- value >>= NUM_BLOB_FIELDS_BIT_SIZE;
56
- const revertCode = Number(value & (2n ** REVERT_CODE_BIT_SIZE - 1n));
57
- value >>= REVERT_CODE_BIT_SIZE;
58
- const contractClassLogLength = Number(value & (2n ** CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE - 1n));
59
- value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
60
- const publicLogsLength = Number(value & (2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n));
61
- value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
62
- const numPrivateLogs = Number(value & (2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n));
63
- value >>= NUM_PRIVATE_LOG_BIT_SIZE;
64
- const numPublicDataWrites = Number(value & (2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n));
65
- value >>= NUM_PUBLIC_DATA_WRITE_BIT_SIZE;
66
- const numL2ToL1Msgs = Number(value & (2n ** NUM_L2_TO_L1_MSG_BIT_SIZE - 1n));
67
- value >>= NUM_L2_TO_L1_MSG_BIT_SIZE;
68
- const numNullifiers = Number(value & (2n ** NUM_NULLIFIER_BIT_SIZE - 1n));
69
- value >>= NUM_NULLIFIER_BIT_SIZE;
70
- const numNoteHashes = Number(value & (2n ** NUM_NOTE_HASH_BIT_SIZE - 1n));
71
- value >>= NUM_NOTE_HASH_BIT_SIZE;
72
- // Do not throw if the prefix doesn't match.
73
- // The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
74
- const prefix = value;
75
- return {
76
- prefix,
77
- numBlobFields,
78
- revertCode,
79
- numNoteHashes,
80
- numNullifiers,
81
- numL2ToL1Msgs,
82
- numPublicDataWrites,
83
- numPrivateLogs,
84
- publicLogsLength,
85
- contractClassLogLength,
86
- };
87
- }
88
-
89
- export function getNumBlobFieldsFromTxStartMarker(field: Fr) {
90
- return Number(field.toBigInt() & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
91
- }
6
+ // Note duplicated from stdlib !
7
+ // This will appear as 0x74785f7374617274 in logs
8
+ export const TX_START_PREFIX = 8392562855083340404n;
9
+ // These are helper constants to decode tx effects from blob encoded fields
10
+ export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX.toString(16).length / 2;
11
+ // 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
12
+ export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7;
13
+ export const REVERT_CODE_PREFIX = 1;
92
14
 
93
- export function isValidTxStartMarker(txStartMarker: TxStartMarker) {
94
- return txStartMarker.prefix === TX_START_PREFIX;
95
- }
15
+ /**
16
+ * Deserializes a blob buffer into an array of field elements.
17
+ *
18
+ * Blobs are converted into BN254 fields to perform a poseidon2 hash on them (fieldHash).
19
+ * This method is sparse, meaning it does not include trailing zeros at the end of the blob.
20
+ *
21
+ * However, we cannot simply trim the zero's from the end of the blob, as some logs may include zero's
22
+ * within them.
23
+ * If we end on a set of zeros, such as the log below:
24
+ * length 7: [ a, b, c, d, e, 0, 0]
25
+ *
26
+ * we will end up with the incorrect hash if we trim the zeros from the end.
27
+ *
28
+ * Each transactions logs contains a TX start prefix, which includes a string followed
29
+ * by the length ( in field elements ) of the transaction's log.
30
+ *
31
+ * This function finds the end of the last transaction's logs, and returns the array up to this point.
32
+ *
33
+ * We search for a series of Tx Prefixes progressing the cursor in the field reader until we hit
34
+ * a field that is not a Tx Prefix, this indicates that we have reached the end of the last transaction's logs.
35
+ *
36
+ * +------------------+------------------+------------------+------------------+
37
+ * | TX1 Start Prefix | TX1 Log Fields | TX2 Start Prefix | Padded zeros |
38
+ * | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
39
+ * +------------------+------------------+------------------+------------------+
40
+ * ^
41
+ * |
42
+ * Function reads until here --------------------------------
43
+ *
44
+ * @param blob - The blob buffer to deserialize.
45
+ * @returns An array of field elements.
46
+ */
47
+ export function deserializeEncodedBlobToFields(blob: BlobBuffer): Fr[] {
48
+ // Convert blob buffer to array of field elements
49
+ const reader = BufferReader.asReader(blob);
50
+ const array = reader.readArray(blob.length >> 5, Fr); // >> 5 = / 32 (bytes per field)
51
+ const fieldReader = FieldReader.asReader(array);
52
+
53
+ // Read fields until we hit zeros at the end
54
+ while (!fieldReader.isFinished()) {
55
+ const currentField = fieldReader.peekField();
56
+
57
+ // Stop when we hit a zero field
58
+ if (!currentField || currentField.isZero()) {
59
+ break;
60
+ }
96
61
 
97
- export function createBlockEndMarker(numTxs: number) {
98
- // Must match the implementation in `block_rollup_public_inputs_composer.nr > create_block_end_marker`.
99
- return new Fr(BLOCK_END_PREFIX * 256n * 256n + BigInt(numTxs));
100
- }
62
+ // Skip the remaining fields in this transaction
63
+ const len = getLengthFromFirstField(currentField);
64
+ fieldReader.skip(len);
65
+ }
101
66
 
102
- export function getNumTxsFromBlockEndMarker(field: Fr) {
103
- return Number(field.toBigInt() & 0xffffn);
67
+ // Return array up to last non-zero field
68
+ return array.slice(0, fieldReader.cursor);
104
69
  }
105
70
 
106
- export function isBlockEndMarker(field: Fr) {
107
- const value = field.toBigInt();
108
- const numTxs = value & 0xffffn;
109
- return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
71
+ /**
72
+ * Get the length of the transaction from the first field.
73
+ *
74
+ * @param firstField - The first field of the transaction.
75
+ * @returns The length of the transaction.
76
+ *
77
+ * @throws If the first field does not include the correct prefix - encoding invalid.
78
+ */
79
+ export function getLengthFromFirstField(firstField: Fr): number {
80
+ // Check that the first field includes the correct prefix
81
+ if (!isValidFirstField(firstField)) {
82
+ throw new Error('Invalid prefix');
83
+ }
84
+ const buf = firstField.toBuffer().subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
85
+ return new Fr(buf.subarray(TX_START_PREFIX_BYTES_LENGTH + 1, TX_START_PREFIX_BYTES_LENGTH + 3)).toNumber();
110
86
  }
111
87
 
112
88
  /**
113
- * Check that the fields are emitted from the circuits and conform to the encoding.
114
- * @param blobFields - The concatenated fields from all blobs of an L1 block.
89
+ * Determines whether a field is the first field of a tx effect
115
90
  */
116
- export function checkBlobFieldsEncoding(blobFields: Fr[]) {
117
- const reader = FieldReader.asReader(blobFields);
118
-
119
- const checkpointPrefix = reader.readField();
120
- if (checkpointPrefix.toBigInt() !== BigInt(blobFields.length)) {
91
+ export function isValidFirstField(field: Fr): boolean {
92
+ const buf = field.toBuffer();
93
+ if (
94
+ !buf
95
+ .subarray(0, field.size - TX_EFFECT_PREFIX_BYTE_LENGTH)
96
+ .equals(Buffer.alloc(field.size - TX_EFFECT_PREFIX_BYTE_LENGTH))
97
+ ) {
121
98
  return false;
122
99
  }
100
+ const sliced = buf.subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
101
+ if (
102
+ // Checking we start with the correct prefix...
103
+ !new Fr(sliced.subarray(0, TX_START_PREFIX_BYTES_LENGTH)).equals(new Fr(TX_START_PREFIX)) ||
104
+ // ...and include the revert code prefix..
105
+ sliced[sliced.length - 3] !== REVERT_CODE_PREFIX ||
106
+ // ...and the following revert code is valid.
107
+ sliced[sliced.length - 1] > 4
108
+ ) {
109
+ return false;
110
+ }
111
+ return true;
112
+ }
123
113
 
124
- const numFieldsInCheckpoint = checkpointPrefix.toNumber();
125
- let seenNumTxs = 0;
126
- while (reader.cursor < numFieldsInCheckpoint) {
127
- const currentField = reader.readField();
128
-
129
- if (isBlockEndMarker(currentField)) {
130
- // Found a block end marker. Confirm that the number of txs in this block is correct.
131
- const numTxs = getNumTxsFromBlockEndMarker(currentField);
132
- if (numTxs !== seenNumTxs) {
133
- return false;
134
- }
135
- seenNumTxs = 0;
136
- // Continue the loop to process the next field.
137
- continue;
138
- }
139
-
140
- // If the field is not a block end marker, it must be a tx start marker.
141
- const txStartMarker = decodeTxStartMarker(currentField);
142
- if (!isValidTxStartMarker(txStartMarker)) {
143
- return false;
144
- }
145
-
146
- seenNumTxs += 1;
147
-
148
- // Skip the remaining fields in this tx. -1 because we already read the tx start marker.
149
- reader.skip(txStartMarker.numBlobFields - 1);
150
- // TODO: Check the encoding of the tx if we want to be more strict.
114
+ /**
115
+ * Extract the fields from a blob buffer, but do not take into account encoding
116
+ * that will include trailing zeros.
117
+ *
118
+ * +------------------+------------------+------------------+------------------+
119
+ * | | | | Padded zeros |
120
+ * | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
121
+ * +------------------+------------------+------------------+------------------+
122
+ * ^
123
+ * |
124
+ * Function reads until here ----------------------
125
+ */
126
+ export function extractBlobFieldsFromBuffer(blob: BlobBuffer): Fr[] {
127
+ const reader = BufferReader.asReader(blob);
128
+ const array = reader.readArray(blob.length >> 5, Fr);
129
+
130
+ // Find the index of the last non-zero field
131
+ let lastNonZeroIndex = array.length - 1;
132
+ while (lastNonZeroIndex >= 0 && array[lastNonZeroIndex].isZero()) {
133
+ lastNonZeroIndex--;
151
134
  }
152
135
 
153
- return true;
136
+ // Return the trimmed array
137
+ return array.slice(0, lastNonZeroIndex + 1);
154
138
  }
package/src/index.ts CHANGED
@@ -1,10 +1,23 @@
1
+ import cKzg from 'c-kzg';
2
+
3
+ const { loadTrustedSetup } = cKzg;
4
+
1
5
  export * from './blob.js';
2
6
  export * from './blob_batching.js';
3
- export * from './blob_utils.js';
4
- export * from './circuit_types/index.js';
5
- export * from './deserialize.js';
6
7
  export * from './encoding.js';
7
- export * from './errors.js';
8
- export * from './hash.js';
9
8
  export * from './interface.js';
9
+ export * from './errors.js';
10
+ export * from './blob_batching_public_inputs.js';
10
11
  export * from './sponge_blob.js';
12
+
13
+ try {
14
+ loadTrustedSetup(8); // See https://notes.ethereum.org/@jtraglia/windowed_multiplications
15
+ } catch (error: any) {
16
+ if (error.message.includes('trusted setup is already loaded')) {
17
+ // NB: The c-kzg lib has no way of checking whether the setup is loaded or not,
18
+ // and it throws an error if it's already loaded, even though nothing is wrong.
19
+ // This is a rudimentary way of ensuring we load the trusted setup if we need it.
20
+ } else {
21
+ throw new Error(error);
22
+ }
23
+ }
@@ -1,4 +1,3 @@
1
- import { TWO_POW_64 } from '@aztec/constants';
2
1
  import { type FieldsOf, makeTuple } from '@aztec/foundation/array';
3
2
  import { poseidon2Permutation } from '@aztec/foundation/crypto';
4
3
  import { Fr } from '@aztec/foundation/fields';
@@ -11,17 +10,17 @@ import {
11
10
  } from '@aztec/foundation/serialize';
12
11
 
13
12
  /**
14
- * A Poseidon2 sponge used to accumulate data that will be added to blobs.
13
+ * A Poseidon2 sponge used to accumulate data that will be added to a blob.
15
14
  * See noir-projects/noir-protocol-circuits/crates/types/src/abis/sponge_blob.nr.
16
15
  */
17
16
  export class SpongeBlob {
18
17
  constructor(
19
- /** Sponge with absorbed fields that will go into one or more blobs. */
18
+ /** Sponge with absorbed tx effects that will go into a blob. */
20
19
  public readonly sponge: Poseidon2Sponge,
21
20
  /** Number of effects absorbed so far. */
22
- public numAbsorbedFields: number,
21
+ public fields: number,
23
22
  /** Number of effects that will be absorbed. */
24
- public readonly numExpectedFields: number,
23
+ public readonly expectedFields: number,
25
24
  ) {}
26
25
 
27
26
  static fromBuffer(buffer: Buffer | BufferReader): SpongeBlob {
@@ -30,11 +29,11 @@ export class SpongeBlob {
30
29
  }
31
30
 
32
31
  toBuffer() {
33
- return serializeToBuffer(...SpongeBlob.getFields(this));
32
+ return serializeToBuffer(this.sponge, this.fields, this.expectedFields);
34
33
  }
35
34
 
36
35
  static getFields(fields: FieldsOf<SpongeBlob>) {
37
- return [fields.sponge, fields.numAbsorbedFields, fields.numExpectedFields];
36
+ return [fields.sponge, fields.fields, fields.expectedFields];
38
37
  }
39
38
 
40
39
  toFields(): Fr[] {
@@ -55,19 +54,19 @@ export class SpongeBlob {
55
54
  }
56
55
 
57
56
  async absorb(fields: Fr[]) {
58
- if (this.numAbsorbedFields + fields.length > this.numExpectedFields) {
57
+ if (this.fields + fields.length > this.expectedFields) {
59
58
  throw new Error(
60
- `Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${this.numExpectedFields}`,
59
+ `Attempted to fill spongeblob with ${this.fields + fields.length}, but it has a max of ${this.expectedFields}`,
61
60
  );
62
61
  }
63
62
  await this.sponge.absorb(fields);
64
- this.numAbsorbedFields += fields.length;
63
+ this.fields += fields.length;
65
64
  }
66
65
 
67
66
  async squeeze(): Promise<Fr> {
68
67
  // If the blob sponge is not 'full', we append 1 to match Poseidon2::hash_internal()
69
68
  // NB: There is currently no use case in which we don't 'fill' a blob sponge, but adding for completeness
70
- if (this.numAbsorbedFields != this.numExpectedFields) {
69
+ if (this.fields != this.expectedFields) {
71
70
  await this.sponge.absorb([Fr.ONE]);
72
71
  }
73
72
  return this.sponge.squeeze();
@@ -77,17 +76,8 @@ export class SpongeBlob {
77
76
  return new SpongeBlob(Poseidon2Sponge.empty(), 0, 0);
78
77
  }
79
78
 
80
- /**
81
- * Initialize the sponge blob with the number of expected fields in the checkpoint and absorb it as the first field.
82
- * Note: `numExpectedFields` includes the first field absorbed in this method.
83
- */
84
- static async init(numExpectedFields: number): Promise<SpongeBlob> {
85
- // This must match what the checkpoint root rollup circuit expects.
86
- // See noir-projects/noir-protocol-circuits/types/src/abis/sponge_blob.nr -> init_for_checkpoint.
87
- const sponge = Poseidon2Sponge.init(numExpectedFields);
88
- await sponge.absorb([new Fr(numExpectedFields)]);
89
- const numAbsorbedFields = 1;
90
- return new SpongeBlob(sponge, numAbsorbedFields, numExpectedFields);
79
+ static init(expectedFields: number): SpongeBlob {
80
+ return new SpongeBlob(Poseidon2Sponge.init(expectedFields), 0, expectedFields);
91
81
  }
92
82
  }
93
83
 
@@ -141,8 +131,8 @@ export class Poseidon2Sponge {
141
131
  );
142
132
  }
143
133
 
144
- static init(numExpectedFields: number): Poseidon2Sponge {
145
- const iv = new Fr(numExpectedFields).mul(new Fr(TWO_POW_64));
134
+ static init(expectedFields: number): Poseidon2Sponge {
135
+ const iv = new Fr(expectedFields).mul(new Fr(BigInt('18446744073709551616')));
146
136
  const sponge = Poseidon2Sponge.empty();
147
137
  sponge.state[3] = iv;
148
138
  return sponge;
package/src/testing.ts CHANGED
@@ -1,13 +1,11 @@
1
- import { FIELDS_PER_BLOB } from '@aztec/constants';
2
1
  import { makeTuple } from '@aztec/foundation/array';
3
- import { randomInt } from '@aztec/foundation/crypto';
2
+ import { toBufferBE } from '@aztec/foundation/bigint-buffer';
4
3
  import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
5
4
 
6
5
  import { Blob } from './blob.js';
7
- import { BatchedBlobAccumulator } from './blob_batching.js';
8
- import { getBlobsPerL1Block } from './blob_utils.js';
9
- import { FinalBlobBatchingChallenges } from './circuit_types/index.js';
10
- import { createBlockEndMarker, encodeTxStartMarker } from './encoding.js';
6
+ import { BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
7
+ import { BlobAccumulatorPublicInputs, BlockBlobPublicInputs } from './blob_batching_public_inputs.js';
8
+ import { TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
11
9
  import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
12
10
 
13
11
  /**
@@ -48,44 +46,36 @@ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
48
46
  );
49
47
  }
50
48
 
51
- export function makeEncodedTxBlobFields(length: number): Fr[] {
52
- const txStartMarker = {
53
- numBlobFields: length,
54
- // The rest of the values don't matter. The test components using it do not try to deserialize everything.
55
- // Only `checkBlobFieldsEncoding` is used and it only looks at `numBlobFields`. This might change in the future
56
- // when we add more thorough checks to `checkBlobFieldsEncoding`.
57
- revertCode: 0,
58
- numNoteHashes: 0,
59
- numNullifiers: 0,
60
- numL2ToL1Msgs: 0,
61
- numPublicDataWrites: 0,
62
- numPrivateLogs: 0,
63
- publicLogsLength: 0,
64
- contractClassLogLength: 0,
65
- };
66
-
67
- return [
68
- encodeTxStartMarker(txStartMarker),
69
- ...Array.from({ length: length - 1 }, () => new Fr(randomInt(Number.MAX_SAFE_INTEGER))), // -1 to account for the tx start marker.
70
- ];
71
- }
72
-
73
- export function makeEncodedBlockBlobFields(...lengths: number[]): Fr[] {
74
- return [
75
- ...(lengths.length > 0 ? makeEncodedTxBlobFields(lengths[0] - 1) : []), // -1 to account for the block end marker.
76
- ...lengths.slice(1).flatMap(length => makeEncodedTxBlobFields(length)),
77
- createBlockEndMarker(lengths.length),
78
- ];
49
+ /**
50
+ * Makes arbitrary block blob public inputs.
51
+ * Note: will not verify inside the circuit.
52
+ * @param seed - The seed to use for generating the blob inputs.
53
+ * @returns A block blob public inputs instance.
54
+ */
55
+ export function makeBlockBlobPublicInputs(seed = 1): BlockBlobPublicInputs {
56
+ const startBlobAccumulator = makeBatchedBlobAccumulator(seed);
57
+ return new BlockBlobPublicInputs(
58
+ BlobAccumulatorPublicInputs.fromBatchedBlobAccumulator(startBlobAccumulator),
59
+ BlobAccumulatorPublicInputs.fromBatchedBlobAccumulator(makeBatchedBlobAccumulator(seed + 1)),
60
+ startBlobAccumulator.finalBlobChallenges,
61
+ );
79
62
  }
80
63
 
81
- // Create blob fields for a checkpoint with a single block.
82
- export function makeEncodedBlobFields(length: number): Fr[] {
83
- if (length <= 2) {
84
- throw new Error('Encoded blob fields length must be greater than 2');
85
- }
86
-
87
- const checkpointPrefix = new Fr(length);
88
- return [checkpointPrefix, ...makeEncodedBlockBlobFields(length - 1)]; // -1 to account for the checkpoint prefix.
64
+ // TODO: copied form stdlib tx effect
65
+ function encodeFirstField(length: number): Fr {
66
+ const lengthBuf = Buffer.alloc(2);
67
+ lengthBuf.writeUInt16BE(length, 0);
68
+ return new Fr(
69
+ Buffer.concat([
70
+ toBufferBE(TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH),
71
+ Buffer.alloc(1),
72
+ lengthBuf,
73
+ Buffer.alloc(1),
74
+ Buffer.from([1]),
75
+ Buffer.alloc(1),
76
+ Buffer.alloc(1),
77
+ ]),
78
+ );
89
79
  }
90
80
 
91
81
  /**
@@ -95,26 +85,21 @@ export function makeEncodedBlobFields(length: number): Fr[] {
95
85
  * @param length
96
86
  * @returns
97
87
  */
98
- export function makeEncodedBlob(length: number): Blob {
99
- if (length > FIELDS_PER_BLOB) {
100
- throw new Error(`A single encoded blob must be less than ${FIELDS_PER_BLOB} fields`);
101
- }
102
-
103
- return Blob.fromFields(makeEncodedBlobFields(length));
104
- }
105
-
106
- export function makeEncodedBlobs(length: number): Blob[] {
107
- const fields = makeEncodedBlobFields(length);
108
- return getBlobsPerL1Block(fields);
88
+ export function makeEncodedBlob(length: number): Promise<Blob> {
89
+ return Blob.fromFields([encodeFirstField(length + 1), ...Array.from({ length: length }, () => Fr.random())]);
109
90
  }
110
91
 
111
92
  /**
112
- * Make a blob with random fields.
93
+ * Make an unencoded blob with the given length
113
94
  *
114
95
  * This will fail deserialisation in the archiver
115
96
  * @param length
116
97
  * @returns
117
98
  */
118
- export function makeRandomBlob(length: number): Blob {
99
+ export function makeUnencodedBlob(length: number): Promise<Blob> {
119
100
  return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
120
101
  }
102
+
103
+ export function makeEncodedBlobFields(fields: Fr[]): Promise<Blob> {
104
+ return Blob.fromFields([encodeFirstField(fields.length + 1), ...fields]);
105
+ }
package/src/types.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from './circuit_types/index.js';
2
- export * from './interface.js';
3
1
  export * from './sponge_blob.js';
4
2
 
3
+ // TODO: Separate functions that use c-kzg from classes and export those classes here.
4
+
5
5
  /**
6
6
  * Type definition for the KZG instance returned by Blob.getViemKzgInstance().
7
7
  * Contains the cryptographic functions needed for blob commitment and proof generation.
@@ -1,30 +0,0 @@
1
- import { BLS12Point, Fr } from '@aztec/foundation/fields';
2
- import { Blob } from './blob.js';
3
- /**
4
- * @param blobs - The blobs to emit.
5
- * @returns The blobs' compressed commitments in hex prefixed by the number of blobs. 1 byte for the prefix, 48 bytes
6
- * per blob commitment.
7
- * @dev Used for proposing blocks to validate injected blob commitments match real broadcast blobs.
8
- */
9
- export declare function getPrefixedEthBlobCommitments(blobs: Blob[]): `0x${string}`;
10
- /**
11
- * @param fields - Fields to broadcast in the blob(s)
12
- * @returns As many blobs as required to broadcast the given fields to an L1 block.
13
- *
14
- * @throws If the number of fields does not match what's indicated by the checkpoint prefix.
15
- */
16
- export declare function getBlobsPerL1Block(fields: Fr[]): Blob[];
17
- /**
18
- * Get the fields from all blobs in the checkpoint. Ignoring the fields beyond the length specified by the
19
- * checkpoint prefix (the first field).
20
- *
21
- * @param blobs - The blobs to read fields from. Should be all the blobs in the L1 block proposing the checkpoint.
22
- * @param checkEncoding - Whether to check if the entire encoded blob fields are valid. If false, it will still check
23
- * the checkpoint prefix and throw if there's not enough fields.
24
- * @returns The fields added throughout the checkpoint.
25
- */
26
- export declare function getBlobFieldsInCheckpoint(blobs: Blob[], checkEncoding?: boolean): Fr[];
27
- export declare function computeBlobFieldsHashFromBlobs(blobs: Blob[]): Promise<Fr>;
28
- export declare function computeBlobsHashFromBlobs(blobs: Blob[]): Fr;
29
- export declare function getBlobCommitmentsFromBlobs(blobs: Blob[]): BLS12Point[];
30
- //# sourceMappingURL=blob_utils.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"blob_utils.d.ts","sourceRoot":"","sources":["../src/blob_utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAIjC;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,KAAK,MAAM,EAAE,CAS1E;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,IAAI,EAAE,CASvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,aAAa,UAAQ,GAAG,EAAE,EAAE,CAEpF;AAED,wBAAsB,8BAA8B,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,CAQ/E;AAED,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAE3D;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,UAAU,EAAE,CAEvE"}
@@ -1,60 +0,0 @@
1
- import { FIELDS_PER_BLOB } from '@aztec/constants';
2
- import { BLS12Point } from '@aztec/foundation/fields';
3
- import { Blob } from './blob.js';
4
- import { deserializeEncodedBlobToFields } from './deserialize.js';
5
- import { computeBlobFieldsHash, computeBlobsHash } from './hash.js';
6
- /**
7
- * @param blobs - The blobs to emit.
8
- * @returns The blobs' compressed commitments in hex prefixed by the number of blobs. 1 byte for the prefix, 48 bytes
9
- * per blob commitment.
10
- * @dev Used for proposing blocks to validate injected blob commitments match real broadcast blobs.
11
- */ export function getPrefixedEthBlobCommitments(blobs) {
12
- // Prefix the number of blobs.
13
- const lenBuf = Buffer.alloc(1);
14
- lenBuf.writeUint8(blobs.length);
15
- const blobBuf = Buffer.concat(blobs.map((blob)=>blob.commitment));
16
- const buf = Buffer.concat([
17
- lenBuf,
18
- blobBuf
19
- ]);
20
- return `0x${buf.toString('hex')}`;
21
- }
22
- /**
23
- * @param fields - Fields to broadcast in the blob(s)
24
- * @returns As many blobs as required to broadcast the given fields to an L1 block.
25
- *
26
- * @throws If the number of fields does not match what's indicated by the checkpoint prefix.
27
- */ export function getBlobsPerL1Block(fields) {
28
- if (!fields.length) {
29
- throw new Error('Cannot create blobs from empty fields.');
30
- }
31
- const numBlobs = Math.ceil(fields.length / FIELDS_PER_BLOB);
32
- return Array.from({
33
- length: numBlobs
34
- }, (_, i)=>Blob.fromFields(fields.slice(i * FIELDS_PER_BLOB, (i + 1) * FIELDS_PER_BLOB)));
35
- }
36
- /**
37
- * Get the fields from all blobs in the checkpoint. Ignoring the fields beyond the length specified by the
38
- * checkpoint prefix (the first field).
39
- *
40
- * @param blobs - The blobs to read fields from. Should be all the blobs in the L1 block proposing the checkpoint.
41
- * @param checkEncoding - Whether to check if the entire encoded blob fields are valid. If false, it will still check
42
- * the checkpoint prefix and throw if there's not enough fields.
43
- * @returns The fields added throughout the checkpoint.
44
- */ export function getBlobFieldsInCheckpoint(blobs, checkEncoding = false) {
45
- return deserializeEncodedBlobToFields(Buffer.concat(blobs.map((b)=>b.data)), checkEncoding);
46
- }
47
- export async function computeBlobFieldsHashFromBlobs(blobs) {
48
- const fields = blobs.map((b)=>b.toFields()).flat();
49
- const numBlobFields = fields[0].toNumber();
50
- if (numBlobFields > fields.length) {
51
- throw new Error(`The prefix indicates ${numBlobFields} fields. Got ${fields.length}.`);
52
- }
53
- return await computeBlobFieldsHash(fields.slice(0, numBlobFields));
54
- }
55
- export function computeBlobsHashFromBlobs(blobs) {
56
- return computeBlobsHash(blobs.map((b)=>b.getEthVersionedBlobHash()));
57
- }
58
- export function getBlobCommitmentsFromBlobs(blobs) {
59
- return blobs.map((b)=>BLS12Point.decompress(b.commitment));
60
- }
@@ -1,21 +0,0 @@
1
- import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
2
- import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
3
- /**
4
- * See `noir-projects/noir-protocol-circuits/crates/blob/src/abis/blob_accumulator.nr` for documentation.
5
- */
6
- export declare class BlobAccumulator {
7
- blobCommitmentsHashAcc: Fr;
8
- zAcc: Fr;
9
- yAcc: BLS12Fr;
10
- cAcc: BLS12Point;
11
- gammaAcc: Fr;
12
- gammaPowAcc: BLS12Fr;
13
- constructor(blobCommitmentsHashAcc: Fr, zAcc: Fr, yAcc: BLS12Fr, cAcc: BLS12Point, gammaAcc: Fr, gammaPowAcc: BLS12Fr);
14
- static empty(): BlobAccumulator;
15
- equals(other: BlobAccumulator): boolean;
16
- static fromBuffer(buffer: Buffer | BufferReader): BlobAccumulator;
17
- toBuffer(): Buffer<ArrayBufferLike>;
18
- toFields(): Fr[];
19
- static fromFields(fields: Fr[] | FieldReader): BlobAccumulator;
20
- }
21
- //# sourceMappingURL=blob_accumulator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"blob_accumulator.d.ts","sourceRoot":"","sources":["../../src/circuit_types/blob_accumulator.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,WAAW,EAAqB,MAAM,6BAA6B,CAAC;AAE3F;;GAEG;AACH,qBAAa,eAAe;IAEjB,sBAAsB,EAAE,EAAE;IAC1B,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,OAAO;IACb,IAAI,EAAE,UAAU;IAChB,QAAQ,EAAE,EAAE;IACZ,WAAW,EAAE,OAAO;gBALpB,sBAAsB,EAAE,EAAE,EAC1B,IAAI,EAAE,EAAE,EACR,IAAI,EAAE,OAAO,EACb,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,EAAE,EACZ,WAAW,EAAE,OAAO;IAG7B,MAAM,CAAC,KAAK,IAAI,eAAe;IAI/B,MAAM,CAAC,KAAK,EAAE,eAAe;IAW7B,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,eAAe;IAYjE,QAAQ;IAWR,QAAQ;IAaR,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,WAAW,GAAG,eAAe;CAe/D"}