@aztec/foundation 0.0.1-commit.d3ec352c → 0.0.1-commit.f295ac2
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/array/index.d.ts +2 -1
- package/dest/array/index.d.ts.map +1 -1
- package/dest/array/index.js +1 -0
- package/dest/array/sorted_array.d.ts +10 -0
- package/dest/array/sorted_array.d.ts.map +1 -0
- package/dest/array/sorted_array.js +106 -0
- package/dest/branded-types/block_number.d.ts +4 -1
- package/dest/branded-types/block_number.d.ts.map +1 -1
- package/dest/branded-types/block_number.js +3 -0
- package/dest/branded-types/checkpoint_number.d.ts +10 -2
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
- package/dest/branded-types/checkpoint_number.js +17 -8
- package/dest/branded-types/index.d.ts +3 -2
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +2 -1
- package/dest/branded-types/index_within_checkpoint.d.ts +42 -0
- package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -0
- package/dest/branded-types/index_within_checkpoint.js +59 -0
- package/dest/buffer/buffer16.d.ts +5 -2
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer16.js +4 -2
- package/dest/buffer/buffer32.d.ts +5 -2
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +4 -2
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +15 -4
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +32 -9
- package/dest/config/network_config.d.ts +13 -1
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +3 -1
- package/dest/config/parse-env.d.ts +3 -0
- package/dest/config/parse-env.d.ts.map +1 -0
- package/dest/config/parse-env.js +7 -0
- package/dest/config/secret_value.js +3 -1
- package/dest/crypto/bls/bn254_keystore.js +1 -1
- package/dest/crypto/bls/index.js +1 -1
- package/dest/crypto/bn254/index.d.ts +35 -51
- package/dest/crypto/bn254/index.d.ts.map +1 -1
- package/dest/crypto/bn254/index.js +51 -140
- package/dest/crypto/ecdsa/signature.d.ts +11 -2
- package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
- package/dest/crypto/ecdsa/signature.js +2 -2
- package/dest/crypto/grumpkin/index.d.ts +4 -2
- package/dest/crypto/grumpkin/index.d.ts.map +1 -1
- package/dest/crypto/grumpkin/index.js +2 -1
- package/dest/crypto/keys/index.d.ts +2 -2
- package/dest/crypto/keys/index.d.ts.map +1 -1
- package/dest/crypto/keys/index.js +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +2 -2
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +1 -1
- package/dest/crypto/poseidon/index.d.ts +2 -2
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +1 -1
- package/dest/crypto/random/randomness_singleton.d.ts +2 -2
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.js +3 -3
- package/dest/crypto/schnorr/index.d.ts +3 -2
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +1 -1
- package/dest/crypto/schnorr/signature.d.ts +2 -2
- package/dest/crypto/schnorr/signature.d.ts.map +1 -1
- package/dest/crypto/schnorr/signature.js +2 -2
- package/dest/crypto/sha256/index.d.ts +2 -2
- package/dest/crypto/sha256/index.d.ts.map +1 -1
- package/dest/crypto/sha256/index.js +1 -1
- package/dest/crypto/sha512/index.d.ts +2 -2
- package/dest/crypto/sha512/index.d.ts.map +1 -1
- package/dest/crypto/sha512/index.js +1 -1
- package/dest/crypto/signature/index.d.ts +2 -2
- package/dest/crypto/signature/index.d.ts.map +1 -1
- package/dest/crypto/sync/pedersen/index.d.ts +2 -2
- package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
- package/dest/crypto/sync/pedersen/index.js +1 -1
- package/dest/crypto/sync/poseidon/index.d.ts +2 -2
- package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/sync/poseidon/index.js +1 -1
- package/dest/{fields/bls12_fields.d.ts → curves/bls12/field.d.ts} +3 -3
- package/dest/curves/bls12/field.d.ts.map +1 -0
- package/dest/{fields/bls12_fields.js → curves/bls12/field.js} +12 -9
- package/dest/curves/bls12/index.d.ts +3 -0
- package/dest/curves/bls12/index.d.ts.map +1 -0
- package/dest/curves/bls12/index.js +2 -0
- package/dest/{fields/bls12_point.d.ts → curves/bls12/point.d.ts} +13 -4
- package/dest/curves/bls12/point.d.ts.map +1 -0
- package/dest/{fields/bls12_point.js → curves/bls12/point.js} +10 -8
- package/dest/{fields/fields.d.ts → curves/bn254/field.d.ts} +7 -15
- package/dest/curves/bn254/field.d.ts.map +1 -0
- package/dest/{fields/fields.js → curves/bn254/field.js} +35 -49
- package/dest/curves/bn254/index.d.ts +3 -0
- package/dest/curves/bn254/index.d.ts.map +1 -0
- package/dest/curves/bn254/index.js +2 -0
- package/dest/curves/bn254/point.d.ts +54 -0
- package/dest/curves/bn254/point.d.ts.map +1 -0
- package/dest/curves/bn254/point.js +143 -0
- package/dest/curves/grumpkin/index.d.ts +10 -0
- package/dest/curves/grumpkin/index.d.ts.map +1 -0
- package/dest/curves/grumpkin/index.js +3 -0
- package/dest/{fields → curves/grumpkin}/point.d.ts +13 -4
- package/dest/curves/grumpkin/point.d.ts.map +1 -0
- package/dest/{fields → curves/grumpkin}/point.js +7 -7
- package/dest/error/index.d.ts +4 -4
- package/dest/error/index.d.ts.map +1 -1
- package/dest/eth-address/index.d.ts +3 -2
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +10 -3
- package/dest/eth-signature/eth_signature.d.ts +4 -1
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/iterator/filter.d.ts +3 -0
- package/dest/iterator/filter.d.ts.map +1 -0
- package/dest/iterator/filter.js +7 -0
- package/dest/iterator/index.d.ts +2 -0
- package/dest/iterator/index.d.ts.map +1 -0
- package/dest/iterator/index.js +1 -0
- package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
- package/dest/log/pino-logger.d.ts +1 -1
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +1 -1
- package/dest/retry/index.d.ts +11 -1
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +11 -0
- package/dest/schemas/schemas.d.ts +7 -3
- package/dest/schemas/schemas.d.ts.map +1 -1
- package/dest/schemas/schemas.js +9 -3
- package/dest/schemas/types.d.ts +31 -1
- package/dest/schemas/types.d.ts.map +1 -1
- package/dest/schemas/types.js +25 -1
- package/dest/serialize/buffer_reader.d.ts +7 -4
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -4
- package/dest/serialize/field_reader.d.ts +2 -2
- package/dest/serialize/field_reader.d.ts.map +1 -1
- package/dest/serialize/field_reader.js +1 -1
- package/dest/serialize/free_funcs.d.ts +2 -2
- package/dest/serialize/free_funcs.d.ts.map +1 -1
- package/dest/serialize/free_funcs.js +1 -1
- package/dest/serialize/serialize.d.ts +2 -2
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +1 -1
- package/dest/sleep/index.d.ts +3 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/sleep/index.js +4 -0
- package/dest/timer/date.d.ts +2 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +3 -0
- package/dest/trees/{balanced_merkle_tree.d.ts → balanced_merkle_tree_root.d.ts} +1 -5
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{balanced_merkle_tree.js → balanced_merkle_tree_root.js} +1 -13
- package/dest/trees/hasher.d.ts +4 -1
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +15 -5
- package/dest/trees/index.d.ts +4 -4
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +3 -3
- package/dest/trees/indexed_merkle_tree.js +1 -1
- package/dest/trees/membership_witness.d.ts +8 -2
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/membership_witness.js +1 -1
- package/dest/trees/merkle_tree_calculator.js +1 -1
- package/dest/trees/sibling_path.d.ts +3 -2
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +2 -2
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +1 -5
- package/dest/trees/{unbalanced_merkle_tree.d.ts → unbalanced_merkle_tree_root.d.ts} +3 -5
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -0
- package/dest/trees/{unbalanced_merkle_tree.js → unbalanced_merkle_tree_root.js} +11 -49
- package/dest/trees/unbalanced_tree_store.d.ts +5 -1
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +49 -1
- package/dest/types/index.d.ts +4 -2
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +3 -0
- package/package.json +25 -7
- package/src/array/index.ts +1 -0
- package/src/array/sorted_array.ts +133 -0
- package/src/branded-types/block_number.ts +5 -0
- package/src/branded-types/checkpoint_number.ts +22 -7
- package/src/branded-types/index.ts +2 -1
- package/src/branded-types/index_within_checkpoint.ts +88 -0
- package/src/buffer/buffer16.ts +2 -2
- package/src/buffer/buffer32.ts +2 -2
- package/src/config/env_var.ts +30 -11
- package/src/config/index.ts +39 -6
- package/src/config/network_config.ts +2 -0
- package/src/config/parse-env.ts +4 -0
- package/src/crypto/bls/bn254_keystore.ts +1 -1
- package/src/crypto/bls/index.ts +1 -1
- package/src/crypto/bn254/index.ts +50 -159
- package/src/crypto/ecdsa/signature.ts +2 -2
- package/src/crypto/grumpkin/index.ts +3 -1
- package/src/crypto/keys/index.ts +1 -1
- package/src/crypto/pedersen/pedersen.wasm.ts +1 -1
- package/src/crypto/poseidon/index.ts +1 -1
- package/src/crypto/random/randomness_singleton.ts +2 -4
- package/src/crypto/schnorr/index.ts +2 -1
- package/src/crypto/schnorr/signature.ts +2 -2
- package/src/crypto/sha256/index.ts +1 -1
- package/src/crypto/sha512/index.ts +1 -1
- package/src/crypto/signature/index.ts +1 -1
- package/src/crypto/sync/pedersen/index.ts +1 -1
- package/src/crypto/sync/poseidon/index.ts +1 -1
- package/src/{fields/bls12_fields.ts → curves/bls12/field.ts} +6 -6
- package/src/curves/bls12/index.ts +2 -0
- package/src/{fields/bls12_point.ts → curves/bls12/point.ts} +7 -7
- package/src/{fields/fields.ts → curves/bn254/field.ts} +33 -56
- package/src/curves/bn254/index.ts +2 -0
- package/src/curves/bn254/point.ts +170 -0
- package/src/curves/grumpkin/index.ts +11 -0
- package/src/{fields → curves/grumpkin}/point.ts +7 -7
- package/src/eth-address/index.ts +8 -2
- package/src/iterator/filter.ts +11 -0
- package/src/iterator/index.ts +1 -0
- package/src/log/pino-logger.ts +2 -1
- package/src/retry/index.ts +18 -0
- package/src/schemas/schemas.ts +14 -3
- package/src/schemas/types.ts +33 -0
- package/src/serialize/buffer_reader.ts +21 -9
- package/src/serialize/field_reader.ts +1 -1
- package/src/serialize/free_funcs.ts +1 -1
- package/src/serialize/serialize.ts +1 -1
- package/src/sleep/index.ts +6 -0
- package/src/timer/date.ts +4 -0
- package/src/trees/{balanced_merkle_tree.ts → balanced_merkle_tree_root.ts} +1 -12
- package/src/trees/hasher.ts +13 -0
- package/src/trees/index.ts +3 -3
- package/src/trees/indexed_merkle_tree.ts +1 -1
- package/src/trees/membership_witness.ts +1 -1
- package/src/trees/merkle_tree_calculator.ts +1 -1
- package/src/trees/sibling_path.ts +2 -2
- package/src/trees/unbalanced_merkle_tree_calculator.ts +1 -12
- package/src/trees/{unbalanced_merkle_tree.ts → unbalanced_merkle_tree_root.ts} +17 -61
- package/src/trees/unbalanced_tree_store.ts +57 -2
- package/src/types/index.ts +6 -1
- package/dest/crypto/bn254/bn254_utils.d.ts +0 -39
- package/dest/crypto/bn254/bn254_utils.d.ts.map +0 -1
- package/dest/crypto/bn254/bn254_utils.js +0 -56
- package/dest/crypto/index.d.ts +0 -17
- package/dest/crypto/index.d.ts.map +0 -1
- package/dest/crypto/index.js +0 -16
- package/dest/fields/bls12_fields.d.ts.map +0 -1
- package/dest/fields/bls12_point.d.ts.map +0 -1
- package/dest/fields/coordinate.d.ts +0 -63
- package/dest/fields/coordinate.d.ts.map +0 -1
- package/dest/fields/coordinate.js +0 -96
- package/dest/fields/fields.d.ts.map +0 -1
- package/dest/fields/index.d.ts +0 -6
- package/dest/fields/index.d.ts.map +0 -1
- package/dest/fields/index.js +0 -5
- package/dest/fields/point.d.ts.map +0 -1
- package/dest/index.d.ts +0 -31
- package/dest/index.d.ts.map +0 -1
- package/dest/index.js +0 -31
- package/dest/json-rpc/server/telemetry.d.ts +0 -2
- package/dest/json-rpc/server/telemetry.d.ts.map +0 -1
- package/dest/json-rpc/server/telemetry.js +0 -0
- package/dest/trees/balanced_merkle_tree.d.ts.map +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +0 -1
- package/src/crypto/bn254/bn254_utils.ts +0 -64
- package/src/crypto/index.ts +0 -16
- package/src/fields/coordinate.ts +0 -104
- package/src/fields/index.ts +0 -5
- package/src/index.ts +0 -31
- package/src/json-rpc/server/telemetry.ts +0 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BN254_G1_GENERATOR,
|
|
3
|
+
BN254_G2_GENERATOR,
|
|
4
|
+
BarretenbergSync,
|
|
5
|
+
type Bn254G1Point as BbApiBn254G1Point,
|
|
6
|
+
type Bn254G2Point as BbApiBn254G2Point,
|
|
7
|
+
} from '@aztec/bb.js';
|
|
8
|
+
|
|
9
|
+
import { Fq, Fr } from './field.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* BN254 G1 point using foundation field classes.
|
|
13
|
+
* Represents a point on the BN254 elliptic curve in affine coordinates.
|
|
14
|
+
*/
|
|
15
|
+
export class Bn254G1Point {
|
|
16
|
+
constructor(
|
|
17
|
+
public readonly x: Fq,
|
|
18
|
+
public readonly y: Fq,
|
|
19
|
+
) {}
|
|
20
|
+
|
|
21
|
+
private toBbApiPoint(): BbApiBn254G1Point {
|
|
22
|
+
return {
|
|
23
|
+
x: this.x.toBuffer(),
|
|
24
|
+
y: this.y.toBuffer(),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
private static fromBbApiPoint(point: BbApiBn254G1Point): Bn254G1Point {
|
|
29
|
+
return new Bn254G1Point(Fq.fromBuffer(Buffer.from(point.x)), Fq.fromBuffer(Buffer.from(point.y)));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async isOnCurve(): Promise<boolean> {
|
|
33
|
+
await BarretenbergSync.initSingleton();
|
|
34
|
+
const api = BarretenbergSync.getSingleton();
|
|
35
|
+
|
|
36
|
+
const apiPoint = this.toBbApiPoint();
|
|
37
|
+
const response = api.bn254G1IsOnCurve({ point: apiPoint });
|
|
38
|
+
return response.isOnCurve;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get the generator point for BN254 G1, or perform scalar multiplication.
|
|
43
|
+
* When called without arguments, returns the base generator point.
|
|
44
|
+
* When called with a scalar, returns scalar * generator (useful for public key derivation).
|
|
45
|
+
*/
|
|
46
|
+
static async generator(scalar?: Fr): Promise<Bn254G1Point> {
|
|
47
|
+
if (!scalar) {
|
|
48
|
+
return new Bn254G1Point(
|
|
49
|
+
Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.x)),
|
|
50
|
+
Fq.fromBuffer(Buffer.from(BN254_G1_GENERATOR.y)),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
await BarretenbergSync.initSingleton();
|
|
55
|
+
const api = BarretenbergSync.getSingleton();
|
|
56
|
+
|
|
57
|
+
const response = api.bn254G1Mul({
|
|
58
|
+
point: BN254_G1_GENERATOR,
|
|
59
|
+
scalar: scalar.toBuffer(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return Bn254G1Point.fromBbApiPoint(response.point);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Decompress a BN254 G1 point from compressed form (32 bytes).
|
|
67
|
+
* The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
|
|
68
|
+
* in the most significant bit.
|
|
69
|
+
*/
|
|
70
|
+
static async fromCompressed(compressed: Buffer): Promise<Bn254G1Point> {
|
|
71
|
+
if (compressed.length !== 32) {
|
|
72
|
+
throw new Error('Invalid compressed point length');
|
|
73
|
+
}
|
|
74
|
+
await BarretenbergSync.initSingleton();
|
|
75
|
+
const api = BarretenbergSync.getSingleton();
|
|
76
|
+
|
|
77
|
+
const response = api.bn254G1FromCompressed({
|
|
78
|
+
compressed: new Uint8Array(compressed),
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return Bn254G1Point.fromBbApiPoint(response.point);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Compress this BN254 G1 point to 32 bytes.
|
|
86
|
+
* The compressed format encodes the x-coordinate and the sign bit of the y-coordinate
|
|
87
|
+
* in the most significant bit (bit 255).
|
|
88
|
+
*/
|
|
89
|
+
compress(): Buffer {
|
|
90
|
+
const xBytes = this.x.toBuffer();
|
|
91
|
+
// Get the least significant bit of y to determine the sign
|
|
92
|
+
const yLsb = this.y.toBigInt() & 1n;
|
|
93
|
+
// If y is odd, set the most significant bit (bit 255) of the output
|
|
94
|
+
if (yLsb === 1n) {
|
|
95
|
+
xBytes[0] |= 0x80;
|
|
96
|
+
}
|
|
97
|
+
return xBytes;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
equals(other: Bn254G1Point): boolean {
|
|
101
|
+
return this.x.equals(other.x) && this.y.equals(other.y);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
toString(): string {
|
|
105
|
+
return `Bn254G1Point(x: ${this.x.toString()}, y: ${this.y.toString()})`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* BN254 G2 point using foundation field classes.
|
|
111
|
+
* Represents a point on the BN254 G2 curve (twist curve) in affine coordinates.
|
|
112
|
+
* G2 points use extension field coordinates (Fq2).
|
|
113
|
+
*/
|
|
114
|
+
export class Bn254G2Point {
|
|
115
|
+
constructor(
|
|
116
|
+
public readonly x: [Fq, Fq],
|
|
117
|
+
public readonly y: [Fq, Fq],
|
|
118
|
+
) {}
|
|
119
|
+
|
|
120
|
+
private toBbApiPoint(): BbApiBn254G2Point {
|
|
121
|
+
return {
|
|
122
|
+
x: [this.x[0].toBuffer(), this.x[1].toBuffer()],
|
|
123
|
+
y: [this.y[0].toBuffer(), this.y[1].toBuffer()],
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private static fromBbApiPoint(point: BbApiBn254G2Point): Bn254G2Point {
|
|
128
|
+
return new Bn254G2Point(
|
|
129
|
+
[Fq.fromBuffer(Buffer.from(point.x[0])), Fq.fromBuffer(Buffer.from(point.x[1]))],
|
|
130
|
+
[Fq.fromBuffer(Buffer.from(point.y[0])), Fq.fromBuffer(Buffer.from(point.y[1]))],
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Get the generator point for BN254 G2, or perform scalar multiplication.
|
|
136
|
+
* When called without arguments, returns the base generator point.
|
|
137
|
+
* When called with a scalar, returns scalar * generator.
|
|
138
|
+
*/
|
|
139
|
+
static async generator(scalar?: Fr): Promise<Bn254G2Point> {
|
|
140
|
+
if (!scalar) {
|
|
141
|
+
return new Bn254G2Point(
|
|
142
|
+
[Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.x[1]))],
|
|
143
|
+
[Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[0])), Fq.fromBuffer(Buffer.from(BN254_G2_GENERATOR.y[1]))],
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
await BarretenbergSync.initSingleton();
|
|
148
|
+
const api = BarretenbergSync.getSingleton();
|
|
149
|
+
|
|
150
|
+
const response = api.bn254G2Mul({
|
|
151
|
+
point: BN254_G2_GENERATOR as BbApiBn254G2Point,
|
|
152
|
+
scalar: scalar.toBuffer(),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return Bn254G2Point.fromBbApiPoint(response.point);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
equals(other: Bn254G2Point): boolean {
|
|
159
|
+
return (
|
|
160
|
+
this.x[0].equals(other.x[0]) &&
|
|
161
|
+
this.x[1].equals(other.x[1]) &&
|
|
162
|
+
this.y[0].equals(other.y[0]) &&
|
|
163
|
+
this.y[1].equals(other.y[1])
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
toString(): string {
|
|
168
|
+
return `Bn254G2Point(x: (${this.x[0].toString()}, ${this.x[1].toString()}), y: (${this.y[0].toString()}, ${this.y[1].toString()}))`;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Fq } from '../bn254/field.js';
|
|
2
|
+
|
|
3
|
+
export * from './point.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* GrumpkinScalar is an Fq.
|
|
7
|
+
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
8
|
+
* the Aztec Protocol Specs.
|
|
9
|
+
*/
|
|
10
|
+
export type GrumpkinScalar = Fq;
|
|
11
|
+
export const GrumpkinScalar = Fq;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { toBigIntBE } from '
|
|
2
|
-
import { poseidon2Hash } from '
|
|
3
|
-
import { randomBoolean } from '
|
|
4
|
-
import { hexSchemaFor } from '
|
|
5
|
-
import { BufferReader, FieldReader, serializeToBuffer } from '
|
|
6
|
-
import { bufferToHex, hexToBuffer } from '
|
|
7
|
-
import { Fr } from '
|
|
1
|
+
import { toBigIntBE } from '../../bigint-buffer/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../../crypto/poseidon/index.js';
|
|
3
|
+
import { randomBoolean } from '../../crypto/random/index.js';
|
|
4
|
+
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
5
|
+
import { BufferReader, FieldReader, serializeToBuffer } from '../../serialize/index.js';
|
|
6
|
+
import { bufferToHex, hexToBuffer } from '../../string/index.js';
|
|
7
|
+
import { Fr } from '../bn254/field.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Represents a Point on an elliptic curve with x and y coordinates.
|
package/src/eth-address/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { inspect } from 'util';
|
|
|
2
2
|
|
|
3
3
|
import { keccak256String } from '../crypto/keccak/index.js';
|
|
4
4
|
import { randomBytes } from '../crypto/random/index.js';
|
|
5
|
-
import { Fr } from '../
|
|
5
|
+
import { Fr } from '../curves/bn254/index.js';
|
|
6
6
|
import { hexSchemaFor } from '../schemas/utils.js';
|
|
7
7
|
import { BufferReader, FieldReader } from '../serialize/index.js';
|
|
8
8
|
import { TypeRegistry } from '../serialize/type_registry.js';
|
|
@@ -249,7 +249,7 @@ export class EthAddress {
|
|
|
249
249
|
/** Converts a number into an address. Useful for testing. */
|
|
250
250
|
static fromNumber(num: bigint | number): EthAddress {
|
|
251
251
|
const buffer = Buffer.alloc(EthAddress.SIZE_IN_BYTES);
|
|
252
|
-
buffer.writeBigUInt64BE(BigInt(num),
|
|
252
|
+
buffer.writeBigUInt64BE(BigInt(num), EthAddress.SIZE_IN_BYTES - 8);
|
|
253
253
|
return new EthAddress(buffer);
|
|
254
254
|
}
|
|
255
255
|
|
|
@@ -261,6 +261,12 @@ export class EthAddress {
|
|
|
261
261
|
// Serialization from hex string.
|
|
262
262
|
return hexSchemaFor(EthAddress, EthAddress.isAddress);
|
|
263
263
|
}
|
|
264
|
+
|
|
265
|
+
static areEqual(a: EthAddress | string, b: EthAddress | string) {
|
|
266
|
+
const addrA = typeof a === 'string' ? EthAddress.fromString(a) : a;
|
|
267
|
+
const addrB = typeof b === 'string' ? EthAddress.fromString(b) : b;
|
|
268
|
+
return addrA.equals(addrB);
|
|
269
|
+
}
|
|
264
270
|
}
|
|
265
271
|
|
|
266
272
|
// For deserializing JSON.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Wraps an async iterable iterator such that it filters values based on a predicate. */
|
|
2
|
+
export async function* filter<T>(
|
|
3
|
+
iterator: AsyncIterableIterator<T>,
|
|
4
|
+
predicate: (item: T) => boolean | Promise<boolean>,
|
|
5
|
+
): AsyncIterableIterator<T> {
|
|
6
|
+
for await (const item of iterator) {
|
|
7
|
+
if (await predicate(item)) {
|
|
8
|
+
yield item;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { filter } from './filter.js';
|
package/src/log/pino-logger.ts
CHANGED
|
@@ -5,7 +5,8 @@ import type { Writable } from 'stream';
|
|
|
5
5
|
import { inspect } from 'util';
|
|
6
6
|
|
|
7
7
|
import { compactArray } from '../collection/array.js';
|
|
8
|
-
import {
|
|
8
|
+
import type { EnvVar } from '../config/index.js';
|
|
9
|
+
import { parseBooleanEnv } from '../config/parse-env.js';
|
|
9
10
|
import { GoogleCloudLoggerConfig } from './gcloud-logger-config.js';
|
|
10
11
|
import { getLogLevelFromFilters, parseEnv } from './log-filters.js';
|
|
11
12
|
import type { LogLevel } from './log-levels.js';
|
package/src/retry/index.ts
CHANGED
|
@@ -103,3 +103,21 @@ export async function retryUntil<T>(
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Convenience wrapper around retryUntil with fast polling for tests.
|
|
109
|
+
* Uses 10s timeout and 100ms polling interval by default.
|
|
110
|
+
*
|
|
111
|
+
* @param fn - The function to retry until it returns a truthy value.
|
|
112
|
+
* @param name - Description of what we're waiting for (for error messages).
|
|
113
|
+
* @param timeout - Optional timeout in seconds. Defaults to 10s.
|
|
114
|
+
* @param interval - Optional interval in seconds. Defaults to 0.1s (100ms).
|
|
115
|
+
*/
|
|
116
|
+
export function retryFastUntil<T>(
|
|
117
|
+
fn: () => (T | undefined) | Promise<T | undefined>,
|
|
118
|
+
name = '',
|
|
119
|
+
timeout = 10,
|
|
120
|
+
interval = 0.1,
|
|
121
|
+
) {
|
|
122
|
+
return retryUntil(fn, name, timeout, interval);
|
|
123
|
+
}
|
package/src/schemas/schemas.ts
CHANGED
|
@@ -4,10 +4,10 @@ import { EpochNumber, EpochNumberSchema } from '../branded-types/epoch.js';
|
|
|
4
4
|
import { SlotNumber, SlotNumberSchema } from '../branded-types/slot.js';
|
|
5
5
|
import { Buffer32 } from '../buffer/buffer32.js';
|
|
6
6
|
import { SecretValue } from '../config/secret_value.js';
|
|
7
|
+
import { Fq, Fr } from '../curves/bn254/field.js';
|
|
8
|
+
import { Point } from '../curves/grumpkin/point.js';
|
|
7
9
|
import { EthAddress } from '../eth-address/index.js';
|
|
8
|
-
import {
|
|
9
|
-
import { Point } from '../fields/point.js';
|
|
10
|
-
import { isHex, withoutHexPrefix } from '../string/index.js';
|
|
10
|
+
import { isHex, withHexPrefix, withoutHexPrefix } from '../string/index.js';
|
|
11
11
|
import { bufferSchema, hexSchema } from './utils.js';
|
|
12
12
|
|
|
13
13
|
export const schemas = {
|
|
@@ -58,6 +58,14 @@ export const schemas = {
|
|
|
58
58
|
.max(2 ** 32 - 1),
|
|
59
59
|
),
|
|
60
60
|
|
|
61
|
+
/** Coerces input to UInt64. */
|
|
62
|
+
UInt64: z.union([z.bigint(), z.number(), z.string()]).pipe(
|
|
63
|
+
z.coerce
|
|
64
|
+
.bigint()
|
|
65
|
+
.min(0n)
|
|
66
|
+
.max(2n ** 64n - 1n),
|
|
67
|
+
),
|
|
68
|
+
|
|
61
69
|
/** Accepts a hex string as a Buffer32 type. */
|
|
62
70
|
Buffer32: z.string().refine(isHex, 'Not a valid hex string').transform(Buffer32.fromString),
|
|
63
71
|
|
|
@@ -74,6 +82,9 @@ export const schemas = {
|
|
|
74
82
|
/** Hex string with an optional 0x prefix which gets removed as part of the parsing. */
|
|
75
83
|
HexString: hexSchema,
|
|
76
84
|
|
|
85
|
+
/** Hex string with an optional 0x prefix which gets enforced as part of the parsing. */
|
|
86
|
+
HexStringWith0x: z.string().refine(isHex, 'Not a valid hex string').transform(withHexPrefix),
|
|
87
|
+
|
|
77
88
|
/** A secret config value */
|
|
78
89
|
SecretValue: SecretValue.schema,
|
|
79
90
|
};
|
package/src/schemas/types.ts
CHANGED
|
@@ -1,3 +1,36 @@
|
|
|
1
1
|
import type { ZodType } from 'zod';
|
|
2
2
|
|
|
3
3
|
export type ZodFor<T> = ZodType<T, any, any>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a schema validator that enforces all properties of type T are present in the schema.
|
|
7
|
+
* This provides compile-time safety to ensure schemas don't miss optional properties.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* interface MyConfig {
|
|
12
|
+
* foo: string;
|
|
13
|
+
* bar?: number;
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* // ✅ This will work - all keys present
|
|
17
|
+
* const schema1 = zodFor<MyConfig>()(z.object({
|
|
18
|
+
* foo: z.string(),
|
|
19
|
+
* bar: z.number().optional(),
|
|
20
|
+
* }));
|
|
21
|
+
*
|
|
22
|
+
* // ❌ This will error - 'bar' is missing
|
|
23
|
+
* const schema2 = zodFor<MyConfig>()(z.object({
|
|
24
|
+
* foo: z.string(),
|
|
25
|
+
* }));
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export function zodFor<T>() {
|
|
29
|
+
return (schema => schema) as <S extends ZodType<any, any, any>>(
|
|
30
|
+
schema: keyof T extends keyof S['_output']
|
|
31
|
+
? keyof S['_output'] extends keyof T
|
|
32
|
+
? S
|
|
33
|
+
: S & { __error__: 'Schema has extra keys not in type'; __extra__: Exclude<keyof S['_output'], keyof T> }
|
|
34
|
+
: S & { __error__: 'Schema is missing keys from type'; __missing__: Exclude<keyof T, keyof S['_output']> },
|
|
35
|
+
) => S;
|
|
36
|
+
}
|
|
@@ -224,15 +224,22 @@ export class BufferReader {
|
|
|
224
224
|
* deserializing each one using the 'fromBuffer' method of 'itemDeserializer'.
|
|
225
225
|
*
|
|
226
226
|
* @param itemDeserializer - Object with 'fromBuffer' method to deserialize vector elements.
|
|
227
|
+
* @param maxSize - Optional maximum allowed size for the vector. If the size exceeds this, an error is thrown.
|
|
227
228
|
* @returns An array of deserialized elements of type T.
|
|
228
229
|
*/
|
|
229
|
-
public readVector<T>(
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
230
|
+
public readVector<T>(
|
|
231
|
+
itemDeserializer: {
|
|
232
|
+
/**
|
|
233
|
+
* A method to deserialize data from a buffer.
|
|
234
|
+
*/
|
|
235
|
+
fromBuffer: (reader: BufferReader) => T;
|
|
236
|
+
},
|
|
237
|
+
maxSize?: number,
|
|
238
|
+
): T[] {
|
|
235
239
|
const size = this.readNumber();
|
|
240
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
241
|
+
throw new Error(`Vector size ${size} exceeds maximum allowed ${maxSize}`);
|
|
242
|
+
}
|
|
236
243
|
const result = new Array<T>(size);
|
|
237
244
|
for (let i = 0; i < size; i++) {
|
|
238
245
|
result[i] = itemDeserializer.fromBuffer(this);
|
|
@@ -344,10 +351,11 @@ export class BufferReader {
|
|
|
344
351
|
* The method first reads the size of the string, then reads the corresponding
|
|
345
352
|
* number of bytes from the buffer and converts them to a string.
|
|
346
353
|
*
|
|
354
|
+
* @param maxSize - Optional maximum allowed size for the string buffer. If the size exceeds this, an error is thrown.
|
|
347
355
|
* @returns The read string from the buffer.
|
|
348
356
|
*/
|
|
349
|
-
public readString(): string {
|
|
350
|
-
return this.readBuffer().toString();
|
|
357
|
+
public readString(maxSize?: number): string {
|
|
358
|
+
return this.readBuffer(maxSize).toString();
|
|
351
359
|
}
|
|
352
360
|
|
|
353
361
|
/**
|
|
@@ -356,10 +364,14 @@ export class BufferReader {
|
|
|
356
364
|
* a Buffer with that size containing the bytes. Useful for reading variable-length
|
|
357
365
|
* binary data encoded as (size, data) format.
|
|
358
366
|
*
|
|
367
|
+
* @param maxSize - Optional maximum allowed size for the buffer. If the size exceeds this, an error is thrown.
|
|
359
368
|
* @returns A Buffer containing the read bytes.
|
|
360
369
|
*/
|
|
361
|
-
public readBuffer(): Buffer {
|
|
370
|
+
public readBuffer(maxSize?: number): Buffer {
|
|
362
371
|
const size = this.readNumber();
|
|
372
|
+
if (maxSize !== undefined && size > maxSize) {
|
|
373
|
+
throw new Error(`Buffer size ${size} exceeds maximum allowed ${maxSize}`);
|
|
374
|
+
}
|
|
363
375
|
this.#rangeCheck(size);
|
|
364
376
|
return this.readBytes(size);
|
|
365
377
|
}
|
package/src/sleep/index.ts
CHANGED
|
@@ -73,3 +73,9 @@ export class InterruptibleSleep {
|
|
|
73
73
|
export function sleep<T>(ms: number, returnValue?: T): Promise<T> {
|
|
74
74
|
return new Promise(resolve => setTimeout(() => resolve(returnValue as T), ms));
|
|
75
75
|
}
|
|
76
|
+
|
|
77
|
+
/** Sleeps until the target date */
|
|
78
|
+
export function sleepUntil<T>(target: Date, now: Date, returnValue?: T): Promise<T> {
|
|
79
|
+
const ms = target.getTime() - now.getTime();
|
|
80
|
+
return sleep(ms, returnValue);
|
|
81
|
+
}
|
package/src/timer/date.ts
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import type { AsyncHasher, Hasher } from './hasher.js';
|
|
4
|
-
|
|
5
|
-
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
6
|
-
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
7
|
-
|
|
8
|
-
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
9
|
-
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
10
|
-
|
|
11
|
-
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
12
|
-
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
1
|
+
import { pedersenMerkleHash, poseidonMerkleHash, shaMerkleHash } from './hasher.js';
|
|
13
2
|
|
|
14
3
|
export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
|
|
15
4
|
|
package/src/trees/hasher.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { pedersenHash as pedersenHashArray } from '../crypto/pedersen/index.js';
|
|
2
|
+
import { poseidon2Hash } from '../crypto/poseidon/index.js';
|
|
3
|
+
import { sha256Trunc } from '../crypto/sha256/index.js';
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* Defines hasher interface used by Merkle trees.
|
|
3
7
|
*/
|
|
@@ -37,3 +41,12 @@ export interface AsyncHasher {
|
|
|
37
41
|
*/
|
|
38
42
|
hashInputs(inputs: Buffer[]): Promise<Buffer<ArrayBuffer>>;
|
|
39
43
|
}
|
|
44
|
+
|
|
45
|
+
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
46
|
+
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
47
|
+
|
|
48
|
+
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
49
|
+
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
50
|
+
|
|
51
|
+
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
52
|
+
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
package/src/trees/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './unbalanced_merkle_tree.js';
|
|
3
|
-
export * from './unbalanced_tree_store.js';
|
|
1
|
+
export * from './balanced_merkle_tree_root.js';
|
|
4
2
|
export * from './merkle_tree_calculator.js';
|
|
5
3
|
export * from './merkle_tree.js';
|
|
6
4
|
export * from './indexed_merkle_tree_calculator.js';
|
|
@@ -10,3 +8,5 @@ export * from './membership_witness.js';
|
|
|
10
8
|
export * from './hasher.js';
|
|
11
9
|
export * from './indexed_tree_leaf.js';
|
|
12
10
|
export * from './unbalanced_merkle_tree_calculator.js';
|
|
11
|
+
export * from './unbalanced_merkle_tree_root.js';
|
|
12
|
+
export * from './unbalanced_tree_store.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Fr } from '../
|
|
1
|
+
import { Fr } from '../curves/bn254/index.js';
|
|
2
2
|
import { assertLength } from '../serialize/index.js';
|
|
3
3
|
import type { IndexedTreeLeafPreimage } from './indexed_tree_leaf.js';
|
|
4
4
|
import { MembershipWitness } from './membership_witness.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { assertMemberLength } from '../array/array.js';
|
|
2
2
|
import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
|
|
3
|
-
import { Fr } from '../
|
|
3
|
+
import { Fr } from '../curves/bn254/field.js';
|
|
4
4
|
import { schemas } from '../schemas/schemas.js';
|
|
5
5
|
import { BufferReader, type Tuple, serializeToBuffer } from '../serialize/index.js';
|
|
6
6
|
import type { SiblingPath } from './sibling_path.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { makeTuple } from '../array/array.js';
|
|
2
|
-
import { pedersenHash } from '../crypto/index.js';
|
|
3
|
-
import { Fr } from '../
|
|
2
|
+
import { pedersenHash } from '../crypto/pedersen/index.js';
|
|
3
|
+
import { Fr } from '../curves/bn254/index.js';
|
|
4
4
|
import { schemas } from '../schemas/index.js';
|
|
5
5
|
import {
|
|
6
6
|
type Tuple,
|
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
import { shaMerkleHash } from './
|
|
2
|
-
import type { Hasher } from './hasher.js';
|
|
1
|
+
import { type Hasher, shaMerkleHash } from './hasher.js';
|
|
3
2
|
import { SiblingPath } from './sibling_path.js';
|
|
4
3
|
import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
5
4
|
|
|
6
|
-
export function computeCompressedUnbalancedMerkleTreeRoot(
|
|
7
|
-
leaves: Buffer[],
|
|
8
|
-
valueToCompress = Buffer.alloc(32),
|
|
9
|
-
emptyRoot = Buffer.alloc(32),
|
|
10
|
-
hasher?: Hasher['hash'],
|
|
11
|
-
): Buffer {
|
|
12
|
-
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
13
|
-
return calculator.getRoot();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
5
|
interface TreeNode {
|
|
17
6
|
value: Buffer;
|
|
18
7
|
leafIndex?: number;
|