@aztec/foundation 0.72.1 → 0.73.0
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/abi/abi.d.ts +3 -2
- package/dest/abi/abi.d.ts.map +1 -1
- package/dest/abi/abi.js +22 -5
- package/dest/abi/event_selector.d.ts +1 -1
- package/dest/abi/event_selector.d.ts.map +1 -1
- package/dest/abi/event_selector.js +3 -3
- package/dest/abi/function_selector.d.ts +3 -13
- package/dest/abi/function_selector.d.ts.map +1 -1
- package/dest/abi/function_selector.js +5 -16
- package/dest/blob/blob.d.ts +150 -0
- package/dest/blob/blob.d.ts.map +1 -0
- package/dest/blob/blob.js +249 -0
- package/dest/blob/encoding.d.ts +53 -0
- package/dest/blob/encoding.d.ts.map +1 -0
- package/dest/blob/encoding.js +87 -0
- package/dest/blob/index.d.ts +4 -58
- package/dest/blob/index.d.ts.map +1 -1
- package/dest/blob/index.js +6 -142
- package/dest/blob/interface.d.ts +10 -0
- package/dest/blob/interface.d.ts.map +1 -0
- package/dest/blob/interface.js +2 -0
- package/dest/blob/mocks.d.ts +5 -0
- package/dest/blob/mocks.d.ts.map +1 -0
- package/dest/blob/mocks.js +25 -0
- package/dest/config/env_var.d.ts +1 -1
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +1 -0
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +11 -14
- package/dest/crypto/index.d.ts +0 -7
- package/dest/crypto/index.d.ts.map +1 -1
- package/dest/crypto/index.js +1 -11
- package/dest/crypto/keys/index.d.ts +1 -1
- package/dest/crypto/keys/index.d.ts.map +1 -1
- package/dest/crypto/keys/index.js +5 -5
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +3 -3
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +13 -10
- package/dest/crypto/poseidon/index.d.ts +5 -5
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +20 -19
- package/dest/crypto/sync/index.d.ts +3 -0
- package/dest/crypto/sync/index.d.ts.map +1 -0
- package/dest/crypto/sync/index.js +5 -0
- package/dest/crypto/sync/pedersen/index.d.ts +21 -0
- package/dest/crypto/sync/pedersen/index.d.ts.map +1 -0
- package/dest/crypto/sync/pedersen/index.js +37 -0
- package/dest/crypto/sync/poseidon/index.d.ts +26 -0
- package/dest/crypto/sync/poseidon/index.d.ts.map +1 -0
- package/dest/crypto/sync/poseidon/index.js +57 -0
- package/dest/fields/fields.js +4 -4
- package/dest/fields/point.d.ts +1 -1
- package/dest/iterable/toArray.d.ts +1 -1
- package/dest/iterable/toArray.d.ts.map +1 -1
- package/dest/iterable/toArray.js +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.js +2 -2
- package/dest/json-rpc/convert.d.ts +1 -1
- package/dest/json-rpc/convert.d.ts.map +1 -1
- package/dest/json-rpc/convert.js +2 -2
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.js +4 -2
- package/dest/message/index.d.ts +32 -0
- package/dest/message/index.d.ts.map +1 -0
- package/dest/message/index.js +23 -0
- package/dest/mutex/index.d.ts.map +1 -1
- package/dest/mutex/index.js +6 -4
- package/dest/promise/running-promise.d.ts +2 -1
- package/dest/promise/running-promise.d.ts.map +1 -1
- package/dest/promise/running-promise.js +6 -3
- package/dest/schemas/parse.d.ts +1 -1
- package/dest/schemas/parse.d.ts.map +1 -1
- package/dest/schemas/parse.js +2 -2
- package/dest/serialize/field_reader.d.ts +18 -0
- package/dest/serialize/field_reader.d.ts.map +1 -1
- package/dest/serialize/field_reader.js +32 -2
- package/dest/worker/browser/start_web_module.d.ts.map +1 -1
- package/dest/worker/browser/start_web_module.js +2 -1
- package/dest/worker/browser/web_worker.d.ts.map +1 -1
- package/dest/worker/browser/web_worker.js +2 -1
- package/dest/worker/node/node_worker.d.ts.map +1 -1
- package/dest/worker/node/node_worker.js +2 -1
- package/dest/worker/node/start_node_module.d.ts.map +1 -1
- package/dest/worker/node/start_node_module.js +2 -1
- package/package.json +5 -3
- package/src/abi/abi.ts +28 -8
- package/src/abi/event_selector.ts +2 -2
- package/src/abi/function_selector.ts +7 -27
- package/src/blob/blob.ts +294 -0
- package/src/blob/encoding.ts +98 -0
- package/src/blob/index.ts +5 -183
- package/src/blob/interface.ts +11 -0
- package/src/blob/mocks.ts +30 -0
- package/src/config/env_var.ts +7 -4
- package/src/config/index.ts +10 -12
- package/src/crypto/index.ts +0 -12
- package/src/crypto/keys/index.ts +5 -4
- package/src/crypto/pedersen/pedersen.wasm.ts +13 -14
- package/src/crypto/poseidon/index.ts +24 -39
- package/src/crypto/sync/index.ts +6 -0
- package/src/crypto/sync/pedersen/index.ts +45 -0
- package/src/crypto/sync/poseidon/index.ts +76 -0
- package/src/fields/fields.ts +3 -3
- package/src/iterable/toArray.ts +3 -1
- package/src/json-rpc/client/safe_json_rpc_client.ts +1 -1
- package/src/json-rpc/convert.ts +2 -2
- package/src/json-rpc/server/safe_json_rpc_server.ts +3 -1
- package/src/message/index.ts +43 -0
- package/src/mutex/index.ts +5 -4
- package/src/promise/running-promise.ts +4 -1
- package/src/schemas/parse.ts +2 -2
- package/src/serialize/field_reader.ts +34 -1
- package/src/worker/browser/start_web_module.ts +1 -0
- package/src/worker/browser/web_worker.ts +1 -0
- package/src/worker/node/node_worker.ts +1 -0
- package/src/worker/node/start_node_module.ts +1 -0
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
2
|
+
import { BufferReader, FieldReader } from '@aztec/foundation/serialize';
|
|
3
|
+
|
|
4
|
+
import type { Blob as BlobBuffer } from 'c-kzg';
|
|
5
|
+
|
|
6
|
+
// This will appear as 0x74785f7374617274 in logs
|
|
7
|
+
export const TX_START_PREFIX = 8392562855083340404n;
|
|
8
|
+
// These are helper constants to decode tx effects from blob encoded fields
|
|
9
|
+
export const TX_START_PREFIX_BYTES_LENGTH = TX_START_PREFIX.toString(16).length / 2;
|
|
10
|
+
// 7 bytes for: | 0 | txlen[0] | txlen[1] | 0 | REVERT_CODE_PREFIX | 0 | revertCode |
|
|
11
|
+
export const TX_EFFECT_PREFIX_BYTE_LENGTH = TX_START_PREFIX_BYTES_LENGTH + 7;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Deserializes a blob buffer into an array of field elements.
|
|
15
|
+
*
|
|
16
|
+
* Blobs are converted into BN254 fields to perform a poseidon2 hash on them (fieldHash).
|
|
17
|
+
* This method is sparse, meaning it does not include trailing zeros at the end of the blob.
|
|
18
|
+
*
|
|
19
|
+
* However, we cannot simply trim the zero's from the end of the blob, as some logs may include zero's
|
|
20
|
+
* within them.
|
|
21
|
+
* If we end on a set of zeros, such as the log below:
|
|
22
|
+
* length 7: [ a, b, c, d, e, 0, 0]
|
|
23
|
+
*
|
|
24
|
+
* we will end up with the incorrect hash if we trim the zeros from the end.
|
|
25
|
+
*
|
|
26
|
+
* Each transactions logs contains a TX start prefix, which includes a string followed
|
|
27
|
+
* by the length ( in field elements ) of the transaction's log.
|
|
28
|
+
*
|
|
29
|
+
* This function finds the end of the last transaction's logs, and returns the array up to this point.
|
|
30
|
+
*
|
|
31
|
+
* We search for a series of Tx Prefixes progressing the cursor in the field reader until we hit
|
|
32
|
+
* a field that is not a Tx Prefix, this indicates that we have reached the end of the last transaction's logs.
|
|
33
|
+
*
|
|
34
|
+
* +------------------+------------------+------------------+------------------+
|
|
35
|
+
* | TX1 Start Prefix | TX1 Log Fields | TX2 Start Prefix | Padded zeros |
|
|
36
|
+
* | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
|
|
37
|
+
* +------------------+------------------+------------------+------------------+
|
|
38
|
+
* ^
|
|
39
|
+
* |
|
|
40
|
+
* Function reads until here --------------------------------
|
|
41
|
+
*
|
|
42
|
+
* @param blob - The blob buffer to deserialize.
|
|
43
|
+
* @returns An array of field elements.
|
|
44
|
+
*/
|
|
45
|
+
export function deserializeEncodedBlobFields(blob: BlobBuffer): Fr[] {
|
|
46
|
+
// Convert blob buffer to array of field elements
|
|
47
|
+
const reader = BufferReader.asReader(blob);
|
|
48
|
+
const array = reader.readArray(blob.length >> 5, Fr); // >> 5 = / 32 (bytes per field)
|
|
49
|
+
const fieldReader = FieldReader.asReader(array);
|
|
50
|
+
|
|
51
|
+
// Read fields until we hit zeros at the end
|
|
52
|
+
while (!fieldReader.isFinished()) {
|
|
53
|
+
const currentField = fieldReader.peekField();
|
|
54
|
+
|
|
55
|
+
// Stop when we hit a zero field
|
|
56
|
+
if (!currentField || currentField.isZero()) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Skip the remaining fields in this transaction
|
|
61
|
+
const len = getLengthFromFirstField(currentField);
|
|
62
|
+
fieldReader.skip(len);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Return array up to last non-zero field
|
|
66
|
+
return array.slice(0, fieldReader.cursor);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getLengthFromFirstField(firstField: Fr): number {
|
|
70
|
+
const buf = firstField.toBuffer().subarray(-TX_EFFECT_PREFIX_BYTE_LENGTH);
|
|
71
|
+
return new Fr(buf.subarray(TX_START_PREFIX_BYTES_LENGTH + 1, TX_START_PREFIX_BYTES_LENGTH + 3)).toNumber();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Extract the fields from a blob buffer, but do not take into account encoding
|
|
76
|
+
* that will include trailing zeros.
|
|
77
|
+
*
|
|
78
|
+
* +------------------+------------------+------------------+------------------+
|
|
79
|
+
* | | | | Padded zeros |
|
|
80
|
+
* | [3 a,b,c] | [3, a, b, c] | [5 d,e,f,0,0] | [0, 0, 0, .., 0] |
|
|
81
|
+
* +------------------+------------------+------------------+------------------+
|
|
82
|
+
* ^
|
|
83
|
+
* |
|
|
84
|
+
* Function reads until here ----------------------
|
|
85
|
+
*/
|
|
86
|
+
export function extractBlobFieldsFromBuffer(blob: BlobBuffer): Fr[] {
|
|
87
|
+
const reader = BufferReader.asReader(blob);
|
|
88
|
+
const array = reader.readArray(blob.length >> 5, Fr);
|
|
89
|
+
|
|
90
|
+
// Find the index of the last non-zero field
|
|
91
|
+
let lastNonZeroIndex = array.length - 1;
|
|
92
|
+
while (lastNonZeroIndex >= 0 && array[lastNonZeroIndex].isZero()) {
|
|
93
|
+
lastNonZeroIndex--;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Return the trimmed array
|
|
97
|
+
return array.slice(0, lastNonZeroIndex + 1);
|
|
98
|
+
}
|
package/src/blob/index.ts
CHANGED
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
import cKzg from 'c-kzg';
|
|
2
|
-
import type { Blob as BlobBuffer } from 'c-kzg';
|
|
3
2
|
|
|
4
|
-
import { poseidon2Hash, sha256 } from '../crypto/index.js';
|
|
5
|
-
import { Fr } from '../fields/index.js';
|
|
6
|
-
import { BufferReader, serializeToBuffer } from '../serialize/index.js';
|
|
7
|
-
|
|
8
|
-
// Importing directly from 'c-kzg' does not work, ignoring import/no-named-as-default-member err:
|
|
9
3
|
/* eslint-disable import/no-named-as-default-member */
|
|
4
|
+
const { loadTrustedSetup } = cKzg;
|
|
10
5
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
computeKzgProof,
|
|
16
|
-
loadTrustedSetup,
|
|
17
|
-
verifyKzgProof,
|
|
18
|
-
} = cKzg;
|
|
6
|
+
export * from './blob.js';
|
|
7
|
+
export * from './mocks.js';
|
|
8
|
+
export * from './encoding.js';
|
|
9
|
+
export * from './interface.js';
|
|
19
10
|
|
|
20
11
|
try {
|
|
21
12
|
loadTrustedSetup();
|
|
@@ -28,172 +19,3 @@ try {
|
|
|
28
19
|
throw new Error(error);
|
|
29
20
|
}
|
|
30
21
|
}
|
|
31
|
-
|
|
32
|
-
// The prefix to the EVM blobHash, defined here: https://eips.ethereum.org/EIPS/eip-4844#specification
|
|
33
|
-
export const VERSIONED_HASH_VERSION_KZG = 0x01;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* A class to create, manage, and prove EVM blobs.
|
|
37
|
-
*/
|
|
38
|
-
export class Blob {
|
|
39
|
-
constructor(
|
|
40
|
-
/** The blob to be broadcast on L1 in bytes form. */
|
|
41
|
-
public readonly data: BlobBuffer,
|
|
42
|
-
/** The hash of all tx effects inside the blob. Used in generating the challenge z and proving that we have included all required effects. */
|
|
43
|
-
public readonly fieldsHash: Fr,
|
|
44
|
-
/** Challenge point z (= H(H(tx_effects), kzgCommmitment). Used such that p(z) = y. */
|
|
45
|
-
public readonly challengeZ: Fr,
|
|
46
|
-
/** Evaluation y = p(z), where p() is the blob polynomial. BLS12 field element, rep. as BigNum in nr, bigint in ts. */
|
|
47
|
-
public readonly evaluationY: Buffer,
|
|
48
|
-
/** Commitment to the blob C. Used in compressed BLS12 point format (48 bytes). */
|
|
49
|
-
public readonly commitment: Buffer,
|
|
50
|
-
/** KZG opening proof for y = p(z). The commitment to quotient polynomial Q, used in compressed BLS12 point format (48 bytes). */
|
|
51
|
-
public readonly proof: Buffer,
|
|
52
|
-
) {}
|
|
53
|
-
|
|
54
|
-
static fromFields(fields: Fr[], multiBlobFieldsHash?: Fr): Blob {
|
|
55
|
-
if (fields.length > FIELD_ELEMENTS_PER_BLOB) {
|
|
56
|
-
throw new Error(
|
|
57
|
-
`Attempted to overfill blob with ${fields.length} elements. The maximum is ${FIELD_ELEMENTS_PER_BLOB}`,
|
|
58
|
-
);
|
|
59
|
-
}
|
|
60
|
-
const dataWithoutZeros = serializeToBuffer(fields);
|
|
61
|
-
const data = Buffer.concat([dataWithoutZeros], BYTES_PER_BLOB);
|
|
62
|
-
|
|
63
|
-
// This matches the output of SpongeBlob.squeeze() in the blob circuit
|
|
64
|
-
const fieldsHash = multiBlobFieldsHash ? multiBlobFieldsHash : poseidon2Hash(fields);
|
|
65
|
-
const commitment = Buffer.from(blobToKzgCommitment(data));
|
|
66
|
-
const challengeZ = poseidon2Hash([fieldsHash, ...commitmentToFields(commitment)]);
|
|
67
|
-
const res = computeKzgProof(data, challengeZ.toBuffer());
|
|
68
|
-
if (!verifyKzgProof(commitment, challengeZ.toBuffer(), res[1], res[0])) {
|
|
69
|
-
throw new Error(`KZG proof did not verify.`);
|
|
70
|
-
}
|
|
71
|
-
const proof = Buffer.from(res[0]);
|
|
72
|
-
const evaluationY = Buffer.from(res[1]);
|
|
73
|
-
|
|
74
|
-
return new Blob(dataWithoutZeros, fieldsHash, challengeZ, evaluationY, commitment, proof);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 48 bytes encoded in fields as [Fr, Fr] = [0->31, 31->48]
|
|
78
|
-
commitmentToFields(): [Fr, Fr] {
|
|
79
|
-
return commitmentToFields(this.commitment);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Returns ethereum's versioned blob hash, following kzg_to_versioned_hash: https://eips.ethereum.org/EIPS/eip-4844#helpers
|
|
83
|
-
getEthVersionedBlobHash(): Buffer {
|
|
84
|
-
const hash = sha256(this.commitment);
|
|
85
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
86
|
-
return hash;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
static getEthVersionedBlobHash(commitment: Buffer): Buffer {
|
|
90
|
-
const hash = sha256(commitment);
|
|
91
|
-
hash[0] = VERSIONED_HASH_VERSION_KZG;
|
|
92
|
-
return hash;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
toBuffer(): Buffer {
|
|
96
|
-
return Buffer.from(
|
|
97
|
-
serializeToBuffer(
|
|
98
|
-
this.data.length,
|
|
99
|
-
this.data,
|
|
100
|
-
this.fieldsHash,
|
|
101
|
-
this.challengeZ,
|
|
102
|
-
this.evaluationY.length,
|
|
103
|
-
this.evaluationY,
|
|
104
|
-
this.commitment.length,
|
|
105
|
-
this.commitment,
|
|
106
|
-
this.proof.length,
|
|
107
|
-
this.proof,
|
|
108
|
-
),
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
static fromBuffer(buf: Buffer | BufferReader): Blob {
|
|
113
|
-
const reader = BufferReader.asReader(buf);
|
|
114
|
-
return new Blob(
|
|
115
|
-
reader.readUint8Array(),
|
|
116
|
-
reader.readObject(Fr),
|
|
117
|
-
reader.readObject(Fr),
|
|
118
|
-
reader.readBuffer(),
|
|
119
|
-
reader.readBuffer(),
|
|
120
|
-
reader.readBuffer(),
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Pad the blob data to it's full size before posting
|
|
126
|
-
*/
|
|
127
|
-
get dataWithZeros(): BlobBuffer {
|
|
128
|
-
return Buffer.concat([this.data], BYTES_PER_BLOB);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get the size of the blob in bytes
|
|
133
|
-
*/
|
|
134
|
-
getSize() {
|
|
135
|
-
return this.data.length;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// Returns a proof of opening of the blob to verify on L1 using the point evaluation precompile:
|
|
139
|
-
// * input[:32] - versioned_hash
|
|
140
|
-
// * input[32:64] - z
|
|
141
|
-
// * input[64:96] - y
|
|
142
|
-
// * input[96:144] - commitment C
|
|
143
|
-
// * input[144:192] - proof (a commitment to the quotient polynomial q(X))
|
|
144
|
-
// See https://eips.ethereum.org/EIPS/eip-4844#point-evaluation-precompile
|
|
145
|
-
getEthBlobEvaluationInputs(): `0x${string}` {
|
|
146
|
-
const buf = Buffer.concat([
|
|
147
|
-
this.getEthVersionedBlobHash(),
|
|
148
|
-
this.challengeZ.toBuffer(),
|
|
149
|
-
this.evaluationY,
|
|
150
|
-
this.commitment,
|
|
151
|
-
this.proof,
|
|
152
|
-
]);
|
|
153
|
-
return `0x${buf.toString('hex')}`;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
static getEthBlobEvaluationInputs(blobs: Blob[]): `0x${string}` {
|
|
157
|
-
let buf = Buffer.alloc(0);
|
|
158
|
-
blobs.forEach(blob => {
|
|
159
|
-
buf = Buffer.concat([
|
|
160
|
-
buf,
|
|
161
|
-
blob.getEthVersionedBlobHash(),
|
|
162
|
-
blob.challengeZ.toBuffer(),
|
|
163
|
-
blob.evaluationY,
|
|
164
|
-
blob.commitment,
|
|
165
|
-
blob.proof,
|
|
166
|
-
]);
|
|
167
|
-
});
|
|
168
|
-
// For multiple blobs, we prefix the number of blobs:
|
|
169
|
-
const lenBuf = Buffer.alloc(1);
|
|
170
|
-
lenBuf.writeUint8(blobs.length);
|
|
171
|
-
buf = Buffer.concat([lenBuf, buf]);
|
|
172
|
-
return `0x${buf.toString('hex')}`;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
static getViemKzgInstance() {
|
|
176
|
-
return {
|
|
177
|
-
blobToKzgCommitment: cKzg.blobToKzgCommitment,
|
|
178
|
-
computeBlobKzgProof: cKzg.computeBlobKzgProof,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Returns as many blobs as we require to broadcast the given fields
|
|
183
|
-
// Assumes we share the fields hash between all blobs
|
|
184
|
-
static getBlobs(fields: Fr[]): Blob[] {
|
|
185
|
-
const numBlobs = Math.max(Math.ceil(fields.length / FIELD_ELEMENTS_PER_BLOB), 1);
|
|
186
|
-
const multiBlobFieldsHash = poseidon2Hash(fields);
|
|
187
|
-
const res = [];
|
|
188
|
-
for (let i = 0; i < numBlobs; i++) {
|
|
189
|
-
const end = fields.length < (i + 1) * FIELD_ELEMENTS_PER_BLOB ? fields.length : (i + 1) * FIELD_ELEMENTS_PER_BLOB;
|
|
190
|
-
res.push(Blob.fromFields(fields.slice(i * FIELD_ELEMENTS_PER_BLOB, end), multiBlobFieldsHash));
|
|
191
|
-
}
|
|
192
|
-
return res;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// 48 bytes encoded in fields as [Fr, Fr] = [0->31, 31->48]
|
|
197
|
-
function commitmentToFields(commitment: Buffer): [Fr, Fr] {
|
|
198
|
-
return [new Fr(commitment.subarray(0, 31)), new Fr(commitment.subarray(31, 48))];
|
|
199
|
-
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The relevant parts of a response from https://ethereum.github.io/beacon-APIs/?urls.primaryName=dev#/Beacon/getBlobSidecars
|
|
3
|
+
*/
|
|
4
|
+
export interface BlobJson {
|
|
5
|
+
blob: string;
|
|
6
|
+
index?: number;
|
|
7
|
+
// eslint-disable-next-line camelcase
|
|
8
|
+
kzg_commitment: string;
|
|
9
|
+
// eslint-disable-next-line camelcase
|
|
10
|
+
kzg_proof: string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { toBufferBE } from '@aztec/foundation/bigint-buffer';
|
|
2
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
3
|
+
|
|
4
|
+
import { Blob } from './blob.js';
|
|
5
|
+
import { TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH } from './encoding.js';
|
|
6
|
+
|
|
7
|
+
// TODO: copied form circuit-types tx effect
|
|
8
|
+
function encodeFirstField(length: number): Fr {
|
|
9
|
+
const lengthBuf = Buffer.alloc(2);
|
|
10
|
+
lengthBuf.writeUInt16BE(length, 0);
|
|
11
|
+
return new Fr(
|
|
12
|
+
Buffer.concat([
|
|
13
|
+
toBufferBE(TX_START_PREFIX, TX_START_PREFIX_BYTES_LENGTH),
|
|
14
|
+
Buffer.alloc(1),
|
|
15
|
+
lengthBuf,
|
|
16
|
+
Buffer.alloc(1),
|
|
17
|
+
Buffer.from([1]),
|
|
18
|
+
Buffer.alloc(1),
|
|
19
|
+
Buffer.alloc(1),
|
|
20
|
+
]),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function makeEncodedBlob(length: number): Promise<Blob> {
|
|
25
|
+
return Blob.fromFields([encodeFirstField(length + 1), ...Array.from({ length: length }, () => Fr.random())]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function makeEncodedBlobFields(fields: Fr[]): Promise<Blob> {
|
|
29
|
+
return Blob.fromFields([encodeFirstField(fields.length + 1), ...fields]);
|
|
30
|
+
}
|
package/src/config/env_var.ts
CHANGED
|
@@ -16,6 +16,8 @@ export type EnvVar =
|
|
|
16
16
|
| 'BB_SKIP_CLEANUP'
|
|
17
17
|
| 'BB_WORKING_DIRECTORY'
|
|
18
18
|
| 'BOOTSTRAP_NODES'
|
|
19
|
+
| 'BLOB_SINK_PORT'
|
|
20
|
+
| 'BLOB_SINK_URL'
|
|
19
21
|
| 'BOT_DA_GAS_LIMIT'
|
|
20
22
|
| 'BOT_FEE_PAYMENT_METHOD'
|
|
21
23
|
| 'BOT_FLUSH_SETUP_TRANSACTIONS'
|
|
@@ -51,7 +53,9 @@ export type EnvVar =
|
|
|
51
53
|
| 'GOVERNANCE_PROPOSER_PAYLOAD_ADDRESS'
|
|
52
54
|
| 'INBOX_CONTRACT_ADDRESS'
|
|
53
55
|
| 'L1_CHAIN_ID'
|
|
54
|
-
| '
|
|
56
|
+
| 'L1_CONSENSUS_HOST_URL'
|
|
57
|
+
| 'L1_CONSENSUS_HOST_API_KEY'
|
|
58
|
+
| 'L1_CONSENSUS_HOST_API_KEY_HEADER'
|
|
55
59
|
| 'L1_PRIVATE_KEY'
|
|
56
60
|
| 'L2_QUEUE_SIZE'
|
|
57
61
|
| 'LOG_ELAPSED_TIME'
|
|
@@ -102,7 +106,6 @@ export type EnvVar =
|
|
|
102
106
|
| 'P2P_UDP_LISTEN_ADDR'
|
|
103
107
|
| 'P2P_ARCHIVED_TX_LIMIT'
|
|
104
108
|
| 'PEER_ID_PRIVATE_KEY'
|
|
105
|
-
| 'PROVER_BLOB_SINK_URL'
|
|
106
109
|
| 'PROOF_VERIFIER_L1_START_BLOCK'
|
|
107
110
|
| 'PROOF_VERIFIER_POLL_INTERVAL_MS'
|
|
108
111
|
| 'PROVER_AGENT_ENABLED'
|
|
@@ -142,7 +145,6 @@ export type EnvVar =
|
|
|
142
145
|
| 'REGISTRY_CONTRACT_ADDRESS'
|
|
143
146
|
| 'ROLLUP_CONTRACT_ADDRESS'
|
|
144
147
|
| 'SEQ_ALLOWED_SETUP_FN'
|
|
145
|
-
| 'SEQ_BLOB_SINK_URL'
|
|
146
148
|
| 'SEQ_MAX_BLOCK_SIZE_IN_BYTES'
|
|
147
149
|
| 'SEQ_MAX_TX_PER_BLOCK'
|
|
148
150
|
| 'SEQ_MIN_TX_PER_BLOCK'
|
|
@@ -205,4 +207,5 @@ export type EnvVar =
|
|
|
205
207
|
| 'FAUCET_L1_ASSETS'
|
|
206
208
|
| 'K8S_POD_NAME'
|
|
207
209
|
| 'K8S_POD_UID'
|
|
208
|
-
| 'K8S_NAMESPACE_NAME'
|
|
210
|
+
| 'K8S_NAMESPACE_NAME'
|
|
211
|
+
| 'CUSTOM_FORWARDER_CONTRACT_ADDRESS';
|
package/src/config/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface ConfigMapping {
|
|
|
9
9
|
printDefault?: (val: any) => string;
|
|
10
10
|
description: string;
|
|
11
11
|
isBoolean?: boolean;
|
|
12
|
+
nested?: Record<string, ConfigMapping>;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export function isBooleanConfigValue<T>(obj: T, key: keyof T): boolean {
|
|
@@ -21,18 +22,15 @@ export function getConfigFromMappings<T>(configMappings: ConfigMappingsType<T>):
|
|
|
21
22
|
const config = {} as T;
|
|
22
23
|
|
|
23
24
|
for (const key in configMappings) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} else if (def !== undefined) {
|
|
34
|
-
(config as any)[key] = def;
|
|
35
|
-
}
|
|
25
|
+
const { env, parseEnv, defaultValue: def, nested } = configMappings[key];
|
|
26
|
+
if (nested) {
|
|
27
|
+
(config as any)[key] = getConfigFromMappings(nested);
|
|
28
|
+
} else {
|
|
29
|
+
const val = env ? process.env[env] : undefined;
|
|
30
|
+
if (val !== undefined) {
|
|
31
|
+
(config as any)[key] = parseEnv ? parseEnv(val) : val;
|
|
32
|
+
} else if (def !== undefined) {
|
|
33
|
+
(config as any)[key] = def;
|
|
36
34
|
}
|
|
37
35
|
}
|
|
38
36
|
}
|
package/src/crypto/index.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
|
-
|
|
3
1
|
export * from './keccak/index.js';
|
|
4
2
|
export * from './random/index.js';
|
|
5
3
|
export * from './sha256/index.js';
|
|
@@ -8,13 +6,3 @@ export * from './pedersen/index.js';
|
|
|
8
6
|
export * from './poseidon/index.js';
|
|
9
7
|
export * from './secp256k1-signer/index.js';
|
|
10
8
|
export * from './keys/index.js';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Init the bb singleton. This constructs (if not already) the barretenberg sync api within bb.js itself.
|
|
14
|
-
* It takes about 100-200ms to initialize. It may not seem like much, but when in conjunction with many other things
|
|
15
|
-
* initializing, developers may want to pick precisely when to incur this cost.
|
|
16
|
-
* If in a test environment, we'll just do it on module load.
|
|
17
|
-
*/
|
|
18
|
-
export async function init() {
|
|
19
|
-
await BarretenbergSync.initSingleton();
|
|
20
|
-
}
|
package/src/crypto/keys/index.ts
CHANGED
|
@@ -2,8 +2,9 @@ import { BarretenbergSync, RawBuffer } from '@aztec/bb.js';
|
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../fields/fields.js';
|
|
4
4
|
|
|
5
|
-
export function vkAsFieldsMegaHonk(input: Buffer): Fr[] {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
export async function vkAsFieldsMegaHonk(input: Buffer): Promise<Fr[]> {
|
|
6
|
+
const api = await BarretenbergSync.initSingleton();
|
|
7
|
+
const result = api.acirVkAsFieldsMegaHonk(new RawBuffer(input));
|
|
8
|
+
|
|
9
|
+
return result.map(bbFr => Fr.fromBuffer(Buffer.from(bbFr.toBuffer()))); // TODO(#4189): remove this conversion
|
|
9
10
|
}
|
|
@@ -7,12 +7,13 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
|
|
|
7
7
|
* Create a pedersen commitment (point) from an array of input fields.
|
|
8
8
|
* Left pads any inputs less than 32 bytes.
|
|
9
9
|
*/
|
|
10
|
-
export function pedersenCommit(input: Buffer[], offset = 0) {
|
|
10
|
+
export async function pedersenCommit(input: Buffer[], offset = 0) {
|
|
11
11
|
if (!input.every(i => i.length <= 32)) {
|
|
12
12
|
throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
|
|
13
13
|
}
|
|
14
14
|
input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
|
|
15
|
-
const
|
|
15
|
+
const api = await BarretenbergSync.initSingleton();
|
|
16
|
+
const point = api.pedersenCommit(
|
|
16
17
|
input.map(i => new FrBarretenberg(i)),
|
|
17
18
|
offset,
|
|
18
19
|
);
|
|
@@ -27,23 +28,21 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
|
|
|
27
28
|
* @param index - The separator index to use for the hash.
|
|
28
29
|
* @returns The pedersen hash.
|
|
29
30
|
*/
|
|
30
|
-
export function pedersenHash(input: Fieldable[], index = 0): Fr {
|
|
31
|
+
export async function pedersenHash(input: Fieldable[], index = 0): Promise<Fr> {
|
|
31
32
|
const inputFields = serializeToFields(input);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
37
|
-
index,
|
|
38
|
-
)
|
|
39
|
-
.toBuffer(),
|
|
40
|
-
),
|
|
33
|
+
const api = await BarretenbergSync.initSingleton();
|
|
34
|
+
const hash = api.pedersenHash(
|
|
35
|
+
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
36
|
+
index,
|
|
41
37
|
);
|
|
38
|
+
return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
|
|
42
39
|
}
|
|
43
40
|
|
|
44
41
|
/**
|
|
45
42
|
* Create a pedersen hash from an arbitrary length buffer.
|
|
46
43
|
*/
|
|
47
|
-
export function pedersenHashBuffer(input: Buffer, index = 0) {
|
|
48
|
-
|
|
44
|
+
export async function pedersenHashBuffer(input: Buffer, index = 0) {
|
|
45
|
+
const api = await BarretenbergSync.initSingleton();
|
|
46
|
+
const result = api.pedersenHashBuffer(input, index);
|
|
47
|
+
return Buffer.from(result.toBuffer());
|
|
49
48
|
}
|
|
@@ -8,17 +8,13 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
|
|
|
8
8
|
* @param input - The input fields to hash.
|
|
9
9
|
* @returns The poseidon hash.
|
|
10
10
|
*/
|
|
11
|
-
export function poseidon2Hash(input: Fieldable[]): Fr {
|
|
11
|
+
export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
12
12
|
const inputFields = serializeToFields(input);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
.poseidon2Hash(
|
|
17
|
-
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
18
|
-
)
|
|
19
|
-
.toBuffer(),
|
|
20
|
-
),
|
|
13
|
+
const api = await BarretenbergSync.initSingleton();
|
|
14
|
+
const hash = api.poseidon2Hash(
|
|
15
|
+
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
21
16
|
);
|
|
17
|
+
return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
|
|
22
18
|
}
|
|
23
19
|
|
|
24
20
|
/**
|
|
@@ -27,29 +23,22 @@ export function poseidon2Hash(input: Fieldable[]): Fr {
|
|
|
27
23
|
* @param separator - The domain separator.
|
|
28
24
|
* @returns The poseidon hash.
|
|
29
25
|
*/
|
|
30
|
-
export function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Fr {
|
|
26
|
+
export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
31
27
|
const inputFields = serializeToFields(input);
|
|
32
28
|
inputFields.unshift(new Fr(separator));
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
38
|
-
)
|
|
39
|
-
.toBuffer(),
|
|
40
|
-
),
|
|
29
|
+
const api = await BarretenbergSync.initSingleton();
|
|
30
|
+
|
|
31
|
+
const hash = api.poseidon2Hash(
|
|
32
|
+
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
41
33
|
);
|
|
34
|
+
return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
|
|
42
35
|
}
|
|
43
36
|
|
|
44
|
-
export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
|
|
37
|
+
export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
|
|
45
38
|
const inputFields = serializeToFields(input);
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
.poseidon2HashAccumulate(inputFields.map(i => new FrBarretenberg(i.toBuffer())))
|
|
50
|
-
.toBuffer(),
|
|
51
|
-
),
|
|
52
|
-
);
|
|
39
|
+
const api = await BarretenbergSync.initSingleton();
|
|
40
|
+
const result = api.poseidon2HashAccumulate(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
|
|
41
|
+
return Fr.fromBuffer(Buffer.from(result.toBuffer()));
|
|
53
42
|
}
|
|
54
43
|
|
|
55
44
|
/**
|
|
@@ -57,19 +46,18 @@ export function poseidon2HashAccumulate(input: Fieldable[]): Fr {
|
|
|
57
46
|
* @param input the input state. Expected to be of size 4.
|
|
58
47
|
* @returns the output state, size 4.
|
|
59
48
|
*/
|
|
60
|
-
export function poseidon2Permutation(input: Fieldable[]): Fr[] {
|
|
49
|
+
export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
61
50
|
const inputFields = serializeToFields(input);
|
|
62
51
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
63
52
|
// assert(input.length === 4, 'Input state must be of size 4');
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
);
|
|
53
|
+
const api = await BarretenbergSync.initSingleton();
|
|
54
|
+
const res = api.poseidon2Permutation(inputFields.map(i => new FrBarretenberg(i.toBuffer())));
|
|
67
55
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
68
56
|
// assert(res.length === 4, 'Output state must be of size 4');
|
|
69
57
|
return res.map(o => Fr.fromBuffer(Buffer.from(o.toBuffer())));
|
|
70
58
|
}
|
|
71
59
|
|
|
72
|
-
export function poseidon2HashBytes(input: Buffer): Fr {
|
|
60
|
+
export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
73
61
|
const inputFields = [];
|
|
74
62
|
for (let i = 0; i < input.length; i += 31) {
|
|
75
63
|
const fieldBytes = Buffer.alloc(32, 0);
|
|
@@ -80,13 +68,10 @@ export function poseidon2HashBytes(input: Buffer): Fr {
|
|
|
80
68
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
81
69
|
}
|
|
82
70
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
.poseidon2Hash(
|
|
87
|
-
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
88
|
-
)
|
|
89
|
-
.toBuffer(),
|
|
90
|
-
),
|
|
71
|
+
const api = await BarretenbergSync.initSingleton();
|
|
72
|
+
const res = api.poseidon2Hash(
|
|
73
|
+
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
91
74
|
);
|
|
75
|
+
|
|
76
|
+
return Fr.fromBuffer(Buffer.from(res.toBuffer()));
|
|
92
77
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { BarretenbergSync, Fr as FrBarretenberg } from '@aztec/bb.js';
|
|
2
|
+
|
|
3
|
+
import { Fr } from '../../../fields/fields.js';
|
|
4
|
+
import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Create a pedersen commitment (point) from an array of input fields.
|
|
8
|
+
* Left pads any inputs less than 32 bytes.
|
|
9
|
+
*/
|
|
10
|
+
export function pedersenCommit(input: Buffer[], offset = 0) {
|
|
11
|
+
if (!input.every(i => i.length <= 32)) {
|
|
12
|
+
throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
|
|
13
|
+
}
|
|
14
|
+
input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
|
|
15
|
+
const point = BarretenbergSync.getSingleton().pedersenCommit(
|
|
16
|
+
input.map(i => new FrBarretenberg(i)),
|
|
17
|
+
offset,
|
|
18
|
+
);
|
|
19
|
+
// toBuffer returns Uint8Arrays (browser/worker-boundary friendly).
|
|
20
|
+
// TODO: rename toTypedArray()?
|
|
21
|
+
return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a pedersen hash (field) from an array of input fields.
|
|
26
|
+
* @param input - The input fieldables to hash.
|
|
27
|
+
* @param index - The separator index to use for the hash.
|
|
28
|
+
* @returns The pedersen hash.
|
|
29
|
+
*/
|
|
30
|
+
export function pedersenHash(input: Fieldable[], index = 0): Fr {
|
|
31
|
+
const inputFields = serializeToFields(input);
|
|
32
|
+
const hash = BarretenbergSync.getSingleton().pedersenHash(
|
|
33
|
+
inputFields.map(i => new FrBarretenberg(i.toBuffer())), // TODO(#4189): remove this stupid conversion
|
|
34
|
+
index,
|
|
35
|
+
);
|
|
36
|
+
return Fr.fromBuffer(Buffer.from(hash.toBuffer()));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Create a pedersen hash from an arbitrary length buffer.
|
|
41
|
+
*/
|
|
42
|
+
export function pedersenHashBuffer(input: Buffer, index = 0) {
|
|
43
|
+
const result = BarretenbergSync.getSingleton().pedersenHashBuffer(input, index);
|
|
44
|
+
return Buffer.from(result.toBuffer());
|
|
45
|
+
}
|