@aztec/blob-lib 0.0.1-commit.b655e406 → 0.0.1-commit.fce3e4f
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/batched_blob.d.ts +25 -0
- package/dest/batched_blob.d.ts.map +1 -0
- package/dest/batched_blob.js +20 -0
- package/dest/blob.d.ts +4 -10
- package/dest/blob.d.ts.map +1 -1
- package/dest/blob_batching.d.ts +33 -83
- package/dest/blob_batching.d.ts.map +1 -1
- package/dest/blob_batching.js +68 -105
- package/dest/blob_utils.d.ts +19 -10
- package/dest/blob_utils.d.ts.map +1 -1
- package/dest/blob_utils.js +28 -19
- package/dest/circuit_types/blob_accumulator.d.ts +2 -1
- package/dest/circuit_types/blob_accumulator.d.ts.map +1 -1
- package/dest/circuit_types/blob_accumulator.js +3 -0
- package/dest/circuit_types/final_blob_accumulator.d.ts +1 -1
- package/dest/circuit_types/final_blob_accumulator.d.ts.map +1 -1
- package/dest/circuit_types/final_blob_batching_challenges.d.ts +1 -1
- package/dest/circuit_types/final_blob_batching_challenges.d.ts.map +1 -1
- package/dest/circuit_types/index.d.ts +1 -1
- package/dest/encoding/block_blob_data.d.ts +22 -0
- package/dest/encoding/block_blob_data.d.ts.map +1 -0
- package/dest/encoding/block_blob_data.js +65 -0
- package/dest/encoding/block_end_marker.d.ts +10 -0
- package/dest/encoding/block_end_marker.d.ts.map +1 -0
- package/dest/encoding/block_end_marker.js +40 -0
- package/dest/encoding/block_end_state_field.d.ts +12 -0
- package/dest/encoding/block_end_state_field.d.ts.map +1 -0
- package/dest/encoding/block_end_state_field.js +39 -0
- package/dest/encoding/checkpoint_blob_data.d.ts +15 -0
- package/dest/encoding/checkpoint_blob_data.d.ts.map +1 -0
- package/dest/encoding/checkpoint_blob_data.js +67 -0
- package/dest/encoding/checkpoint_end_marker.d.ts +8 -0
- package/dest/encoding/checkpoint_end_marker.d.ts.map +1 -0
- package/dest/encoding/checkpoint_end_marker.js +28 -0
- package/dest/encoding/fixtures.d.ts +41 -0
- package/dest/encoding/fixtures.d.ts.map +1 -0
- package/dest/encoding/fixtures.js +139 -0
- package/dest/encoding/index.d.ts +10 -0
- package/dest/encoding/index.d.ts.map +1 -0
- package/dest/encoding/index.js +9 -0
- package/dest/encoding/tx_blob_data.d.ts +19 -0
- package/dest/encoding/tx_blob_data.d.ts.map +1 -0
- package/dest/encoding/tx_blob_data.js +79 -0
- package/dest/encoding/tx_start_marker.d.ts +16 -0
- package/dest/encoding/tx_start_marker.d.ts.map +1 -0
- package/dest/{encoding.js → encoding/tx_start_marker.js} +12 -58
- package/dest/errors.d.ts +1 -1
- package/dest/errors.d.ts.map +1 -1
- package/dest/hash.d.ts +11 -4
- package/dest/hash.d.ts.map +1 -1
- package/dest/hash.js +14 -4
- package/dest/index.d.ts +3 -4
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -3
- package/dest/interface.d.ts +1 -1
- package/dest/kzg_context.d.ts +1 -1
- package/dest/sponge_blob.d.ts +8 -14
- package/dest/sponge_blob.d.ts.map +1 -1
- package/dest/sponge_blob.js +19 -34
- package/dest/testing.d.ts +8 -16
- package/dest/testing.d.ts.map +1 -1
- package/dest/testing.js +34 -64
- package/dest/types.d.ts +2 -1
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +1 -0
- package/package.json +8 -7
- package/src/batched_blob.ts +25 -0
- package/src/blob_batching.ts +81 -123
- package/src/blob_utils.ts +31 -21
- package/src/circuit_types/blob_accumulator.ts +11 -0
- package/src/encoding/block_blob_data.ts +102 -0
- package/src/encoding/block_end_marker.ts +54 -0
- package/src/encoding/block_end_state_field.ts +59 -0
- package/src/encoding/checkpoint_blob_data.ts +95 -0
- package/src/encoding/checkpoint_end_marker.ts +40 -0
- package/src/encoding/fixtures.ts +209 -0
- package/src/encoding/index.ts +9 -0
- package/src/encoding/tx_blob_data.ts +116 -0
- package/src/{encoding.ts → encoding/tx_start_marker.ts} +18 -75
- package/src/hash.ts +14 -4
- package/src/index.ts +2 -3
- package/src/sponge_blob.ts +21 -34
- package/src/testing.ts +46 -73
- package/src/types.ts +1 -0
- package/dest/deserialize.d.ts +0 -14
- package/dest/deserialize.d.ts.map +0 -1
- package/dest/deserialize.js +0 -33
- package/dest/encoding.d.ts +0 -26
- package/dest/encoding.d.ts.map +0 -1
- package/src/deserialize.ts +0 -38
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TX_START_PREFIX } from '@aztec/constants';
|
|
2
2
|
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import { BlobDeserializationError } from '../errors.js';
|
|
5
|
+
|
|
6
|
+
// Must match the implementation in `noir-protocol-circuits/crates/types/src/blob_data/tx_blob_data.nr`.
|
|
4
7
|
|
|
5
8
|
const NUM_BLOB_FIELDS_BIT_SIZE = 32n;
|
|
6
9
|
const REVERT_CODE_BIT_SIZE = 8n;
|
|
@@ -9,11 +12,11 @@ const NUM_NULLIFIER_BIT_SIZE = 16n;
|
|
|
9
12
|
const NUM_L2_TO_L1_MSG_BIT_SIZE = 16n;
|
|
10
13
|
const NUM_PUBLIC_DATA_WRITE_BIT_SIZE = 16n;
|
|
11
14
|
const NUM_PRIVATE_LOG_BIT_SIZE = 16n;
|
|
15
|
+
const PRIVATE_LOGS_LENGTH_BIT_SIZE = 16n;
|
|
12
16
|
const PUBLIC_LOGS_LENGTH_BIT_SIZE = 32n;
|
|
13
17
|
const CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE = 16n;
|
|
14
18
|
|
|
15
19
|
export interface TxStartMarker {
|
|
16
|
-
prefix: bigint;
|
|
17
20
|
numBlobFields: number;
|
|
18
21
|
revertCode: number;
|
|
19
22
|
numNoteHashes: number;
|
|
@@ -21,12 +24,12 @@ export interface TxStartMarker {
|
|
|
21
24
|
numL2ToL1Msgs: number;
|
|
22
25
|
numPublicDataWrites: number;
|
|
23
26
|
numPrivateLogs: number;
|
|
27
|
+
privateLogsLength: number;
|
|
24
28
|
publicLogsLength: number;
|
|
25
29
|
contractClassLogLength: number;
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
export function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>) {
|
|
32
|
+
export function encodeTxStartMarker(txStartMarker: TxStartMarker): Fr {
|
|
30
33
|
let value = TX_START_PREFIX;
|
|
31
34
|
value <<= NUM_NOTE_HASH_BIT_SIZE;
|
|
32
35
|
value += BigInt(txStartMarker.numNoteHashes);
|
|
@@ -38,6 +41,8 @@ export function encodeTxStartMarker(txStartMarker: Omit<TxStartMarker, 'prefix'>
|
|
|
38
41
|
value += BigInt(txStartMarker.numPublicDataWrites);
|
|
39
42
|
value <<= NUM_PRIVATE_LOG_BIT_SIZE;
|
|
40
43
|
value += BigInt(txStartMarker.numPrivateLogs);
|
|
44
|
+
value <<= PRIVATE_LOGS_LENGTH_BIT_SIZE;
|
|
45
|
+
value += BigInt(txStartMarker.privateLogsLength);
|
|
41
46
|
value <<= PUBLIC_LOGS_LENGTH_BIT_SIZE;
|
|
42
47
|
value += BigInt(txStartMarker.publicLogsLength);
|
|
43
48
|
value <<= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
|
|
@@ -59,6 +64,8 @@ export function decodeTxStartMarker(field: Fr): TxStartMarker {
|
|
|
59
64
|
value >>= CONTRACT_CLASS_LOG_LENGTH_BIT_SIZE;
|
|
60
65
|
const publicLogsLength = Number(value & (2n ** PUBLIC_LOGS_LENGTH_BIT_SIZE - 1n));
|
|
61
66
|
value >>= PUBLIC_LOGS_LENGTH_BIT_SIZE;
|
|
67
|
+
const privateLogsLength = Number(value & (2n ** PRIVATE_LOGS_LENGTH_BIT_SIZE - 1n));
|
|
68
|
+
value >>= PRIVATE_LOGS_LENGTH_BIT_SIZE;
|
|
62
69
|
const numPrivateLogs = Number(value & (2n ** NUM_PRIVATE_LOG_BIT_SIZE - 1n));
|
|
63
70
|
value >>= NUM_PRIVATE_LOG_BIT_SIZE;
|
|
64
71
|
const numPublicDataWrites = Number(value & (2n ** NUM_PUBLIC_DATA_WRITE_BIT_SIZE - 1n));
|
|
@@ -69,11 +76,13 @@ export function decodeTxStartMarker(field: Fr): TxStartMarker {
|
|
|
69
76
|
value >>= NUM_NULLIFIER_BIT_SIZE;
|
|
70
77
|
const numNoteHashes = Number(value & (2n ** NUM_NOTE_HASH_BIT_SIZE - 1n));
|
|
71
78
|
value >>= NUM_NOTE_HASH_BIT_SIZE;
|
|
72
|
-
|
|
73
|
-
// The caller function can check it by calling `isValidTxStartMarker`, and decide what to do if it's incorrect.
|
|
79
|
+
|
|
74
80
|
const prefix = value;
|
|
81
|
+
if (prefix !== TX_START_PREFIX) {
|
|
82
|
+
throw new BlobDeserializationError(`Incorrect encoding of blob fields: invalid tx start marker.`);
|
|
83
|
+
}
|
|
84
|
+
|
|
75
85
|
return {
|
|
76
|
-
prefix,
|
|
77
86
|
numBlobFields,
|
|
78
87
|
revertCode,
|
|
79
88
|
numNoteHashes,
|
|
@@ -81,74 +90,8 @@ export function decodeTxStartMarker(field: Fr): TxStartMarker {
|
|
|
81
90
|
numL2ToL1Msgs,
|
|
82
91
|
numPublicDataWrites,
|
|
83
92
|
numPrivateLogs,
|
|
93
|
+
privateLogsLength,
|
|
84
94
|
publicLogsLength,
|
|
85
95
|
contractClassLogLength,
|
|
86
96
|
};
|
|
87
97
|
}
|
|
88
|
-
|
|
89
|
-
export function getNumBlobFieldsFromTxStartMarker(field: Fr) {
|
|
90
|
-
return Number(field.toBigInt() & (2n ** NUM_BLOB_FIELDS_BIT_SIZE - 1n));
|
|
91
|
-
}
|
|
92
|
-
|
|
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;
|
|
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
CHANGED
|
@@ -2,6 +2,7 @@ import { poseidon2Hash, sha256, sha256ToField } from '@aztec/foundation/crypto';
|
|
|
2
2
|
import { BLS12Fr, Fr } from '@aztec/foundation/fields';
|
|
3
3
|
|
|
4
4
|
import { BYTES_PER_BLOB, BYTES_PER_COMMITMENT, kzg } from './kzg_context.js';
|
|
5
|
+
import { SpongeBlob } from './sponge_blob.js';
|
|
5
6
|
|
|
6
7
|
const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
7
8
|
|
|
@@ -24,12 +25,21 @@ export function computeBlobsHash(evmVersionedBlobHashes: Buffer[]): Fr {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* This
|
|
28
|
+
* Computes a non-standard Poseidon2 hash over the provided fields.
|
|
29
|
+
*
|
|
30
|
+
* This function is used to compute:
|
|
31
|
+
* - `blobFieldsHash` of a checkpoint:
|
|
32
|
+
* Verified in the circuit against all fields absorbed into the blob sponge over the entire checkpoint.
|
|
33
|
+
* The exact number of fields is encoded in the checkpoint end marker (the last field).
|
|
34
|
+
* This hash is used when generating the challenge `z` for all blobs in the checkpoint.
|
|
35
|
+
* - `spongeBlobHash` of a block:
|
|
36
|
+
* Computed from the block's tx effects, its end-state, and the blob fields of all prior blocks in the same checkpoint.
|
|
37
|
+
* This hash is included in the block header.
|
|
30
38
|
*/
|
|
31
39
|
export async function computeBlobFieldsHash(fields: Fr[]): Promise<Fr> {
|
|
32
|
-
|
|
40
|
+
const sponge = SpongeBlob.init();
|
|
41
|
+
await sponge.absorb(fields);
|
|
42
|
+
return sponge.squeeze();
|
|
33
43
|
}
|
|
34
44
|
|
|
35
45
|
export function computeBlobCommitment(data: Uint8Array): Buffer {
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
+
export * from './batched_blob.js';
|
|
1
2
|
export * from './blob.js';
|
|
2
3
|
export * from './blob_batching.js';
|
|
3
4
|
export * from './blob_utils.js';
|
|
4
5
|
export * from './circuit_types/index.js';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './encoding.js';
|
|
7
|
-
export * from './errors.js';
|
|
6
|
+
export * from './encoding/index.js';
|
|
8
7
|
export * from './hash.js';
|
|
9
8
|
export * from './interface.js';
|
|
10
9
|
export * from './sponge_blob.js';
|
package/src/sponge_blob.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TWO_POW_64 } from '@aztec/constants';
|
|
1
|
+
import { BLOBS_PER_CHECKPOINT, FIELDS_PER_BLOB, TWO_POW_64 } from '@aztec/constants';
|
|
2
2
|
import { type FieldsOf, makeTuple } from '@aztec/foundation/array';
|
|
3
3
|
import { poseidon2Permutation } from '@aztec/foundation/crypto';
|
|
4
4
|
import { Fr } from '@aztec/foundation/fields';
|
|
@@ -15,18 +15,28 @@ import {
|
|
|
15
15
|
* See noir-projects/noir-protocol-circuits/crates/types/src/abis/sponge_blob.nr.
|
|
16
16
|
*/
|
|
17
17
|
export class SpongeBlob {
|
|
18
|
+
static MAX_FIELDS = BLOBS_PER_CHECKPOINT * FIELDS_PER_BLOB;
|
|
19
|
+
|
|
18
20
|
constructor(
|
|
19
21
|
/** Sponge with absorbed fields that will go into one or more blobs. */
|
|
20
22
|
public readonly sponge: Poseidon2Sponge,
|
|
21
23
|
/** Number of effects absorbed so far. */
|
|
22
24
|
public numAbsorbedFields: number,
|
|
23
|
-
/** Number of effects that will be absorbed. */
|
|
24
|
-
public readonly numExpectedFields: number,
|
|
25
25
|
) {}
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* Initialize the sponge blob to absorb data for a checkpoint.
|
|
29
|
+
*/
|
|
30
|
+
static init(): SpongeBlob {
|
|
31
|
+
// This must match the implementation in noir-projects/noir-protocol-circuits/types/src/abis/sponge_blob.nr
|
|
32
|
+
const iv = new Fr(BigInt(SpongeBlob.MAX_FIELDS) * TWO_POW_64);
|
|
33
|
+
const sponge = Poseidon2Sponge.init(iv);
|
|
34
|
+
return new SpongeBlob(sponge, 0);
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
static fromBuffer(buffer: Buffer | BufferReader): SpongeBlob {
|
|
28
38
|
const reader = BufferReader.asReader(buffer);
|
|
29
|
-
return new SpongeBlob(reader.readObject(Poseidon2Sponge), reader.readNumber()
|
|
39
|
+
return new SpongeBlob(reader.readObject(Poseidon2Sponge), reader.readNumber());
|
|
30
40
|
}
|
|
31
41
|
|
|
32
42
|
toBuffer() {
|
|
@@ -34,7 +44,7 @@ export class SpongeBlob {
|
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
static getFields(fields: FieldsOf<SpongeBlob>) {
|
|
37
|
-
return [fields.sponge, fields.numAbsorbedFields
|
|
47
|
+
return [fields.sponge, fields.numAbsorbedFields];
|
|
38
48
|
}
|
|
39
49
|
|
|
40
50
|
toFields(): Fr[] {
|
|
@@ -43,11 +53,7 @@ export class SpongeBlob {
|
|
|
43
53
|
|
|
44
54
|
static fromFields(fields: Fr[] | FieldReader): SpongeBlob {
|
|
45
55
|
const reader = FieldReader.asReader(fields);
|
|
46
|
-
return new SpongeBlob(
|
|
47
|
-
reader.readObject(Poseidon2Sponge),
|
|
48
|
-
reader.readField().toNumber(),
|
|
49
|
-
reader.readField().toNumber(),
|
|
50
|
-
);
|
|
56
|
+
return new SpongeBlob(reader.readObject(Poseidon2Sponge), reader.readField().toNumber());
|
|
51
57
|
}
|
|
52
58
|
|
|
53
59
|
clone() {
|
|
@@ -55,9 +61,9 @@ export class SpongeBlob {
|
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
async absorb(fields: Fr[]) {
|
|
58
|
-
if (this.numAbsorbedFields + fields.length >
|
|
64
|
+
if (this.numAbsorbedFields + fields.length > SpongeBlob.MAX_FIELDS) {
|
|
59
65
|
throw new Error(
|
|
60
|
-
`Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${
|
|
66
|
+
`Attempted to fill spongeBlob with ${this.numAbsorbedFields + fields.length}, but it has a max of ${SpongeBlob.MAX_FIELDS}`,
|
|
61
67
|
);
|
|
62
68
|
}
|
|
63
69
|
await this.sponge.absorb(fields);
|
|
@@ -65,29 +71,11 @@ export class SpongeBlob {
|
|
|
65
71
|
}
|
|
66
72
|
|
|
67
73
|
async squeeze(): Promise<Fr> {
|
|
68
|
-
|
|
69
|
-
// 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) {
|
|
71
|
-
await this.sponge.absorb([Fr.ONE]);
|
|
72
|
-
}
|
|
73
|
-
return this.sponge.squeeze();
|
|
74
|
+
return await this.sponge.squeeze();
|
|
74
75
|
}
|
|
75
76
|
|
|
76
77
|
static empty(): SpongeBlob {
|
|
77
|
-
return new SpongeBlob(Poseidon2Sponge.empty(), 0
|
|
78
|
-
}
|
|
79
|
-
|
|
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);
|
|
78
|
+
return new SpongeBlob(Poseidon2Sponge.empty(), 0);
|
|
91
79
|
}
|
|
92
80
|
}
|
|
93
81
|
|
|
@@ -141,8 +129,7 @@ export class Poseidon2Sponge {
|
|
|
141
129
|
);
|
|
142
130
|
}
|
|
143
131
|
|
|
144
|
-
static init(
|
|
145
|
-
const iv = new Fr(numExpectedFields).mul(new Fr(TWO_POW_64));
|
|
132
|
+
static init(iv: Fr): Poseidon2Sponge {
|
|
146
133
|
const sponge = Poseidon2Sponge.empty();
|
|
147
134
|
sponge.state[3] = iv;
|
|
148
135
|
return sponge;
|
package/src/testing.ts
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import { FIELDS_PER_BLOB } from '@aztec/constants';
|
|
2
1
|
import { makeTuple } from '@aztec/foundation/array';
|
|
3
|
-
import {
|
|
4
|
-
import { BLS12Fr, BLS12Point, Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BLS12Fq, BLS12Fr, BLS12Point, BLSPointNotOnCurveError, Fr } from '@aztec/foundation/fields';
|
|
5
3
|
|
|
6
4
|
import { Blob } from './blob.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { FinalBlobBatchingChallenges } from './circuit_types/
|
|
10
|
-
import { createBlockEndMarker, encodeTxStartMarker } from './encoding.js';
|
|
5
|
+
import { BlobAccumulator } from './circuit_types/blob_accumulator.js';
|
|
6
|
+
import { FinalBlobAccumulator } from './circuit_types/final_blob_accumulator.js';
|
|
7
|
+
import { FinalBlobBatchingChallenges } from './circuit_types/final_blob_batching_challenges.js';
|
|
11
8
|
import { Poseidon2Sponge, SpongeBlob } from './sponge_blob.js';
|
|
12
9
|
|
|
10
|
+
export * from './encoding/fixtures.js';
|
|
11
|
+
|
|
13
12
|
/**
|
|
14
13
|
* Makes arbitrary poseidon sponge for blob inputs.
|
|
15
14
|
* Note: will not verify inside the circuit.
|
|
@@ -25,87 +24,61 @@ export function makeSpongeBlob(seed = 1): SpongeBlob {
|
|
|
25
24
|
false,
|
|
26
25
|
),
|
|
27
26
|
seed,
|
|
28
|
-
seed + 1,
|
|
29
27
|
);
|
|
30
28
|
}
|
|
31
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Makes an arbitrary but valid BLS12 point. The value is deterministic for a given seed.
|
|
32
|
+
* @param seed - The seed to use for generating the point.
|
|
33
|
+
* @returns A BLS12 point instance.
|
|
34
|
+
*/
|
|
35
|
+
function makeBLS12Point(seed = 1): BLS12Point {
|
|
36
|
+
let accum = 0;
|
|
37
|
+
while (true) {
|
|
38
|
+
try {
|
|
39
|
+
const x = new BLS12Fq(seed + accum);
|
|
40
|
+
const y = BLS12Point.YFromX(x);
|
|
41
|
+
if (y) {
|
|
42
|
+
return new BLS12Point(x, y, false);
|
|
43
|
+
}
|
|
44
|
+
accum++;
|
|
45
|
+
} catch (e: any) {
|
|
46
|
+
if (!(e instanceof BLSPointNotOnCurveError)) {
|
|
47
|
+
throw e;
|
|
48
|
+
}
|
|
49
|
+
// The point is not on the curve - try again
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
32
54
|
/**
|
|
33
55
|
* Makes arbitrary blob public accumulator.
|
|
34
56
|
* Note: will not verify inside the circuit.
|
|
35
57
|
* @param seed - The seed to use for generating the blob accumulator.
|
|
36
58
|
* @returns A blob accumulator instance.
|
|
37
59
|
*/
|
|
38
|
-
export function
|
|
39
|
-
return new
|
|
60
|
+
export function makeBlobAccumulator(seed = 1): BlobAccumulator {
|
|
61
|
+
return new BlobAccumulator(
|
|
40
62
|
new Fr(seed),
|
|
41
|
-
new Fr(seed +
|
|
42
|
-
new BLS12Fr(seed +
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
new
|
|
46
|
-
new BLS12Fr(seed + 4),
|
|
47
|
-
new FinalBlobBatchingChallenges(new Fr(seed + 5), new BLS12Fr(seed + 6)),
|
|
63
|
+
new Fr(seed + 0x10),
|
|
64
|
+
new BLS12Fr(seed + 0x20),
|
|
65
|
+
makeBLS12Point(seed + 0x30),
|
|
66
|
+
new Fr(seed + 0x50),
|
|
67
|
+
new BLS12Fr(seed + 0x60),
|
|
48
68
|
);
|
|
49
69
|
}
|
|
50
70
|
|
|
51
|
-
export function
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
];
|
|
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));
|
|
71
|
+
export function makeFinalBlobAccumulator(seed = 1) {
|
|
72
|
+
return new FinalBlobAccumulator(
|
|
73
|
+
new Fr(seed),
|
|
74
|
+
new Fr(seed + 0x10),
|
|
75
|
+
new BLS12Fr(seed + 0x20),
|
|
76
|
+
makeBLS12Point(seed + 0x30),
|
|
77
|
+
);
|
|
104
78
|
}
|
|
105
79
|
|
|
106
|
-
export function
|
|
107
|
-
|
|
108
|
-
return getBlobsPerL1Block(fields);
|
|
80
|
+
export function makeFinalBlobBatchingChallenges(seed = 1) {
|
|
81
|
+
return new FinalBlobBatchingChallenges(new Fr(seed), new BLS12Fr(seed + 0x10));
|
|
109
82
|
}
|
|
110
83
|
|
|
111
84
|
/**
|
package/src/types.ts
CHANGED
package/dest/deserialize.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
/**
|
|
3
|
-
* Deserializes a buffer into an array of field elements.
|
|
4
|
-
*
|
|
5
|
-
* This function returns the fields that were actually added in a checkpoint. The number of fields is specified by the
|
|
6
|
-
* first field.
|
|
7
|
-
*
|
|
8
|
-
* @param buf - The buffer to deserialize.
|
|
9
|
-
* @param checkEncoding - Whether to check if the encoding is correct. If false, it will still check the checkpoint
|
|
10
|
-
* prefix and throw if there's not enough fields.
|
|
11
|
-
* @returns An array of field elements.
|
|
12
|
-
*/
|
|
13
|
-
export declare function deserializeEncodedBlobToFields(buf: Uint8Array, checkEncoding?: boolean): Fr[];
|
|
14
|
-
//# sourceMappingURL=deserialize.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize.d.ts","sourceRoot":"","sources":["../src/deserialize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAM9C;;;;;;;;;;GAUG;AACH,wBAAgB,8BAA8B,CAAC,GAAG,EAAE,UAAU,EAAE,aAAa,UAAQ,GAAG,EAAE,EAAE,CAoB3F"}
|
package/dest/deserialize.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
-
import { checkBlobFieldsEncoding } from './encoding.js';
|
|
4
|
-
import { BlobDeserializationError } from './errors.js';
|
|
5
|
-
/**
|
|
6
|
-
* Deserializes a buffer into an array of field elements.
|
|
7
|
-
*
|
|
8
|
-
* This function returns the fields that were actually added in a checkpoint. The number of fields is specified by the
|
|
9
|
-
* first field.
|
|
10
|
-
*
|
|
11
|
-
* @param buf - The buffer to deserialize.
|
|
12
|
-
* @param checkEncoding - Whether to check if the encoding is correct. If false, it will still check the checkpoint
|
|
13
|
-
* prefix and throw if there's not enough fields.
|
|
14
|
-
* @returns An array of field elements.
|
|
15
|
-
*/ export function deserializeEncodedBlobToFields(buf, checkEncoding = false) {
|
|
16
|
-
const reader = BufferReader.asReader(buf);
|
|
17
|
-
const firstField = reader.readObject(Fr);
|
|
18
|
-
// Use toBigInt instead of toNumber so that we can catch it and throw a more descriptive error below if the first
|
|
19
|
-
// field is larger than a javascript integer.
|
|
20
|
-
const numFields = firstField.toBigInt();
|
|
21
|
-
const totalFieldsInBuffer = BigInt(buf.length / Fr.SIZE_IN_BYTES);
|
|
22
|
-
if (numFields > totalFieldsInBuffer) {
|
|
23
|
-
throw new BlobDeserializationError(`Failed to deserialize blob fields, this blob was likely not created by us`);
|
|
24
|
-
}
|
|
25
|
-
const numFieldsWithoutPrefix = Number(numFields) - 1;
|
|
26
|
-
const blobFields = [
|
|
27
|
-
firstField
|
|
28
|
-
].concat(reader.readArray(numFieldsWithoutPrefix, Fr));
|
|
29
|
-
if (checkEncoding && !checkBlobFieldsEncoding(blobFields)) {
|
|
30
|
-
throw new BlobDeserializationError(`Incorrect encoding of blob fields, this blob was likely not created by us`);
|
|
31
|
-
}
|
|
32
|
-
return blobFields;
|
|
33
|
-
}
|
package/dest/encoding.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
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;
|
|
18
|
-
export declare function createBlockEndMarker(numTxs: number): Fr;
|
|
19
|
-
export declare function getNumTxsFromBlockEndMarker(field: Fr): number;
|
|
20
|
-
export declare function isBlockEndMarker(field: Fr): boolean;
|
|
21
|
-
/**
|
|
22
|
-
* Check that the fields are emitted from the circuits and conform to the encoding.
|
|
23
|
-
* @param blobFields - The concatenated fields from all blobs of an L1 block.
|
|
24
|
-
*/
|
|
25
|
-
export declare function checkBlobFieldsEncoding(blobFields: Fr[]): boolean;
|
|
26
|
-
//# sourceMappingURL=encoding.d.ts.map
|
package/dest/encoding.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../src/encoding.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AAa9C,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;AAED;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,UAAU,EAAE,EAAE,EAAE,WAsCvD"}
|
package/src/deserialize.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
-
import { BufferReader } from '@aztec/foundation/serialize';
|
|
3
|
-
|
|
4
|
-
import { checkBlobFieldsEncoding } from './encoding.js';
|
|
5
|
-
import { BlobDeserializationError } from './errors.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Deserializes a buffer into an array of field elements.
|
|
9
|
-
*
|
|
10
|
-
* This function returns the fields that were actually added in a checkpoint. The number of fields is specified by the
|
|
11
|
-
* first field.
|
|
12
|
-
*
|
|
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.
|
|
16
|
-
* @returns An array of field elements.
|
|
17
|
-
*/
|
|
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`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const numFieldsWithoutPrefix = Number(numFields) - 1;
|
|
31
|
-
const blobFields = [firstField].concat(reader.readArray(numFieldsWithoutPrefix, Fr));
|
|
32
|
-
|
|
33
|
-
if (checkEncoding && !checkBlobFieldsEncoding(blobFields)) {
|
|
34
|
-
throw new BlobDeserializationError(`Incorrect encoding of blob fields, this blob was likely not created by us`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return blobFields;
|
|
38
|
-
}
|