@aztec/blob-lib 3.0.0-nightly.20251026 → 3.0.0-nightly.20251031
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 +47 -89
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob.js +62 -160
- package/dest/blob_batching.d.ts +14 -46
- package/dest/blob_batching.d.ts.map +1 -1
- package/dest/blob_batching.js +80 -100
- 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 +7 -41
- package/dest/deserialize.d.ts.map +1 -1
- package/dest/deserialize.js +25 -73
- package/dest/encoding.d.ts +5 -0
- package/dest/encoding.d.ts.map +1 -1
- package/dest/encoding.js +35 -0
- 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 +4 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +4 -2
- 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 -2
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +47 -14
- package/dest/types.d.ts +2 -0
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +2 -0
- package/package.json +4 -4
- package/src/blob.ts +67 -180
- package/src/blob_batching.ts +109 -119
- 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 +24 -79
- package/src/encoding.ts +45 -0
- package/src/hash.ts +77 -0
- package/src/index.ts +4 -2
- package/src/sponge_blob.ts +24 -14
- package/src/testing.ts +53 -16
- package/src/types.ts +2 -2
- package/dest/blob_batching_public_inputs.d.ts +0 -57
- package/dest/blob_batching_public_inputs.d.ts.map +0 -1
- package/dest/blob_batching_public_inputs.js +0 -144
- package/src/blob_batching_public_inputs.ts +0 -211
package/src/deserialize.ts
CHANGED
|
@@ -1,93 +1,38 @@
|
|
|
1
1
|
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import { BufferReader
|
|
2
|
+
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { checkBlobFieldsEncoding } from './encoding.js';
|
|
5
|
+
import { BlobDeserializationError } from './errors.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
|
-
* Deserializes a
|
|
8
|
+
* Deserializes a buffer into an array of field elements.
|
|
8
9
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
10
|
+
* This function returns the fields that were actually added in a checkpoint. The number of fields is specified by the
|
|
11
|
+
* first field.
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* length 7: [ a, b, c, d, e, 0, 0]
|
|
16
|
-
*
|
|
17
|
-
* we will end up with the incorrect hash if we trim the zeros from the end.
|
|
18
|
-
*
|
|
19
|
-
* Each transactions logs contains a TX start prefix, which includes a string followed
|
|
20
|
-
* by the length ( in field elements ) of the transaction's log.
|
|
21
|
-
*
|
|
22
|
-
* This function finds the end of the last transaction's logs, and returns the array up to this point.
|
|
23
|
-
*
|
|
24
|
-
* We search for a series of Tx Prefixes progressing the cursor in the field reader until we hit
|
|
25
|
-
* a field that is not a Tx Prefix, this indicates that we have reached the end of the last transaction's logs.
|
|
26
|
-
*
|
|
27
|
-
* +------------------+------------------+------------------+------------------+
|
|
28
|
-
* | TX1 Start Prefix | TX1 Log Fields | TX2 Start Prefix | Padded zeros |
|
|
29
|
-
* | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
|
|
30
|
-
* +------------------+------------------+------------------+------------------+
|
|
31
|
-
* ^
|
|
32
|
-
* |
|
|
33
|
-
* Function reads until here --------------------------------
|
|
34
|
-
*
|
|
35
|
-
* @param blob - The blob buffer to deserialize.
|
|
13
|
+
* @param buf - The buffer to deserialize.
|
|
14
|
+
* @param checkEncoding - Whether to check if the encoding is correct. If false, it will still check the checkpoint
|
|
15
|
+
* prefix and throw if there's not enough fields.
|
|
36
16
|
* @returns An array of field elements.
|
|
37
17
|
*/
|
|
38
|
-
export function deserializeEncodedBlobToFields(
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
// Stop when we hit a zero field
|
|
49
|
-
if (!currentField || currentField.isZero()) {
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// This comes before `getLengthFromFirstField` because an empty block doesn't have fields for the tx effect.
|
|
54
|
-
if (isBlockEndMarker(currentField)) {
|
|
55
|
-
// Include the block end marker in the result
|
|
56
|
-
fieldReader.skip(1);
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Skip the remaining fields in this transaction
|
|
61
|
-
const len = getNumBlobFieldsFromTxStartMarker(currentField);
|
|
62
|
-
fieldReader.skip(len);
|
|
18
|
+
export function deserializeEncodedBlobToFields(buf: Uint8Array, checkEncoding = false): Fr[] {
|
|
19
|
+
const reader = BufferReader.asReader(buf);
|
|
20
|
+
const firstField = reader.readObject(Fr);
|
|
21
|
+
|
|
22
|
+
// Use toBigInt instead of toNumber so that we can catch it and throw a more descriptive error below if the first
|
|
23
|
+
// field is larger than a javascript integer.
|
|
24
|
+
const numFields = firstField.toBigInt();
|
|
25
|
+
const totalFieldsInBuffer = BigInt(buf.length / Fr.SIZE_IN_BYTES);
|
|
26
|
+
if (numFields > totalFieldsInBuffer) {
|
|
27
|
+
throw new BlobDeserializationError(`Failed to deserialize blob fields, this blob was likely not created by us`);
|
|
63
28
|
}
|
|
64
29
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Extract the fields from a blob buffer, but do not take into account encoding
|
|
71
|
-
* that will include trailing zeros.
|
|
72
|
-
*
|
|
73
|
-
* +------------------+------------------+------------------+------------------+
|
|
74
|
-
* | | | | Padded zeros |
|
|
75
|
-
* | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
|
|
76
|
-
* +------------------+------------------+------------------+------------------+
|
|
77
|
-
* ^
|
|
78
|
-
* |
|
|
79
|
-
* Function reads until here ----------------------
|
|
80
|
-
*/
|
|
81
|
-
export function extractBlobFieldsFromBuffer(blob: Uint8Array): Fr[] {
|
|
82
|
-
const reader = BufferReader.asReader(blob);
|
|
83
|
-
const array = reader.readArray(blob.length >> 5, Fr);
|
|
30
|
+
const numFieldsWithoutPrefix = Number(numFields) - 1;
|
|
31
|
+
const blobFields = [firstField].concat(reader.readArray(numFieldsWithoutPrefix, Fr));
|
|
84
32
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
while (lastNonZeroIndex >= 0 && array[lastNonZeroIndex].isZero()) {
|
|
88
|
-
lastNonZeroIndex--;
|
|
33
|
+
if (checkEncoding && !checkBlobFieldsEncoding(blobFields)) {
|
|
34
|
+
throw new BlobDeserializationError(`Incorrect encoding of blob fields, this blob was likely not created by us`);
|
|
89
35
|
}
|
|
90
36
|
|
|
91
|
-
|
|
92
|
-
return array.slice(0, lastNonZeroIndex + 1);
|
|
37
|
+
return blobFields;
|
|
93
38
|
}
|
package/src/encoding.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BLOCK_END_PREFIX, TX_START_PREFIX } from '@aztec/constants';
|
|
2
2
|
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
import { FieldReader } from '@aztec/foundation/serialize';
|
|
3
4
|
|
|
4
5
|
const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
|
|
5
6
|
const REVERT_CODE_BIT_SIZE = 8n;
|
|
@@ -107,3 +108,47 @@ export function isBlockEndMarker(field: Fr) {
|
|
|
107
108
|
const numTxs = value & 0xffffn;
|
|
108
109
|
return value - numTxs === BLOCK_END_PREFIX * 256n * 256n;
|
|
109
110
|
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
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.
|
|
115
|
+
*/
|
|
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)) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
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.
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return true;
|
|
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,8 +1,10 @@
|
|
|
1
1
|
export * from './blob.js';
|
|
2
2
|
export * from './blob_batching.js';
|
|
3
|
+
export * from './blob_utils.js';
|
|
4
|
+
export * from './circuit_types/index.js';
|
|
3
5
|
export * from './deserialize.js';
|
|
4
6
|
export * from './encoding.js';
|
|
5
|
-
export * from './interface.js';
|
|
6
7
|
export * from './errors.js';
|
|
7
|
-
export * from './
|
|
8
|
+
export * from './hash.js';
|
|
9
|
+
export * from './interface.js';
|
|
8
10
|
export * from './sponge_blob.js';
|
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,9 +1,13 @@
|
|
|
1
|
+
import { FIELDS_PER_BLOB } from '@aztec/constants';
|
|
1
2
|
import { makeTuple } from '@aztec/foundation/array';
|
|
3
|
+
import { randomInt } from '@aztec/foundation/crypto';
|
|
2
4
|
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
3
5
|
|
|
4
6
|
import { Blob } from './blob.js';
|
|
5
|
-
import { BatchedBlobAccumulator
|
|
6
|
-
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';
|
|
7
11
|
import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
|
|
8
12
|
|
|
9
13
|
/**
|
|
@@ -44,18 +48,12 @@ export function makeBatchedBlobAccumulator(seed = 1): BatchedBlobAccumulator {
|
|
|
44
48
|
);
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
* Make an encoded blob with the given length
|
|
49
|
-
*
|
|
50
|
-
* This will deserialise correctly in the archiver
|
|
51
|
-
* @param length
|
|
52
|
-
* @returns
|
|
53
|
-
*/
|
|
54
|
-
export function makeEncodedBlob(length: number): Promise<Blob> {
|
|
51
|
+
export function makeEncodedTxBlobFields(length: number): Fr[] {
|
|
55
52
|
const txStartMarker = {
|
|
56
53
|
numBlobFields: length,
|
|
57
|
-
// The rest of the values don't matter. The test components using it
|
|
58
|
-
//
|
|
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`.
|
|
59
57
|
revertCode: 0,
|
|
60
58
|
numNoteHashes: 0,
|
|
61
59
|
numNullifiers: 0,
|
|
@@ -65,10 +63,49 @@ export function makeEncodedBlob(length: number): Promise<Blob> {
|
|
|
65
63
|
publicLogsLength: 0,
|
|
66
64
|
contractClassLogLength: 0,
|
|
67
65
|
};
|
|
68
|
-
|
|
66
|
+
|
|
67
|
+
return [
|
|
69
68
|
encodeTxStartMarker(txStartMarker),
|
|
70
|
-
...Array.from({ length: length - 1 }, () => Fr.
|
|
71
|
-
]
|
|
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
|
+
];
|
|
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.
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Make an encoded blob with the given length
|
|
93
|
+
*
|
|
94
|
+
* This will deserialise correctly in the archiver
|
|
95
|
+
* @param length
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
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);
|
|
72
109
|
}
|
|
73
110
|
|
|
74
111
|
/**
|
|
@@ -78,6 +115,6 @@ export function makeEncodedBlob(length: number): Promise<Blob> {
|
|
|
78
115
|
* @param length
|
|
79
116
|
* @returns
|
|
80
117
|
*/
|
|
81
|
-
export function makeRandomBlob(length: number):
|
|
118
|
+
export function makeRandomBlob(length: number): Blob {
|
|
82
119
|
return Blob.fromFields([...Array.from({ length: length }, () => Fr.random())]);
|
|
83
120
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
export * from './circuit_types/index.js';
|
|
2
|
+
export * from './interface.js';
|
|
1
3
|
export * from './sponge_blob.js';
|
|
2
4
|
|
|
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,57 +0,0 @@
|
|
|
1
|
-
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
|
|
3
|
-
import { inspect } from 'util';
|
|
4
|
-
import { Blob } from './blob.js';
|
|
5
|
-
import { BatchedBlob, BatchedBlobAccumulator, FinalBlobBatchingChallenges } from './blob_batching.js';
|
|
6
|
-
/**
|
|
7
|
-
* See nr BlobAccumulator and ts BatchedBlobAccumulator for documentation.
|
|
8
|
-
*/
|
|
9
|
-
export declare class BlobAccumulator {
|
|
10
|
-
blobCommitmentsHashAcc: Fr;
|
|
11
|
-
zAcc: Fr;
|
|
12
|
-
yAcc: BLS12Fr;
|
|
13
|
-
cAcc: BLS12Point;
|
|
14
|
-
gammaAcc: Fr;
|
|
15
|
-
gammaPowAcc: BLS12Fr;
|
|
16
|
-
constructor(blobCommitmentsHashAcc: Fr, zAcc: Fr, yAcc: BLS12Fr, cAcc: BLS12Point, gammaAcc: Fr, gammaPowAcc: BLS12Fr);
|
|
17
|
-
static empty(): BlobAccumulator;
|
|
18
|
-
equals(other: BlobAccumulator): boolean;
|
|
19
|
-
static fromBuffer(buffer: Buffer | BufferReader): BlobAccumulator;
|
|
20
|
-
toBuffer(): Buffer<ArrayBufferLike>;
|
|
21
|
-
/**
|
|
22
|
-
* Given blobs, accumulate all public inputs state.
|
|
23
|
-
* We assume the input blobs have not been evaluated at z.
|
|
24
|
-
* NOTE: Does NOT accumulate non circuit values including Q. This exists to simulate/check exactly what the circuit is doing
|
|
25
|
-
* and is unsafe for other use. For that reason, a toBatchedBlobAccumulator does not exist. See evaluateBlobs() oracle for usage.
|
|
26
|
-
* @returns An updated blob accumulator.
|
|
27
|
-
*/
|
|
28
|
-
accumulateBlobs(blobs: Blob[], finalBlobChallenges: FinalBlobBatchingChallenges): Promise<BlobAccumulator>;
|
|
29
|
-
toFields(): Fr[];
|
|
30
|
-
static fromFields(fields: Fr[] | FieldReader): BlobAccumulator;
|
|
31
|
-
/**
|
|
32
|
-
* Converts from an accumulator to a struct for the public inputs of our rollup circuits.
|
|
33
|
-
* @returns A BlobAccumulator instance.
|
|
34
|
-
*/
|
|
35
|
-
static fromBatchedBlobAccumulator(accumulator: BatchedBlobAccumulator): BlobAccumulator;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* See nr FinalBlobAccumulator and ts BatchedBlobAccumulator for documentation.
|
|
39
|
-
*/
|
|
40
|
-
export declare class FinalBlobAccumulator {
|
|
41
|
-
blobCommitmentsHash: Fr;
|
|
42
|
-
z: Fr;
|
|
43
|
-
y: BLS12Fr;
|
|
44
|
-
c: BLS12Point;
|
|
45
|
-
constructor(blobCommitmentsHash: Fr, z: Fr, y: BLS12Fr, c: BLS12Point);
|
|
46
|
-
static empty(): FinalBlobAccumulator;
|
|
47
|
-
static fromBuffer(buffer: Buffer | BufferReader): FinalBlobAccumulator;
|
|
48
|
-
toBuffer(): Buffer<ArrayBufferLike>;
|
|
49
|
-
static fromBatchedBlob(blob: BatchedBlob): FinalBlobAccumulator;
|
|
50
|
-
toFields(): Fr[];
|
|
51
|
-
toString(): string;
|
|
52
|
-
equals(other: FinalBlobAccumulator): boolean;
|
|
53
|
-
static random(): FinalBlobAccumulator;
|
|
54
|
-
static fromBatchedBlobAccumulator(accumulator: BatchedBlobAccumulator): FinalBlobAccumulator;
|
|
55
|
-
[inspect.custom](): string;
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=blob_batching_public_inputs.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"blob_batching_public_inputs.d.ts","sourceRoot":"","sources":["../src/blob_batching_public_inputs.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,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,sBAAsB,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AAEtG;;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;;;;;;OAMG;IACG,eAAe,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,mBAAmB,EAAE,2BAA2B;IAerF,QAAQ;IAaR,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,EAAE,EAAE,GAAG,WAAW,GAAG,eAAe;IAgB9D;;;OAGG;IACH,MAAM,CAAC,0BAA0B,CAAC,WAAW,EAAE,sBAAsB;CAUtE;AAED;;GAEG;AACH,qBAAa,oBAAoB;IAEtB,mBAAmB,EAAE,EAAE;IACvB,CAAC,EAAE,EAAE;IACL,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,UAAU;gBAHb,mBAAmB,EAAE,EAAE,EACvB,CAAC,EAAE,EAAE,EACL,CAAC,EAAE,OAAO,EACV,CAAC,EAAE,UAAU;IAGtB,MAAM,CAAC,KAAK,IAAI,oBAAoB;IAIpC,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY,GAAG,oBAAoB;IAUtE,QAAQ;IAIR,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,WAAW;IAIxC,QAAQ;IAUR,QAAQ;IAQR,MAAM,CAAC,KAAK,EAAE,oBAAoB;IAUlC,MAAM,CAAC,MAAM;IAKb,MAAM,CAAC,0BAA0B,CAAC,WAAW,EAAE,sBAAsB;IASrE,CAAC,OAAO,CAAC,MAAM,CAAC;CAQjB"}
|