@aztec/foundation 0.7.2 → 0.7.4
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/.tsbuildinfo +1 -0
- package/dest/abi/abi.d.ts +237 -0
- package/dest/abi/abi.d.ts.map +1 -0
- package/dest/abi/abi.js +37 -0
- package/dest/abi/abi_coder.d.ts +8 -0
- package/dest/abi/abi_coder.d.ts.map +1 -0
- package/dest/abi/abi_coder.js +24 -0
- package/dest/abi/decoder.d.ts +57 -0
- package/dest/abi/decoder.d.ts.map +1 -0
- package/dest/abi/decoder.js +149 -0
- package/dest/abi/decoder.test.d.ts +2 -0
- package/dest/abi/decoder.test.d.ts.map +1 -0
- package/dest/abi/decoder.test.js +70 -0
- package/dest/abi/encoder.d.ts +10 -0
- package/dest/abi/encoder.d.ts.map +1 -0
- package/dest/abi/encoder.js +92 -0
- package/dest/abi/encoder.test.d.ts +2 -0
- package/dest/abi/encoder.test.d.ts.map +1 -0
- package/dest/abi/encoder.test.js +74 -0
- package/dest/abi/function_selector.d.ts +73 -0
- package/dest/abi/function_selector.d.ts.map +1 -0
- package/dest/abi/function_selector.js +104 -0
- package/dest/abi/index.d.ts +6 -0
- package/dest/abi/index.d.ts.map +1 -0
- package/dest/abi/index.js +6 -0
- package/dest/async-map/async_map.test.d.ts +2 -0
- package/dest/async-map/async_map.test.d.ts.map +1 -0
- package/dest/async-map/async_map.test.js +9 -0
- package/dest/async-map/index.d.ts +13 -0
- package/dest/async-map/index.d.ts.map +1 -0
- package/dest/async-map/index.js +19 -0
- package/dest/aztec-address/index.d.ts +114 -0
- package/dest/aztec-address/index.d.ts.map +1 -0
- package/dest/aztec-address/index.js +148 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts +2 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts.map +1 -0
- package/dest/bigint-buffer/bigint-buffer.test.js +18 -0
- package/dest/bigint-buffer/index.d.ts +35 -0
- package/dest/bigint-buffer/index.d.ts.map +1 -0
- package/dest/bigint-buffer/index.js +68 -0
- package/dest/collection/array.d.ts +30 -0
- package/dest/collection/array.d.ts.map +1 -0
- package/dest/collection/array.js +47 -0
- package/dest/collection/index.d.ts +2 -0
- package/dest/collection/index.d.ts.map +1 -0
- package/dest/collection/index.js +2 -0
- package/dest/committable/committable.d.ts +28 -0
- package/dest/committable/committable.d.ts.map +1 -0
- package/dest/committable/committable.js +41 -0
- package/dest/committable/committable.test.d.ts +2 -0
- package/dest/committable/committable.test.d.ts.map +1 -0
- package/dest/committable/committable.test.js +26 -0
- package/dest/committable/index.d.ts +2 -0
- package/dest/committable/index.d.ts.map +1 -0
- package/dest/committable/index.js +2 -0
- package/dest/crypto/index.d.ts +4 -0
- package/dest/crypto/index.d.ts.map +1 -0
- package/dest/crypto/index.js +4 -0
- package/dest/crypto/keccak/index.d.ts +20 -0
- package/dest/crypto/keccak/index.d.ts.map +1 -0
- package/dest/crypto/keccak/index.js +31 -0
- package/dest/crypto/random/index.d.ts +3 -0
- package/dest/crypto/random/index.d.ts.map +1 -0
- package/dest/crypto/random/index.js +36 -0
- package/dest/crypto/random/index.test.d.ts +2 -0
- package/dest/crypto/random/index.test.d.ts.map +1 -0
- package/dest/crypto/random/index.test.js +13 -0
- package/dest/crypto/sha256/index.d.ts +11 -0
- package/dest/crypto/sha256/index.d.ts.map +1 -0
- package/dest/crypto/sha256/index.js +14 -0
- package/dest/crypto/sha256/index.test.d.ts +2 -0
- package/dest/crypto/sha256/index.test.d.ts.map +1 -0
- package/dest/crypto/sha256/index.test.js +11 -0
- package/dest/errors/index.d.ts +8 -0
- package/dest/errors/index.d.ts.map +1 -0
- package/dest/errors/index.js +8 -0
- package/dest/eth-address/eth_address.test.d.ts +2 -0
- package/dest/eth-address/eth_address.test.d.ts.map +1 -0
- package/dest/eth-address/eth_address.test.js +95 -0
- package/dest/eth-address/index.d.ts +144 -0
- package/dest/eth-address/index.d.ts.map +1 -0
- package/dest/eth-address/index.js +228 -0
- package/dest/fields/coordinate.d.ts +67 -0
- package/dest/fields/coordinate.d.ts.map +1 -0
- package/dest/fields/coordinate.js +95 -0
- package/dest/fields/coordinate.test.d.ts +2 -0
- package/dest/fields/coordinate.test.d.ts.map +1 -0
- package/dest/fields/coordinate.test.js +22 -0
- package/dest/fields/fields.d.ts +177 -0
- package/dest/fields/fields.d.ts.map +1 -0
- package/dest/fields/fields.js +219 -0
- package/dest/fields/grumpkin_scalar.d.ts +96 -0
- package/dest/fields/grumpkin_scalar.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.js +125 -0
- package/dest/fields/grumpkin_scalar.test.d.ts +2 -0
- package/dest/fields/grumpkin_scalar.test.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.test.js +45 -0
- package/dest/fields/index.d.ts +5 -0
- package/dest/fields/index.d.ts.map +1 -0
- package/dest/fields/index.js +5 -0
- package/dest/fields/point.d.ts +105 -0
- package/dest/fields/point.d.ts.map +1 -0
- package/dest/fields/point.js +125 -0
- package/dest/fifo/bounded_serial_queue.d.ts +56 -0
- package/dest/fifo/bounded_serial_queue.d.ts.map +1 -0
- package/dest/fifo/bounded_serial_queue.js +94 -0
- package/dest/fifo/index.d.ts +5 -0
- package/dest/fifo/index.d.ts.map +1 -0
- package/dest/fifo/index.js +5 -0
- package/dest/fifo/memory_fifo.d.ts +55 -0
- package/dest/fifo/memory_fifo.d.ts.map +1 -0
- package/dest/fifo/memory_fifo.js +109 -0
- package/dest/fifo/semaphore.d.ts +23 -0
- package/dest/fifo/semaphore.d.ts.map +1 -0
- package/dest/fifo/semaphore.js +30 -0
- package/dest/fifo/serial_queue.d.ts +48 -0
- package/dest/fifo/serial_queue.d.ts.map +1 -0
- package/dest/fifo/serial_queue.js +74 -0
- package/dest/json-rpc/class_converter.d.ts +144 -0
- package/dest/json-rpc/class_converter.d.ts.map +1 -0
- package/dest/json-rpc/class_converter.js +100 -0
- package/dest/json-rpc/client/index.d.ts +2 -0
- package/dest/json-rpc/client/index.d.ts.map +1 -0
- package/dest/json-rpc/client/index.js +2 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts +29 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.js +104 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts +2 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.test.js +20 -0
- package/dest/json-rpc/convert.d.ts +28 -0
- package/dest/json-rpc/convert.d.ts.map +1 -0
- package/dest/json-rpc/convert.js +139 -0
- package/dest/json-rpc/convert.test.d.ts +2 -0
- package/dest/json-rpc/convert.test.d.ts.map +1 -0
- package/dest/json-rpc/convert.test.js +68 -0
- package/dest/json-rpc/fixtures/class_a.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_a.js +17 -0
- package/dest/json-rpc/fixtures/class_b.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_b.js +17 -0
- package/dest/json-rpc/fixtures/test_state.d.ts +47 -0
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/test_state.js +62 -0
- package/dest/json-rpc/index.d.ts +3 -0
- package/dest/json-rpc/index.d.ts.map +1 -0
- package/dest/json-rpc/index.js +3 -0
- package/dest/json-rpc/js_utils.d.ts +13 -0
- package/dest/json-rpc/js_utils.d.ts.map +1 -0
- package/dest/json-rpc/js_utils.js +18 -0
- package/dest/json-rpc/server/index.d.ts +3 -0
- package/dest/json-rpc/server/index.d.ts.map +1 -0
- package/dest/json-rpc/server/index.js +3 -0
- package/dest/json-rpc/server/json_proxy.d.ts +18 -0
- package/dest/json-rpc/server/json_proxy.d.ts.map +1 -0
- package/dest/json-rpc/server/json_proxy.js +36 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts +34 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.js +133 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts +2 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.test.js +22 -0
- package/dest/log/console.d.ts +11 -0
- package/dest/log/console.d.ts.map +1 -0
- package/dest/log/console.js +38 -0
- package/dest/log/debug.d.ts +43 -0
- package/dest/log/debug.d.ts.map +1 -0
- package/dest/log/debug.js +75 -0
- package/dest/log/index.d.ts +9 -0
- package/dest/log/index.d.ts.map +1 -0
- package/dest/log/index.js +5 -0
- package/dest/log/log_history.d.ts +31 -0
- package/dest/log/log_history.d.ts.map +1 -0
- package/dest/log/log_history.js +42 -0
- package/dest/log/log_history.test.d.ts +2 -0
- package/dest/log/log_history.test.d.ts.map +1 -0
- package/dest/log/log_history.test.js +78 -0
- package/dest/log/logger.d.ts +34 -0
- package/dest/log/logger.d.ts.map +1 -0
- package/dest/log/logger.js +78 -0
- package/dest/mutex/index.d.ts +53 -0
- package/dest/mutex/index.d.ts.map +1 -0
- package/dest/mutex/index.js +74 -0
- package/dest/mutex/mutex.test.d.ts +9 -0
- package/dest/mutex/mutex.test.d.ts.map +1 -0
- package/dest/mutex/mutex.test.js +58 -0
- package/dest/mutex/mutex_database.d.ts +10 -0
- package/dest/mutex/mutex_database.d.ts.map +1 -0
- package/dest/mutex/mutex_database.js +2 -0
- package/dest/retry/index.d.ts +44 -0
- package/dest/retry/index.d.ts.map +1 -0
- package/dest/retry/index.js +89 -0
- package/dest/running-promise/index.d.ts +35 -0
- package/dest/running-promise/index.d.ts.map +1 -0
- package/dest/running-promise/index.js +59 -0
- package/dest/serialize/buffer_reader.d.ts +188 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.js +229 -0
- package/dest/serialize/buffer_reader.test.d.ts +2 -0
- package/dest/serialize/buffer_reader.test.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.test.js +156 -0
- package/dest/serialize/deserializer.d.ts +134 -0
- package/dest/serialize/deserializer.d.ts.map +1 -0
- package/dest/serialize/deserializer.js +145 -0
- package/dest/serialize/free_funcs.d.ts +203 -0
- package/dest/serialize/free_funcs.d.ts.map +1 -0
- package/dest/serialize/free_funcs.js +250 -0
- package/dest/serialize/index.d.ts +6 -0
- package/dest/serialize/index.d.ts.map +1 -0
- package/dest/serialize/index.js +6 -0
- package/dest/serialize/serialize.test.d.ts +2 -0
- package/dest/serialize/serialize.test.d.ts.map +1 -0
- package/dest/serialize/serialize.test.js +68 -0
- package/dest/serialize/serializer.d.ts +89 -0
- package/dest/serialize/serializer.d.ts.map +1 -0
- package/dest/serialize/serializer.js +111 -0
- package/dest/serialize/types.d.ts +33 -0
- package/dest/serialize/types.d.ts.map +1 -0
- package/dest/serialize/types.js +22 -0
- package/dest/sleep/index.d.ts +52 -0
- package/dest/sleep/index.d.ts.map +1 -0
- package/dest/sleep/index.js +70 -0
- package/dest/timer/index.d.ts +3 -0
- package/dest/timer/index.d.ts.map +1 -0
- package/dest/timer/index.js +3 -0
- package/dest/timer/timeout.d.ts +41 -0
- package/dest/timer/timeout.d.ts.map +1 -0
- package/dest/timer/timeout.js +62 -0
- package/dest/timer/timer.d.ts +33 -0
- package/dest/timer/timer.d.ts.map +1 -0
- package/dest/timer/timer.js +38 -0
- package/dest/transport/browser/index.d.ts +5 -0
- package/dest/transport/browser/index.d.ts.map +1 -0
- package/dest/transport/browser/index.js +5 -0
- package/dest/transport/browser/message_port_socket.d.ts +37 -0
- package/dest/transport/browser/message_port_socket.d.ts.map +1 -0
- package/dest/transport/browser/message_port_socket.js +46 -0
- package/dest/transport/browser/shared_worker_connector.d.ts +19 -0
- package/dest/transport/browser/shared_worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_connector.js +21 -0
- package/dest/transport/browser/shared_worker_listener.d.ts +38 -0
- package/dest/transport/browser/shared_worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_listener.js +37 -0
- package/dest/transport/browser/worker_connector.d.ts +26 -0
- package/dest/transport/browser/worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/worker_connector.js +30 -0
- package/dest/transport/browser/worker_listener.d.ts +39 -0
- package/dest/transport/browser/worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/worker_listener.js +39 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts +25 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_fn.js +18 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +102 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_proxy.js +56 -0
- package/dest/transport/dispatch/messages.d.ts +52 -0
- package/dest/transport/dispatch/messages.d.ts.map +1 -0
- package/dest/transport/dispatch/messages.js +12 -0
- package/dest/transport/index.d.ts +12 -0
- package/dest/transport/index.d.ts.map +1 -0
- package/dest/transport/index.js +12 -0
- package/dest/transport/interface/connector.d.ts +8 -0
- package/dest/transport/interface/connector.d.ts.map +1 -0
- package/dest/transport/interface/connector.js +2 -0
- package/dest/transport/interface/listener.d.ts +13 -0
- package/dest/transport/interface/listener.d.ts.map +1 -0
- package/dest/transport/interface/listener.js +2 -0
- package/dest/transport/interface/socket.d.ts +13 -0
- package/dest/transport/interface/socket.d.ts.map +1 -0
- package/dest/transport/interface/socket.js +2 -0
- package/dest/transport/interface/transferable.d.ts +68 -0
- package/dest/transport/interface/transferable.d.ts.map +1 -0
- package/dest/transport/interface/transferable.js +63 -0
- package/dest/transport/node/index.d.ts +3 -0
- package/dest/transport/node/index.d.ts.map +1 -0
- package/dest/transport/node/index.js +3 -0
- package/dest/transport/node/node_connector.d.ts +28 -0
- package/dest/transport/node/node_connector.d.ts.map +1 -0
- package/dest/transport/node/node_connector.js +28 -0
- package/dest/transport/node/node_connector_socket.d.ts +42 -0
- package/dest/transport/node/node_connector_socket.d.ts.map +1 -0
- package/dest/transport/node/node_connector_socket.js +48 -0
- package/dest/transport/node/node_listener.d.ts +26 -0
- package/dest/transport/node/node_listener.d.ts.map +1 -0
- package/dest/transport/node/node_listener.js +30 -0
- package/dest/transport/node/node_listener_socket.d.ts +37 -0
- package/dest/transport/node/node_listener_socket.d.ts.map +1 -0
- package/dest/transport/node/node_listener_socket.js +44 -0
- package/dest/transport/transport_client.d.ts +61 -0
- package/dest/transport/transport_client.d.ts.map +1 -0
- package/dest/transport/transport_client.js +94 -0
- package/dest/transport/transport_server.d.ts +56 -0
- package/dest/transport/transport_server.d.ts.map +1 -0
- package/dest/transport/transport_server.js +101 -0
- package/dest/types/index.d.ts +7 -0
- package/dest/types/index.d.ts.map +1 -0
- package/dest/types/index.js +2 -0
- package/dest/url/index.d.ts +9 -0
- package/dest/url/index.d.ts.map +1 -0
- package/dest/url/index.js +66 -0
- package/dest/wasm/empty_wasi_sdk.d.ts +130 -0
- package/dest/wasm/empty_wasi_sdk.d.ts.map +1 -0
- package/dest/wasm/empty_wasi_sdk.js +166 -0
- package/dest/wasm/index.d.ts +2 -0
- package/dest/wasm/index.d.ts.map +1 -0
- package/dest/wasm/index.js +2 -0
- package/dest/wasm/wasm_module.d.ts +135 -0
- package/dest/wasm/wasm_module.d.ts.map +1 -0
- package/dest/wasm/wasm_module.js +205 -0
- package/dest/wasm/wasm_module.test.d.ts +2 -0
- package/dest/wasm/wasm_module.test.d.ts.map +1 -0
- package/dest/wasm/wasm_module.test.js +24 -0
- package/dest/worker/browser/index.d.ts +3 -0
- package/dest/worker/browser/index.d.ts.map +1 -0
- package/dest/worker/browser/index.js +3 -0
- package/dest/worker/browser/start_web_module.d.ts +7 -0
- package/dest/worker/browser/start_web_module.d.ts.map +1 -0
- package/dest/worker/browser/start_web_module.js +22 -0
- package/dest/worker/browser/web_data_store.d.ts +23 -0
- package/dest/worker/browser/web_data_store.d.ts.map +1 -0
- package/dest/worker/browser/web_data_store.js +32 -0
- package/dest/worker/browser/web_worker.d.ts +10 -0
- package/dest/worker/browser/web_worker.d.ts.map +1 -0
- package/dest/worker/browser/web_worker.js +23 -0
- package/dest/worker/data_store.d.ts +20 -0
- package/dest/worker/data_store.d.ts.map +1 -0
- package/dest/worker/data_store.js +2 -0
- package/dest/worker/index.d.ts +3 -0
- package/dest/worker/index.d.ts.map +1 -0
- package/dest/worker/index.js +2 -0
- package/dest/worker/node/index.d.ts +3 -0
- package/dest/worker/node/index.d.ts.map +1 -0
- package/dest/worker/node/index.js +3 -0
- package/dest/worker/node/node_data_store.d.ts +12 -0
- package/dest/worker/node/node_data_store.d.ts.map +1 -0
- package/dest/worker/node/node_data_store.js +21 -0
- package/dest/worker/node/node_worker.d.ts +6 -0
- package/dest/worker/node/node_worker.d.ts.map +1 -0
- package/dest/worker/node/node_worker.js +20 -0
- package/dest/worker/node/start_node_module.d.ts +7 -0
- package/dest/worker/node/start_node_module.d.ts.map +1 -0
- package/dest/worker/node/start_node_module.js +26 -0
- package/dest/worker/wasm_worker.d.ts +9 -0
- package/dest/worker/wasm_worker.d.ts.map +1 -0
- package/dest/worker/wasm_worker.js +2 -0
- package/dest/worker/worker_pool.d.ts +40 -0
- package/dest/worker/worker_pool.d.ts.map +1 -0
- package/dest/worker/worker_pool.js +62 -0
- package/package.json +1 -1
- package/src/abi/abi.ts +8 -2
- package/src/abi/encoder.ts +5 -3
- package/Dockerfile +0 -15
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
|
|
2
|
+
import { randomBytes } from '../crypto/index.js';
|
|
3
|
+
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
4
|
+
/**
|
|
5
|
+
* Fr represents a field of integers modulo the prime number MODULUS.
|
|
6
|
+
* 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.
|
|
7
|
+
* Some use cases include working with cryptographic operations and finite fields.
|
|
8
|
+
*/
|
|
9
|
+
export class Fr {
|
|
10
|
+
constructor(value) {
|
|
11
|
+
const isFr = (value) => !!value.toBigInt;
|
|
12
|
+
this.value = isFr(value) ? value.toBigInt() : BigInt(value);
|
|
13
|
+
if (this.value > Fr.MAX_VALUE) {
|
|
14
|
+
throw new Error(`Fr out of range ${value}.`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Generates a random Fr or Fq instance with a value modulo the respective class' MODULUS.
|
|
19
|
+
* This method uses randomBytes to generate a random 32-byte buffer, converts it to a bigint
|
|
20
|
+
* and takes the modulus of the result with the class' MODULUS constant.
|
|
21
|
+
*
|
|
22
|
+
* @returns A new Fr or Fq instance with a random value.
|
|
23
|
+
*/
|
|
24
|
+
static random() {
|
|
25
|
+
const r = toBigIntBE(randomBytes(Fr.SIZE_IN_BYTES)) % Fr.MODULUS;
|
|
26
|
+
return new Fr(r);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Returns a new zero-value field.
|
|
30
|
+
* @returns A new zero-value field.
|
|
31
|
+
*/
|
|
32
|
+
static zero() {
|
|
33
|
+
return new Fr(0n);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create an instance of the corresponding class (Fr or Fq) from a Buffer or a BufferReader.
|
|
37
|
+
* Reads 'SIZE_IN_BYTES' bytes from the given Buffer or BufferReader and constructs an instance with the decoded value.
|
|
38
|
+
* If the input is a Buffer, it is internally converted to a BufferReader before reading.
|
|
39
|
+
* Throws an error if the input length is invalid or the decoded value is out of range.
|
|
40
|
+
*
|
|
41
|
+
* @param buffer - The Buffer or BufferReader containing the bytes representing the value.
|
|
42
|
+
* @returns An instance of the corresponding class (Fr or Fq) with the decoded value.
|
|
43
|
+
*/
|
|
44
|
+
static fromBuffer(buffer) {
|
|
45
|
+
const reader = BufferReader.asReader(buffer);
|
|
46
|
+
return new Fr(toBigIntBE(reader.readBytes(Fr.SIZE_IN_BYTES)));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Create a Fr instance from a hex-encoded string.
|
|
50
|
+
* The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
|
|
51
|
+
* Throws an error if the input length is invalid or the address value is out of range.
|
|
52
|
+
*
|
|
53
|
+
* @param address - The hex-encoded string representing the field element.
|
|
54
|
+
* @returns A Fr instance.
|
|
55
|
+
*/
|
|
56
|
+
static fromString(address) {
|
|
57
|
+
return Fr.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Converts the value of the instance to a buffer with a specified length.
|
|
61
|
+
* The method uses the provided value and size in bytes to create a buffer representation
|
|
62
|
+
* of the numeric value. This can be useful for serialization and communication purposes.
|
|
63
|
+
*
|
|
64
|
+
* @returns A buffer representing the instance's value.
|
|
65
|
+
*/
|
|
66
|
+
toBuffer() {
|
|
67
|
+
return toBufferBE(this.value, Fr.SIZE_IN_BYTES);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Converts the value of the Fr or Fq class instance to a hexadecimal string.
|
|
71
|
+
* The resulting string is prefixed with '0x' and represents the bigint value
|
|
72
|
+
* in base 16.
|
|
73
|
+
*
|
|
74
|
+
* @param padTo32 - Whether to pad the string to 32 bytes.
|
|
75
|
+
* @returns A hex-encoded string representing the value of the class instance.
|
|
76
|
+
*/
|
|
77
|
+
toString(padTo32 = false) {
|
|
78
|
+
return toHex(this.value, padTo32);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Retrieves the underlying bigint.
|
|
82
|
+
* This method mostly exists to match user expectations, as value is already public.
|
|
83
|
+
* @returns The underlying bigint.
|
|
84
|
+
*/
|
|
85
|
+
toBigInt() {
|
|
86
|
+
return this.value;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Returns a shortened string representation of the Fr value, formatted with '0x' prefix and ellipsis in the middle.
|
|
90
|
+
* The resulting string has first 10 characters (including '0x') and last 4 characters of the full hexadecimal value.
|
|
91
|
+
*
|
|
92
|
+
* @returns A shorter, human-readable string representation of the Fr value.
|
|
93
|
+
*/
|
|
94
|
+
toShortString() {
|
|
95
|
+
const str = this.toString();
|
|
96
|
+
return `${str.slice(0, 10)}...${str.slice(-4)}`;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Checks if the provided Fr instance is equal to the current instance.
|
|
100
|
+
* Two instances are considered equal if their 'value' properties are the same.
|
|
101
|
+
*
|
|
102
|
+
* @param rhs - The Fr instance to compare with the current instance.
|
|
103
|
+
* @returns A boolean indicating whether the two instances are equal.
|
|
104
|
+
*/
|
|
105
|
+
equals(rhs) {
|
|
106
|
+
return this.value === rhs.value;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if the instance value is zero.
|
|
110
|
+
* The method returns true if the value of the instance is 0n (zero in BigInt representation),
|
|
111
|
+
* otherwise, it returns false.
|
|
112
|
+
*
|
|
113
|
+
* @returns A boolean indicating whether the instance value is zero or not.
|
|
114
|
+
*/
|
|
115
|
+
isZero() {
|
|
116
|
+
return this.value === 0n;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Converts the current value of the Fq or Fr instance to a friendly JSON representation.
|
|
120
|
+
* The output will be a hexadecimal string prefixed with '0x'.
|
|
121
|
+
*
|
|
122
|
+
* @returns A '0x' prefixed hexadecimal string representing the current value.
|
|
123
|
+
*/
|
|
124
|
+
toFriendlyJSON() {
|
|
125
|
+
return this.toString();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
Fr.ZERO = new Fr(0n);
|
|
129
|
+
Fr.MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
130
|
+
Fr.MAX_VALUE = Fr.MODULUS - 1n;
|
|
131
|
+
Fr.SIZE_IN_BYTES = 32;
|
|
132
|
+
/**
|
|
133
|
+
* Fq represents a field element in a prime finite field with modulus defined by the constant MODULUS.
|
|
134
|
+
* It provides methods for creating, manipulating, and comparing field elements, as well as converting
|
|
135
|
+
* them to/from different data types like Buffers and hex strings. Field elements are used in various
|
|
136
|
+
* cryptographic protocols and operations, such as elliptic curve cryptography.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* const fqElem = new Fq(BigInt("123456789"));
|
|
140
|
+
* const randomFqElem = Fq.random();
|
|
141
|
+
* const fromBufferFqElem = Fq.fromBuffer(buffer);
|
|
142
|
+
*/
|
|
143
|
+
export class Fq {
|
|
144
|
+
constructor(
|
|
145
|
+
/**
|
|
146
|
+
* The element's value as a bigint in the finite field.
|
|
147
|
+
*/
|
|
148
|
+
value) {
|
|
149
|
+
this.value = value;
|
|
150
|
+
if (value > Fq.MAX_VALUE) {
|
|
151
|
+
throw new Error(`Fq out of range ${value}.`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Generates a random Fr or Fq instance with a value within the range of their respective modulus.
|
|
156
|
+
* The random value is generated from a byte array of length equal to SIZE_IN_BYTES, then truncated
|
|
157
|
+
* to the appropriate modulus before creating the new Fr or Fq instance.
|
|
158
|
+
*
|
|
159
|
+
* @returns A new Fr or Fq instance with a randomly generated value.
|
|
160
|
+
*/
|
|
161
|
+
static random() {
|
|
162
|
+
const r = toBigIntBE(randomBytes(64)) % Fq.MODULUS;
|
|
163
|
+
return new this(r);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Create an instance of the calling class (Fr or Fq) from a given buffer or BufferReader.
|
|
167
|
+
* Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
|
|
168
|
+
* with that value. Throws an error if the value is out of range for the calling class.
|
|
169
|
+
*
|
|
170
|
+
* @param buffer - The input buffer or BufferReader containing the bytes representing the value.
|
|
171
|
+
* @returns An instance of the calling class (Fr or Fq) initialized with the bigint value.
|
|
172
|
+
*/
|
|
173
|
+
static fromBuffer(buffer) {
|
|
174
|
+
const reader = BufferReader.asReader(buffer);
|
|
175
|
+
return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Converts the bigint value of the instance to a Buffer representation.
|
|
179
|
+
* The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
|
|
180
|
+
*
|
|
181
|
+
* @returns A Buffer containing the byte representation of the instance's value.
|
|
182
|
+
*/
|
|
183
|
+
toBuffer() {
|
|
184
|
+
return toBufferBE(this.value, Fq.SIZE_IN_BYTES);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Converts the Fq value to a hexadecimal string representation.
|
|
188
|
+
* The resulting string is prefixed with '0x' and contains the exact number of hex characters required
|
|
189
|
+
* to represent the numeric value of this instance.
|
|
190
|
+
*
|
|
191
|
+
* @returns A hexadecimal string representing the Fq value.
|
|
192
|
+
*/
|
|
193
|
+
toString() {
|
|
194
|
+
return toHex(this.value);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Check if the value of the current instance is zero.
|
|
198
|
+
* This function compares the internal 'value' property with 0n (BigInt representation of zero).
|
|
199
|
+
* Returns true if the value is zero, otherwise returns false.
|
|
200
|
+
*
|
|
201
|
+
* @returns A boolean indicating whether the value is zero or not.
|
|
202
|
+
*/
|
|
203
|
+
isZero() {
|
|
204
|
+
return this.value === 0n;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Converts the value of the Fr or Fq instance to a friendly JSON format.
|
|
208
|
+
* The output is a hexadecimal string representation of the value with '0x' prefix.
|
|
209
|
+
*
|
|
210
|
+
* @returns A string representing the value in the JSON format.
|
|
211
|
+
*/
|
|
212
|
+
toFriendlyJSON() {
|
|
213
|
+
return this.toString();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
Fq.MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
217
|
+
Fq.MAX_VALUE = Fr.MODULUS - 1n;
|
|
218
|
+
Fq.SIZE_IN_BYTES =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T0FBTyxHQUFHLEVBQUUsQ0FBQztBQUM1QixnQkFBYSxHQUFHLEVBQUUsQ0FBQyJ9
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a field element in a prime finite field with modulus defined by the constant MODULUS.
|
|
6
|
+
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
7
|
+
* the Aztec Yellow Paper.
|
|
8
|
+
*/
|
|
9
|
+
export declare class GrumpkinScalar {
|
|
10
|
+
/**
|
|
11
|
+
* The element's value as a bigint in the finite field.
|
|
12
|
+
*/
|
|
13
|
+
readonly value: bigint;
|
|
14
|
+
static MODULUS: bigint;
|
|
15
|
+
static MAX_VALUE: bigint;
|
|
16
|
+
static SIZE_IN_BYTES: number;
|
|
17
|
+
private static HIGH_SHIFT;
|
|
18
|
+
private static LOW_MASK;
|
|
19
|
+
constructor(
|
|
20
|
+
/**
|
|
21
|
+
* The element's value as a bigint in the finite field.
|
|
22
|
+
*/
|
|
23
|
+
value: bigint);
|
|
24
|
+
get low(): Fr;
|
|
25
|
+
get high(): Fr;
|
|
26
|
+
/**
|
|
27
|
+
* Deserialize a grumpkin scalar serialized in 2 Fr.
|
|
28
|
+
* @param high - The high Fr element.
|
|
29
|
+
* @param low - The low Fr element.
|
|
30
|
+
* @returns A GrumpkinScalar instance with the value of the two Fr elements.
|
|
31
|
+
*/
|
|
32
|
+
static fromHighLow(high: Fr, low: Fr): GrumpkinScalar;
|
|
33
|
+
/**
|
|
34
|
+
* Generates a random GrumpkinScalar.
|
|
35
|
+
*
|
|
36
|
+
* @returns A new GrumpkinScalar instance with a randomly generated value.
|
|
37
|
+
*/
|
|
38
|
+
static random(): GrumpkinScalar;
|
|
39
|
+
/**
|
|
40
|
+
* Create an instance of GrumpkinScalar from a given buffer or BufferReader.
|
|
41
|
+
* @remarks Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
|
|
42
|
+
* with that value. Throws an error if the value is out of range for the calling class.
|
|
43
|
+
*
|
|
44
|
+
* @param buffer - The input buffer or BufferReader containing the bytes representing the value.
|
|
45
|
+
* @returns A GrumpkinScalar instance.
|
|
46
|
+
*/
|
|
47
|
+
static fromBuffer(buffer: Buffer | BufferReader): GrumpkinScalar;
|
|
48
|
+
/**
|
|
49
|
+
* Like fromBuffer, but reduces the value modulo MODULUS.
|
|
50
|
+
*
|
|
51
|
+
* @param buffer - The Buffer or BufferReader containing the bytes representing the value.
|
|
52
|
+
* @returns GrumpkinScalar with the decoded value.
|
|
53
|
+
*/
|
|
54
|
+
static fromBufferWithReduction(buffer: Buffer | BufferReader): GrumpkinScalar;
|
|
55
|
+
/**
|
|
56
|
+
* Create a GrumpkinScalar instance from a hex-encoded string.
|
|
57
|
+
* The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
|
|
58
|
+
* Throws an error if the input length is invalid or the address value is out of range.
|
|
59
|
+
*
|
|
60
|
+
* @param address - The hex-encoded string representing the field element.
|
|
61
|
+
* @returns A GrumpkinScalar instance.
|
|
62
|
+
*/
|
|
63
|
+
static fromString(address: string): GrumpkinScalar;
|
|
64
|
+
/**
|
|
65
|
+
* Converts the bigint value of the instance to a Buffer representation.
|
|
66
|
+
* The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
|
|
67
|
+
*
|
|
68
|
+
* @returns A Buffer containing the byte representation of the instance's value.
|
|
69
|
+
*/
|
|
70
|
+
toBuffer(): Buffer;
|
|
71
|
+
/**
|
|
72
|
+
* Converts the GrumpkinScalar value to a hexadecimal string representation.
|
|
73
|
+
* The resulting string is prefixed with '0x' and contains the exact number of hex characters required
|
|
74
|
+
* to represent the numeric value of this instance.
|
|
75
|
+
*
|
|
76
|
+
* @param padTo32 - Whether to pad the resulting string to 32 bytes.
|
|
77
|
+
* @returns A hexadecimal string representing the GrumpkinScalar value.
|
|
78
|
+
*/
|
|
79
|
+
toString(padTo32?: boolean): `0x${string}`;
|
|
80
|
+
/**
|
|
81
|
+
* Check if the value of the current instance is zero.
|
|
82
|
+
* This function compares the internal 'value' property with 0n (BigInt representation of zero).
|
|
83
|
+
* Returns true if the value is zero, otherwise returns false.
|
|
84
|
+
*
|
|
85
|
+
* @returns A boolean indicating whether the value is zero or not.
|
|
86
|
+
*/
|
|
87
|
+
isZero(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Converts the value of the GrumpkinScalar instance to a friendly JSON format.
|
|
90
|
+
* The output is a hexadecimal string representation of the value with '0x' prefix.
|
|
91
|
+
*
|
|
92
|
+
* @returns A string representing the value in the JSON format.
|
|
93
|
+
*/
|
|
94
|
+
toFriendlyJSON(): `0x${string}`;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=grumpkin_scalar.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grumpkin_scalar.d.ts","sourceRoot":"","sources":["../../src/fields/grumpkin_scalar.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEjC;;;;GAIG;AACH,qBAAa,cAAc;IAUvB;;OAEG;aACa,KAAK,EAAE,MAAM;IAZ/B,MAAM,CAAC,OAAO,SAAuE;IACrF,MAAM,CAAC,SAAS,SAA+B;IAC/C,MAAM,CAAC,aAAa,SAAM;IAG1B,OAAO,CAAC,MAAM,CAAC,UAAU,CAAkD;IAC3E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAA0C;;IAG/D;;OAEG;IACa,KAAK,EAAE,MAAM;IAO/B,IAAI,GAAG,IAAI,EAAE,CAEZ;IAED,IAAI,IAAI,IAAI,EAAE,CAEb;IAED;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,cAAc;IAIrD;;;;OAIG;IACH,MAAM,CAAC,MAAM;IAKb;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAK/C;;;;;OAKG;IACH,MAAM,CAAC,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAM5D;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM;IAIjC;;;;;OAKG;IACH,QAAQ;IAIR;;;;;;;OAOG;IACH,QAAQ,CAAC,OAAO,UAAQ;IAIxB;;;;;;OAMG;IACH,MAAM;IAIN;;;;;OAKG;IACH,cAAc;CAGf"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { toBigIntBE, toBufferBE, toHex } from '../bigint-buffer/index.js';
|
|
2
|
+
import { randomBytes } from '../crypto/index.js';
|
|
3
|
+
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
4
|
+
import { Fr } from './fields.js';
|
|
5
|
+
/**
|
|
6
|
+
* Represents a field element in a prime finite field with modulus defined by the constant MODULUS.
|
|
7
|
+
* @remarks Called GrumpkinScalar because it is used to represent elements in Grumpkin's scalar field as defined in
|
|
8
|
+
* the Aztec Yellow Paper.
|
|
9
|
+
*/
|
|
10
|
+
export class GrumpkinScalar {
|
|
11
|
+
constructor(
|
|
12
|
+
/**
|
|
13
|
+
* The element's value as a bigint in the finite field.
|
|
14
|
+
*/
|
|
15
|
+
value) {
|
|
16
|
+
this.value = value;
|
|
17
|
+
if (value > GrumpkinScalar.MAX_VALUE) {
|
|
18
|
+
throw new Error(`GrumpkinScalar out of range ${value}.`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
get low() {
|
|
22
|
+
return new Fr(this.value & GrumpkinScalar.LOW_MASK);
|
|
23
|
+
}
|
|
24
|
+
get high() {
|
|
25
|
+
return new Fr(this.value >> GrumpkinScalar.HIGH_SHIFT);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Deserialize a grumpkin scalar serialized in 2 Fr.
|
|
29
|
+
* @param high - The high Fr element.
|
|
30
|
+
* @param low - The low Fr element.
|
|
31
|
+
* @returns A GrumpkinScalar instance with the value of the two Fr elements.
|
|
32
|
+
*/
|
|
33
|
+
static fromHighLow(high, low) {
|
|
34
|
+
return new GrumpkinScalar((high.value << GrumpkinScalar.HIGH_SHIFT) + low.value);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Generates a random GrumpkinScalar.
|
|
38
|
+
*
|
|
39
|
+
* @returns A new GrumpkinScalar instance with a randomly generated value.
|
|
40
|
+
*/
|
|
41
|
+
static random() {
|
|
42
|
+
const r = toBigIntBE(randomBytes(64)) % GrumpkinScalar.MODULUS;
|
|
43
|
+
return new this(r);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Create an instance of GrumpkinScalar from a given buffer or BufferReader.
|
|
47
|
+
* @remarks Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
|
|
48
|
+
* with that value. Throws an error if the value is out of range for the calling class.
|
|
49
|
+
*
|
|
50
|
+
* @param buffer - The input buffer or BufferReader containing the bytes representing the value.
|
|
51
|
+
* @returns A GrumpkinScalar instance.
|
|
52
|
+
*/
|
|
53
|
+
static fromBuffer(buffer) {
|
|
54
|
+
const reader = BufferReader.asReader(buffer);
|
|
55
|
+
return new this(toBigIntBE(reader.readBytes(this.SIZE_IN_BYTES)));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Like fromBuffer, but reduces the value modulo MODULUS.
|
|
59
|
+
*
|
|
60
|
+
* @param buffer - The Buffer or BufferReader containing the bytes representing the value.
|
|
61
|
+
* @returns GrumpkinScalar with the decoded value.
|
|
62
|
+
*/
|
|
63
|
+
static fromBufferWithReduction(buffer) {
|
|
64
|
+
const reader = BufferReader.asReader(buffer);
|
|
65
|
+
const value = toBigIntBE(reader.readBytes(GrumpkinScalar.SIZE_IN_BYTES)) % GrumpkinScalar.MODULUS;
|
|
66
|
+
return new GrumpkinScalar(value);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a GrumpkinScalar instance from a hex-encoded string.
|
|
70
|
+
* The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
|
|
71
|
+
* Throws an error if the input length is invalid or the address value is out of range.
|
|
72
|
+
*
|
|
73
|
+
* @param address - The hex-encoded string representing the field element.
|
|
74
|
+
* @returns A GrumpkinScalar instance.
|
|
75
|
+
*/
|
|
76
|
+
static fromString(address) {
|
|
77
|
+
return GrumpkinScalar.fromBuffer(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Converts the bigint value of the instance to a Buffer representation.
|
|
81
|
+
* The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
|
|
82
|
+
*
|
|
83
|
+
* @returns A Buffer containing the byte representation of the instance's value.
|
|
84
|
+
*/
|
|
85
|
+
toBuffer() {
|
|
86
|
+
return toBufferBE(this.value, GrumpkinScalar.SIZE_IN_BYTES);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Converts the GrumpkinScalar value to a hexadecimal string representation.
|
|
90
|
+
* The resulting string is prefixed with '0x' and contains the exact number of hex characters required
|
|
91
|
+
* to represent the numeric value of this instance.
|
|
92
|
+
*
|
|
93
|
+
* @param padTo32 - Whether to pad the resulting string to 32 bytes.
|
|
94
|
+
* @returns A hexadecimal string representing the GrumpkinScalar value.
|
|
95
|
+
*/
|
|
96
|
+
toString(padTo32 = false) {
|
|
97
|
+
return toHex(this.value, padTo32);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Check if the value of the current instance is zero.
|
|
101
|
+
* This function compares the internal 'value' property with 0n (BigInt representation of zero).
|
|
102
|
+
* Returns true if the value is zero, otherwise returns false.
|
|
103
|
+
*
|
|
104
|
+
* @returns A boolean indicating whether the value is zero or not.
|
|
105
|
+
*/
|
|
106
|
+
isZero() {
|
|
107
|
+
return this.value === 0n;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Converts the value of the GrumpkinScalar instance to a friendly JSON format.
|
|
111
|
+
* The output is a hexadecimal string representation of the value with '0x' prefix.
|
|
112
|
+
*
|
|
113
|
+
* @returns A string representing the value in the JSON format.
|
|
114
|
+
*/
|
|
115
|
+
toFriendlyJSON() {
|
|
116
|
+
return this.toString();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
GrumpkinScalar.MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
120
|
+
GrumpkinScalar.MAX_VALUE = GrumpkinScalar.MODULUS - 1n;
|
|
121
|
+
GrumpkinScalar.SIZE_IN_BYTES = 32;
|
|
122
|
+
// The following constants are used to split a GrumpkinScalar into two Fr elements.
|
|
123
|
+
GrumpkinScalar.HIGH_SHIFT = BigInt((GrumpkinScalar.SIZE_IN_BYTES / 2) * 8);
|
|
124
|
+
GrumpkinScalar.LOW_MASK = (1n << GrumpkinScalar.HIGH_SHIFT) - 1n;
|
|
125
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ3J1bXBraW5fc2NhbGFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2ZpZWxkcy9ncnVtcGtpbl9zY2FsYXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxVQUFVLEVBQUUsS0FBSyxFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFDMUUsT0FBTyxFQUFFLFdBQVcsRUFBRSxNQUFNLG9CQUFvQixDQUFDO0FBQ2pELE9BQU8sRUFBRSxZQUFZLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUM3RCxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sYUFBY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DO0lBRUQ7Ozs7OztPQU1HO0lBQ0gsTUFBTTtRQUNKLE9BQU8sSUFBSSxDQUFDLEtBQUssS0FBSyxFQUFFLENBQUM7SUFDM0IsQ0FBQztJQUVEOzs7OztPQUtHO0lBQ0gsY0FBYztRQUNaLE9BQU8sSUFBSSxDQUFDLFFBQVEsRUFBRSxDQUFDO0lBQ3pCLENBQUM7O0FBN0hNLHNCQUFPLEdBQUcsbUVBQW1FLENBQUM7QUFDOUUsd0JBQVMsR0FBRyxjQUFjLENBQUMsT0FBTyxHQUFHLEVBQUUsQ0FBQztBQUN4Qyw0QkFBYSxHQUFHLEVBQUUsQ0FBQztBQUUxQixtRkFBbUY7QUFDcEUseUJBQVUsR0FBRyxNQUFNLENBQUMsQ0FBQyxjQUFjLENBQUMsYUFBYSxHQUFHLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO0FBQzVELHVCQUFRLEdBQUcsQ0FBQyxFQUFFLElBQUksY0FBYyxDQUFDLFVBQVUsQ0FBQyxHQUFHLEVBQUUsQ0FBQyJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grumpkin_scalar.test.d.ts","sourceRoot":"","sources":["../../src/fields/grumpkin_scalar.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { GrumpkinScalar } from './grumpkin_scalar.js';
|
|
2
|
+
describe('GrumpkinScalar Serialization', () => {
|
|
3
|
+
// Test case for GrumpkinScalar.fromHighLow
|
|
4
|
+
it('fromHighLow should serialize and deserialize correctly', () => {
|
|
5
|
+
const original = GrumpkinScalar.random();
|
|
6
|
+
const high = original.high;
|
|
7
|
+
const low = original.low;
|
|
8
|
+
const deserialized = GrumpkinScalar.fromHighLow(high, low);
|
|
9
|
+
// Check if the deserialized instance is equal to the original
|
|
10
|
+
expect(deserialized).toEqual(original);
|
|
11
|
+
});
|
|
12
|
+
// Test case for GrumpkinScalar.fromBuffer
|
|
13
|
+
it('fromBuffer should serialize and deserialize correctly', () => {
|
|
14
|
+
const original = GrumpkinScalar.random();
|
|
15
|
+
const buffer = original.toBuffer();
|
|
16
|
+
const deserialized = GrumpkinScalar.fromBuffer(buffer);
|
|
17
|
+
// Check if the deserialized instance is equal to the original
|
|
18
|
+
expect(deserialized).toEqual(original);
|
|
19
|
+
});
|
|
20
|
+
// Test case for GrumpkinScalar.fromString
|
|
21
|
+
it('fromString should serialize and deserialize correctly', () => {
|
|
22
|
+
const original = GrumpkinScalar.random();
|
|
23
|
+
const hexString = original.toString();
|
|
24
|
+
const deserialized = GrumpkinScalar.fromString(hexString);
|
|
25
|
+
// Check if the deserialized instance is equal to the original
|
|
26
|
+
expect(deserialized).toEqual(original);
|
|
27
|
+
});
|
|
28
|
+
// Test case for GrumpkinScalar.toBuffer
|
|
29
|
+
it('toBuffer should serialize and deserialize correctly', () => {
|
|
30
|
+
const original = GrumpkinScalar.random();
|
|
31
|
+
const buffer = original.toBuffer();
|
|
32
|
+
const deserialized = GrumpkinScalar.fromBuffer(buffer);
|
|
33
|
+
// Check if the deserialized instance is equal to the original
|
|
34
|
+
expect(deserialized).toEqual(original);
|
|
35
|
+
});
|
|
36
|
+
// Test case for GrumpkinScalar.toString
|
|
37
|
+
it('toString should serialize and deserialize correctly', () => {
|
|
38
|
+
const original = GrumpkinScalar.random();
|
|
39
|
+
const hexString = original.toString();
|
|
40
|
+
const deserialized = GrumpkinScalar.fromString(hexString);
|
|
41
|
+
// Check if the deserialized instance is equal to the original
|
|
42
|
+
expect(deserialized).toEqual(original);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ3J1bXBraW5fc2NhbGFyLnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZmllbGRzL2dydW1wa2luX3NjYWxhci50ZXN0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ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BQUM7QUFDTCxDQUFDLENBQUMsQ0FBQyJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fields/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export * from './fields.js';
|
|
2
|
+
export * from './point.js';
|
|
3
|
+
export * from './coordinate.js';
|
|
4
|
+
export * from './grumpkin_scalar.js';
|
|
5
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZmllbGRzL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsYUFBYSxDQUFDO0FBQzVCLGNBQWMsWUFBWSxDQUFDO0FBQzNCLGNBQWMsaUJBQWlCLENBQUM7QUFDaEMsY0FBYyxzQkFBc0IsQ0FBQyJ9
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents a Point on an elliptic curve with x and y coordinates.
|
|
6
|
+
* The Point class provides methods for creating instances from different input types,
|
|
7
|
+
* converting instances to various output formats, and checking the equality of points.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Point {
|
|
10
|
+
/**
|
|
11
|
+
* The point's x coordinate
|
|
12
|
+
*/
|
|
13
|
+
readonly x: Fr;
|
|
14
|
+
/**
|
|
15
|
+
* The point's y coordinate
|
|
16
|
+
*/
|
|
17
|
+
readonly y: Fr;
|
|
18
|
+
static ZERO: Point;
|
|
19
|
+
static SIZE_IN_BYTES: number;
|
|
20
|
+
/** Used to differentiate this class from AztecAddress */
|
|
21
|
+
readonly kind = "point";
|
|
22
|
+
constructor(
|
|
23
|
+
/**
|
|
24
|
+
* The point's x coordinate
|
|
25
|
+
*/
|
|
26
|
+
x: Fr,
|
|
27
|
+
/**
|
|
28
|
+
* The point's y coordinate
|
|
29
|
+
*/
|
|
30
|
+
y: Fr);
|
|
31
|
+
/**
|
|
32
|
+
* Generate a random Point instance.
|
|
33
|
+
*
|
|
34
|
+
* @returns A randomly generated Point instance.
|
|
35
|
+
*/
|
|
36
|
+
static random(): Point;
|
|
37
|
+
/**
|
|
38
|
+
* Create a Point instance from a given buffer or BufferReader.
|
|
39
|
+
* The input 'buffer' should have exactly 64 bytes representing the x and y coordinates.
|
|
40
|
+
*
|
|
41
|
+
* @param buffer - The buffer or BufferReader containing the x and y coordinates of the point.
|
|
42
|
+
* @returns A Point instance.
|
|
43
|
+
*/
|
|
44
|
+
static fromBuffer(buffer: Buffer | BufferReader): Point;
|
|
45
|
+
/**
|
|
46
|
+
* Create a Point instance from a hex-encoded string.
|
|
47
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 128 hex characters representing the x and y coordinates.
|
|
48
|
+
* Throws an error if the input length is invalid or coordinate values are out of range.
|
|
49
|
+
*
|
|
50
|
+
* @param address - The hex-encoded string representing the Point coordinates.
|
|
51
|
+
* @returns A Point instance.
|
|
52
|
+
*/
|
|
53
|
+
static fromString(address: string): Point;
|
|
54
|
+
/**
|
|
55
|
+
* Returns the contents of the point as an array of 2 fields.
|
|
56
|
+
* @returns The point as an array of 2 fields
|
|
57
|
+
*/
|
|
58
|
+
toFields(): Fr[];
|
|
59
|
+
/**
|
|
60
|
+
* Returns the contents of the point as BigInts.
|
|
61
|
+
* @returns The point as BigInts
|
|
62
|
+
*/
|
|
63
|
+
toBigInts(): {
|
|
64
|
+
x: bigint;
|
|
65
|
+
y: bigint;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Converts the Point instance to a Buffer representaion of the coordinates.
|
|
69
|
+
* The outputs buffer length will be 64, the length of both coordinates not represented as fields.
|
|
70
|
+
* @returns A Buffer representation of the Point instance.
|
|
71
|
+
*/
|
|
72
|
+
toBuffer(): Buffer;
|
|
73
|
+
/**
|
|
74
|
+
* Convert the Point instance to a hexadecimal string representation.
|
|
75
|
+
* The output string is prefixed with '0x' and consists of exactly 128 hex characters,
|
|
76
|
+
* representing the concatenated x and y coordinates of the point.
|
|
77
|
+
*
|
|
78
|
+
* @returns A hex-encoded string representing the Point instance.
|
|
79
|
+
*/
|
|
80
|
+
toString(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Generate a short string representation of the Point instance.
|
|
83
|
+
* The returned string includes the first 10 and last 4 characters of the full string representation,
|
|
84
|
+
* with '...' in between to indicate truncation. This is useful for displaying or logging purposes
|
|
85
|
+
* when the full string representation may be too long.
|
|
86
|
+
*
|
|
87
|
+
* @returns A truncated string representation of the Point instance.
|
|
88
|
+
*/
|
|
89
|
+
toShortString(): string;
|
|
90
|
+
/**
|
|
91
|
+
* Check if two Point instances are equal by comparing their buffer values.
|
|
92
|
+
* Returns true if the buffer values are the same, and false otherwise.
|
|
93
|
+
*
|
|
94
|
+
* @param rhs - The Point instance to compare with the current instance.
|
|
95
|
+
* @returns A boolean indicating whether the two Point instances are equal.
|
|
96
|
+
*/
|
|
97
|
+
equals(rhs: Point): boolean;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Does this object look like a point?
|
|
101
|
+
* @param obj - Object to test if it is a point.
|
|
102
|
+
* @returns Whether it looks like a point.
|
|
103
|
+
*/
|
|
104
|
+
export declare function isPoint(obj: object): obj is Point;
|
|
105
|
+
//# sourceMappingURL=point.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"point.d.ts","sourceRoot":"","sources":["../../src/fields/point.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAC7D,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEjC;;;;GAIG;AACH,qBAAa,KAAK;IAQd;;OAEG;aACa,CAAC,EAAE,EAAE;IACrB;;OAEG;aACa,CAAC,EAAE,EAAE;IAdvB,MAAM,CAAC,IAAI,QAA+B;IAC1C,MAAM,CAAC,aAAa,SAAwB;IAE5C,yDAAyD;IACzD,SAAgB,IAAI,WAAW;;IAG7B;;OAEG;IACa,CAAC,EAAE,EAAE;IACrB;;OAEG;IACa,CAAC,EAAE,EAAE;IAGvB;;;;OAIG;IACH,MAAM,CAAC,MAAM;IAKb;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAK/C;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM;IAIjC;;;OAGG;IACH,QAAQ;IAIR;;;OAGG;IACH,SAAS;;;;IAOT;;;;OAIG;IACH,QAAQ;IAIR;;;;;;OAMG;IACH,QAAQ;IAIR;;;;;;;OAOG;IACH,aAAa;IAKb;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,EAAE,KAAK;CAGlB;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,KAAK,CAIjD"}
|