@aztec/blob-lib 3.0.0-canary.a9708bd → 3.0.0-manual.20251030
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.
- package/dest/blob.d.ts +52 -95
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob.js +73 -165
- package/dest/blob_batching.d.ts +15 -48
- package/dest/blob_batching.d.ts.map +1 -1
- package/dest/blob_batching.js +81 -120
- package/dest/blob_utils.d.ts +30 -0
- package/dest/blob_utils.d.ts.map +1 -0
- package/dest/blob_utils.js +60 -0
- package/dest/circuit_types/blob_accumulator.d.ts +21 -0
- package/dest/circuit_types/blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/blob_accumulator.js +58 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts +22 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_accumulator.js +63 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts +15 -0
- package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -0
- package/dest/circuit_types/final_blob_batching_challenges.js +25 -0
- package/dest/circuit_types/index.d.ts +4 -0
- package/dest/circuit_types/index.d.ts.map +1 -0
- package/dest/circuit_types/index.js +4 -0
- package/dest/deserialize.d.ts +14 -0
- package/dest/deserialize.d.ts.map +1 -0
- package/dest/deserialize.js +33 -0
- package/dest/encoding.d.ts +22 -62
- package/dest/encoding.d.ts.map +1 -1
- package/dest/encoding.js +114 -104
- package/dest/hash.d.ts +35 -0
- package/dest/hash.d.ts.map +1 -0
- package/dest/hash.js +69 -0
- package/dest/index.d.ts +5 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +5 -15
- package/dest/kzg_context.d.ts +4 -0
- package/dest/kzg_context.d.ts.map +1 -0
- package/dest/kzg_context.js +5 -0
- package/dest/sponge_blob.d.ts +13 -9
- package/dest/sponge_blob.d.ts.map +1 -1
- package/dest/sponge_blob.js +28 -17
- package/dest/testing.d.ts +7 -12
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +54 -41
- package/dest/types.d.ts +16 -0
- package/dest/types.d.ts.map +1 -0
- package/dest/types.js +3 -0
- package/package.json +6 -4
- package/src/blob.ts +76 -191
- package/src/blob_batching.ts +109 -137
- package/src/blob_utils.ts +71 -0
- package/src/circuit_types/blob_accumulator.ts +84 -0
- package/src/circuit_types/final_blob_accumulator.ts +75 -0
- package/src/circuit_types/final_blob_batching_challenges.ts +29 -0
- package/src/circuit_types/index.ts +4 -0
- package/src/deserialize.ts +38 -0
- package/src/encoding.ts +136 -120
- package/src/hash.ts +77 -0
- package/src/index.ts +5 -18
- package/src/kzg_context.ts +5 -0
- package/src/sponge_blob.ts +24 -14
- package/src/testing.ts +55 -40
- package/src/types.ts +16 -0
- package/dest/blob_batching_public_inputs.d.ts +0 -71
- package/dest/blob_batching_public_inputs.d.ts.map +0 -1
- package/dest/blob_batching_public_inputs.js +0 -168
- package/src/blob_batching_public_inputs.ts +0 -252
package/src/encoding.ts
CHANGED
|
@@ -1,138 +1,154 @@
|
|
|
1
|
+
import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
|
|
1
2
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import {
|
|
3
|
+
import { FieldReader } from '@aztec/foundation/serialize';
|
|
3
4
|
|
|
4
|
-
|
|
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;
|
|
5
14
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
+
}
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
}
|
|
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
|
+
}
|
|
61
51
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
+
}
|
|
66
88
|
|
|
67
|
-
|
|
68
|
-
return
|
|
89
|
+
export function getNumBlobFieldsFromTxStartMarker(field: Fr) {
|
|
90
|
+
return Number(field.toBigInt() & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
|
|
69
91
|
}
|
|
70
92
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
93
|
+
export function isValidTxStartMarker(txStartMarker: TxStartMarker) {
|
|
94
|
+
return txStartMarker.prefix === TX_START_PREFIX;
|
|
95
|
+
}
|
|
96
|
+
|
|
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
|
+
}
|
|
101
|
+
|
|
102
|
+
export function getNumTxsFromBlockEndMarker(field: Fr) {
|
|
103
|
+
return Number(field.toBigInt() & 0xffffn);
|
|
104
|
+
}
|
|
105
|
+
|
|
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;
|
|
86
110
|
}
|
|
87
111
|
|
|
88
112
|
/**
|
|
89
|
-
*
|
|
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.
|
|
90
115
|
*/
|
|
91
|
-
export function
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
.equals(Buffer.alloc(field.size - TX_EFFECT_PREFIX_BYTE_LENGTH))
|
|
97
|
-
) {
|
|
98
|
-
return false;
|
|
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
|
-
) {
|
|
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)) {
|
|
109
121
|
return false;
|
|
110
122
|
}
|
|
111
|
-
return true;
|
|
112
|
-
}
|
|
113
123
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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.
|
|
134
151
|
}
|
|
135
152
|
|
|
136
|
-
|
|
137
|
-
return array.slice(0, lastNonZeroIndex + 1);
|
|
153
|
+
return true;
|
|
138
154
|
}
|
package/src/hash.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
|
|
2
|
+
import { BLS12Fr, Fr } from '@aztec/foundation/fields';
|
|
3
|
+
|
|
4
|
+
import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
|
|
5
|
+
|
|
6
|
+
const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
|
|
10
|
+
*/
|
|
11
|
+
export function computeEthVersionedBlobHash(commitment: Buffer): Buffer {
|
|
12
|
+
const hash = sha256(commitment);
|
|
13
|
+
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
14
|
+
return hash;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// TODO(#13430): The blobsHash is confusingly similar to blobCommitmentsHash, calculated from below blobCommitments:
|
|
18
|
+
// - blobsHash := sha256([blobhash_0, ..., blobhash_m]) = a hash of all blob hashes in a block with m+1 blobs inserted into the header, exists so a user can cross check blobs.
|
|
19
|
+
// - blobCommitmentsHash := sha256( ...sha256(sha256(C_0), C_1) ... C_n) = iteratively calculated hash of all blob commitments in an epoch with n+1 blobs (see calculateBlobCommitmentsHash()),
|
|
20
|
+
// exists so we can validate injected commitments to the rollup circuits correspond to the correct real blobs.
|
|
21
|
+
// We may be able to combine these values e.g. blobCommitmentsHash := sha256( ...sha256(sha256(blobshash_0), blobshash_1) ... blobshash_l) for an epoch with l+1 blocks.
|
|
22
|
+
export function computeBlobsHash(evmVersionedBlobHashes: Buffer[]): Fr {
|
|
23
|
+
return sha256ToField(evmVersionedBlobHashes);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The hash of the fields added throughout the checkpoint. The exact number of fields is specified by the checkpoint
|
|
28
|
+
* prefix (the first field). It's verified in the circuit against the fields absorbed into the sponge blob.
|
|
29
|
+
* This hash is used in generating the challenge z for all blobs in the same checkpoint.
|
|
30
|
+
*/
|
|
31
|
+
export async function computeBlobFieldsHash(fields: Fr[]): Promise<Fr> {
|
|
32
|
+
return await poseidon2Hash(fields);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function computeBlobCommitment(data: Uint8Array): Buffer {
|
|
36
|
+
if (data.length !== BYTES_PER_BLOB) {
|
|
37
|
+
throw new Error(`Expected ${BYTES_PER_BLOB} bytes per blob. Got ${data.length}.`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return Buffer.from(kzg.blobToKzgCommitment(data));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Get the commitment fields of the blob, to compute the challenge z.
|
|
45
|
+
*
|
|
46
|
+
* The 48-byte commitment is encoded into two field elements:
|
|
47
|
+
* +-------------------+------------------------+
|
|
48
|
+
* | 31 bytes | 17 bytes |
|
|
49
|
+
* +-------------------+------------------------+
|
|
50
|
+
* | Field Element 1 | Field Element 2 |
|
|
51
|
+
* | [0][bytes 0-30] | [0...0][bytes 31-47] |
|
|
52
|
+
* +-------------------+------------------------+
|
|
53
|
+
*
|
|
54
|
+
* @param commitment - The commitment to convert to fields. Computed from `computeBlobCommitment`.
|
|
55
|
+
* @returns The fields representing the commitment buffer.
|
|
56
|
+
*/
|
|
57
|
+
export function commitmentToFields(commitment: Buffer): [Fr, Fr] {
|
|
58
|
+
if (commitment.length !== BYTES_PER_COMMITMENT) {
|
|
59
|
+
throw new Error(`Expected ${BYTES_PER_COMMITMENT} bytes for blob commitment. Got ${commitment.length}.`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, BYTES_PER_COMMITMENT))];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function computeChallengeZ(blobFieldsHash: Fr, commitment: Buffer): Promise<Fr> {
|
|
66
|
+
const commitmentFields = commitmentToFields(commitment);
|
|
67
|
+
return await poseidon2Hash([blobFieldsHash, commitmentFields[0], commitmentFields[1]]);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Hash each u128 limb of the noir bignum struct representing the BLS field, to mimic the hash accumulation in the
|
|
72
|
+
* rollup circuits.
|
|
73
|
+
*/
|
|
74
|
+
export async function hashNoirBigNumLimbs(field: BLS12Fr): Promise<Fr> {
|
|
75
|
+
const num = field.toNoirBigNum();
|
|
76
|
+
return await poseidon2Hash(num.limbs.map(Fr.fromHexString));
|
|
77
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,23 +1,10 @@
|
|
|
1
|
-
import cKzg from 'c-kzg';
|
|
2
|
-
|
|
3
|
-
const { loadTrustedSetup } = cKzg;
|
|
4
|
-
|
|
5
1
|
export * from './blob.js';
|
|
6
2
|
export * from './blob_batching.js';
|
|
3
|
+
export * from './blob_utils.js';
|
|
4
|
+
export * from './circuit_types/index.js';
|
|
5
|
+
export * from './deserialize.js';
|
|
7
6
|
export * from './encoding.js';
|
|
8
|
-
export * from './interface.js';
|
|
9
7
|
export * from './errors.js';
|
|
10
|
-
export * from './
|
|
8
|
+
export * from './hash.js';
|
|
9
|
+
export * from './interface.js';
|
|
11
10
|
export * from './sponge_blob.js';
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
loadTrustedSetup();
|
|
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
|
-
}
|
package/src/sponge_blob.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TWO_POW_64 } from '@aztec/constants';
|
|
1
2
|
import { type FieldsOf, makeTuple } from '@aztec/foundation/array';
|
|
2
3
|
import { poseidon2Permutation } from '@aztec/foundation/crypto';
|
|
3
4
|
import { Fr } from '@aztec/foundation/fields';
|
|
@@ -10,17 +11,17 @@ import {
|
|
|
10
11
|
} from '@aztec/foundation/serialize';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
|
-
* A Poseidon2 sponge used to accumulate data that will be added to
|
|
14
|
+
* A Poseidon2 sponge used to accumulate data that will be added to blobs.
|
|
14
15
|
* See noir-projects/noir-protocol-circuits/crates/types/src/abis/sponge_blob.nr.
|
|
15
16
|
*/
|
|
16
17
|
export class SpongeBlob {
|
|
17
18
|
constructor(
|
|
18
|
-
/** Sponge with absorbed
|
|
19
|
+
/** Sponge with absorbed fields that will go into one or more blobs. */
|
|
19
20
|
public readonly sponge: Poseidon2Sponge,
|
|
20
21
|
/** Number of effects absorbed so far. */
|
|
21
|
-
public
|
|
22
|
+
public numAbsorbedFields: number,
|
|
22
23
|
/** Number of effects that will be absorbed. */
|
|
23
|
-
public readonly
|
|
24
|
+
public readonly numExpectedFields: number,
|
|
24
25
|
) {}
|
|
25
26
|
|
|
26
27
|
static fromBuffer(buffer: Buffer | BufferReader): SpongeBlob {
|
|
@@ -29,11 +30,11 @@ export class SpongeBlob {
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
toBuffer() {
|
|
32
|
-
return serializeToBuffer(
|
|
33
|
+
return serializeToBuffer(...SpongeBlob.getFields(this));
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
static getFields(fields: FieldsOf<SpongeBlob>) {
|
|
36
|
-
return [fields.sponge, fields.
|
|
37
|
+
return [fields.sponge, fields.numAbsorbedFields, fields.numExpectedFields];
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
toFields(): Fr[] {
|
|
@@ -54,19 +55,19 @@ export class SpongeBlob {
|
|
|
54
55
|
}
|
|
55
56
|
|
|
56
57
|
async absorb(fields: Fr[]) {
|
|
57
|
-
if (this.
|
|
58
|
+
if (this.numAbsorbedFields + fields.length > this.numExpectedFields) {
|
|
58
59
|
throw new Error(
|
|
59
|
-
`Attempted to fill
|
|
60
|
+
`Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${this.numExpectedFields}`,
|
|
60
61
|
);
|
|
61
62
|
}
|
|
62
63
|
await this.sponge.absorb(fields);
|
|
63
|
-
this.
|
|
64
|
+
this.numAbsorbedFields += fields.length;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
async squeeze(): Promise<Fr> {
|
|
67
68
|
// If the blob sponge is not 'full', we append 1 to match Poseidon2::hash_internal()
|
|
68
69
|
// NB: There is currently no use case in which we don't 'fill' a blob sponge, but adding for completeness
|
|
69
|
-
if (this.
|
|
70
|
+
if (this.numAbsorbedFields != this.numExpectedFields) {
|
|
70
71
|
await this.sponge.absorb([Fr.ONE]);
|
|
71
72
|
}
|
|
72
73
|
return this.sponge.squeeze();
|
|
@@ -76,8 +77,17 @@ export class SpongeBlob {
|
|
|
76
77
|
return new SpongeBlob(Poseidon2Sponge.empty(), 0, 0);
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
|
|
80
|
-
|
|
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);
|
|
81
91
|
}
|
|
82
92
|
}
|
|
83
93
|
|
|
@@ -131,8 +141,8 @@ export class Poseidon2Sponge {
|
|
|
131
141
|
);
|
|
132
142
|
}
|
|
133
143
|
|
|
134
|
-
static init(
|
|
135
|
-
const iv = new Fr(
|
|
144
|
+
static init(numExpectedFields: number): Poseidon2Sponge {
|
|
145
|
+
const iv = new Fr(numExpectedFields).mul(new Fr(TWO_POW_64));
|
|
136
146
|
const sponge = Poseidon2Sponge.empty();
|
|
137
147
|
sponge.state[3] = iv;
|
|
138
148
|
return sponge;
|
package/src/testing.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
import { FIELDS_PER_BLOB } from '@aztec/constants';
|
|
1
2
|
import { makeTuple } from '@aztec/foundation/array';
|
|
2
|
-
import {
|
|
3
|
+
import { randomInt } from '@aztec/foundation/crypto';
|
|
3
4
|
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
4
5
|
|
|
5
6
|
import { Blob } from './blob.js';
|
|
6
|
-
import { BatchedBlobAccumulator
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
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';
|
|
9
11
|
import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
|
|
10
12
|
|
|
11
13
|
/**
|
|
@@ -46,36 +48,44 @@ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
|
|
|
46
48
|
);
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
];
|
|
62
71
|
}
|
|
63
72
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
);
|
|
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
|
+
];
|
|
79
|
+
}
|
|
80
|
+
|
|
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.
|
|
79
89
|
}
|
|
80
90
|
|
|
81
91
|
/**
|
|
@@ -85,21 +95,26 @@ function encodeFirstField(length: number): Fr {
|
|
|
85
95
|
* @param length
|
|
86
96
|
* @returns
|
|
87
97
|
*/
|
|
88
|
-
export function makeEncodedBlob(length: number):
|
|
89
|
-
|
|
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);
|
|
90
109
|
}
|
|
91
110
|
|
|
92
111
|
/**
|
|
93
|
-
* Make
|
|
112
|
+
* Make a blob with random fields.
|
|
94
113
|
*
|
|
95
114
|
* This will fail deserialisation in the archiver
|
|
96
115
|
* @param length
|
|
97
116
|
* @returns
|
|
98
117
|
*/
|
|
99
|
-
export function
|
|
118
|
+
export function makeRandomBlob(length: number): Blob {
|
|
100
119
|
return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
|
|
101
120
|
}
|
|
102
|
-
|
|
103
|
-
export function makeEncodedBlobFields(fields: Fr[]): Promise<Blob> {
|
|
104
|
-
return Blob.fromFields([encodeFirstField(fields.length + 1), ...fields]);
|
|
105
|
-
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export * from './circuit_types/index.js';
|
|
2
|
+
export * from './interface.js';
|
|
3
|
+
export * from './sponge_blob.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Type definition for the KZG instance returned by Blob.getViemKzgInstance().
|
|
7
|
+
* Contains the cryptographic functions needed for blob commitment and proof generation.
|
|
8
|
+
*/
|
|
9
|
+
export interface BlobKzgInstance {
|
|
10
|
+
/** Function to compute KZG commitment from blob data */
|
|
11
|
+
blobToKzgCommitment(blob: Uint8Array): Uint8Array;
|
|
12
|
+
/** Function to compute KZG proof for blob data */
|
|
13
|
+
computeBlobKzgProof(blob: Uint8Array, commitment: Uint8Array): Uint8Array;
|
|
14
|
+
/** Function to compute both blob data cells and their corresponding KZG proofs for EIP7594 */
|
|
15
|
+
computeCellsAndKzgProofs(blob: Uint8Array): [Uint8Array[], Uint8Array[]];
|
|
16
|
+
}
|