@aztec/foundation 0.0.1-commit.2ed92850 → 0.0.1-commit.2f68f620
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/array/sorted_array.d.ts +6 -1
- package/dest/array/sorted_array.d.ts.map +1 -1
- package/dest/array/sorted_array.js +18 -15
- package/dest/bigint-buffer/index.d.ts +1 -1
- package/dest/bigint-buffer/index.d.ts.map +1 -1
- package/dest/bigint-buffer/index.js +6 -1
- package/dest/branded-types/block_number.d.ts +3 -3
- package/dest/branded-types/block_number.d.ts.map +1 -1
- package/dest/branded-types/block_number.js +15 -4
- package/dest/branded-types/buffer32_hash.d.ts +27 -0
- package/dest/branded-types/buffer32_hash.d.ts.map +1 -0
- package/dest/branded-types/buffer32_hash.js +12 -0
- package/dest/branded-types/checkpoint_number.d.ts +9 -3
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -1
- package/dest/branded-types/checkpoint_number.js +11 -0
- package/dest/branded-types/epoch.d.ts +2 -2
- package/dest/branded-types/epoch.d.ts.map +1 -1
- package/dest/branded-types/index.d.ts +2 -1
- package/dest/branded-types/index.d.ts.map +1 -1
- package/dest/branded-types/index.js +1 -0
- package/dest/branded-types/index_within_checkpoint.d.ts +2 -2
- package/dest/branded-types/index_within_checkpoint.d.ts.map +1 -1
- package/dest/branded-types/slot.d.ts +5 -2
- package/dest/branded-types/slot.d.ts.map +1 -1
- package/dest/branded-types/slot.js +3 -0
- package/dest/buffer/buffer16.d.ts +3 -1
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer32.d.ts +28 -61
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +29 -63
- package/dest/buffer/index.d.ts +2 -1
- package/dest/buffer/index.d.ts.map +1 -1
- package/dest/buffer/index.js +1 -0
- package/dest/buffer/utils.d.ts +3 -0
- package/dest/buffer/utils.d.ts.map +1 -0
- package/dest/buffer/utils.js +7 -0
- package/dest/collection/array.d.ts +17 -1
- package/dest/collection/array.d.ts.map +1 -1
- package/dest/collection/array.js +57 -0
- package/dest/collection/index.d.ts +3 -1
- package/dest/collection/index.d.ts.map +1 -1
- package/dest/collection/index.js +2 -0
- package/dest/collection/lru_map.d.ts +41 -0
- package/dest/collection/lru_map.d.ts.map +1 -0
- package/dest/collection/lru_map.js +119 -0
- package/dest/collection/lru_set.d.ts +35 -0
- package/dest/collection/lru_set.d.ts.map +1 -0
- package/dest/collection/lru_set.js +95 -0
- package/dest/committable/committable.js +1 -1
- package/dest/config/env_var.d.ts +2 -2
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/index.d.ts +47 -30
- package/dest/config/index.d.ts.map +1 -1
- package/dest/config/index.js +44 -44
- package/dest/config/network_config.d.ts +15 -41
- package/dest/config/network_config.d.ts.map +1 -1
- package/dest/config/network_config.js +4 -1
- package/dest/config/network_name.d.ts +2 -2
- package/dest/config/network_name.d.ts.map +1 -1
- package/dest/config/network_name.js +3 -3
- package/dest/config/secret_value.d.ts +2 -2
- package/dest/config/secret_value.d.ts.map +1 -1
- package/dest/crypto/aes128/index.d.ts +2 -1
- package/dest/crypto/aes128/index.d.ts.map +1 -1
- package/dest/crypto/aes128/index.js +11 -2
- package/dest/crypto/bls/bn254_keystore.d.ts +10 -177
- package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
- package/dest/crypto/bls/bn254_keystore.js +22 -34
- package/dest/crypto/poseidon/index.d.ts +1 -1
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +40 -33
- package/dest/crypto/random/randomness_singleton.d.ts +4 -3
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.js +4 -4
- package/dest/crypto/schnorr/index.d.ts +8 -7
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +6 -6
- package/dest/crypto/schnorr/signature.d.ts +9 -29
- package/dest/crypto/schnorr/signature.d.ts.map +1 -1
- package/dest/crypto/schnorr/signature.js +20 -36
- package/dest/crypto/secp256k1-signer/utils.d.ts +12 -1
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
- package/dest/crypto/secp256k1-signer/utils.js +26 -0
- package/dest/curves/bls12/field.d.ts +3 -3
- package/dest/curves/bls12/field.d.ts.map +1 -1
- package/dest/curves/bls12/field.js +0 -5
- package/dest/curves/bls12/point.d.ts +2 -2
- package/dest/curves/bls12/point.d.ts.map +1 -1
- package/dest/curves/bn254/field.d.ts +24 -19
- package/dest/curves/bn254/field.d.ts.map +1 -1
- package/dest/curves/bn254/field.js +28 -19
- package/dest/curves/grumpkin/point.d.ts +14 -28
- package/dest/curves/grumpkin/point.d.ts.map +1 -1
- package/dest/curves/grumpkin/point.js +29 -43
- package/dest/eth-address/index.d.ts +2 -2
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +0 -3
- package/dest/eth-signature/eth_signature.d.ts +3 -2
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/eth-signature/eth_signature.js +7 -2
- package/dest/fifo/fifo_frame_reader.d.ts +41 -0
- package/dest/fifo/fifo_frame_reader.d.ts.map +1 -0
- package/dest/fifo/fifo_frame_reader.js +91 -0
- package/dest/fifo/index.d.ts +2 -0
- package/dest/fifo/index.d.ts.map +1 -0
- package/dest/fifo/index.js +1 -0
- package/dest/fifo_set/fifo_set.d.ts +15 -0
- package/dest/fifo_set/fifo_set.d.ts.map +1 -0
- package/dest/fifo_set/fifo_set.js +39 -0
- package/dest/fifo_set/index.d.ts +2 -0
- package/dest/fifo_set/index.d.ts.map +1 -0
- package/dest/fifo_set/index.js +1 -0
- package/dest/jest/setup.js +28 -1
- package/dest/json-rpc/client/fetch.d.ts +1 -1
- package/dest/json-rpc/client/fetch.d.ts.map +1 -1
- package/dest/json-rpc/client/fetch.js +4 -3
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts +2 -1
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.js +3 -3
- package/dest/json-rpc/client/undici.d.ts +1 -1
- package/dest/json-rpc/client/undici.d.ts.map +1 -1
- package/dest/json-rpc/client/undici.js +5 -3
- package/dest/json-rpc/fixtures/test_state.d.ts +4 -8
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/test_state.js +57 -17
- package/dest/json-rpc/server/api_key_auth.d.ts +19 -0
- package/dest/json-rpc/server/api_key_auth.d.ts.map +1 -0
- package/dest/json-rpc/server/api_key_auth.js +57 -0
- package/dest/json-rpc/server/index.d.ts +2 -1
- package/dest/json-rpc/server/index.d.ts.map +1 -1
- package/dest/json-rpc/server/index.js +1 -0
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts +8 -3
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.js +27 -13
- package/dest/log/bigint-utils.d.ts +5 -0
- package/dest/log/bigint-utils.d.ts.map +1 -0
- package/dest/log/bigint-utils.js +21 -0
- package/dest/log/gcloud-logger-config.d.ts +1 -1
- package/dest/log/gcloud-logger-config.d.ts.map +1 -1
- package/dest/log/gcloud-logger-config.js +3 -0
- package/dest/log/libp2p_logger.d.ts +5 -2
- package/dest/log/libp2p_logger.d.ts.map +1 -1
- package/dest/log/libp2p_logger.js +14 -4
- package/dest/log/log-filters.d.ts +17 -4
- package/dest/log/log-filters.d.ts.map +1 -1
- package/dest/log/log-filters.js +26 -12
- package/dest/log/pino-logger-server.d.ts +9 -0
- package/dest/log/pino-logger-server.d.ts.map +1 -0
- package/dest/log/pino-logger-server.js +18 -0
- package/dest/log/pino-logger.d.ts +37 -8
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +137 -29
- package/dest/noir/noir_package_config.d.ts +19 -104
- package/dest/noir/noir_package_config.d.ts.map +1 -1
- package/dest/noir/noir_package_config.js +1 -1
- package/dest/queue/base_memory_queue.d.ts +2 -2
- package/dest/queue/base_memory_queue.d.ts.map +1 -1
- package/dest/queue/batch_queue.d.ts +1 -1
- package/dest/queue/batch_queue.d.ts.map +1 -1
- package/dest/queue/batch_queue.js +1 -0
- package/dest/queue/semaphore.d.ts +5 -1
- package/dest/queue/semaphore.d.ts.map +1 -1
- package/dest/retry/index.d.ts +12 -1
- package/dest/retry/index.d.ts.map +1 -1
- package/dest/retry/index.js +21 -0
- package/dest/schemas/api.d.ts +12 -10
- package/dest/schemas/api.d.ts.map +1 -1
- package/dest/schemas/api.js +7 -1
- package/dest/schemas/parse.d.ts +4 -4
- package/dest/schemas/parse.d.ts.map +1 -1
- package/dest/schemas/parse.js +6 -5
- package/dest/schemas/schemas.d.ts +15 -15
- package/dest/schemas/types.d.ts +3 -3
- package/dest/schemas/types.d.ts.map +1 -1
- package/dest/schemas/utils.d.ts +7 -11
- package/dest/schemas/utils.d.ts.map +1 -1
- package/dest/schemas/utils.js +2 -18
- package/dest/serialize/buffer_reader.d.ts +8 -1
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +13 -0
- package/dest/serialize/buffer_sink.d.ts +134 -0
- package/dest/serialize/buffer_sink.d.ts.map +1 -0
- package/dest/serialize/buffer_sink.js +297 -0
- package/dest/serialize/index.d.ts +2 -1
- package/dest/serialize/index.d.ts.map +1 -1
- package/dest/serialize/index.js +1 -0
- package/dest/serialize/serialize.d.ts +19 -1
- package/dest/serialize/serialize.d.ts.map +1 -1
- package/dest/serialize/serialize.js +31 -0
- package/dest/sleep/index.d.ts +2 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/sleep/index.js +10 -1
- package/dest/string/index.js +1 -1
- package/dest/timer/date.d.ts +25 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/date.js +33 -0
- package/dest/timer/index.d.ts +2 -2
- package/dest/timer/index.d.ts.map +1 -1
- package/dest/timer/index.js +1 -1
- package/dest/timer/timeout.d.ts +3 -1
- package/dest/timer/timeout.d.ts.map +1 -1
- package/dest/timer/timeout.js +26 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +11 -2
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -1
- package/dest/transport/index.d.ts +1 -2
- package/dest/transport/index.d.ts.map +1 -1
- package/dest/transport/index.js +0 -1
- package/dest/transport/transport_client.js +2 -2
- package/dest/trees/balanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/balanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/balanced_merkle_tree_root.js +2 -3
- package/dest/trees/hasher.d.ts +3 -2
- package/dest/trees/hasher.d.ts.map +1 -1
- package/dest/trees/hasher.js +5 -5
- package/dest/trees/indexed_merkle_tree_calculator.d.ts +1 -1
- package/dest/trees/indexed_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/indexed_merkle_tree_calculator.js +5 -1
- package/dest/trees/membership_witness.d.ts +2 -2
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.d.ts +4 -2
- package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.js +1 -5
- package/dest/trees/sibling_path.d.ts +5 -4
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/sibling_path.js +10 -8
- package/dest/trees/unbalanced_merkle_tree_root.d.ts +2 -3
- package/dest/trees/unbalanced_merkle_tree_root.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_root.js +2 -3
- package/dest/types/index.d.ts +25 -1
- package/dest/types/index.d.ts.map +1 -1
- package/dest/types/index.js +18 -0
- package/package.json +27 -4
- package/src/array/sorted_array.ts +22 -17
- package/src/bigint-buffer/index.ts +6 -1
- package/src/branded-types/block_number.ts +20 -2
- package/src/branded-types/buffer32_hash.ts +38 -0
- package/src/branded-types/checkpoint_number.ts +16 -1
- package/src/branded-types/epoch.ts +1 -1
- package/src/branded-types/index.ts +1 -0
- package/src/branded-types/index_within_checkpoint.ts +1 -1
- package/src/branded-types/slot.ts +6 -1
- package/src/buffer/buffer16.ts +3 -0
- package/src/buffer/buffer32.ts +39 -68
- package/src/buffer/index.ts +1 -0
- package/src/buffer/utils.ts +8 -0
- package/src/collection/array.ts +55 -0
- package/src/collection/index.ts +2 -0
- package/src/collection/lru_map.ts +143 -0
- package/src/collection/lru_set.ts +115 -0
- package/src/committable/committable.ts +1 -1
- package/src/config/env_var.ts +75 -21
- package/src/config/index.ts +116 -96
- package/src/config/network_config.ts +3 -0
- package/src/config/network_name.ts +6 -6
- package/src/config/secret_value.ts +1 -1
- package/src/crypto/aes128/index.ts +11 -2
- package/src/crypto/bls/bn254_keystore.ts +33 -43
- package/src/crypto/poseidon/index.ts +42 -34
- package/src/crypto/random/randomness_singleton.ts +9 -5
- package/src/crypto/schnorr/index.ts +9 -8
- package/src/crypto/schnorr/signature.ts +17 -46
- package/src/crypto/secp256k1-signer/utils.ts +32 -0
- package/src/curves/bls12/field.ts +0 -7
- package/src/curves/bn254/field.ts +41 -36
- package/src/curves/grumpkin/point.ts +23 -40
- package/src/eth-address/index.ts +0 -4
- package/src/eth-signature/eth_signature.ts +7 -1
- package/src/fifo/fifo_frame_reader.ts +113 -0
- package/src/fifo/index.ts +1 -0
- package/src/fifo_set/fifo_set.ts +52 -0
- package/src/fifo_set/index.ts +1 -0
- package/src/jest/setup.mjs +31 -1
- package/src/json-rpc/client/fetch.ts +4 -3
- package/src/json-rpc/client/safe_json_rpc_client.ts +4 -2
- package/src/json-rpc/client/undici.ts +5 -3
- package/src/json-rpc/fixtures/test_state.ts +10 -10
- package/src/json-rpc/server/api_key_auth.ts +63 -0
- package/src/json-rpc/server/index.ts +1 -0
- package/src/json-rpc/server/safe_json_rpc_server.ts +31 -13
- package/src/log/bigint-utils.ts +25 -0
- package/src/log/gcloud-logger-config.ts +5 -0
- package/src/log/libp2p_logger.ts +12 -5
- package/src/log/log-filters.ts +29 -11
- package/src/log/pino-logger-server.ts +25 -0
- package/src/log/pino-logger.ts +160 -39
- package/src/noir/noir_package_config.ts +32 -20
- package/src/queue/base_memory_queue.ts +1 -1
- package/src/queue/batch_queue.ts +1 -0
- package/src/queue/semaphore.ts +5 -0
- package/src/retry/index.ts +30 -0
- package/src/schemas/api.ts +25 -25
- package/src/schemas/parse.ts +9 -6
- package/src/schemas/schemas.ts +4 -4
- package/src/schemas/types.ts +2 -2
- package/src/schemas/utils.ts +4 -38
- package/src/serialize/buffer_reader.ts +15 -0
- package/src/serialize/buffer_sink.ts +350 -0
- package/src/serialize/index.ts +1 -0
- package/src/serialize/serialize.ts +32 -0
- package/src/sleep/index.ts +10 -1
- package/src/string/index.ts +1 -1
- package/src/timer/date.ts +48 -0
- package/src/timer/index.ts +1 -1
- package/src/timer/timeout.ts +31 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +11 -1
- package/src/transport/index.ts +0 -1
- package/src/transport/transport_client.ts +2 -2
- package/src/trees/balanced_merkle_tree_root.ts +2 -5
- package/src/trees/hasher.ts +6 -3
- package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
- package/src/trees/merkle_tree_calculator.ts +4 -10
- package/src/trees/sibling_path.ts +11 -6
- package/src/trees/unbalanced_merkle_tree_root.ts +2 -5
- package/src/types/index.ts +52 -0
- package/dest/crypto/serialize.d.ts +0 -51
- package/dest/crypto/serialize.d.ts.map +0 -1
- package/dest/crypto/serialize.js +0 -68
- package/dest/transport/dispatch/create_dispatch_fn.d.ts +0 -25
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +0 -1
- package/dest/transport/dispatch/create_dispatch_fn.js +0 -17
- package/src/crypto/serialize.ts +0 -85
- package/src/transport/dispatch/create_dispatch_fn.ts +0 -35
|
@@ -43,7 +43,7 @@ export class SecretValue<T> {
|
|
|
43
43
|
/**
|
|
44
44
|
* Returns a Zod schema
|
|
45
45
|
*/
|
|
46
|
-
static schema<O>(valueSchema: ZodType<O, any
|
|
46
|
+
static schema<O>(valueSchema: ZodType<O, any>): ZodType<SecretValue<O>, any> {
|
|
47
47
|
return valueSchema.transform(value => new SecretValue<O>(value));
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -59,10 +59,19 @@ export class Aes128 {
|
|
|
59
59
|
* @param iv - AES initialization vector.
|
|
60
60
|
* @param key - Key to decrypt with.
|
|
61
61
|
* @returns Decrypted data.
|
|
62
|
+
* @throws If the decrypted buffer has invalid PKCS#7 padding.
|
|
62
63
|
*/
|
|
63
64
|
public async decryptBufferCBC(data: Uint8Array, iv: Uint8Array, key: Uint8Array) {
|
|
64
65
|
const paddedBuffer = await this.decryptBufferCBCKeepPadding(data, iv, key);
|
|
65
|
-
const
|
|
66
|
-
|
|
66
|
+
const paddingLen = paddedBuffer[paddedBuffer.length - 1];
|
|
67
|
+
if (paddingLen === 0 || paddingLen > 16) {
|
|
68
|
+
throw new Error(`Invalid PKCS#7 padding length: ${paddingLen}`);
|
|
69
|
+
}
|
|
70
|
+
for (let i = paddedBuffer.length - paddingLen; i < paddedBuffer.length; i++) {
|
|
71
|
+
if (paddedBuffer[i] !== paddingLen) {
|
|
72
|
+
throw new Error('Invalid PKCS#7 padding');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return paddedBuffer.subarray(0, paddedBuffer.length - paddingLen);
|
|
67
76
|
}
|
|
68
77
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
2
2
|
|
|
3
|
-
import { createCipheriv, createDecipheriv, createHash, pbkdf2Sync, randomUUID } from 'crypto';
|
|
3
|
+
import { createCipheriv, createDecipheriv, createHash, pbkdf2, pbkdf2Sync, randomUUID } from 'crypto';
|
|
4
4
|
import { readFileSync } from 'fs';
|
|
5
|
+
import { promisify } from 'util';
|
|
5
6
|
import { z } from 'zod';
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -100,24 +101,15 @@ export interface Bn254KeystoreInterface {
|
|
|
100
101
|
version: number;
|
|
101
102
|
}
|
|
102
103
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
* Uses PBKDF2 with SHA-256 for key derivation and AES-128-CTR for encryption,
|
|
107
|
-
* following the EIP-2335 specification format.
|
|
108
|
-
*
|
|
109
|
-
* @param password - Password for encrypting the private key
|
|
110
|
-
* @param privateKeyHex - Private key as 0x-prefixed hex string (32 bytes)
|
|
111
|
-
* @param pubkeyHex - Public key as hex string (compressed or uncompressed)
|
|
112
|
-
* @param derivationPath - BIP-44 style derivation path (e.g., "m/12381/3600/0/0/0")
|
|
113
|
-
* @returns BN254 keystore object ready to be serialized to JSON
|
|
114
|
-
* @throws Error if private key is not 32-byte hex
|
|
115
|
-
*/
|
|
116
|
-
export function createBn254Keystore(
|
|
117
|
-
password: string,
|
|
104
|
+
const pbkdf2Async = promisify(pbkdf2);
|
|
105
|
+
|
|
106
|
+
function createBn254KeystoreFromDerivedKey(
|
|
118
107
|
privateKeyHex: string,
|
|
119
108
|
pubkeyHex: string,
|
|
120
109
|
derivationPath: string,
|
|
110
|
+
salt: Buffer,
|
|
111
|
+
iv: Buffer,
|
|
112
|
+
dk: Buffer,
|
|
121
113
|
): Bn254Keystore {
|
|
122
114
|
const ensureHex = (hex: string) => hex.replace(/^0x/i, '');
|
|
123
115
|
const privHex = ensureHex(privateKeyHex);
|
|
@@ -125,11 +117,7 @@ export function createBn254Keystore(
|
|
|
125
117
|
throw new Error('BLS private key must be 32-byte hex');
|
|
126
118
|
}
|
|
127
119
|
|
|
128
|
-
const salt = randomBytes(32);
|
|
129
|
-
const iv = randomBytes(16);
|
|
130
|
-
const dk = pbkdf2Sync(Buffer.from(password.normalize('NFKD'), 'utf8'), salt, 262144, 32, 'sha256');
|
|
131
120
|
const cipherKey = dk.subarray(0, 16);
|
|
132
|
-
|
|
133
121
|
const cipher = createCipheriv('aes-128-ctr', cipherKey, iv);
|
|
134
122
|
const plaintext = Buffer.from(privHex, 'hex');
|
|
135
123
|
const ciphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
@@ -166,6 +154,31 @@ export function createBn254Keystore(
|
|
|
166
154
|
};
|
|
167
155
|
}
|
|
168
156
|
|
|
157
|
+
/**
|
|
158
|
+
* Creates a BN254 keystore object for a BN254 BLS private key.
|
|
159
|
+
*
|
|
160
|
+
* Uses PBKDF2 with SHA-256 for key derivation and AES-128-CTR for encryption,
|
|
161
|
+
* following the EIP-2335 specification format.
|
|
162
|
+
*
|
|
163
|
+
* @param password - Password for encrypting the private key
|
|
164
|
+
* @param privateKeyHex - Private key as 0x-prefixed hex string (32 bytes)
|
|
165
|
+
* @param pubkeyHex - Public key as hex string (compressed or uncompressed)
|
|
166
|
+
* @param derivationPath - BIP-44 style derivation path (e.g., "m/12381/3600/0/0/0")
|
|
167
|
+
* @returns BN254 keystore object ready to be serialized to JSON
|
|
168
|
+
* @throws Error if private key is not 32-byte hex
|
|
169
|
+
*/
|
|
170
|
+
export async function createBn254Keystore(
|
|
171
|
+
password: string,
|
|
172
|
+
privateKeyHex: string,
|
|
173
|
+
pubkeyHex: string,
|
|
174
|
+
derivationPath: string,
|
|
175
|
+
): Promise<Bn254Keystore> {
|
|
176
|
+
const salt = randomBytes(32);
|
|
177
|
+
const iv = randomBytes(16);
|
|
178
|
+
const dk = await pbkdf2Async(Buffer.from(password.normalize('NFKD'), 'utf8'), salt, 262144, 32, 'sha256');
|
|
179
|
+
return createBn254KeystoreFromDerivedKey(privateKeyHex, pubkeyHex, derivationPath, salt, iv, dk);
|
|
180
|
+
}
|
|
181
|
+
|
|
169
182
|
/**
|
|
170
183
|
* Loads and validates a BN254 keystore file.
|
|
171
184
|
*
|
|
@@ -262,26 +275,3 @@ export function decryptBn254KeystoreFromObject(keystore: Bn254Keystore, password
|
|
|
262
275
|
throw new Bn254KeystoreError(`Failed to decrypt keystore: ${String(error)}`, error as Error);
|
|
263
276
|
}
|
|
264
277
|
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Validates that a decrypted private key matches the public key in the keystore.
|
|
268
|
-
*
|
|
269
|
-
* @param privateKeyHex - Decrypted private key (0x-prefixed)
|
|
270
|
-
* @param expectedPubkey - Expected public key from keystore
|
|
271
|
-
* @param computePublicKey - Function to compute public key from private key
|
|
272
|
-
* @returns true if keys match, false otherwise
|
|
273
|
-
*/
|
|
274
|
-
export function verifyBn254Keypair(
|
|
275
|
-
privateKeyHex: string,
|
|
276
|
-
expectedPubkey: string,
|
|
277
|
-
computePublicKey: (privateKey: string) => string,
|
|
278
|
-
): boolean {
|
|
279
|
-
try {
|
|
280
|
-
const computedPubkey = computePublicKey(privateKeyHex);
|
|
281
|
-
const normalizedExpected = expectedPubkey.toLowerCase().replace(/^0x/i, '');
|
|
282
|
-
const normalizedComputed = computedPubkey.toLowerCase().replace(/^0x/i, '');
|
|
283
|
-
return normalizedExpected === normalizedComputed;
|
|
284
|
-
} catch {
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
import { BarretenbergSync } from '@aztec/bb.js';
|
|
1
|
+
import { Barretenberg, BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../curves/bn254/field.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
|
|
5
5
|
|
|
6
|
+
const IS_BROWSER = typeof self !== 'undefined';
|
|
7
|
+
|
|
8
|
+
async function poseidon2HashFields(inputFields: Fr[]): Promise<Fr> {
|
|
9
|
+
if (IS_BROWSER) {
|
|
10
|
+
await BarretenbergSync.initSingleton();
|
|
11
|
+
const api = BarretenbergSync.getSingleton();
|
|
12
|
+
const response = api.poseidon2Hash({
|
|
13
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
14
|
+
});
|
|
15
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
16
|
+
} else {
|
|
17
|
+
await Barretenberg.initSingleton();
|
|
18
|
+
const api = Barretenberg.getSingleton();
|
|
19
|
+
const response = await api.poseidon2Hash({
|
|
20
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
21
|
+
});
|
|
22
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
6
26
|
/**
|
|
7
27
|
* Create a poseidon hash (field) from an array of input fields.
|
|
8
28
|
* @param input - The input fields to hash.
|
|
9
29
|
* @returns The poseidon hash.
|
|
10
30
|
*/
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
await BarretenbergSync.initSingleton();
|
|
14
|
-
const api = BarretenbergSync.getSingleton();
|
|
15
|
-
const response = api.poseidon2Hash({
|
|
16
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
17
|
-
});
|
|
18
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
31
|
+
export function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
32
|
+
return poseidon2HashFields(serializeToFields(input));
|
|
19
33
|
}
|
|
20
34
|
|
|
21
35
|
/**
|
|
@@ -24,15 +38,10 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
|
24
38
|
* @param separator - The domain separator.
|
|
25
39
|
* @returns The poseidon hash.
|
|
26
40
|
*/
|
|
27
|
-
export
|
|
41
|
+
export function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
28
42
|
const inputFields = serializeToFields(input);
|
|
29
43
|
inputFields.unshift(new Fr(separator));
|
|
30
|
-
|
|
31
|
-
const api = BarretenbergSync.getSingleton();
|
|
32
|
-
const response = api.poseidon2Hash({
|
|
33
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
34
|
-
});
|
|
35
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
44
|
+
return poseidon2HashFields(inputFields);
|
|
36
45
|
}
|
|
37
46
|
|
|
38
47
|
/**
|
|
@@ -42,19 +51,24 @@ export async function poseidon2HashWithSeparator(input: Fieldable[], separator:
|
|
|
42
51
|
*/
|
|
43
52
|
export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
44
53
|
const inputFields = serializeToFields(input);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
if (IS_BROWSER) {
|
|
55
|
+
await BarretenbergSync.initSingleton();
|
|
56
|
+
const api = BarretenbergSync.getSingleton();
|
|
57
|
+
const response = api.poseidon2Permutation({
|
|
58
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
59
|
+
});
|
|
60
|
+
return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
|
|
61
|
+
} else {
|
|
62
|
+
await Barretenberg.initSingleton();
|
|
63
|
+
const api = Barretenberg.getSingleton();
|
|
64
|
+
const response = await api.poseidon2Permutation({
|
|
65
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
66
|
+
});
|
|
67
|
+
return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
|
|
68
|
+
}
|
|
55
69
|
}
|
|
56
70
|
|
|
57
|
-
export
|
|
71
|
+
export function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
58
72
|
const inputFields = [];
|
|
59
73
|
for (let i = 0; i < input.length; i += 31) {
|
|
60
74
|
const fieldBytes = Buffer.alloc(32, 0);
|
|
@@ -65,11 +79,5 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
|
65
79
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
66
80
|
}
|
|
67
81
|
|
|
68
|
-
|
|
69
|
-
const api = BarretenbergSync.getSingleton();
|
|
70
|
-
const response = api.poseidon2Hash({
|
|
71
|
-
inputs: inputFields.map(i => i.toBuffer()),
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
82
|
+
return poseidon2HashFields(inputFields);
|
|
75
83
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createLogger } from '../../log/pino-logger.js';
|
|
1
|
+
import { type Logger, type LoggerBindings, createLogger } from '../../log/pino-logger.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A number generator which is used as a source of randomness in the system. If the SEED env variable is set, the
|
|
@@ -12,9 +12,13 @@ export class RandomnessSingleton {
|
|
|
12
12
|
private static instance: RandomnessSingleton;
|
|
13
13
|
|
|
14
14
|
private counter = 0;
|
|
15
|
-
private
|
|
15
|
+
private log: Logger;
|
|
16
16
|
|
|
17
|
-
private constructor(
|
|
17
|
+
private constructor(
|
|
18
|
+
private readonly seed?: number,
|
|
19
|
+
bindings?: LoggerBindings,
|
|
20
|
+
) {
|
|
21
|
+
this.log = createLogger('foundation:randomness_singleton', bindings);
|
|
18
22
|
if (seed !== undefined) {
|
|
19
23
|
this.log.debug(`Using pseudo-randomness with seed: ${seed}`);
|
|
20
24
|
this.counter = seed;
|
|
@@ -23,10 +27,10 @@ export class RandomnessSingleton {
|
|
|
23
27
|
}
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
public static getInstance(): RandomnessSingleton {
|
|
30
|
+
public static getInstance(bindings?: LoggerBindings): RandomnessSingleton {
|
|
27
31
|
if (!RandomnessSingleton.instance) {
|
|
28
32
|
const seed = process.env.SEED ? Number(process.env.SEED) : undefined;
|
|
29
|
-
RandomnessSingleton.instance = new RandomnessSingleton(seed);
|
|
33
|
+
RandomnessSingleton.instance = new RandomnessSingleton(seed, bindings);
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
return RandomnessSingleton.instance;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
|
+
import type { Fr } from '@aztec/foundation/curves/bn254';
|
|
2
3
|
import type { GrumpkinScalar } from '@aztec/foundation/curves/grumpkin';
|
|
3
4
|
import { Point } from '@aztec/foundation/curves/grumpkin';
|
|
4
5
|
|
|
@@ -23,33 +24,33 @@ export class Schnorr {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
|
-
* Constructs a Schnorr signature
|
|
27
|
-
* @param msg -
|
|
27
|
+
* Constructs a Schnorr signature over a 32-byte message field element.
|
|
28
|
+
* @param msg - The message hash, as a grumpkin base field element.
|
|
28
29
|
* @param privateKey - The private key of the signer.
|
|
29
30
|
* @returns A Schnorr signature of the form (s, e).
|
|
30
31
|
*/
|
|
31
|
-
public async constructSignature(msg:
|
|
32
|
+
public async constructSignature(msg: Fr, privateKey: GrumpkinScalar) {
|
|
32
33
|
await BarretenbergSync.initSingleton();
|
|
33
34
|
const api = BarretenbergSync.getSingleton();
|
|
34
35
|
const response = api.schnorrConstructSignature({
|
|
35
|
-
|
|
36
|
+
messageField: msg.toBuffer(),
|
|
36
37
|
privateKey: privateKey.toBuffer(),
|
|
37
38
|
});
|
|
38
39
|
return new SchnorrSignature(Buffer.from([...response.s, ...response.e]));
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
|
-
* Verifies a Schnorr signature
|
|
43
|
-
* @param msg -
|
|
43
|
+
* Verifies a Schnorr signature against a Grumpkin public key.
|
|
44
|
+
* @param msg - The message hash, as a grumpkin base field element.
|
|
44
45
|
* @param pubKey - The Grumpkin public key of the signer.
|
|
45
46
|
* @param sig - The Schnorr signature.
|
|
46
47
|
* @returns True or false.
|
|
47
48
|
*/
|
|
48
|
-
public async verifySignature(msg:
|
|
49
|
+
public async verifySignature(msg: Fr, pubKey: Point, sig: SchnorrSignature) {
|
|
49
50
|
await BarretenbergSync.initSingleton();
|
|
50
51
|
const api = BarretenbergSync.getSingleton();
|
|
51
52
|
const response = api.schnorrVerifySignature({
|
|
52
|
-
|
|
53
|
+
messageField: msg.toBuffer(),
|
|
53
54
|
publicKey: { x: pubKey.x.toBuffer(), y: pubKey.y.toBuffer() },
|
|
54
55
|
s: sig.s,
|
|
55
56
|
e: sig.e,
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { randomBytes } from '@aztec/foundation/crypto/random';
|
|
2
1
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
3
|
-
import {
|
|
2
|
+
import { mapTuple } from '@aztec/foundation/serialize';
|
|
4
3
|
|
|
5
4
|
import type { Signature } from '../signature/index.js';
|
|
6
5
|
|
|
@@ -14,46 +13,12 @@ export class SchnorrSignature implements Signature {
|
|
|
14
13
|
*/
|
|
15
14
|
public static SIZE = 64;
|
|
16
15
|
|
|
17
|
-
/**
|
|
18
|
-
* An empty signature.
|
|
19
|
-
*/
|
|
20
|
-
public static EMPTY = new SchnorrSignature(Buffer.alloc(64));
|
|
21
|
-
|
|
22
16
|
constructor(private buffer: Buffer) {
|
|
23
17
|
if (buffer.length !== SchnorrSignature.SIZE) {
|
|
24
18
|
throw new Error(`Invalid signature buffer of length ${buffer.length}.`);
|
|
25
19
|
}
|
|
26
20
|
}
|
|
27
21
|
|
|
28
|
-
/**
|
|
29
|
-
* Determines if the provided signature is valid or not.
|
|
30
|
-
* @param signature - The data to be checked.
|
|
31
|
-
* @returns Boolean indicating if the provided data is a valid schnorr signature.
|
|
32
|
-
*/
|
|
33
|
-
public static isSignature(signature: string) {
|
|
34
|
-
return /^(0x)?[0-9a-f]{128}$/i.test(signature);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Constructs a SchnorrSignature from the provided string.
|
|
39
|
-
* @param signature - The string to be converted to a schnorr signature.
|
|
40
|
-
* @returns The constructed schnorr signature.
|
|
41
|
-
*/
|
|
42
|
-
public static fromString(signature: string) {
|
|
43
|
-
if (!SchnorrSignature.isSignature(signature)) {
|
|
44
|
-
throw new Error(`Invalid signature string: ${signature}`);
|
|
45
|
-
}
|
|
46
|
-
return new SchnorrSignature(Buffer.from(signature.replace(/^0x/i, ''), 'hex'));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Generates a random schnorr signature.
|
|
51
|
-
* @returns The randomly constructed signature.
|
|
52
|
-
*/
|
|
53
|
-
public static random() {
|
|
54
|
-
return new SchnorrSignature(randomBytes(64));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
22
|
/**
|
|
58
23
|
* Returns the 's' component of the signature.
|
|
59
24
|
* @returns A buffer containing the signature's 's' component.
|
|
@@ -78,16 +43,6 @@ export class SchnorrSignature implements Signature {
|
|
|
78
43
|
return this.buffer;
|
|
79
44
|
}
|
|
80
45
|
|
|
81
|
-
/**
|
|
82
|
-
* Deserializes from a buffer.
|
|
83
|
-
* @param buffer - The buffer representation of the object.
|
|
84
|
-
* @returns The new object.
|
|
85
|
-
*/
|
|
86
|
-
static fromBuffer(buffer: Buffer | BufferReader): SchnorrSignature {
|
|
87
|
-
const reader = BufferReader.asReader(buffer);
|
|
88
|
-
return new SchnorrSignature(reader.readBytes(SchnorrSignature.SIZE));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
46
|
/**
|
|
92
47
|
* Returns the full signature as a hex string.
|
|
93
48
|
* @returns A string containing the signature in hex format.
|
|
@@ -113,4 +68,20 @@ export class SchnorrSignature implements Signature {
|
|
|
113
68
|
|
|
114
69
|
return mapTuple([buf1, buf2, buf3], Fr.fromBuffer);
|
|
115
70
|
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Splits the signature into the four 128-bit limbs that Noir's `EmbeddedCurveScalar` consumes:
|
|
74
|
+
* `[s.lo, s.hi, e.lo, e.hi]`, where each component scalar is encoded as `lo + hi * 2^128`.
|
|
75
|
+
*
|
|
76
|
+
* Each 32-byte big-endian component is sliced into its top 16 bytes (`hi`) and bottom 16 bytes
|
|
77
|
+
* (`lo`); each half is zero-padded into a 32-byte buffer and decoded as `Fr` (big-endian).
|
|
78
|
+
*/
|
|
79
|
+
toLimbFields(): [Fr, Fr, Fr, Fr] {
|
|
80
|
+
const limb = (start: number) => {
|
|
81
|
+
const buf = Buffer.alloc(32);
|
|
82
|
+
this.buffer.copy(buf, 16, start, start + 16);
|
|
83
|
+
return Fr.fromBuffer(buf);
|
|
84
|
+
};
|
|
85
|
+
return [limb(16), limb(0), limb(48), limb(32)];
|
|
86
|
+
}
|
|
116
87
|
}
|
|
@@ -210,3 +210,35 @@ export function recoverPublicKey(hash: Buffer32, signature: Signature, opts: Rec
|
|
|
210
210
|
const publicKey = sig.recoverPublicKey(hash.buffer).toHex(false);
|
|
211
211
|
return Buffer.from(publicKey, 'hex');
|
|
212
212
|
}
|
|
213
|
+
|
|
214
|
+
/** Arbitrary hash used for testing signature recoverability. */
|
|
215
|
+
const PROBE_HASH = Buffer32.fromBuffer(keccak256(Buffer.from('signature-recoverability-probe')));
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Generates a random valid ECDSA signature that is recoverable to some address.
|
|
219
|
+
* Since Signature.random() produces real signatures via secp256k1 signing, the result is always
|
|
220
|
+
* recoverable, but we verify defensively by checking tryRecoverAddress.
|
|
221
|
+
*/
|
|
222
|
+
export function generateRecoverableSignature(): Signature {
|
|
223
|
+
for (let i = 0; i < 100; i++) {
|
|
224
|
+
const sig = Signature.random();
|
|
225
|
+
if (tryRecoverAddress(PROBE_HASH, sig) !== undefined) {
|
|
226
|
+
return sig;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
throw new Secp256k1Error('Failed to generate a recoverable signature after 100 attempts');
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Generates a random signature where ECDSA address recovery fails.
|
|
234
|
+
* Uses random r/s values (not from real signing) so that r is unlikely to be a valid secp256k1 x-coordinate.
|
|
235
|
+
*/
|
|
236
|
+
export function generateUnrecoverableSignature(): Signature {
|
|
237
|
+
for (let i = 0; i < 100; i++) {
|
|
238
|
+
const sig = new Signature(Buffer32.random(), Buffer32.random(), 27);
|
|
239
|
+
if (tryRecoverAddress(PROBE_HASH, sig) === undefined) {
|
|
240
|
+
return sig;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
throw new Secp256k1Error('Failed to generate an unrecoverable signature after 100 attempts');
|
|
244
|
+
}
|
|
@@ -6,7 +6,6 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
|
|
|
6
6
|
import { randomBytes } from '../../crypto/random/index.js';
|
|
7
7
|
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
8
8
|
import { BufferReader } from '../../serialize/buffer_reader.js';
|
|
9
|
-
import { TypeRegistry } from '../../serialize/type_registry.js';
|
|
10
9
|
import { Fr } from '../bn254/field.js';
|
|
11
10
|
|
|
12
11
|
/**
|
|
@@ -323,9 +322,6 @@ export class BLS12Fr extends BLS12Field {
|
|
|
323
322
|
}
|
|
324
323
|
}
|
|
325
324
|
|
|
326
|
-
// For deserializing JSON.
|
|
327
|
-
TypeRegistry.register('BLS12Fr', BLS12Fr);
|
|
328
|
-
|
|
329
325
|
/**
|
|
330
326
|
* Fq field class.
|
|
331
327
|
* @dev This class is used to represent elements of BLS12-381 base field.
|
|
@@ -458,6 +454,3 @@ export class BLS12Fq extends BLS12Field {
|
|
|
458
454
|
return hexSchemaFor(BLS12Fq);
|
|
459
455
|
}
|
|
460
456
|
}
|
|
461
|
-
|
|
462
|
-
// For deserializing JSON.
|
|
463
|
-
TypeRegistry.register('BLS12Fq', BLS12Fq);
|
|
@@ -6,9 +6,7 @@ import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
|
|
|
6
6
|
import { randomBytes } from '../../crypto/random/index.js';
|
|
7
7
|
import { hexSchemaFor } from '../../schemas/utils.js';
|
|
8
8
|
import { BufferReader } from '../../serialize/buffer_reader.js';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
/* eslint-disable @typescript-eslint/no-unsafe-declaration-merging */
|
|
9
|
+
import type { BufferSink } from '../../serialize/buffer_sink.js';
|
|
12
10
|
|
|
13
11
|
/**
|
|
14
12
|
* Represents a field derived from BaseField.
|
|
@@ -67,10 +65,16 @@ abstract class BaseField {
|
|
|
67
65
|
protected abstract modulus(): bigint;
|
|
68
66
|
|
|
69
67
|
/**
|
|
70
|
-
* Converts the bigint to a Buffer.
|
|
68
|
+
* Converts the bigint to a Buffer. With a sink, streams the 32 big-endian bytes straight in (no allocation)
|
|
69
|
+
* and returns undefined; without one, returns a freshly allocated buffer.
|
|
71
70
|
*/
|
|
72
|
-
toBuffer(): Buffer
|
|
73
|
-
|
|
71
|
+
toBuffer(): Buffer;
|
|
72
|
+
toBuffer(sink: BufferSink): void;
|
|
73
|
+
toBuffer(sink?: BufferSink): Buffer | void {
|
|
74
|
+
if (!sink) {
|
|
75
|
+
return toBufferBE(this.asBigInt, BaseField.SIZE_IN_BYTES);
|
|
76
|
+
}
|
|
77
|
+
sink.writeField(this.asBigInt);
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
toString(): `0x${string}` {
|
|
@@ -118,14 +122,18 @@ abstract class BaseField {
|
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
cmp(rhs: BaseField): -1 | 0 | 1 {
|
|
121
|
-
|
|
122
|
-
return this.asBigInt === rhsBigInt ? 0 : this.asBigInt < rhsBigInt ? -1 : 1;
|
|
125
|
+
return BaseField.cmpAsBigInt(this.asBigInt, rhs.asBigInt);
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
static cmp(lhs: BaseField, rhs: BaseField): -1 | 0 | 1 {
|
|
126
129
|
return lhs.cmp(rhs);
|
|
127
130
|
}
|
|
128
131
|
|
|
132
|
+
// Actual bigint comparison. Arguments must have been validated previously.
|
|
133
|
+
static cmpAsBigInt(lhs: bigint, rhs: bigint): -1 | 0 | 1 {
|
|
134
|
+
return lhs === rhs ? 0 : lhs < rhs ? -1 : 1;
|
|
135
|
+
}
|
|
136
|
+
|
|
129
137
|
isZero(): boolean {
|
|
130
138
|
return this.asBigInt === 0n;
|
|
131
139
|
}
|
|
@@ -183,10 +191,25 @@ function fromHexString<T extends BaseField>(buf: string, f: DerivedField<T>) {
|
|
|
183
191
|
return new f(toBigIntBE(buffer));
|
|
184
192
|
}
|
|
185
193
|
|
|
186
|
-
/**
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Abstract unbranded base class for BN254 scalar field elements.
|
|
196
|
+
* Extend this instead of Fr when defining a branded subtype (e.g. BlockHash)
|
|
197
|
+
* to avoid inheriting Fr's `_branding`.
|
|
198
|
+
*/
|
|
199
|
+
export abstract class BaseFr extends BaseField {
|
|
200
|
+
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
201
|
+
|
|
202
|
+
constructor(value: number | bigint | boolean | BaseField | Buffer) {
|
|
203
|
+
super(value);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
protected modulus() {
|
|
207
|
+
return BaseFr.MODULUS;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
toJSON() {
|
|
211
|
+
return this.toString();
|
|
212
|
+
}
|
|
190
213
|
}
|
|
191
214
|
|
|
192
215
|
/**
|
|
@@ -194,10 +217,12 @@ export interface Fr {
|
|
|
194
217
|
* @dev This class is used to represent elements of BN254 scalar field or elements in the base field of Grumpkin.
|
|
195
218
|
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
196
219
|
*/
|
|
197
|
-
export class Fr extends
|
|
220
|
+
export class Fr extends BaseFr {
|
|
221
|
+
/** Branding for nominal typing. */
|
|
222
|
+
declare private readonly _branding: 'Fr';
|
|
223
|
+
static override MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
198
224
|
static ZERO = new Fr(0n);
|
|
199
225
|
static ONE = new Fr(1n);
|
|
200
|
-
static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
|
|
201
226
|
static MAX_FIELD_VALUE = new Fr(this.MODULUS - 1n);
|
|
202
227
|
|
|
203
228
|
constructor(value: number | bigint | boolean | Fr | Buffer) {
|
|
@@ -208,10 +233,6 @@ export class Fr extends BaseField {
|
|
|
208
233
|
return `Fr<${this.toString()}>`;
|
|
209
234
|
}
|
|
210
235
|
|
|
211
|
-
protected modulus() {
|
|
212
|
-
return Fr.MODULUS;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
236
|
static random() {
|
|
216
237
|
return random(Fr);
|
|
217
238
|
}
|
|
@@ -316,10 +337,6 @@ export class Fr extends BaseField {
|
|
|
316
337
|
return Fr.fromBuffer(Buffer.from(response.value));
|
|
317
338
|
}
|
|
318
339
|
|
|
319
|
-
toJSON() {
|
|
320
|
-
return this.toString();
|
|
321
|
-
}
|
|
322
|
-
|
|
323
340
|
/**
|
|
324
341
|
* Creates an Fr instance from a plain object without Zod validation.
|
|
325
342
|
* This method is optimized for performance and skips validation, making it suitable
|
|
@@ -341,23 +358,14 @@ export class Fr extends BaseField {
|
|
|
341
358
|
}
|
|
342
359
|
}
|
|
343
360
|
|
|
344
|
-
// For deserializing JSON.
|
|
345
|
-
TypeRegistry.register('Fr', Fr);
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Branding to ensure fields are not interchangeable types.
|
|
349
|
-
*/
|
|
350
|
-
export interface Fq {
|
|
351
|
-
/** Brand. */
|
|
352
|
-
_branding: 'Fq';
|
|
353
|
-
}
|
|
354
|
-
|
|
355
361
|
/**
|
|
356
362
|
* Fq field class.
|
|
357
363
|
* @dev This class is used to represent elements of BN254 base field or elements in the scalar field of Grumpkin.
|
|
358
364
|
* (Grumpkin's scalar field corresponds to BN254's base field and vice versa.)
|
|
359
365
|
*/
|
|
360
366
|
export class Fq extends BaseField {
|
|
367
|
+
/** Branding for nominal typing. */
|
|
368
|
+
declare private readonly _branding: 'Fq';
|
|
361
369
|
static ZERO = new Fq(0n);
|
|
362
370
|
static MODULUS = 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n;
|
|
363
371
|
private static HIGH_SHIFT = BigInt((BaseField.SIZE_IN_BYTES / 2) * 8);
|
|
@@ -466,9 +474,6 @@ export class Fq extends BaseField {
|
|
|
466
474
|
}
|
|
467
475
|
}
|
|
468
476
|
|
|
469
|
-
// For deserializing JSON.
|
|
470
|
-
TypeRegistry.register('Fq', Fq);
|
|
471
|
-
|
|
472
477
|
// Beware: Performance bottleneck below
|
|
473
478
|
|
|
474
479
|
/**
|