@aztec/foundation 0.23.0 → 0.24.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/fields/fields.js +3 -3
- package/package.json +2 -2
- package/src/abi/abi.ts +337 -0
- package/src/abi/buffer.ts +36 -0
- package/src/abi/decoder.ts +176 -0
- package/src/abi/encoder.ts +143 -0
- package/src/abi/index.ts +6 -0
- package/src/abi/selector.ts +243 -0
- package/src/abi/utils.ts +50 -0
- package/src/array/array.ts +86 -0
- package/src/array/index.ts +1 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +36 -0
- package/src/bigint-buffer/index.ts +87 -0
- package/src/collection/array.ts +64 -0
- package/src/collection/index.ts +1 -0
- package/src/committable/committable.ts +46 -0
- package/src/committable/index.ts +1 -0
- package/src/crypto/index.ts +16 -0
- package/src/crypto/keccak/index.ts +33 -0
- package/src/crypto/pedersen/index.ts +1 -0
- 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 +42 -0
- package/src/crypto/random/index.ts +42 -0
- package/src/crypto/sha256/index.ts +3 -0
- package/src/errors/index.ts +6 -0
- package/src/eth-address/index.ts +234 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +328 -0
- package/src/fields/index.ts +3 -0
- package/src/fields/point.ts +145 -0
- package/src/fifo/bounded_serial_queue.ts +100 -0
- package/src/fifo/index.ts +4 -0
- package/src/fifo/memory_fifo.ts +118 -0
- package/src/fifo/semaphore.ts +33 -0
- package/src/fifo/serial_queue.ts +81 -0
- package/src/index.ts +29 -0
- package/src/json-rpc/README.md +55 -0
- package/src/json-rpc/class_converter.ts +213 -0
- package/src/json-rpc/client/index.ts +1 -0
- package/src/json-rpc/client/json_rpc_client.ts +147 -0
- package/src/json-rpc/convert.ts +163 -0
- package/src/json-rpc/fixtures/class_a.ts +15 -0
- package/src/json-rpc/fixtures/class_b.ts +15 -0
- package/src/json-rpc/fixtures/test_state.ts +59 -0
- package/src/json-rpc/index.ts +8 -0
- package/src/json-rpc/js_utils.ts +20 -0
- package/src/json-rpc/server/index.ts +2 -0
- package/src/json-rpc/server/json_proxy.ts +60 -0
- package/src/json-rpc/server/json_rpc_server.ts +269 -0
- package/src/log/console.ts +39 -0
- package/src/log/debug.ts +83 -0
- package/src/log/index.ts +5 -0
- package/src/log/log_fn.ts +5 -0
- package/src/log/log_history.ts +44 -0
- package/src/log/logger.ts +137 -0
- package/src/mutex/index.ts +83 -0
- package/src/mutex/mutex_database.ts +12 -0
- package/src/noir/index.ts +1 -0
- package/src/noir/noir_package_config.ts +54 -0
- package/src/retry/index.ts +99 -0
- package/src/running-promise/index.ts +60 -0
- package/src/serialize/buffer_reader.ts +286 -0
- package/src/serialize/field_reader.ts +143 -0
- package/src/serialize/free_funcs.ts +147 -0
- package/src/serialize/index.ts +5 -0
- package/src/serialize/serialize.ts +303 -0
- package/src/serialize/types.ts +40 -0
- package/src/sleep/index.ts +71 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/test_data.ts +36 -0
- package/src/timer/elapsed.ts +23 -0
- package/src/timer/index.ts +3 -0
- package/src/timer/timeout.ts +64 -0
- package/src/timer/timer.ts +48 -0
- package/src/transport/browser/index.ts +4 -0
- package/src/transport/browser/message_port_socket.ts +48 -0
- package/src/transport/browser/shared_worker_connector.ts +21 -0
- package/src/transport/browser/shared_worker_listener.ts +53 -0
- package/src/transport/browser/worker_connector.ts +30 -0
- package/src/transport/browser/worker_listener.ts +54 -0
- package/src/transport/dispatch/create_dispatch_fn.ts +35 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +141 -0
- package/src/transport/dispatch/messages.ts +58 -0
- package/src/transport/index.ts +11 -0
- package/src/transport/interface/connector.ts +9 -0
- package/src/transport/interface/listener.ts +16 -0
- package/src/transport/interface/socket.ts +15 -0
- package/src/transport/interface/transferable.ts +125 -0
- package/src/transport/node/index.ts +2 -0
- package/src/transport/node/node_connector.ts +30 -0
- package/src/transport/node/node_connector_socket.ts +52 -0
- package/src/transport/node/node_listener.ts +34 -0
- package/src/transport/node/node_listener_socket.ts +48 -0
- package/src/transport/transport_client.ts +131 -0
- package/src/transport/transport_server.ts +108 -0
- package/src/trees/index.ts +54 -0
- package/src/types/index.ts +8 -0
- package/src/url/index.ts +73 -0
- package/src/wasm/README.md +6 -0
- package/src/wasm/empty_wasi_sdk.ts +166 -0
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +27 -0
- package/src/wasm/index.ts +1 -0
- package/src/wasm/wasm_module.ts +260 -0
- package/src/worker/browser/index.ts +2 -0
- package/src/worker/browser/start_web_module.ts +23 -0
- package/src/worker/browser/web_data_store.ts +38 -0
- package/src/worker/browser/web_worker.ts +24 -0
- package/src/worker/data_store.ts +19 -0
- package/src/worker/index.ts +2 -0
- package/src/worker/node/index.ts +2 -0
- package/src/worker/node/node_data_store.ts +27 -0
- package/src/worker/node/node_worker.ts +22 -0
- package/src/worker/node/start_node_module.ts +29 -0
- package/src/worker/wasm_worker.ts +7 -0
- package/src/worker/worker_pool.ts +73 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { Fq, Fr } from '../fields/fields.js';
|
|
2
|
+
import { Tuple } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The FieldReader class provides a utility for reading various data types from a field array.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* Create a new instance of FieldReader with an array of fields and an optional offset.
|
|
9
|
+
* Use the provided methods to read desired data types from the field array.
|
|
10
|
+
* The reading methods automatically advance the internal index.
|
|
11
|
+
*/
|
|
12
|
+
export class FieldReader {
|
|
13
|
+
private index: number;
|
|
14
|
+
private length: number;
|
|
15
|
+
constructor(private fields: Fr[], offset = 0) {
|
|
16
|
+
this.index = offset;
|
|
17
|
+
this.length = fields.length;
|
|
18
|
+
if (offset >= this.length) {
|
|
19
|
+
throw new Error('Offset out of bounds.');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a FieldReader instance from either a field array or an existing FieldReader.
|
|
25
|
+
*
|
|
26
|
+
* @param fields - A field array or FieldReader to initialize the FieldReader.
|
|
27
|
+
* @returns An instance of FieldReader.
|
|
28
|
+
*/
|
|
29
|
+
public static asReader(fields: Fr[] | FieldReader): FieldReader {
|
|
30
|
+
if (fields instanceof FieldReader) {
|
|
31
|
+
return fields;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new FieldReader(fields);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Reads a single field from the array.
|
|
39
|
+
*
|
|
40
|
+
* @returns A field.
|
|
41
|
+
*/
|
|
42
|
+
public readField(): Fr {
|
|
43
|
+
if (this.index === this.length) {
|
|
44
|
+
throw new Error('Not enough fields to be consumed.');
|
|
45
|
+
}
|
|
46
|
+
return this.fields[this.index++];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Reads a Fq from the array.
|
|
51
|
+
*
|
|
52
|
+
* @returns An Fq.
|
|
53
|
+
*/
|
|
54
|
+
public readFq(): Fq {
|
|
55
|
+
return Fq.fromHighLow(this.readField(), this.readField());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Reads and returns the next boolean value from the field array.
|
|
60
|
+
* Advances the internal index by 1, treating the field at the current index as a boolean value.
|
|
61
|
+
* Returns true if the field is non-zero, false otherwise.
|
|
62
|
+
* Throw if the value is not 0 or 1.
|
|
63
|
+
*
|
|
64
|
+
* @returns A boolean value representing the field at the current index.
|
|
65
|
+
*/
|
|
66
|
+
public readBoolean(): boolean {
|
|
67
|
+
const field = this.readField();
|
|
68
|
+
const value = field.toBigInt();
|
|
69
|
+
if (value > 1n) {
|
|
70
|
+
throw new Error('Field is not a boolean.');
|
|
71
|
+
}
|
|
72
|
+
return value == 1n;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Reads a 32-bit unsigned integer from the field array at the current index position.
|
|
77
|
+
* Updates the index position by 1 after reading the number.
|
|
78
|
+
* Throw if the value is greater than 2 ** 32.
|
|
79
|
+
*
|
|
80
|
+
* @returns The read 32-bit unsigned integer value.
|
|
81
|
+
*/
|
|
82
|
+
public readU32(): number {
|
|
83
|
+
const field = this.readField();
|
|
84
|
+
const value = field.toBigInt();
|
|
85
|
+
if (value >= 1n << 32n) {
|
|
86
|
+
throw new Error('Field is not a u32.');
|
|
87
|
+
}
|
|
88
|
+
return Number(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read an array of a fixed size field array.
|
|
93
|
+
*
|
|
94
|
+
* @param size - The fixed number of fields in the array.
|
|
95
|
+
* @returns An array of fields.
|
|
96
|
+
*/
|
|
97
|
+
public readFieldArray<N extends number>(size: N): Tuple<Fr, N> {
|
|
98
|
+
const result: Fr[] = [];
|
|
99
|
+
for (let i = 0; i < size; ++i) {
|
|
100
|
+
result.push(this.readField());
|
|
101
|
+
}
|
|
102
|
+
return result as Tuple<Fr, N>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Read an array of a fixed size with elements of type T from the field array.
|
|
107
|
+
* The 'itemDeserializer' object should have a 'fromFields' method that takes a FieldReader instance as input,
|
|
108
|
+
* and returns an instance of the desired deserialized data type T.
|
|
109
|
+
* This method will call the 'fromFields' method for each element in the array and return the resulting array.
|
|
110
|
+
*
|
|
111
|
+
* @param size - The fixed number of elements in the array.
|
|
112
|
+
* @param itemDeserializer - An object with a 'fromFields' method to deserialize individual elements of type T.
|
|
113
|
+
* @returns An array of instances of type T.
|
|
114
|
+
*/
|
|
115
|
+
public readArray<T, N extends number>(
|
|
116
|
+
size: N,
|
|
117
|
+
itemDeserializer: {
|
|
118
|
+
/**
|
|
119
|
+
* A function for deserializing data from a FieldReader instance.
|
|
120
|
+
*/
|
|
121
|
+
fromFields: (reader: FieldReader) => T;
|
|
122
|
+
},
|
|
123
|
+
): Tuple<T, N> {
|
|
124
|
+
const result = Array.from({ length: size }, () => itemDeserializer.fromFields(this));
|
|
125
|
+
return result as Tuple<T, N>;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Reads a serialized object from a field array and returns the deserialized object using the given deserializer.
|
|
130
|
+
*
|
|
131
|
+
* @typeparam T - The type of the deserialized object.
|
|
132
|
+
* @param deserializer - An object with a 'fromFields' method that takes a FieldReader instance and returns an instance of the deserialized object.
|
|
133
|
+
* @returns The deserialized object of type T.
|
|
134
|
+
*/
|
|
135
|
+
public readObject<T>(deserializer: {
|
|
136
|
+
/**
|
|
137
|
+
* A method that takes a FieldReader instance and returns an instance of the deserialized data type.
|
|
138
|
+
*/
|
|
139
|
+
fromFields: (reader: FieldReader) => T;
|
|
140
|
+
}): T {
|
|
141
|
+
return deserializer.fromFields(this);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { Fr } from '../fields/fields.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Convert a boolean value to its corresponding byte representation in a Buffer of size 1.
|
|
5
|
+
* The function takes a boolean value and writes it into a new buffer as either 1 (true) or 0 (false).
|
|
6
|
+
* This method is useful for converting a boolean value into a binary format that can be stored or transmitted easily.
|
|
7
|
+
*
|
|
8
|
+
* @param b - The boolean value to be converted.
|
|
9
|
+
* @returns A Buffer containing the byte representation of the input boolean value.
|
|
10
|
+
*/
|
|
11
|
+
export function boolToByte(b: boolean) {
|
|
12
|
+
const buf = Buffer.alloc(1);
|
|
13
|
+
buf.writeUInt8(b ? 1 : 0);
|
|
14
|
+
return buf;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @param n - The input number to be converted to a big-endian unsigned 16-bit integer Buffer.
|
|
19
|
+
* @param bufferSize - Optional, the size of the output Buffer (default is 2).
|
|
20
|
+
* @returns A Buffer containing the big-endian unsigned 16-bit integer representation of the input number.
|
|
21
|
+
*/
|
|
22
|
+
export function numToUInt16BE(n: number, bufferSize = 2) {
|
|
23
|
+
const buf = Buffer.alloc(bufferSize);
|
|
24
|
+
buf.writeUInt16BE(n, bufferSize - 2);
|
|
25
|
+
return buf;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Convert a number into a 4-byte little-endian unsigned integer buffer.
|
|
30
|
+
* The input number is serialized as an unsigned 32-bit integer in little-endian byte order,
|
|
31
|
+
* and returned as a Buffer of specified size (defaults to 4).
|
|
32
|
+
* If the provided bufferSize is greater than 4, the additional bytes will be padded with zeros.
|
|
33
|
+
*
|
|
34
|
+
* @param n - The number to be converted into a little-endian unsigned integer buffer.
|
|
35
|
+
* @param bufferSize - Optional, the size of the output buffer (default value is 4).
|
|
36
|
+
* @returns A Buffer containing the serialized little-endian unsigned integer representation of the input number.
|
|
37
|
+
*/
|
|
38
|
+
export function numToUInt32LE(n: number, bufferSize = 4) {
|
|
39
|
+
const buf = Buffer.alloc(bufferSize);
|
|
40
|
+
buf.writeUInt32LE(n, bufferSize - 4);
|
|
41
|
+
return buf;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Convert a number to a big-endian unsigned 32-bit integer Buffer.
|
|
46
|
+
* This function takes a number and an optional buffer size as input and creates a Buffer with the specified size (defaults to 4) containing the big-endian representation of the input number as an unsigned 32-bit integer. Note that the bufferSize should be greater than or equal to 4, otherwise the output Buffer might truncate the serialized value.
|
|
47
|
+
*
|
|
48
|
+
* @param n - The input number to be converted to a big-endian unsigned 32-bit integer Buffer.
|
|
49
|
+
* @param bufferSize - Optional, the size of the output Buffer (default is 4).
|
|
50
|
+
* @returns A Buffer containing the big-endian unsigned 32-bit integer representation of the input number.
|
|
51
|
+
*/
|
|
52
|
+
export function numToUInt32BE(n: number, bufferSize = 4) {
|
|
53
|
+
const buf = Buffer.alloc(bufferSize);
|
|
54
|
+
buf.writeUInt32BE(n, bufferSize - 4);
|
|
55
|
+
return buf;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Serialize a number into a big-endian signed 32-bit integer Buffer with the specified buffer size.
|
|
60
|
+
* This function converts the input number into its binary representation and stores it in a Buffer
|
|
61
|
+
* with the provided buffer size. By default, the buffer size is set to 4 bytes which represents a 32-bit integer.
|
|
62
|
+
* The function will use the last 4 bytes of the buffer to store the serialized number. If the input number
|
|
63
|
+
* is outside the range of a 32-bit signed integer, the resulting serialization may be incorrect due to truncation.
|
|
64
|
+
*
|
|
65
|
+
* @param n - The number to be serialized as a signed 32-bit integer.
|
|
66
|
+
* @param bufferSize - Optional, the size of the output Buffer (default is 4 bytes).
|
|
67
|
+
* @returns A Buffer containing the serialized big-endian signed 32-bit integer.
|
|
68
|
+
*/
|
|
69
|
+
export function numToInt32BE(n: number, bufferSize = 4) {
|
|
70
|
+
const buf = Buffer.alloc(bufferSize);
|
|
71
|
+
buf.writeInt32BE(n, bufferSize - 4);
|
|
72
|
+
return buf;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Convert a number to an 8-bit unsigned integer and return it as a Buffer of length 1.
|
|
77
|
+
* The input number is written as an 8-bit unsigned integer into the buffer. This function
|
|
78
|
+
* is useful for converting small numeric values to a standardized binary format that can be
|
|
79
|
+
* easily stored or transmitted.
|
|
80
|
+
*
|
|
81
|
+
* @param n - The number to be converted to an 8-bit unsigned integer.
|
|
82
|
+
* @returns A Buffer containing the 8-bit unsigned integer representation of the input number.
|
|
83
|
+
*/
|
|
84
|
+
export function numToUInt8(n: number) {
|
|
85
|
+
const bufferSize = 1;
|
|
86
|
+
const buf = Buffer.alloc(bufferSize);
|
|
87
|
+
buf.writeUInt8(n, 0);
|
|
88
|
+
return buf;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Adds a 4-byte byte-length prefix to a buffer.
|
|
93
|
+
* @param buf - The input Buffer to be prefixed
|
|
94
|
+
* @returns A Buffer with 4-byte byte-length prefix.
|
|
95
|
+
*/
|
|
96
|
+
export function prefixBufferWithLength(buf: Buffer) {
|
|
97
|
+
const lengthBuf = Buffer.alloc(4);
|
|
98
|
+
lengthBuf.writeUInt32BE(buf.length, 0);
|
|
99
|
+
return Buffer.concat([lengthBuf, buf]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Parse a buffer as a big integer.
|
|
104
|
+
*/
|
|
105
|
+
export function toBigInt(buf: Buffer): bigint {
|
|
106
|
+
const hex = buf.toString('hex');
|
|
107
|
+
if (hex.length === 0) {
|
|
108
|
+
return BigInt(0);
|
|
109
|
+
}
|
|
110
|
+
return BigInt(`0x${hex}`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Stores full 256 bits of information in 2 fields.
|
|
115
|
+
* @param buf - 32 bytes of data
|
|
116
|
+
* @returns 2 field elements
|
|
117
|
+
*/
|
|
118
|
+
export function to2Fields(buf: Buffer): [Fr, Fr] {
|
|
119
|
+
if (buf.length !== 32) {
|
|
120
|
+
throw new Error('Buffer must be 32 bytes');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Split the hash into two fields, a high and a low
|
|
124
|
+
const buf1 = Buffer.concat([Buffer.alloc(16), buf.subarray(0, 16)]);
|
|
125
|
+
const buf2 = Buffer.concat([Buffer.alloc(16), buf.subarray(16, 32)]);
|
|
126
|
+
|
|
127
|
+
return [Fr.fromBuffer(buf1), Fr.fromBuffer(buf2)];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Reconstructs the original 32 bytes of data from 2 field elements.
|
|
132
|
+
* @param field1 - First field element
|
|
133
|
+
* @param field2 - Second field element
|
|
134
|
+
* @returns 32 bytes of data as a Buffer
|
|
135
|
+
*/
|
|
136
|
+
export function from2Fields(field1: Fr, field2: Fr): Buffer {
|
|
137
|
+
// Convert the field elements back to buffers
|
|
138
|
+
const buf1 = field1.toBuffer();
|
|
139
|
+
const buf2 = field2.toBuffer();
|
|
140
|
+
|
|
141
|
+
// Remove the padding (first 16 bytes) from each buffer
|
|
142
|
+
const originalPart1 = buf1.slice(16, 32);
|
|
143
|
+
const originalPart2 = buf2.slice(16, 32);
|
|
144
|
+
|
|
145
|
+
// Concatenate the two parts to form the original buffer
|
|
146
|
+
return Buffer.concat([originalPart1, originalPart2]);
|
|
147
|
+
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
|
|
2
|
+
import { Fr } from '../fields/fields.js';
|
|
3
|
+
import { numToUInt32BE } from './free_funcs.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* For serializing an array of fixed length buffers.
|
|
7
|
+
* TODO move to foundation pkg.
|
|
8
|
+
* @param arr - Array of buffers.
|
|
9
|
+
* @returns The serialized buffers.
|
|
10
|
+
*/
|
|
11
|
+
export function serializeBufferArrayToVector(arr: Buffer[]): Buffer {
|
|
12
|
+
const lengthBuf = Buffer.alloc(4);
|
|
13
|
+
lengthBuf.writeUInt32BE(arr.length, 0);
|
|
14
|
+
return Buffer.concat([lengthBuf, ...arr]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Helper function for deserializeArrayFromVector.
|
|
19
|
+
*/
|
|
20
|
+
type DeserializeFn<T> = (
|
|
21
|
+
buf: Buffer,
|
|
22
|
+
offset: number,
|
|
23
|
+
) => {
|
|
24
|
+
/**
|
|
25
|
+
* The deserialized type.
|
|
26
|
+
*/
|
|
27
|
+
elem: T;
|
|
28
|
+
/**
|
|
29
|
+
* How many bytes to advance by.
|
|
30
|
+
*/
|
|
31
|
+
adv: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Deserializes an array from a vector on an element-by-element basis.
|
|
36
|
+
* @param deserialize - A function used to deserialize each element of the vector.
|
|
37
|
+
* @param vector - The vector to deserialize.
|
|
38
|
+
* @param offset - The position in the vector to start deserializing from.
|
|
39
|
+
* @returns Deserialized array and how many bytes we advanced by.
|
|
40
|
+
*
|
|
41
|
+
* TODO: move to foundation pkg.
|
|
42
|
+
*/
|
|
43
|
+
export function deserializeArrayFromVector<T>(
|
|
44
|
+
deserialize: DeserializeFn<T>,
|
|
45
|
+
vector: Buffer,
|
|
46
|
+
offset = 0,
|
|
47
|
+
): {
|
|
48
|
+
/**
|
|
49
|
+
* The deserialized array.
|
|
50
|
+
*/
|
|
51
|
+
elem: T[];
|
|
52
|
+
/**
|
|
53
|
+
* How many bytes we advanced by.
|
|
54
|
+
*/
|
|
55
|
+
adv: number;
|
|
56
|
+
} {
|
|
57
|
+
let pos = offset;
|
|
58
|
+
const size = vector.readUInt32BE(pos);
|
|
59
|
+
pos += 4;
|
|
60
|
+
const arr = new Array<T>(size);
|
|
61
|
+
for (let i = 0; i < size; ++i) {
|
|
62
|
+
const { elem, adv } = deserialize(vector, pos);
|
|
63
|
+
pos += adv;
|
|
64
|
+
arr[i] = elem;
|
|
65
|
+
}
|
|
66
|
+
return { elem: arr, adv: pos - offset };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Cast a uint8 array to a number.
|
|
71
|
+
* @param array - The uint8 array.
|
|
72
|
+
* @returns The number.
|
|
73
|
+
*/
|
|
74
|
+
export function uint8ArrayToNum(array: Uint8Array): number {
|
|
75
|
+
const buf = Buffer.from(array);
|
|
76
|
+
return buf.readUint32LE();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Serializes a boolean to a buffer.
|
|
81
|
+
* @param value - Value to serialize.
|
|
82
|
+
* @returns The serialized boolean.
|
|
83
|
+
*/
|
|
84
|
+
export function boolToBuffer(value: boolean, bufferSize = 1): Buffer {
|
|
85
|
+
const buf = Buffer.alloc(bufferSize);
|
|
86
|
+
buf.writeUInt8(value ? 1 : 0, bufferSize - 1);
|
|
87
|
+
return buf;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Deserialize the 256-bit number at address `offset`.
|
|
92
|
+
* @param buf - The buffer.
|
|
93
|
+
* @param offset - The address.
|
|
94
|
+
* @returns The deserialized 256-bit field.
|
|
95
|
+
*/
|
|
96
|
+
export function deserializeField(buf: Buffer, offset = 0) {
|
|
97
|
+
const adv = 32;
|
|
98
|
+
return { elem: buf.slice(offset, offset + adv), adv };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A type that can be written to a buffer. */
|
|
102
|
+
export type Bufferable =
|
|
103
|
+
| boolean
|
|
104
|
+
| Buffer
|
|
105
|
+
| number
|
|
106
|
+
| string
|
|
107
|
+
| {
|
|
108
|
+
/**
|
|
109
|
+
* Serialize to a buffer.
|
|
110
|
+
*/
|
|
111
|
+
toBuffer: () => Buffer;
|
|
112
|
+
}
|
|
113
|
+
| Bufferable[];
|
|
114
|
+
|
|
115
|
+
/** A type that can be converted to a Field or a Field array. */
|
|
116
|
+
export type Fieldeable =
|
|
117
|
+
| Fr
|
|
118
|
+
| boolean
|
|
119
|
+
| number
|
|
120
|
+
| bigint
|
|
121
|
+
| {
|
|
122
|
+
/** Serialize to a field. */
|
|
123
|
+
toField: () => Fr;
|
|
124
|
+
}
|
|
125
|
+
| {
|
|
126
|
+
/** Serialize to an array of fields. */
|
|
127
|
+
toFields: () => Fr[];
|
|
128
|
+
}
|
|
129
|
+
| Fieldeable[];
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Serializes a list of objects contiguously.
|
|
133
|
+
* @param objs - Objects to serialize.
|
|
134
|
+
* @returns A buffer list with the concatenation of all fields.
|
|
135
|
+
*/
|
|
136
|
+
export function serializeToBufferArray(...objs: Bufferable[]): Buffer[] {
|
|
137
|
+
let ret: Buffer[] = [];
|
|
138
|
+
for (const obj of objs) {
|
|
139
|
+
if (Array.isArray(obj)) {
|
|
140
|
+
ret = [...ret, ...serializeToBufferArray(...obj)];
|
|
141
|
+
} else if (Buffer.isBuffer(obj)) {
|
|
142
|
+
ret.push(obj);
|
|
143
|
+
} else if (typeof obj === 'boolean') {
|
|
144
|
+
ret.push(boolToBuffer(obj));
|
|
145
|
+
} else if (typeof obj === 'number') {
|
|
146
|
+
// Note: barretenberg assumes everything is big-endian
|
|
147
|
+
ret.push(numToUInt32BE(obj)); // TODO: Are we always passing numbers as UInt32?
|
|
148
|
+
} else if (typeof obj === 'string') {
|
|
149
|
+
ret.push(numToUInt32BE(obj.length));
|
|
150
|
+
ret.push(Buffer.from(obj));
|
|
151
|
+
} else {
|
|
152
|
+
ret.push(obj.toBuffer());
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return ret;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Serializes a list of objects contiguously.
|
|
160
|
+
* @param objs - Objects to serialize.
|
|
161
|
+
* @returns An array of fields with the concatenation of all fields.
|
|
162
|
+
*/
|
|
163
|
+
export function serializeToFields(...objs: Fieldeable[]): Fr[] {
|
|
164
|
+
let ret: Fr[] = [];
|
|
165
|
+
for (const obj of objs) {
|
|
166
|
+
if (Array.isArray(obj)) {
|
|
167
|
+
ret = [...ret, ...serializeToFields(...obj)];
|
|
168
|
+
} else if (obj instanceof Fr) {
|
|
169
|
+
ret.push(obj);
|
|
170
|
+
} else if (typeof obj === 'boolean' || typeof obj === 'number' || typeof obj === 'bigint') {
|
|
171
|
+
ret.push(new Fr(obj));
|
|
172
|
+
} else if ('toFields' in obj) {
|
|
173
|
+
ret = [...ret, ...obj.toFields()];
|
|
174
|
+
} else {
|
|
175
|
+
ret.push(obj.toField());
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return ret;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Serializes a list of objects contiguously.
|
|
183
|
+
* @param objs - Objects to serialize.
|
|
184
|
+
* @returns A single buffer with the concatenation of all fields.
|
|
185
|
+
*/
|
|
186
|
+
export function serializeToBuffer(...objs: Bufferable[]): Buffer {
|
|
187
|
+
return Buffer.concat(serializeToBufferArray(...objs));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Returns a user-friendly JSON representation of an object, showing buffers as hex strings.
|
|
192
|
+
* @param obj - Object to json-stringify.
|
|
193
|
+
* @returns A JSON string.
|
|
194
|
+
*/
|
|
195
|
+
export function toFriendlyJSON(obj: object): string {
|
|
196
|
+
return JSON.stringify(
|
|
197
|
+
obj,
|
|
198
|
+
(key, value) => {
|
|
199
|
+
if (value !== null && typeof value === 'object' && value.type === 'Buffer' && Array.isArray(value.data)) {
|
|
200
|
+
return '0x' + Buffer.from(value.data).toString('hex');
|
|
201
|
+
} else if (typeof value === 'bigint') {
|
|
202
|
+
return value.toString();
|
|
203
|
+
} else if (
|
|
204
|
+
value &&
|
|
205
|
+
(
|
|
206
|
+
value as {
|
|
207
|
+
/**
|
|
208
|
+
* Signature of the target serialization function.
|
|
209
|
+
*/
|
|
210
|
+
toFriendlyJSON: () => string;
|
|
211
|
+
}
|
|
212
|
+
).toFriendlyJSON
|
|
213
|
+
) {
|
|
214
|
+
return value.toFriendlyJSON();
|
|
215
|
+
} else {
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
2,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Serialize a BigInt value into a Buffer of specified width.
|
|
225
|
+
* The function converts the input BigInt into its big-endian representation and stores it in a Buffer of the given width.
|
|
226
|
+
* If the width is not provided, a default value of 32 bytes will be used. It is important to provide an appropriate width
|
|
227
|
+
* to avoid truncation or incorrect serialization of large BigInt values.
|
|
228
|
+
*
|
|
229
|
+
* @param n - The BigInt value to be serialized.
|
|
230
|
+
* @param width - The width (in bytes) of the output Buffer, optional with default value 32.
|
|
231
|
+
* @returns A Buffer containing the serialized BigInt value in big-endian format.
|
|
232
|
+
*/
|
|
233
|
+
export function serializeBigInt(n: bigint, width = 32) {
|
|
234
|
+
return toBufferBE(n, width);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Deserialize a big integer from a buffer, given an offset and width.
|
|
239
|
+
* Reads the specified number of bytes from the buffer starting at the offset, converts it to a big integer, and returns the deserialized result along with the number of bytes read (advanced).
|
|
240
|
+
*
|
|
241
|
+
* @param buf - The buffer containing the big integer to be deserialized.
|
|
242
|
+
* @param offset - The position in the buffer where the big integer starts. Defaults to 0.
|
|
243
|
+
* @param width - The number of bytes to read from the buffer for the big integer. Defaults to 32.
|
|
244
|
+
* @returns An object containing the deserialized big integer value ('elem') and the number of bytes advanced ('adv').
|
|
245
|
+
*/
|
|
246
|
+
export function deserializeBigInt(buf: Buffer, offset = 0, width = 32) {
|
|
247
|
+
return { elem: toBigIntBE(buf.subarray(offset, offset + width)), adv: width };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Serializes a Date object into a Buffer containing its timestamp as a big integer value.
|
|
252
|
+
* The resulting Buffer has a fixed width of 8 bytes, representing a 64-bit big-endian integer.
|
|
253
|
+
* This function is useful for converting date values into a binary format that can be stored or transmitted easily.
|
|
254
|
+
*
|
|
255
|
+
* @param date - The Date object to be serialized.
|
|
256
|
+
* @returns A Buffer containing the serialized timestamp of the input Date object.
|
|
257
|
+
*/
|
|
258
|
+
export function serializeDate(date: Date) {
|
|
259
|
+
return serializeBigInt(BigInt(date.getTime()), 8);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Deserialize a boolean value from a given buffer at the specified offset.
|
|
264
|
+
* Reads a single byte at the provided offset in the buffer and returns
|
|
265
|
+
* the deserialized boolean value along with the number of bytes read (adv).
|
|
266
|
+
*
|
|
267
|
+
* @param buf - The buffer containing the serialized boolean value.
|
|
268
|
+
* @param offset - The position in the buffer to start reading the boolean value.
|
|
269
|
+
* @returns An object containing the deserialized boolean value (elem) and the number of bytes read (adv).
|
|
270
|
+
*/
|
|
271
|
+
export function deserializeBool(buf: Buffer, offset = 0) {
|
|
272
|
+
const adv = 1;
|
|
273
|
+
return { elem: buf.readUInt8(offset), adv };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Deserialize a 4-byte unsigned integer from a buffer, starting at the specified offset.
|
|
278
|
+
* The deserialization reads 4 bytes from the given buffer and converts it into a number.
|
|
279
|
+
* Returns an object containing the deserialized unsigned integer and the number of bytes advanced (4).
|
|
280
|
+
*
|
|
281
|
+
* @param buf - The buffer containing the serialized unsigned integer.
|
|
282
|
+
* @param offset - The starting position in the buffer to deserialize from (default is 0).
|
|
283
|
+
* @returns An object with the deserialized unsigned integer as 'elem' and the number of bytes advanced ('adv') as 4.
|
|
284
|
+
*/
|
|
285
|
+
export function deserializeUInt32(buf: Buffer, offset = 0) {
|
|
286
|
+
const adv = 4;
|
|
287
|
+
return { elem: buf.readUInt32BE(offset), adv };
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Deserialize a signed 32-bit integer from a buffer at the given offset.
|
|
292
|
+
* The input 'buf' should be a Buffer containing binary data, and 'offset' should be the position in the buffer
|
|
293
|
+
* where the signed 32-bit integer starts. Returns an object with both the deserialized integer (elem) and the
|
|
294
|
+
* number of bytes advanced in the buffer (adv, always equal to 4).
|
|
295
|
+
*
|
|
296
|
+
* @param buf - The buffer containing the binary data.
|
|
297
|
+
* @param offset - Optional, the position in the buffer where the signed 32-bit integer starts (default is 0).
|
|
298
|
+
* @returns An object with the deserialized integer as 'elem' and the number of bytes advanced as 'adv'.
|
|
299
|
+
*/
|
|
300
|
+
export function deserializeInt32(buf: Buffer, offset = 0) {
|
|
301
|
+
const adv = 4;
|
|
302
|
+
return { elem: buf.readInt32BE(offset), adv };
|
|
303
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a fixed-length array.
|
|
3
|
+
*/
|
|
4
|
+
export type Tuple<T, N extends number> = N extends N ? (number extends N ? T[] : _Tuple<T, N, []>) : never;
|
|
5
|
+
/**
|
|
6
|
+
* Recursive type helper for constructing a fixed-length tuple of a given type.
|
|
7
|
+
* This is utilized internally by Tuple to create the final fixed-length tuple.
|
|
8
|
+
*/
|
|
9
|
+
type _Tuple<T, N extends number, R extends unknown[]> = R['length'] extends N ? R : _Tuple<T, N, [T, ...R]>;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Check an array size, and cast it to a tuple.
|
|
13
|
+
* @param array - The array.
|
|
14
|
+
* @param n - The size.
|
|
15
|
+
* @returns The case tuple, or throws Error.
|
|
16
|
+
*/
|
|
17
|
+
export function assertLength<T, N extends number>(array: T[], n: N): Tuple<T, N> {
|
|
18
|
+
if (array.length !== n) {
|
|
19
|
+
throw new Error(`Wrong 'fixed array' size. Expected ${n}, got ${array.length}.`);
|
|
20
|
+
}
|
|
21
|
+
return array as Tuple<T, N>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Annoying, mapping a tuple does not preserve length.
|
|
25
|
+
* This is a helper to preserve length during a map operation.
|
|
26
|
+
* @typeparam T - The original array type.
|
|
27
|
+
*/
|
|
28
|
+
type MapTuple<T extends any[], F extends (item: any) => any> = {
|
|
29
|
+
[K in keyof T]: T[K] extends infer U ? (F extends (item: U) => infer V ? V : never) : never;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Annoyingly, mapping a tuple does not preserve length.
|
|
34
|
+
* This is a helper to preserve length during a map operation.
|
|
35
|
+
* @see https://github.com/microsoft/TypeScript/issues/29841.
|
|
36
|
+
* @param array - A tuple array.
|
|
37
|
+
*/
|
|
38
|
+
export function mapTuple<T extends any[], F extends (item: any) => any>(tuple: T, fn: F): MapTuple<T, F> {
|
|
39
|
+
return tuple.map(fn) as MapTuple<T, F>;
|
|
40
|
+
}
|