@aztec/foundation 0.14.2 → 0.15.1
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/decoder.js +3 -3
- package/dest/abi/encoder.d.ts +1 -1
- package/dest/abi/encoder.d.ts.map +1 -1
- package/dest/abi/encoder.js +3 -3
- package/dest/abi/function_selector.d.ts +2 -1
- package/dest/abi/function_selector.d.ts.map +1 -1
- package/dest/abi/function_selector.js +6 -5
- package/dest/aztec-address/index.d.ts +10 -108
- package/dest/aztec-address/index.d.ts.map +1 -1
- package/dest/aztec-address/index.js +18 -139
- package/dest/bigint-buffer/index.d.ts.map +1 -1
- package/dest/bigint-buffer/index.js +7 -4
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +7 -4
- package/dest/crypto/index.d.ts +1 -0
- package/dest/crypto/index.d.ts.map +1 -1
- package/dest/crypto/index.js +2 -1
- package/dest/crypto/pedersen/index.d.ts +1 -9
- package/dest/crypto/pedersen/index.d.ts.map +1 -1
- package/dest/crypto/pedersen/index.js +2 -298
- package/dest/crypto/pedersen/pedersen.elliptic.d.ts +12 -0
- package/dest/crypto/pedersen/pedersen.elliptic.d.ts.map +1 -0
- package/dest/crypto/pedersen/pedersen.elliptic.js +575 -0
- package/dest/crypto/pedersen/pedersen.noble.d.ts +10 -0
- package/dest/crypto/pedersen/pedersen.noble.d.ts.map +1 -0
- package/dest/crypto/pedersen/pedersen.noble.js +560 -0
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +16 -0
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -0
- package/dest/crypto/pedersen/pedersen.wasm.js +38 -0
- package/dest/crypto/random/index.d.ts.map +1 -1
- package/dest/crypto/random/index.js +5 -3
- package/dest/crypto/sha256/index.d.ts +0 -8
- package/dest/crypto/sha256/index.d.ts.map +1 -1
- package/dest/crypto/sha256/index.js +1 -12
- package/dest/fields/fields.d.ts +69 -160
- package/dest/fields/fields.d.ts.map +1 -1
- package/dest/fields/fields.js +142 -185
- package/dest/fields/index.d.ts +0 -1
- package/dest/fields/index.d.ts.map +1 -1
- package/dest/fields/index.js +1 -2
- package/dest/fields/point.d.ts.map +1 -1
- package/dest/fields/point.js +5 -4
- package/dest/json-rpc/class_converter.d.ts.map +1 -1
- package/dest/json-rpc/class_converter.js +7 -5
- package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -1
- package/dest/json-rpc/client/json_rpc_client.js +3 -2
- package/dest/log/console.d.ts +1 -1
- package/dest/log/console.d.ts.map +1 -1
- package/dest/log/debug.d.ts +1 -1
- package/dest/log/debug.d.ts.map +1 -1
- package/dest/log/index.d.ts +1 -4
- package/dest/log/index.d.ts.map +1 -1
- package/dest/log/index.js +2 -1
- package/dest/log/log_fn.d.ts +5 -0
- package/dest/log/log_fn.d.ts.map +1 -0
- package/dest/log/log_fn.js +2 -0
- package/dest/log/logger.d.ts +1 -1
- package/dest/log/logger.d.ts.map +1 -1
- package/dest/log/logger.js +7 -4
- package/dest/serialize/buffer_reader.d.ts +0 -16
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +1 -21
- package/dest/serialize/free_funcs.d.ts +6 -0
- package/dest/serialize/free_funcs.d.ts.map +1 -1
- package/dest/serialize/free_funcs.js +11 -1
- package/dest/timer/timer.d.ts +4 -0
- package/dest/timer/timer.d.ts.map +1 -1
- package/dest/timer/timer.js +10 -4
- package/dest/transport/interface/transferable.d.ts +1 -1
- package/dest/transport/interface/transferable.d.ts.map +1 -1
- package/dest/transport/interface/transferable.js +5 -3
- package/dest/wasm/empty_wasi_sdk.d.ts +1 -1
- package/dest/wasm/wasm_module.d.ts.map +1 -1
- package/dest/wasm/wasm_module.js +4 -3
- package/package.json +8 -3
- package/src/abi/decoder.ts +2 -2
- package/src/abi/encoder.ts +3 -2
- package/src/abi/function_selector.ts +5 -4
- package/src/aztec-address/index.ts +17 -149
- package/src/bigint-buffer/index.ts +9 -3
- package/src/collection/array.ts +9 -3
- package/src/crypto/index.ts +1 -0
- package/src/crypto/pedersen/index.ts +1 -309
- package/src/crypto/pedersen/pedersen.elliptic.ts +584 -0
- package/src/crypto/pedersen/pedersen.noble.ts +573 -0
- package/src/crypto/pedersen/pedersen.wasm.ts +46 -0
- package/src/crypto/random/index.ts +6 -2
- package/src/crypto/sha256/index.ts +0 -13
- package/src/fields/fields.ts +197 -197
- package/src/fields/index.ts +0 -1
- package/src/fields/point.ts +5 -3
- package/src/json-rpc/class_converter.ts +8 -4
- package/src/json-rpc/client/json_rpc_client.ts +3 -1
- package/src/log/console.ts +1 -1
- package/src/log/debug.ts +1 -1
- package/src/log/index.ts +1 -6
- package/src/log/log_fn.ts +5 -0
- package/src/log/logger.ts +10 -4
- package/src/serialize/buffer_reader.ts +0 -22
- package/src/serialize/free_funcs.ts +11 -0
- package/src/timer/timer.ts +10 -3
- package/src/transport/interface/transferable.ts +7 -3
- package/src/wasm/wasm_module.ts +3 -1
- package/dest/fields/grumpkin_scalar.d.ts +0 -96
- package/dest/fields/grumpkin_scalar.d.ts.map +0 -1
- package/dest/fields/grumpkin_scalar.js +0 -125
- package/src/fields/grumpkin_scalar.ts +0 -138
package/src/fields/fields.ts
CHANGED
|
@@ -1,256 +1,256 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
|
|
1
|
+
import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
|
|
3
2
|
import { randomBytes } from '../crypto/index.js';
|
|
4
3
|
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
5
4
|
|
|
5
|
+
const ZERO_BUFFER = Buffer.alloc(32);
|
|
6
|
+
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
|
8
|
+
|
|
6
9
|
/**
|
|
7
|
-
*
|
|
8
|
-
* It provides utility functions to work with elements in this field, such as conversions between different representations and checks for equality and zero values. The elements can be serialized to and deserialized from byte buffers or strings.
|
|
9
|
-
* Some use cases include working with cryptographic operations and finite fields.
|
|
10
|
+
* Represents a field derived from BaseField.
|
|
10
11
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
14
|
-
static MAX_VALUE = Fr.MODULUS - 1n;
|
|
15
|
-
static SIZE_IN_BYTES = 32;
|
|
16
|
-
|
|
12
|
+
type DerivedField<T extends BaseField> = {
|
|
13
|
+
new (value: any): T;
|
|
17
14
|
/**
|
|
18
|
-
*
|
|
15
|
+
* All derived fields will specify a MODULUS.
|
|
19
16
|
*/
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
constructor(value: boolean | bigint | number | Fr | AztecAddress) {
|
|
23
|
-
const isFr = (value: boolean | bigint | number | Fr | AztecAddress): value is Fr | AztecAddress =>
|
|
24
|
-
!!(value as Fr).toBigInt;
|
|
25
|
-
this.value = isFr(value) ? value.toBigInt() : BigInt(value);
|
|
26
|
-
if (this.value > Fr.MAX_VALUE) {
|
|
27
|
-
throw new Error(`Fr out of range ${value}.`);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
17
|
+
MODULUS: bigint;
|
|
18
|
+
};
|
|
30
19
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Base field class.
|
|
22
|
+
* Conversions from Buffer to BigInt and vice-versa are not cheap.
|
|
23
|
+
* We allow construction with either form and lazily convert to other as needed.
|
|
24
|
+
* We only check we are within the field modulus when initializing with bigint.
|
|
25
|
+
* If NODE_ENV === 'test', we will always initialize both types to check the modulus.
|
|
26
|
+
* This is also necessary in test environment as a lot of tests just use deep equality to check equality.
|
|
27
|
+
* WARNING: This could lead to a bugs in production that don't reveal in tests, but it's low risk.
|
|
28
|
+
*/
|
|
29
|
+
abstract class BaseField {
|
|
30
|
+
static SIZE_IN_BYTES = 32;
|
|
31
|
+
private asBuffer?: Buffer;
|
|
32
|
+
private asBigInt?: bigint;
|
|
42
33
|
|
|
43
34
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
return
|
|
35
|
+
* Return bigint representation.
|
|
36
|
+
* @deprecated Just to get things compiling. Use toBigInt().
|
|
37
|
+
* */
|
|
38
|
+
get value(): bigint {
|
|
39
|
+
return this.toBigInt();
|
|
49
40
|
}
|
|
50
41
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
42
|
+
protected constructor(value: number | bigint | boolean | BaseField | Buffer) {
|
|
43
|
+
if (value instanceof Buffer) {
|
|
44
|
+
this.asBuffer =
|
|
45
|
+
value.length === 32 ? value : Buffer.concat([Buffer.alloc(BaseField.SIZE_IN_BYTES - value.length), value]);
|
|
46
|
+
} else if (typeof value === 'bigint' || typeof value === 'number' || typeof value === 'boolean') {
|
|
47
|
+
this.asBigInt = BigInt(value);
|
|
48
|
+
if (this.asBigInt >= this.modulus()) {
|
|
49
|
+
throw new Error('Value >= to field modulus.');
|
|
50
|
+
}
|
|
51
|
+
} else if (value instanceof BaseField) {
|
|
52
|
+
this.asBuffer = value.asBuffer;
|
|
53
|
+
this.asBigInt = value.asBigInt;
|
|
54
|
+
} else {
|
|
55
|
+
throw new Error(`Type '${typeof value}' with value '${value}' passed to BaseField ctor.`);
|
|
56
|
+
}
|
|
64
57
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
*/
|
|
73
|
-
static fromString(address: string) {
|
|
74
|
-
return Fr.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
58
|
+
// Loads of our tests are just doing deep equality rather than calling e.g. toBigInt() first.
|
|
59
|
+
// This ensures the deep equality passes regardless of the internal representation.
|
|
60
|
+
// It also ensures the value range is checked even when initializing as a buffer.
|
|
61
|
+
if (process.env.NODE_ENV === 'test') {
|
|
62
|
+
this.toBuffer();
|
|
63
|
+
this.toBigInt();
|
|
64
|
+
}
|
|
75
65
|
}
|
|
76
66
|
|
|
67
|
+
protected abstract modulus(): bigint;
|
|
68
|
+
|
|
77
69
|
/**
|
|
78
|
-
*
|
|
79
|
-
* The method uses the provided value and size in bytes to create a buffer representation
|
|
80
|
-
* of the numeric value. This can be useful for serialization and communication purposes.
|
|
81
|
-
*
|
|
82
|
-
* @returns A buffer representing the instance's value.
|
|
70
|
+
* We return a copy of the Buffer to ensure this remains immutable.
|
|
83
71
|
*/
|
|
84
|
-
toBuffer() {
|
|
85
|
-
|
|
72
|
+
toBuffer(): Buffer {
|
|
73
|
+
if (!this.asBuffer) {
|
|
74
|
+
this.asBuffer = toBufferBE(this.asBigInt!, 32);
|
|
75
|
+
}
|
|
76
|
+
return Buffer.from(this.asBuffer);
|
|
86
77
|
}
|
|
87
78
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
* The resulting string is prefixed with '0x' and represents the bigint value
|
|
91
|
-
* in base 16.
|
|
92
|
-
*
|
|
93
|
-
* @param padTo32 - Whether to pad the string to 32 bytes.
|
|
94
|
-
* @returns A hex-encoded string representing the value of the class instance.
|
|
95
|
-
*/
|
|
96
|
-
toString(padTo32 = false): `0x${string}` {
|
|
97
|
-
return toHex(this.value, padTo32);
|
|
79
|
+
toString(): `0x${string}` {
|
|
80
|
+
return `0x${this.toBuffer().toString('hex')}`;
|
|
98
81
|
}
|
|
99
82
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
83
|
+
toBigInt(): bigint {
|
|
84
|
+
if (this.asBigInt === undefined) {
|
|
85
|
+
this.asBigInt = toBigIntBE(this.asBuffer!);
|
|
86
|
+
if (this.asBigInt >= this.modulus()) {
|
|
87
|
+
throw new Error('Value >= to field modulus.');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return this.asBigInt;
|
|
107
91
|
}
|
|
108
92
|
|
|
109
|
-
|
|
110
|
-
* Returns a shortened string representation of the Fr value, formatted with '0x' prefix and ellipsis in the middle.
|
|
111
|
-
* The resulting string has first 10 characters (including '0x') and last 4 characters of the full hexadecimal value.
|
|
112
|
-
*
|
|
113
|
-
* @returns A shorter, human-readable string representation of the Fr value.
|
|
114
|
-
*/
|
|
115
|
-
toShortString() {
|
|
93
|
+
toShortString(): string {
|
|
116
94
|
const str = this.toString();
|
|
117
95
|
return `${str.slice(0, 10)}...${str.slice(-4)}`;
|
|
118
96
|
}
|
|
119
97
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
* Two instances are considered equal if their 'value' properties are the same.
|
|
123
|
-
*
|
|
124
|
-
* @param rhs - The Fr instance to compare with the current instance.
|
|
125
|
-
* @returns A boolean indicating whether the two instances are equal.
|
|
126
|
-
*/
|
|
127
|
-
equals(rhs: Fr) {
|
|
128
|
-
return this.value === rhs.value;
|
|
98
|
+
equals(rhs: BaseField): boolean {
|
|
99
|
+
return this.toBuffer().equals(rhs.toBuffer());
|
|
129
100
|
}
|
|
130
101
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
* The method returns true if the value of the instance is 0n (zero in BigInt representation),
|
|
134
|
-
* otherwise, it returns false.
|
|
135
|
-
*
|
|
136
|
-
* @returns A boolean indicating whether the instance value is zero or not.
|
|
137
|
-
*/
|
|
138
|
-
isZero() {
|
|
139
|
-
return this.value === 0n;
|
|
102
|
+
isZero(): boolean {
|
|
103
|
+
return this.toBuffer().equals(ZERO_BUFFER);
|
|
140
104
|
}
|
|
141
105
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
* The output will be a hexadecimal string prefixed with '0x'.
|
|
145
|
-
*
|
|
146
|
-
* @returns A '0x' prefixed hexadecimal string representing the current value.
|
|
147
|
-
*/
|
|
148
|
-
toFriendlyJSON() {
|
|
149
|
-
return this.toString();
|
|
106
|
+
toFriendlyJSON(): string {
|
|
107
|
+
return `0x${this.toBigInt().toString()}`;
|
|
150
108
|
}
|
|
151
109
|
|
|
152
|
-
/** Returns self. */
|
|
153
110
|
toField() {
|
|
154
111
|
return this;
|
|
155
112
|
}
|
|
156
113
|
}
|
|
157
114
|
|
|
158
115
|
/**
|
|
159
|
-
*
|
|
160
|
-
* It
|
|
161
|
-
* them to/from different data types like Buffers and hex strings. Field elements are used in various
|
|
162
|
-
* cryptographic protocols and operations, such as elliptic curve cryptography.
|
|
163
|
-
*
|
|
164
|
-
* @example
|
|
165
|
-
* const fqElem = new Fq(BigInt("123456789"));
|
|
166
|
-
* const randomFqElem = Fq.random();
|
|
167
|
-
* const fromBufferFqElem = Fq.fromBuffer(buffer);
|
|
116
|
+
* Constructs a field from a Buffer of BufferReader.
|
|
117
|
+
* It maybe not read the full 32 bytes if the Buffer is shorter, but it will padded in BaseField constructor.
|
|
168
118
|
*/
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
119
|
+
function fromBuffer<T extends BaseField>(buffer: Buffer | BufferReader, f: DerivedField<T>) {
|
|
120
|
+
const reader = BufferReader.asReader(buffer);
|
|
121
|
+
return new f(reader.readBytes(BaseField.SIZE_IN_BYTES));
|
|
122
|
+
}
|
|
173
123
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
)
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Constructs a field from a Buffer, but reduces it first.
|
|
126
|
+
* This requires a conversion to a bigint first so the initial underlying representation will be a bigint.
|
|
127
|
+
*/
|
|
128
|
+
function fromBufferReduce<T extends BaseField>(buffer: Buffer, f: DerivedField<T>) {
|
|
129
|
+
return new f(toBigIntBE(buffer) % f.MODULUS);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* To ensure a field is uniformly random, it's important to reduce a 512 bit value.
|
|
134
|
+
* If you reduced a 256 bit number, there would a be a high skew in the lower range of the field.
|
|
135
|
+
*/
|
|
136
|
+
function random<T extends BaseField>(f: DerivedField<T>): T {
|
|
137
|
+
return fromBufferReduce(randomBytes(64), f);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Constructs a field from a 0x prefixed hex string.
|
|
142
|
+
*/
|
|
143
|
+
function fromString<T extends BaseField>(buf: string, f: DerivedField<T>) {
|
|
144
|
+
const buffer = Buffer.from(buf.replace(/^0x/i, ''), 'hex');
|
|
145
|
+
return new f(buffer);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Branding to ensure fields are not interchangeable types.
|
|
150
|
+
*/
|
|
151
|
+
export interface Fr {
|
|
152
|
+
/** Brand. */
|
|
153
|
+
_branding: 'Fr';
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Fr field class.
|
|
158
|
+
*/
|
|
159
|
+
export class Fr extends BaseField {
|
|
160
|
+
static ZERO = new Fr(0n);
|
|
161
|
+
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
162
|
+
|
|
163
|
+
constructor(value: number | bigint | boolean | Fr | Buffer) {
|
|
164
|
+
super(value);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
protected modulus() {
|
|
168
|
+
return Fr.MODULUS;
|
|
183
169
|
}
|
|
184
170
|
|
|
185
|
-
/**
|
|
186
|
-
* Generates a random Fr or Fq instance with a value within the range of their respective modulus.
|
|
187
|
-
* The random value is generated from a byte array of length equal to SIZE_IN_BYTES, then truncated
|
|
188
|
-
* to the appropriate modulus before creating the new Fr or Fq instance.
|
|
189
|
-
*
|
|
190
|
-
* @returns A new Fr or Fq instance with a randomly generated value.
|
|
191
|
-
*/
|
|
192
171
|
static random() {
|
|
193
|
-
|
|
194
|
-
|
|
172
|
+
return random(Fr);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
static zero() {
|
|
176
|
+
return Fr.ZERO;
|
|
195
177
|
}
|
|
196
178
|
|
|
197
|
-
/**
|
|
198
|
-
* Create an instance of the calling class (Fr or Fq) from a given buffer or BufferReader.
|
|
199
|
-
* Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
|
|
200
|
-
* with that value. Throws an error if the value is out of range for the calling class.
|
|
201
|
-
*
|
|
202
|
-
* @param buffer - The input buffer or BufferReader containing the bytes representing the value.
|
|
203
|
-
* @returns An instance of the calling class (Fr or Fq) initialized with the bigint value.
|
|
204
|
-
*/
|
|
205
179
|
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
206
|
-
|
|
207
|
-
return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
|
|
180
|
+
return fromBuffer(buffer, Fr);
|
|
208
181
|
}
|
|
209
182
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
* The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
|
|
213
|
-
*
|
|
214
|
-
* @returns A Buffer containing the byte representation of the instance's value.
|
|
215
|
-
*/
|
|
216
|
-
toBuffer() {
|
|
217
|
-
return toBufferBE(this.value, Fq.SIZE_IN_BYTES);
|
|
183
|
+
static fromBufferReduce(buffer: Buffer) {
|
|
184
|
+
return fromBufferReduce(buffer, Fr);
|
|
218
185
|
}
|
|
219
186
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
* The resulting string is prefixed with '0x' and contains the exact number of hex characters required
|
|
223
|
-
* to represent the numeric value of this instance.
|
|
224
|
-
*
|
|
225
|
-
* @returns A hexadecimal string representing the Fq value.
|
|
226
|
-
*/
|
|
227
|
-
toString() {
|
|
228
|
-
return toHex(this.value);
|
|
187
|
+
static fromString(buf: string) {
|
|
188
|
+
return fromString(buf, Fr);
|
|
229
189
|
}
|
|
190
|
+
}
|
|
230
191
|
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
192
|
+
/**
|
|
193
|
+
* Branding to ensure fields are not interchangeable types.
|
|
194
|
+
*/
|
|
195
|
+
export interface Fq {
|
|
196
|
+
/** Brand. */
|
|
197
|
+
_branding: 'Fq';
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Fq field class.
|
|
202
|
+
*/
|
|
203
|
+
export class Fq extends BaseField {
|
|
204
|
+
static ZERO = new Fq(0n);
|
|
205
|
+
static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
206
|
+
private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
|
|
207
|
+
private static LOW_MASK = (1n << Fq.HIGH_SHIFT) - 1n;
|
|
208
|
+
|
|
209
|
+
get low(): Fr {
|
|
210
|
+
return new Fr(this.toBigInt() & Fq.LOW_MASK);
|
|
240
211
|
}
|
|
241
212
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
* The output is a hexadecimal string representation of the value with '0x' prefix.
|
|
245
|
-
*
|
|
246
|
-
* @returns A string representing the value in the JSON format.
|
|
247
|
-
*/
|
|
248
|
-
toFriendlyJSON() {
|
|
249
|
-
return this.toString();
|
|
213
|
+
get high(): Fr {
|
|
214
|
+
return new Fr(this.toBigInt() >> Fq.HIGH_SHIFT);
|
|
250
215
|
}
|
|
251
216
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
217
|
+
constructor(value: number | bigint | boolean | Fq | Buffer) {
|
|
218
|
+
super(value);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
protected modulus() {
|
|
222
|
+
return Fq.MODULUS;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
static random() {
|
|
226
|
+
return random(Fq);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
static zero() {
|
|
230
|
+
return Fq.ZERO;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
static fromBuffer(buffer: Buffer | BufferReader) {
|
|
234
|
+
return fromBuffer(buffer, Fq);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static fromBufferReduce(buffer: Buffer) {
|
|
238
|
+
return fromBufferReduce(buffer, Fq);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
static fromString(buf: string) {
|
|
242
|
+
return fromString(buf, Fq);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
static fromHighLow(high: Fr, low: Fr): Fq {
|
|
246
|
+
return new Fq((high.toBigInt() << Fq.HIGH_SHIFT) + low.toBigInt());
|
|
255
247
|
}
|
|
256
248
|
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* GrumpkinScalar is an Fq.
|
|
252
|
+
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
253
|
+
* the Aztec Yellow Paper.
|
|
254
|
+
*/
|
|
255
|
+
export type GrumpkinScalar = Fq;
|
|
256
|
+
export const GrumpkinScalar = Fq;
|
package/src/fields/index.ts
CHANGED
package/src/fields/point.ts
CHANGED
|
@@ -72,8 +72,8 @@ export class Point {
|
|
|
72
72
|
*/
|
|
73
73
|
toBigInts() {
|
|
74
74
|
return {
|
|
75
|
-
x: this.x.
|
|
76
|
-
y: this.y.
|
|
75
|
+
x: this.x.toBigInt(),
|
|
76
|
+
y: this.y.toBigInt(),
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -128,7 +128,9 @@ export class Point {
|
|
|
128
128
|
* @returns Whether it looks like a point.
|
|
129
129
|
*/
|
|
130
130
|
export function isPoint(obj: object): obj is Point {
|
|
131
|
-
if (!obj)
|
|
131
|
+
if (!obj) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
132
134
|
const point = obj as Point;
|
|
133
135
|
return point.kind === 'point' && point.x !== undefined && point.y !== undefined;
|
|
134
136
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assert
|
|
1
|
+
import { assert } from './js_utils.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents a class compatible with our class conversion system.
|
|
@@ -133,7 +133,7 @@ export class ClassConverter {
|
|
|
133
133
|
register(type: string, class_: IOClass, encoding: ClassEncoding) {
|
|
134
134
|
assert(type !== 'Buffer', "'Buffer' handling is hardcoded. Cannot use as name.");
|
|
135
135
|
assert(
|
|
136
|
-
|
|
136
|
+
class_.prototype['toString'] || class_.prototype['toJSON'],
|
|
137
137
|
`Class ${type} must define a toString() OR toJSON() method.`,
|
|
138
138
|
);
|
|
139
139
|
assert(
|
|
@@ -197,9 +197,13 @@ export class ClassConverter {
|
|
|
197
197
|
*/
|
|
198
198
|
private lookupObject(classObj: any) {
|
|
199
199
|
const nameResult = this.toName.get(classObj.constructor);
|
|
200
|
-
if (nameResult)
|
|
200
|
+
if (nameResult) {
|
|
201
|
+
return { type: nameResult[0], encoding: nameResult[1] };
|
|
202
|
+
}
|
|
201
203
|
const classResult = this.toClass.get(classObj.constructor.name);
|
|
202
|
-
if (classResult)
|
|
204
|
+
if (classResult) {
|
|
205
|
+
return { type: classObj.constructor.name, encoding: classResult[1] };
|
|
206
|
+
}
|
|
203
207
|
throw new Error(`Could not find class ${classObj.constructor.name} in lookup.`);
|
|
204
208
|
}
|
|
205
209
|
}
|
|
@@ -123,7 +123,9 @@ export function createJsonRpcClient<T extends object>(
|
|
|
123
123
|
{},
|
|
124
124
|
{
|
|
125
125
|
get: (target, rpcMethod: string) => {
|
|
126
|
-
if (['then', 'catch'].includes(rpcMethod))
|
|
126
|
+
if (['then', 'catch'].includes(rpcMethod)) {
|
|
127
|
+
return Reflect.get(target, rpcMethod);
|
|
128
|
+
}
|
|
127
129
|
return (...params: any[]) => {
|
|
128
130
|
debug(format(`JsonRpcClient.constructor`, 'proxy', rpcMethod, '<-', params));
|
|
129
131
|
return request(rpcMethod, params);
|
package/src/log/console.ts
CHANGED
package/src/log/debug.ts
CHANGED
package/src/log/index.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
/** Structured log data to include with the message. */
|
|
2
|
-
export type LogData = Record<string, string | number | bigint | boolean>;
|
|
3
|
-
|
|
4
|
-
/** A callable logger instance. */
|
|
5
|
-
export type LogFn = (msg: string, data?: LogData) => void;
|
|
6
|
-
|
|
7
1
|
export * from './console.js';
|
|
8
2
|
export * from './debug.js';
|
|
9
3
|
export * from './logger.js';
|
|
10
4
|
export * from './log_history.js';
|
|
5
|
+
export * from './log_fn.js';
|
package/src/log/logger.ts
CHANGED
|
@@ -2,7 +2,7 @@ import debug from 'debug';
|
|
|
2
2
|
import isNode from 'detect-node';
|
|
3
3
|
import { isatty } from 'tty';
|
|
4
4
|
|
|
5
|
-
import { LogData, LogFn } from './
|
|
5
|
+
import { LogData, LogFn } from './log_fn.js';
|
|
6
6
|
|
|
7
7
|
// Matches a subset of Winston log levels
|
|
8
8
|
const LogLevels = ['silent', 'error', 'warn', 'info', 'verbose', 'debug'] as const;
|
|
@@ -39,7 +39,9 @@ export type DebugLogger = LogFn & Logger;
|
|
|
39
39
|
*/
|
|
40
40
|
export function createDebugLogger(name: string): DebugLogger {
|
|
41
41
|
const debugLogger = debug(name);
|
|
42
|
-
if (currentLevel === 'debug')
|
|
42
|
+
if (currentLevel === 'debug') {
|
|
43
|
+
debugLogger.enabled = true;
|
|
44
|
+
}
|
|
43
45
|
|
|
44
46
|
const logger = {
|
|
45
47
|
silent: () => {},
|
|
@@ -78,7 +80,9 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
|
|
|
78
80
|
|
|
79
81
|
msg = data ? `${msg} ${fmtLogData(data)}` : msg;
|
|
80
82
|
if (debug.enabled) {
|
|
81
|
-
if (level !== 'debug')
|
|
83
|
+
if (level !== 'debug') {
|
|
84
|
+
msg = `${level.toUpperCase()} ${msg}`;
|
|
85
|
+
}
|
|
82
86
|
debug(msg);
|
|
83
87
|
} else if (LogLevels.indexOf(level) <= LogLevels.indexOf(currentLevel)) {
|
|
84
88
|
printLog(`${getPrefix(debug, level)} ${msg}`);
|
|
@@ -94,7 +98,9 @@ function logWithDebug(debug: debug.Debugger, level: LogLevel, msg: string, data?
|
|
|
94
98
|
function getPrefix(debugLogger: debug.Debugger, level: LogLevel) {
|
|
95
99
|
const levelLabel = currentLevel !== level ? ` ${level.toUpperCase()}` : '';
|
|
96
100
|
const prefix = `${debugLogger.namespace.replace(/^aztec:/, '')}${levelLabel}`;
|
|
97
|
-
if (!isNode || !isatty(process.stderr.fd))
|
|
101
|
+
if (!isNode || !isatty(process.stderr.fd)) {
|
|
102
|
+
return prefix;
|
|
103
|
+
}
|
|
98
104
|
const colorIndex = debug.selectColor(debugLogger.namespace) as number;
|
|
99
105
|
const colorCode = '\u001B[3' + (colorIndex < 8 ? colorIndex : '8;5;' + colorIndex);
|
|
100
106
|
return ` ${colorCode};1m${prefix}\u001B[0m`;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Fq, Fr } from '../fields/fields.js';
|
|
2
1
|
import { Tuple } from './types.js';
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -83,27 +82,6 @@ export class BufferReader {
|
|
|
83
82
|
return Buffer.from(this.buffer.subarray(this.index - n, this.index));
|
|
84
83
|
}
|
|
85
84
|
|
|
86
|
-
/**
|
|
87
|
-
* Reads a Fr (finite field) element from the buffer using the 'fromBuffer' method of the Fr class.
|
|
88
|
-
* The Fr class should provide a 'fromBuffer' method that takes a BufferReader instance as input.
|
|
89
|
-
*
|
|
90
|
-
* @returns An instance of the Fr class representing the finite field element.
|
|
91
|
-
*/
|
|
92
|
-
public readFr(): Fr {
|
|
93
|
-
return Fr.fromBuffer(this);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Reads the next Fq element from the buffer using the Fq.fromBuffer method.
|
|
98
|
-
* The Fq element represents a finite field in elliptic curve cryptography and is used for calculations.
|
|
99
|
-
* Advances the internal buffer index by the number of bytes read.
|
|
100
|
-
*
|
|
101
|
-
* @returns An Fq instance representing the finite field element.
|
|
102
|
-
*/
|
|
103
|
-
public readFq(): Fq {
|
|
104
|
-
return Fq.fromBuffer(this);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
85
|
/**
|
|
108
86
|
* Reads a vector of numbers from the buffer and returns it as an array of numbers.
|
|
109
87
|
* The method utilizes the 'readVector' method, passing a deserializer that reads numbers.
|
|
@@ -15,6 +15,17 @@ export function boolToByte(b: boolean) {
|
|
|
15
15
|
return buf;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* @param n - The input number to be converted to a big-endian unsigned 16-bit integer Buffer.
|
|
20
|
+
* @param bufferSize - Optional, the size of the output Buffer (default is 2).
|
|
21
|
+
* @returns A Buffer containing the big-endian unsigned 16-bit integer representation of the input number.
|
|
22
|
+
*/
|
|
23
|
+
export function numToUInt16BE(n: number, bufferSize = 2) {
|
|
24
|
+
const buf = Buffer.alloc(bufferSize);
|
|
25
|
+
buf.writeUInt16BE(n, bufferSize - 2);
|
|
26
|
+
return buf;
|
|
27
|
+
}
|
|
28
|
+
|
|
18
29
|
/**
|
|
19
30
|
* Convert a number into a 4-byte little-endian unsigned integer buffer.
|
|
20
31
|
* The input number is serialized as an unsigned 32-bit integer in little-endian byte order,
|