@aztec/foundation 0.0.1-commit.b655e406 → 0.0.1-commit.d3ec352c
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/array.d.ts +1 -9
- package/dest/array/array.d.ts.map +1 -1
- package/dest/array/array.js +0 -11
- package/dest/array/index.d.ts +1 -1
- package/dest/async-map/index.d.ts +1 -1
- package/dest/async-pool/index.d.ts +1 -1
- package/dest/bigint/index.d.ts +1 -1
- package/dest/bigint-buffer/index.d.ts +1 -1
- package/dest/branded-types/block_number.d.ts +53 -0
- package/dest/branded-types/block_number.d.ts.map +1 -0
- package/dest/branded-types/block_number.js +75 -0
- package/dest/branded-types/checkpoint_number.d.ts +46 -0
- package/dest/branded-types/checkpoint_number.d.ts.map +1 -0
- package/dest/branded-types/checkpoint_number.js +68 -0
- package/dest/branded-types/epoch.d.ts +42 -0
- package/dest/branded-types/epoch.d.ts.map +1 -0
- package/dest/branded-types/epoch.js +59 -0
- package/dest/branded-types/index.d.ts +6 -0
- package/dest/branded-types/index.d.ts.map +1 -0
- package/dest/branded-types/index.js +4 -0
- package/dest/branded-types/slot.d.ts +42 -0
- package/dest/branded-types/slot.d.ts.map +1 -0
- package/dest/branded-types/slot.js +59 -0
- package/dest/branded-types/types.d.ts +5 -0
- package/dest/branded-types/types.d.ts.map +1 -0
- package/dest/branded-types/types.js +1 -0
- package/dest/buffer/buffer16.d.ts +1 -4
- package/dest/buffer/buffer16.d.ts.map +1 -1
- package/dest/buffer/buffer32.d.ts +1 -4
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/index.d.ts +1 -1
- package/dest/collection/array.d.ts +1 -1
- package/dest/collection/index.d.ts +1 -1
- package/dest/collection/object.d.ts +1 -1
- package/dest/committable/committable.d.ts +1 -1
- package/dest/committable/committable.d.ts.map +1 -1
- package/dest/committable/index.d.ts +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 +1 -1
- package/dest/config/network_config.d.ts +1 -1
- package/dest/config/network_name.d.ts +1 -1
- package/dest/config/secret_value.d.ts +1 -1
- package/dest/config/secret_value.d.ts.map +1 -1
- package/dest/crypto/aes128/index.d.ts +1 -1
- package/dest/crypto/aes128/index.d.ts.map +1 -1
- package/dest/crypto/aes128/index.js +2 -6
- package/dest/crypto/bls/bn254_keystore.d.ts +15 -15
- package/dest/crypto/bls/bn254_keystore.d.ts.map +1 -1
- package/dest/crypto/bls/index.d.ts +1 -1
- package/dest/crypto/bn254/bn254_utils.d.ts +1 -1
- package/dest/crypto/bn254/index.d.ts +1 -1
- package/dest/crypto/bn254/index.d.ts.map +1 -1
- package/dest/crypto/bn254/index.js +3 -0
- package/dest/crypto/ecdsa/index.d.ts +1 -1
- package/dest/crypto/ecdsa/index.d.ts.map +1 -1
- package/dest/crypto/ecdsa/index.js +4 -12
- package/dest/crypto/ecdsa/signature.d.ts +1 -10
- package/dest/crypto/ecdsa/signature.d.ts.map +1 -1
- package/dest/crypto/grumpkin/index.d.ts +1 -1
- package/dest/crypto/grumpkin/index.d.ts.map +1 -1
- package/dest/crypto/grumpkin/index.js +5 -15
- package/dest/crypto/index.d.ts +1 -1
- package/dest/crypto/keccak/index.d.ts +1 -1
- package/dest/crypto/keys/index.d.ts +1 -1
- package/dest/crypto/keys/index.js +1 -3
- package/dest/crypto/pedersen/index.d.ts +1 -1
- package/dest/crypto/pedersen/pedersen.noble.d.ts +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.d.ts +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +3 -9
- package/dest/crypto/poseidon/index.d.ts +1 -1
- package/dest/crypto/poseidon/index.js +5 -15
- package/dest/crypto/random/index.d.ts +1 -1
- package/dest/crypto/random/index.d.ts.map +1 -1
- package/dest/crypto/random/randomness_singleton.d.ts +1 -1
- package/dest/crypto/random/randomness_singleton.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.d.ts +1 -1
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +3 -9
- package/dest/crypto/schnorr/signature.d.ts +1 -1
- package/dest/crypto/schnorr/signature.d.ts.map +1 -1
- package/dest/crypto/secp256k1/index.d.ts +1 -1
- package/dest/crypto/secp256k1/index.d.ts.map +1 -1
- package/dest/crypto/secp256k1/index.js +3 -9
- package/dest/crypto/secp256k1-signer/index.d.ts +1 -1
- package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts +1 -1
- package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts.map +1 -1
- package/dest/crypto/secp256k1-signer/utils.d.ts +1 -1
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
- package/dest/crypto/serialize.d.ts +1 -1
- package/dest/crypto/sha256/index.d.ts +1 -1
- package/dest/crypto/sha256/index.d.ts.map +1 -1
- package/dest/crypto/sha512/index.d.ts +2 -2
- package/dest/crypto/sha512/index.d.ts.map +1 -1
- package/dest/crypto/signature/index.d.ts +1 -1
- package/dest/crypto/sync/index.d.ts +1 -1
- package/dest/crypto/sync/index.js +1 -3
- package/dest/crypto/sync/pedersen/index.d.ts +1 -1
- package/dest/crypto/sync/poseidon/index.d.ts +1 -1
- package/dest/decorators/index.d.ts +1 -1
- package/dest/decorators/memoize.d.ts +1 -1
- package/dest/decorators/memoize.d.ts.map +1 -1
- package/dest/error/index.d.ts +4 -4
- package/dest/error/index.d.ts.map +1 -1
- package/dest/eth-address/index.d.ts +10 -1
- package/dest/eth-address/index.d.ts.map +1 -1
- package/dest/eth-address/index.js +17 -0
- package/dest/eth-signature/eth_signature.d.ts +1 -4
- package/dest/eth-signature/eth_signature.d.ts.map +1 -1
- package/dest/eth-signature/index.d.ts +1 -1
- package/dest/fields/bls12_fields.d.ts +1 -1
- package/dest/fields/bls12_fields.d.ts.map +1 -1
- package/dest/fields/bls12_point.d.ts +1 -10
- package/dest/fields/bls12_point.d.ts.map +1 -1
- package/dest/fields/coordinate.d.ts +1 -4
- package/dest/fields/coordinate.d.ts.map +1 -1
- package/dest/fields/fields.d.ts +10 -1
- package/dest/fields/fields.d.ts.map +1 -1
- package/dest/fields/fields.js +14 -0
- package/dest/fields/index.d.ts +1 -1
- package/dest/fields/point.d.ts +11 -11
- package/dest/fields/point.d.ts.map +1 -1
- package/dest/fields/point.js +17 -0
- package/dest/fs/index.d.ts +1 -1
- package/dest/fs/run_in_dir.d.ts +1 -1
- package/dest/fs/try_rm_dir.d.ts +1 -1
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/iterable/all.d.ts +1 -1
- package/dest/iterable/filter.d.ts +1 -1
- package/dest/iterable/index.d.ts +1 -1
- package/dest/iterable/isAsyncIt.d.ts +1 -1
- package/dest/iterable/map.d.ts +1 -1
- package/dest/iterable/peek.d.ts +1 -1
- package/dest/iterable/sort.d.ts +1 -1
- package/dest/iterable/take.d.ts +1 -1
- package/dest/iterable/toArray.d.ts +1 -1
- package/dest/json-rpc/client/fetch.d.ts +1 -1
- package/dest/json-rpc/client/index.d.ts +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts +1 -1
- package/dest/json-rpc/client/undici.d.ts +1 -1
- package/dest/json-rpc/convert.d.ts +1 -1
- package/dest/json-rpc/errors.d.ts +1 -1
- package/dest/json-rpc/errors.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_a.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/class_b.d.ts +3 -3
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -1
- package/dest/json-rpc/fixtures/test_state.d.ts +1 -1
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -1
- package/dest/json-rpc/index.d.ts +1 -1
- package/dest/json-rpc/js_utils.d.ts +1 -1
- package/dest/json-rpc/js_utils.d.ts.map +1 -1
- package/dest/json-rpc/server/index.d.ts +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts +1 -1
- package/dest/json-rpc/server/safe_json_rpc_server.d.ts.map +1 -1
- package/dest/json-rpc/server/telemetry.d.ts +1 -1
- package/dest/json-rpc/test/index.d.ts +1 -1
- package/dest/json-rpc/test/integration.d.ts +1 -1
- package/dest/log/console.d.ts +1 -1
- package/dest/log/gcloud-logger-config.d.ts +1 -1
- package/dest/log/gcloud-logger-config.d.ts.map +1 -1
- package/dest/log/index.d.ts +1 -1
- package/dest/log/libp2p_logger.d.ts +1 -1
- package/dest/log/log-filters.d.ts +1 -1
- package/dest/log/log-levels.d.ts +1 -1
- package/dest/log/log_fn.d.ts +1 -1
- package/dest/log/noir_debug_log_util.d.ts +1 -1
- package/dest/log/pino-logger.d.ts +2 -2
- package/dest/message/index.d.ts +1 -1
- package/dest/message/index.d.ts.map +1 -1
- package/dest/mutex/index.d.ts +1 -1
- package/dest/mutex/index.d.ts.map +1 -1
- package/dest/mutex/mutex_database.d.ts +1 -1
- package/dest/noir/index.d.ts +1 -1
- package/dest/noir/noir_package_config.d.ts +9 -9
- package/dest/number/index.d.ts +1 -1
- package/dest/profiler/index.d.ts +1 -1
- package/dest/profiler/profiler.d.ts +1 -1
- package/dest/promise/index.d.ts +1 -1
- package/dest/promise/running-promise.d.ts +1 -1
- package/dest/promise/running-promise.d.ts.map +1 -1
- package/dest/promise/utils.d.ts +1 -1
- package/dest/queue/base_memory_queue.d.ts +1 -1
- 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/bounded_serial_queue.d.ts +1 -1
- package/dest/queue/bounded_serial_queue.d.ts.map +1 -1
- package/dest/queue/fifo_memory_queue.d.ts +1 -1
- package/dest/queue/fifo_memory_queue.d.ts.map +1 -1
- package/dest/queue/index.d.ts +1 -1
- package/dest/queue/priority_memory_queue.d.ts +1 -1
- package/dest/queue/priority_memory_queue.d.ts.map +1 -1
- package/dest/queue/priority_queue.d.ts +1 -1
- package/dest/queue/priority_queue.d.ts.map +1 -1
- package/dest/queue/semaphore.d.ts +1 -1
- package/dest/queue/semaphore.d.ts.map +1 -1
- package/dest/queue/serial_queue.d.ts +1 -1
- package/dest/queue/serial_queue.d.ts.map +1 -1
- package/dest/retry/index.d.ts +1 -1
- package/dest/running-promise/index.d.ts +1 -1
- package/dest/schemas/api.d.ts +1 -1
- package/dest/schemas/index.d.ts +1 -1
- package/dest/schemas/parse.d.ts +1 -1
- package/dest/schemas/schemas.d.ts +9 -15
- package/dest/schemas/schemas.d.ts.map +1 -1
- package/dest/schemas/schemas.js +5 -7
- package/dest/schemas/types.d.ts +1 -1
- package/dest/schemas/utils.d.ts +4 -4
- package/dest/schemas/utils.d.ts.map +1 -1
- package/dest/schemas/utils.js +15 -5
- package/dest/serialize/buffer_reader.d.ts +1 -1
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/field_reader.d.ts +1 -1
- package/dest/serialize/field_reader.d.ts.map +1 -1
- package/dest/serialize/free_funcs.d.ts +1 -1
- package/dest/serialize/index.d.ts +1 -1
- package/dest/serialize/serialize.d.ts +1 -1
- package/dest/serialize/type_registry.d.ts +1 -1
- package/dest/serialize/type_registry.d.ts.map +1 -1
- package/dest/serialize/types.d.ts +1 -1
- package/dest/sleep/index.d.ts +1 -1
- package/dest/sleep/index.d.ts.map +1 -1
- package/dest/string/index.d.ts +1 -1
- package/dest/testing/files/index.d.ts +2 -1
- package/dest/testing/files/index.d.ts.map +1 -1
- package/dest/testing/files/index.js +1 -1
- package/dest/testing/formatting.d.ts +1 -1
- package/dest/testing/index.d.ts +1 -1
- package/dest/testing/port_allocator.d.ts +1 -1
- package/dest/testing/snapshot_serializer.d.ts +1 -1
- package/dest/testing/test_data.d.ts +1 -1
- package/dest/timer/date.d.ts +1 -1
- package/dest/timer/date.d.ts.map +1 -1
- package/dest/timer/elapsed.d.ts +1 -1
- package/dest/timer/index.d.ts +1 -1
- package/dest/timer/timeout.d.ts +1 -1
- package/dest/timer/timeout.d.ts.map +1 -1
- package/dest/timer/timer.d.ts +1 -1
- package/dest/timer/timer.d.ts.map +1 -1
- package/dest/transport/dispatch/create_dispatch_fn.d.ts +1 -1
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -1
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +1 -1
- package/dest/transport/dispatch/messages.d.ts +1 -1
- package/dest/transport/index.d.ts +1 -1
- package/dest/transport/interface/connector.d.ts +1 -1
- package/dest/transport/interface/listener.d.ts +1 -1
- package/dest/transport/interface/socket.d.ts +1 -1
- package/dest/transport/interface/transferable.d.ts +1 -1
- package/dest/transport/node/index.d.ts +1 -1
- package/dest/transport/node/node_connector.d.ts +1 -1
- package/dest/transport/node/node_connector.d.ts.map +1 -1
- package/dest/transport/node/node_connector_socket.d.ts +1 -1
- package/dest/transport/node/node_connector_socket.d.ts.map +1 -1
- package/dest/transport/node/node_listener.d.ts +1 -1
- package/dest/transport/node/node_listener.d.ts.map +1 -1
- package/dest/transport/node/node_listener_socket.d.ts +1 -1
- package/dest/transport/node/node_listener_socket.d.ts.map +1 -1
- package/dest/transport/transport_client.d.ts +1 -1
- package/dest/transport/transport_client.d.ts.map +1 -1
- package/dest/transport/transport_server.d.ts +1 -10
- package/dest/transport/transport_server.d.ts.map +1 -1
- package/dest/trees/balanced_merkle_tree.d.ts +22 -0
- package/dest/trees/balanced_merkle_tree.d.ts.map +1 -0
- package/dest/trees/balanced_merkle_tree.js +64 -0
- package/dest/trees/hasher.d.ts +1 -1
- package/dest/trees/index.d.ts +2 -1
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +1 -0
- package/dest/trees/indexed_merkle_tree.d.ts +1 -1
- package/dest/trees/indexed_merkle_tree.d.ts.map +1 -1
- 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_tree_leaf.d.ts +1 -1
- package/dest/trees/membership_witness.d.ts +2 -23
- package/dest/trees/membership_witness.d.ts.map +1 -1
- package/dest/trees/merkle_tree.d.ts +1 -1
- package/dest/trees/merkle_tree.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.d.ts +2 -12
- package/dest/trees/merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/merkle_tree_calculator.js +1 -31
- package/dest/trees/sibling_path.d.ts +4 -38
- package/dest/trees/sibling_path.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts +5 -3
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree.js +32 -9
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +6 -5
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +36 -40
- package/dest/trees/unbalanced_tree_store.d.ts +1 -1
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/types/index.d.ts +1 -1
- package/dest/url/index.d.ts +1 -1
- package/dest/validation/index.d.ts +1 -1
- package/package.json +7 -5
- package/src/array/array.ts +0 -16
- package/src/branded-types/block_number.ts +112 -0
- package/src/branded-types/checkpoint_number.ts +100 -0
- package/src/branded-types/epoch.ts +88 -0
- package/src/branded-types/index.ts +6 -0
- package/src/branded-types/slot.ts +88 -0
- package/src/branded-types/types.ts +2 -0
- package/src/config/env_var.ts +12 -2
- package/src/crypto/aes128/index.ts +2 -2
- package/src/crypto/bn254/index.ts +3 -0
- package/src/crypto/ecdsa/index.ts +4 -4
- package/src/crypto/grumpkin/index.ts +5 -5
- package/src/crypto/keys/index.ts +1 -1
- package/src/crypto/pedersen/pedersen.wasm.ts +3 -3
- package/src/crypto/poseidon/index.ts +5 -5
- package/src/crypto/schnorr/index.ts +3 -3
- package/src/crypto/secp256k1/index.ts +3 -3
- package/src/crypto/sync/index.ts +1 -1
- package/src/eth-address/index.ts +19 -0
- package/src/fields/fields.ts +16 -0
- package/src/fields/point.ts +19 -0
- package/src/index.ts +1 -0
- package/src/schemas/schemas.ts +10 -10
- package/src/schemas/utils.ts +21 -8
- package/src/testing/files/index.ts +1 -1
- package/src/trees/balanced_merkle_tree.ts +82 -0
- package/src/trees/index.ts +1 -0
- package/src/trees/merkle_tree_calculator.ts +1 -34
- package/src/trees/unbalanced_merkle_tree.ts +54 -6
- package/src/trees/unbalanced_merkle_tree_calculator.ts +34 -41
|
@@ -10,7 +10,7 @@ import { type Fieldable, serializeToFields } from '../../serialize/serialize.js'
|
|
|
10
10
|
*/
|
|
11
11
|
export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
12
12
|
const inputFields = serializeToFields(input);
|
|
13
|
-
await BarretenbergSync.initSingleton(
|
|
13
|
+
await BarretenbergSync.initSingleton();
|
|
14
14
|
const api = BarretenbergSync.getSingleton();
|
|
15
15
|
const response = api.poseidon2Hash({
|
|
16
16
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
@@ -27,7 +27,7 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
|
27
27
|
export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
28
28
|
const inputFields = serializeToFields(input);
|
|
29
29
|
inputFields.unshift(new Fr(separator));
|
|
30
|
-
await BarretenbergSync.initSingleton(
|
|
30
|
+
await BarretenbergSync.initSingleton();
|
|
31
31
|
const api = BarretenbergSync.getSingleton();
|
|
32
32
|
const response = api.poseidon2Hash({
|
|
33
33
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
@@ -37,7 +37,7 @@ export async function poseidon2HashWithSeparator(input: Fieldable[], separator:
|
|
|
37
37
|
|
|
38
38
|
export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
|
|
39
39
|
const inputFields = serializeToFields(input);
|
|
40
|
-
await BarretenbergSync.initSingleton(
|
|
40
|
+
await BarretenbergSync.initSingleton();
|
|
41
41
|
const api = BarretenbergSync.getSingleton();
|
|
42
42
|
const response = api.poseidon2HashAccumulate({
|
|
43
43
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
@@ -54,7 +54,7 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
|
54
54
|
const inputFields = serializeToFields(input);
|
|
55
55
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
56
56
|
// assert(input.length === 4, 'Input state must be of size 4');
|
|
57
|
-
await BarretenbergSync.initSingleton(
|
|
57
|
+
await BarretenbergSync.initSingleton();
|
|
58
58
|
const api = BarretenbergSync.getSingleton();
|
|
59
59
|
const response = api.poseidon2Permutation({
|
|
60
60
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
@@ -75,7 +75,7 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
|
75
75
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
await BarretenbergSync.initSingleton(
|
|
78
|
+
await BarretenbergSync.initSingleton();
|
|
79
79
|
const api = BarretenbergSync.getSingleton();
|
|
80
80
|
const response = api.poseidon2Hash({
|
|
81
81
|
inputs: inputFields.map(i => i.toBuffer()),
|
|
@@ -15,7 +15,7 @@ export class Schnorr {
|
|
|
15
15
|
* @returns A grumpkin public key.
|
|
16
16
|
*/
|
|
17
17
|
public async computePublicKey(privateKey: GrumpkinScalar): Promise<Point> {
|
|
18
|
-
await BarretenbergSync.initSingleton(
|
|
18
|
+
await BarretenbergSync.initSingleton();
|
|
19
19
|
const api = BarretenbergSync.getSingleton();
|
|
20
20
|
const response = api.schnorrComputePublicKey({ privateKey: privateKey.toBuffer() });
|
|
21
21
|
return Point.fromBuffer(Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]));
|
|
@@ -28,7 +28,7 @@ export class Schnorr {
|
|
|
28
28
|
* @returns A Schnorr signature of the form (s, e).
|
|
29
29
|
*/
|
|
30
30
|
public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) {
|
|
31
|
-
await BarretenbergSync.initSingleton(
|
|
31
|
+
await BarretenbergSync.initSingleton();
|
|
32
32
|
const api = BarretenbergSync.getSingleton();
|
|
33
33
|
const response = api.schnorrConstructSignature({
|
|
34
34
|
message: msg,
|
|
@@ -45,7 +45,7 @@ export class Schnorr {
|
|
|
45
45
|
* @returns True or false.
|
|
46
46
|
*/
|
|
47
47
|
public async verifySignature(msg: Uint8Array, pubKey: Point, sig: SchnorrSignature) {
|
|
48
|
-
await BarretenbergSync.initSingleton(
|
|
48
|
+
await BarretenbergSync.initSingleton();
|
|
49
49
|
const api = BarretenbergSync.getSingleton();
|
|
50
50
|
const response = api.schnorrVerifySignature({
|
|
51
51
|
message: msg,
|
|
@@ -27,7 +27,7 @@ export class Secp256k1 {
|
|
|
27
27
|
* @returns Result of the multiplication.
|
|
28
28
|
*/
|
|
29
29
|
public async mul(point: Uint8Array, scalar: Uint8Array) {
|
|
30
|
-
await BarretenbergSync.initSingleton(
|
|
30
|
+
await BarretenbergSync.initSingleton();
|
|
31
31
|
const api = BarretenbergSync.getSingleton();
|
|
32
32
|
const response = api.secp256k1Mul({
|
|
33
33
|
point: { x: point.subarray(0, 32), y: point.subarray(32, 64) },
|
|
@@ -41,7 +41,7 @@ export class Secp256k1 {
|
|
|
41
41
|
* @returns Random field element.
|
|
42
42
|
*/
|
|
43
43
|
public async getRandomFr() {
|
|
44
|
-
await BarretenbergSync.initSingleton(
|
|
44
|
+
await BarretenbergSync.initSingleton();
|
|
45
45
|
const api = BarretenbergSync.getSingleton();
|
|
46
46
|
const response = api.secp256k1GetRandomFr({ dummy: 0 });
|
|
47
47
|
return Buffer.from(response.value);
|
|
@@ -53,7 +53,7 @@ export class Secp256k1 {
|
|
|
53
53
|
* @returns Buffer representation of the field element.
|
|
54
54
|
*/
|
|
55
55
|
public async reduce512BufferToFr(uint512Buf: Buffer) {
|
|
56
|
-
await BarretenbergSync.initSingleton(
|
|
56
|
+
await BarretenbergSync.initSingleton();
|
|
57
57
|
const api = BarretenbergSync.getSingleton();
|
|
58
58
|
const response = api.secp256k1Reduce512({ input: uint512Buf });
|
|
59
59
|
return Buffer.from(response.value);
|
package/src/crypto/sync/index.ts
CHANGED
package/src/eth-address/index.ts
CHANGED
|
@@ -41,6 +41,24 @@ export class EthAddress {
|
|
|
41
41
|
return new EthAddress(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Creates an EthAddress from a plain object without Zod validation.
|
|
46
|
+
* This method is optimized for performance and skips validation, making it suitable
|
|
47
|
+
* for deserializing trusted data (e.g., from C++ via MessagePack).
|
|
48
|
+
* Handles buffers (20 or 32 bytes), strings, or existing instances.
|
|
49
|
+
* @param obj - Plain object, buffer, string, or EthAddress instance
|
|
50
|
+
* @returns An EthAddress instance
|
|
51
|
+
*/
|
|
52
|
+
public static fromPlainObject(obj: any): EthAddress {
|
|
53
|
+
if (obj instanceof EthAddress) {
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
if (obj instanceof Buffer || Buffer.isBuffer(obj)) {
|
|
57
|
+
return obj.length === 20 ? new EthAddress(obj) : EthAddress.fromField(new Fr(obj));
|
|
58
|
+
}
|
|
59
|
+
return EthAddress.fromString(obj);
|
|
60
|
+
}
|
|
61
|
+
|
|
44
62
|
/**
|
|
45
63
|
* Create a random EthAddress instance with 20 random bytes.
|
|
46
64
|
* This method generates a new Ethereum address with a randomly generated set of 20 bytes.
|
|
@@ -240,6 +258,7 @@ export class EthAddress {
|
|
|
240
258
|
}
|
|
241
259
|
|
|
242
260
|
static get schema() {
|
|
261
|
+
// Serialization from hex string.
|
|
243
262
|
return hexSchemaFor(EthAddress, EthAddress.isAddress);
|
|
244
263
|
}
|
|
245
264
|
}
|
package/src/fields/fields.ts
CHANGED
|
@@ -335,7 +335,23 @@ export class Fr extends BaseField {
|
|
|
335
335
|
return this.toString();
|
|
336
336
|
}
|
|
337
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Creates an Fr instance from a plain object without Zod validation.
|
|
340
|
+
* This method is optimized for performance and skips validation, making it suitable
|
|
341
|
+
* for deserializing trusted data (e.g., from C++ via MessagePack).
|
|
342
|
+
* Handles buffers, strings, numbers, bigints, or existing instances.
|
|
343
|
+
* @param obj - Plain object, buffer, string, number, bigint, boolean, or Fr instance
|
|
344
|
+
* @returns An Fr instance
|
|
345
|
+
*/
|
|
346
|
+
static fromPlainObject(obj: any): Fr {
|
|
347
|
+
if (obj instanceof Fr) {
|
|
348
|
+
return obj;
|
|
349
|
+
}
|
|
350
|
+
return new Fr(obj);
|
|
351
|
+
}
|
|
352
|
+
|
|
338
353
|
static get schema() {
|
|
354
|
+
// Serialization from hex string.
|
|
339
355
|
return hexSchemaFor(Fr);
|
|
340
356
|
}
|
|
341
357
|
}
|
package/src/fields/point.ts
CHANGED
|
@@ -42,9 +42,28 @@ export class Point {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
static get schema() {
|
|
45
|
+
// Serialization from hex string.
|
|
45
46
|
return hexSchemaFor(Point);
|
|
46
47
|
}
|
|
47
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Creates a Point from a plain object without Zod validation.
|
|
51
|
+
* This method is optimized for performance and skips validation, making it suitable
|
|
52
|
+
* for deserializing trusted data (e.g., from C++ via MessagePack).
|
|
53
|
+
* Handles buffers, existing instances, or objects with x, y, and isInfinite fields.
|
|
54
|
+
* @param obj - Plain object, buffer, or Point instance
|
|
55
|
+
* @returns A Point instance
|
|
56
|
+
*/
|
|
57
|
+
static fromPlainObject(obj: any): Point {
|
|
58
|
+
if (obj instanceof Point) {
|
|
59
|
+
return obj;
|
|
60
|
+
}
|
|
61
|
+
if (obj instanceof Buffer || Buffer.isBuffer(obj)) {
|
|
62
|
+
return Point.fromBuffer(obj);
|
|
63
|
+
}
|
|
64
|
+
return new Point(Fr.fromPlainObject(obj.x), Fr.fromPlainObject(obj.y), obj.isInfinite ?? false);
|
|
65
|
+
}
|
|
66
|
+
|
|
48
67
|
/**
|
|
49
68
|
* Generate a random Point instance.
|
|
50
69
|
*
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// Reexport all folders at the root for packages targeting CommonJS
|
|
2
2
|
export * as asyncMap from './async-map/index.js';
|
|
3
|
+
export * as brandedTypes from './branded-types/index.js';
|
|
3
4
|
export * as bigintBuffer from './bigint-buffer/index.js';
|
|
4
5
|
export * as collection from './collection/index.js';
|
|
5
6
|
export * as committable from './committable/index.js';
|
package/src/schemas/schemas.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
2
|
|
|
3
|
+
import { EpochNumber, EpochNumberSchema } from '../branded-types/epoch.js';
|
|
4
|
+
import { SlotNumber, SlotNumberSchema } from '../branded-types/slot.js';
|
|
3
5
|
import { Buffer32 } from '../buffer/buffer32.js';
|
|
4
6
|
import { SecretValue } from '../config/secret_value.js';
|
|
5
7
|
import { EthAddress } from '../eth-address/index.js';
|
|
@@ -9,6 +11,12 @@ import { isHex, withoutHexPrefix } from '../string/index.js';
|
|
|
9
11
|
import { bufferSchema, hexSchema } from './utils.js';
|
|
10
12
|
|
|
11
13
|
export const schemas = {
|
|
14
|
+
/** Accepts a number, bigint, or string as an EpochNumber. */
|
|
15
|
+
EpochNumber: EpochNumberSchema,
|
|
16
|
+
|
|
17
|
+
/** Accepts a number, bigint, or string as a SlotNumber. */
|
|
18
|
+
SlotNumber: SlotNumberSchema,
|
|
19
|
+
|
|
12
20
|
/** Accepts a hex string. */
|
|
13
21
|
EthAddress: EthAddress.schema,
|
|
14
22
|
|
|
@@ -54,15 +62,7 @@ export const schemas = {
|
|
|
54
62
|
Buffer32: z.string().refine(isHex, 'Not a valid hex string').transform(Buffer32.fromString),
|
|
55
63
|
|
|
56
64
|
/** Accepts a base64 string or an object `{ type: 'Buffer', data: [byte, byte...] }` as a buffer. */
|
|
57
|
-
Buffer:
|
|
58
|
-
bufferSchema,
|
|
59
|
-
z
|
|
60
|
-
.object({
|
|
61
|
-
type: z.literal('Buffer'),
|
|
62
|
-
data: z.array(z.number().int().min(0).max(255)),
|
|
63
|
-
})
|
|
64
|
-
.transform(({ data }) => Buffer.from(data)),
|
|
65
|
-
]),
|
|
65
|
+
Buffer: bufferSchema,
|
|
66
66
|
|
|
67
67
|
/** Accepts a hex string as a buffer. */
|
|
68
68
|
BufferHex: z
|
|
@@ -79,4 +79,4 @@ export const schemas = {
|
|
|
79
79
|
};
|
|
80
80
|
|
|
81
81
|
// These are needed to avoid errors such as: "The inferred type of 'YourClassSchema' cannot be named without a reference to..."
|
|
82
|
-
export type { EthAddress, Fq, Fr, Point };
|
|
82
|
+
export type { EpochNumber, EthAddress, Fq, Fr, Point, SlotNumber };
|
package/src/schemas/utils.ts
CHANGED
|
@@ -21,13 +21,25 @@ export const hexSchema = z.string().refine(isHex, 'Not a valid hex string').tran
|
|
|
21
21
|
// Copied from zod internals, which was copied from https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
22
22
|
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
23
23
|
|
|
24
|
-
/** Schema for a buffer represented as a base64 string. */
|
|
25
|
-
export const bufferSchema = z
|
|
26
|
-
.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
/** Schema for a buffer represented as a base64 string or a Buffer object. */
|
|
25
|
+
export const bufferSchema: ZodFor<Buffer> = z.union([
|
|
26
|
+
// Serialization from base64 string.
|
|
27
|
+
z
|
|
28
|
+
.string()
|
|
29
|
+
// We only test the str for base64 if it's shorter than 1024 bytes, otherwise we've run into maximum
|
|
30
|
+
// stack size exceeded errors when trying to validate excessively long strings (such as contract bytecode).
|
|
31
|
+
.refine(str => str.length > 1024 || base64Regex.test(str), 'Not a valid base64 string')
|
|
32
|
+
.transform(data => Buffer.from(data, 'base64')),
|
|
33
|
+
// Serialization from Buffer-annotated object.
|
|
34
|
+
z
|
|
35
|
+
.object({
|
|
36
|
+
type: z.literal('Buffer'),
|
|
37
|
+
data: z.array(z.number().int().min(0).max(255)),
|
|
38
|
+
})
|
|
39
|
+
.transform(({ data }) => Buffer.from(data)),
|
|
40
|
+
// Serialization from Buffer
|
|
41
|
+
z.instanceof(Buffer),
|
|
42
|
+
]);
|
|
31
43
|
|
|
32
44
|
export class ZodNullableOptional<T extends ZodTypeAny> extends ZodOptional<T> {
|
|
33
45
|
_isNullableOptional = true;
|
|
@@ -88,12 +100,13 @@ export function hexSchemaFor<TClass extends { fromString(str: string): any } | {
|
|
|
88
100
|
*/
|
|
89
101
|
export function bufferSchemaFor<TClass extends { fromBuffer(buf: Buffer): any }>(
|
|
90
102
|
klazz: TClass,
|
|
103
|
+
refinement?: (buf: Buffer) => boolean,
|
|
91
104
|
): ZodType<
|
|
92
105
|
TClass extends { fromBuffer(buf: Buffer): infer TInstance } ? ToJsonIs<TInstance, Buffer> : never,
|
|
93
106
|
any,
|
|
94
107
|
string
|
|
95
108
|
> {
|
|
96
|
-
return bufferSchema.transform(klazz.fromBuffer.bind(klazz));
|
|
109
|
+
return bufferSchema.refine(refinement ?? (() => true), 'Not a valid buffer').transform(klazz.fromBuffer.bind(klazz));
|
|
97
110
|
}
|
|
98
111
|
|
|
99
112
|
/** Creates a schema for a js Map type that matches the serialization used in jsonStringify. */
|
|
@@ -63,7 +63,7 @@ export function updateProtocolCircuitSampleInputs(circuitName: string, value: st
|
|
|
63
63
|
logger(`Updated test data in ${targetFile} for ${circuitName}`);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
function getPathToFile(targetFileFromRepoRoot: string) {
|
|
66
|
+
export function getPathToFile(targetFileFromRepoRoot: string) {
|
|
67
67
|
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../../../../../');
|
|
68
68
|
if (!existsSync(join(repoRoot, 'CODEOWNERS'))) {
|
|
69
69
|
throw new Error(`Path to repo root is incorrect (got ${repoRoot})`);
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { pedersenHash as pedersenHashArray, poseidon2Hash, sha256Trunc } from '@aztec/foundation/crypto';
|
|
2
|
+
|
|
3
|
+
import type { AsyncHasher, Hasher } from './hasher.js';
|
|
4
|
+
|
|
5
|
+
export const shaMerkleHash: Hasher['hash'] = (left: Buffer, right: Buffer) =>
|
|
6
|
+
sha256Trunc(Buffer.concat([left, right])) as Buffer<ArrayBuffer>;
|
|
7
|
+
|
|
8
|
+
export const pedersenMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
9
|
+
(await pedersenHashArray([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
10
|
+
|
|
11
|
+
export const poseidonMerkleHash: AsyncHasher['hash'] = async (left: Buffer, right: Buffer) =>
|
|
12
|
+
(await poseidon2Hash([left, right])).toBuffer() as Buffer<ArrayBuffer>;
|
|
13
|
+
|
|
14
|
+
export const computeBalancedShaRoot = (leaves: Buffer[]) => computeBalancedMerkleTreeRoot(leaves);
|
|
15
|
+
|
|
16
|
+
export const computeBalancedPedersenRoot = async (leaves: Buffer[]) =>
|
|
17
|
+
await computeBalancedMerkleTreeRootAsync(leaves, pedersenMerkleHash);
|
|
18
|
+
|
|
19
|
+
export const computeBalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
20
|
+
await computeBalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Computes the Merkle root with the provided leaves **synchronously**.
|
|
24
|
+
* This method uses a synchronous hash function (defaults to `sha256Trunc`).
|
|
25
|
+
*
|
|
26
|
+
* @throws If the number of leaves is not a power of two.
|
|
27
|
+
*/
|
|
28
|
+
export function computeBalancedMerkleTreeRoot(leaves: Buffer[], hasher = shaMerkleHash): Buffer {
|
|
29
|
+
const height = getTreeHeight(leaves);
|
|
30
|
+
let nodes = leaves.slice();
|
|
31
|
+
|
|
32
|
+
for (let i = 0; i < height; ++i) {
|
|
33
|
+
let j = 0;
|
|
34
|
+
for (; j < nodes.length / 2; ++j) {
|
|
35
|
+
const l = nodes[j * 2];
|
|
36
|
+
const r = nodes[j * 2 + 1];
|
|
37
|
+
nodes[j] = hasher(l, r);
|
|
38
|
+
}
|
|
39
|
+
nodes = nodes.slice(0, j);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return nodes[0];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Computes the Merkle root with the provided leaves **asynchronously**.
|
|
47
|
+
* This method uses an asynchronous hash function (defaults to `pedersenHash`).
|
|
48
|
+
*
|
|
49
|
+
* @throws If the number of leaves is not a power of two.
|
|
50
|
+
*/
|
|
51
|
+
export async function computeBalancedMerkleTreeRootAsync(
|
|
52
|
+
leaves: Buffer[],
|
|
53
|
+
hasher = poseidonMerkleHash,
|
|
54
|
+
): Promise<Buffer> {
|
|
55
|
+
const height = getTreeHeight(leaves);
|
|
56
|
+
let nodes = leaves.slice();
|
|
57
|
+
|
|
58
|
+
for (let i = 0; i < height; ++i) {
|
|
59
|
+
let j = 0;
|
|
60
|
+
for (; j < nodes.length / 2; ++j) {
|
|
61
|
+
const l = nodes[j * 2];
|
|
62
|
+
const r = nodes[j * 2 + 1];
|
|
63
|
+
nodes[j] = await hasher(l, r);
|
|
64
|
+
}
|
|
65
|
+
nodes = nodes.slice(0, j);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return nodes[0];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getTreeHeight(leaves: Buffer[]) {
|
|
72
|
+
if (leaves.length === 0) {
|
|
73
|
+
throw new Error('Cannot compute a Merkle root with no leaves');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const height = Math.log2(leaves.length);
|
|
77
|
+
if (!Number.isInteger(height)) {
|
|
78
|
+
throw new Error('Cannot compute a Merkle root with a non-power-of-two number of leaves');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return height;
|
|
82
|
+
}
|
package/src/trees/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { pedersenHash
|
|
1
|
+
import { pedersenHash } from '@aztec/foundation/crypto';
|
|
2
2
|
|
|
3
3
|
import type { AsyncHasher } from './hasher.js';
|
|
4
4
|
import { MerkleTree } from './merkle_tree.js';
|
|
@@ -69,37 +69,4 @@ export class MerkleTreeCalculator {
|
|
|
69
69
|
|
|
70
70
|
return leaves[0];
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Computes the Merkle root with the provided leaves **synchronously**.
|
|
75
|
-
*
|
|
76
|
-
* This method uses a synchronous hash function (defaults to `sha256Trunc`) and **does not** allow for padding.
|
|
77
|
-
* If the number of leaves is not a power of two, it throws an error.
|
|
78
|
-
* This contrasts with the above non-static async method `computeTreeRoot`, which can handle any number of leaves by
|
|
79
|
-
* padding with zero hashes.
|
|
80
|
-
*/
|
|
81
|
-
static computeTreeRootSync(leaves: Buffer[], hasher = sha256Trunc): Buffer {
|
|
82
|
-
if (leaves.length === 0) {
|
|
83
|
-
throw new Error('Cannot compute a Merkle root with no leaves');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const height = Math.log2(leaves.length);
|
|
87
|
-
if (!Number.isInteger(height)) {
|
|
88
|
-
throw new Error('Cannot compute a Merkle root with a non-power-of-two number of leaves');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
let nodes = leaves.slice();
|
|
92
|
-
|
|
93
|
-
for (let i = 0; i < height; ++i) {
|
|
94
|
-
let j = 0;
|
|
95
|
-
for (; j < nodes.length / 2; ++j) {
|
|
96
|
-
const l = nodes[j * 2];
|
|
97
|
-
const r = nodes[j * 2 + 1];
|
|
98
|
-
nodes[j] = hasher(Buffer.concat([l, r]));
|
|
99
|
-
}
|
|
100
|
-
nodes = nodes.slice(0, j);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return nodes[0];
|
|
104
|
-
}
|
|
105
72
|
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
computeBalancedMerkleTreeRoot,
|
|
3
|
+
computeBalancedMerkleTreeRootAsync,
|
|
4
|
+
poseidonMerkleHash,
|
|
5
|
+
shaMerkleHash,
|
|
6
|
+
} from './balanced_merkle_tree.js';
|
|
7
|
+
|
|
8
|
+
export const computeUnbalancedShaRoot = (leaves: Buffer[]) => computeUnbalancedMerkleTreeRoot(leaves, shaMerkleHash);
|
|
9
|
+
|
|
10
|
+
export const computeUnbalancedPoseidonRoot = async (leaves: Buffer[]) =>
|
|
11
|
+
await computeUnbalancedMerkleTreeRootAsync(leaves, poseidonMerkleHash);
|
|
3
12
|
|
|
4
13
|
/**
|
|
5
14
|
* Computes the Merkle root of an unbalanced tree.
|
|
@@ -22,9 +31,48 @@ import { MerkleTreeCalculator } from './merkle_tree_calculator.js';
|
|
|
22
31
|
* But if we instead combine the size-4 and size-8 subtrees first (depth 4), and then merge with the size-2 subtree
|
|
23
32
|
* (depth 1), the final tree has a depth of 5.
|
|
24
33
|
*/
|
|
25
|
-
export function computeUnbalancedMerkleTreeRoot(
|
|
34
|
+
export function computeUnbalancedMerkleTreeRoot(
|
|
35
|
+
leaves: Buffer[],
|
|
36
|
+
hasher = shaMerkleHash,
|
|
37
|
+
emptyRoot = Buffer.alloc(32),
|
|
38
|
+
): Buffer {
|
|
39
|
+
if (!leaves.length) {
|
|
40
|
+
return emptyRoot;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (leaves.length === 1) {
|
|
44
|
+
return leaves[0];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let numRemainingLeaves = leaves.length;
|
|
48
|
+
let subtreeSize = 1;
|
|
49
|
+
let root: Buffer | undefined;
|
|
50
|
+
while (numRemainingLeaves > 1) {
|
|
51
|
+
if ((numRemainingLeaves & subtreeSize) !== 0) {
|
|
52
|
+
const subtreeLeaves = leaves.slice(numRemainingLeaves - subtreeSize, numRemainingLeaves);
|
|
53
|
+
const subtreeRoot = computeBalancedMerkleTreeRoot(subtreeLeaves, hasher);
|
|
54
|
+
if (!root) {
|
|
55
|
+
root = subtreeRoot;
|
|
56
|
+
} else {
|
|
57
|
+
root = hasher(subtreeRoot, root);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
numRemainingLeaves -= subtreeSize;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
subtreeSize *= 2;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return root!;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function computeUnbalancedMerkleTreeRootAsync(
|
|
70
|
+
leaves: Buffer[],
|
|
71
|
+
hasher = poseidonMerkleHash,
|
|
72
|
+
emptyRoot = Buffer.alloc(32),
|
|
73
|
+
): Promise<Buffer> {
|
|
26
74
|
if (!leaves.length) {
|
|
27
|
-
|
|
75
|
+
return emptyRoot;
|
|
28
76
|
}
|
|
29
77
|
|
|
30
78
|
if (leaves.length === 1) {
|
|
@@ -37,11 +85,11 @@ export function computeUnbalancedMerkleTreeRoot(leaves: Buffer[], hasher = sha25
|
|
|
37
85
|
while (numRemainingLeaves > 1) {
|
|
38
86
|
if ((numRemainingLeaves & subtreeSize) !== 0) {
|
|
39
87
|
const subtreeLeaves = leaves.slice(numRemainingLeaves - subtreeSize, numRemainingLeaves);
|
|
40
|
-
const subtreeRoot =
|
|
88
|
+
const subtreeRoot = await computeBalancedMerkleTreeRootAsync(subtreeLeaves, hasher);
|
|
41
89
|
if (!root) {
|
|
42
90
|
root = subtreeRoot;
|
|
43
91
|
} else {
|
|
44
|
-
root = hasher(
|
|
92
|
+
root = await hasher(subtreeRoot, root);
|
|
45
93
|
}
|
|
46
94
|
|
|
47
95
|
numRemainingLeaves -= subtreeSize;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { shaMerkleHash } from './balanced_merkle_tree.js';
|
|
2
2
|
import type { Hasher } from './hasher.js';
|
|
3
3
|
import { SiblingPath } from './sibling_path.js';
|
|
4
4
|
import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_store.js';
|
|
@@ -6,9 +6,10 @@ import { type TreeNodeLocation, UnbalancedTreeStore } from './unbalanced_tree_st
|
|
|
6
6
|
export function computeCompressedUnbalancedMerkleTreeRoot(
|
|
7
7
|
leaves: Buffer[],
|
|
8
8
|
valueToCompress = Buffer.alloc(32),
|
|
9
|
+
emptyRoot = Buffer.alloc(32),
|
|
9
10
|
hasher?: Hasher['hash'],
|
|
10
11
|
): Buffer {
|
|
11
|
-
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, hasher);
|
|
12
|
+
const calculator = UnbalancedMerkleTreeCalculator.create(leaves, valueToCompress, emptyRoot, hasher);
|
|
12
13
|
return calculator.getRoot();
|
|
13
14
|
}
|
|
14
15
|
|
|
@@ -32,12 +33,9 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
32
33
|
public constructor(
|
|
33
34
|
private readonly leaves: Buffer[],
|
|
34
35
|
private readonly valueToCompress: Buffer,
|
|
36
|
+
private readonly emptyRoot: Buffer,
|
|
35
37
|
private readonly hasher: Hasher['hash'],
|
|
36
38
|
) {
|
|
37
|
-
if (leaves.length === 0) {
|
|
38
|
-
throw Error('Cannot create a compressed unbalanced tree with 0 leaves.');
|
|
39
|
-
}
|
|
40
|
-
|
|
41
39
|
this.store = new UnbalancedTreeStore(leaves.length);
|
|
42
40
|
this.buildTree();
|
|
43
41
|
}
|
|
@@ -45,9 +43,10 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
45
43
|
static create(
|
|
46
44
|
leaves: Buffer[],
|
|
47
45
|
valueToCompress = Buffer.alloc(0),
|
|
48
|
-
|
|
46
|
+
emptyRoot = Buffer.alloc(32),
|
|
47
|
+
hasher = shaMerkleHash,
|
|
49
48
|
) {
|
|
50
|
-
return new UnbalancedMerkleTreeCalculator(leaves, valueToCompress, hasher);
|
|
49
|
+
return new UnbalancedMerkleTreeCalculator(leaves, valueToCompress, emptyRoot, hasher);
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
/**
|
|
@@ -113,8 +112,8 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
113
112
|
// Start with the leaves that are not compressed.
|
|
114
113
|
let toProcess = this.leafLocations.filter((_, i) => !this.leaves[i].equals(this.valueToCompress));
|
|
115
114
|
if (!toProcess.length) {
|
|
116
|
-
// All leaves are compressed. Set
|
|
117
|
-
this.store.setNode({ level: 0, index: 0 }, { value:
|
|
115
|
+
// All leaves are compressed. Set empty root to the root.
|
|
116
|
+
this.store.setNode({ level: 0, index: 0 }, { value: this.emptyRoot });
|
|
118
117
|
return;
|
|
119
118
|
}
|
|
120
119
|
|
|
@@ -138,10 +137,7 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
138
137
|
const shouldShiftUp = !sibling || sibling.value.equals(this.valueToCompress);
|
|
139
138
|
if (shouldShiftUp) {
|
|
140
139
|
// The node becomes the parent if the sibling is a compressed leaf.
|
|
141
|
-
|
|
142
|
-
if (!isLeaf) {
|
|
143
|
-
this.shiftChildrenUp(location);
|
|
144
|
-
}
|
|
140
|
+
this.shiftNodeUp(location, parentLocation);
|
|
145
141
|
} else {
|
|
146
142
|
// Hash the value with the (right) sibling and update the parent node.
|
|
147
143
|
const node = this.store.getNode(location)!;
|
|
@@ -157,38 +153,35 @@ export class UnbalancedMerkleTreeCalculator {
|
|
|
157
153
|
}
|
|
158
154
|
}
|
|
159
155
|
|
|
160
|
-
private shiftNodeUp(
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
156
|
+
private shiftNodeUp(from: TreeNodeLocation, to: TreeNodeLocation) {
|
|
157
|
+
// Collect all nodes that need to shift.
|
|
158
|
+
const nodesToShift = this.collectNodesToShift(from, to);
|
|
159
|
+
// Move all nodes to their new locations.
|
|
160
|
+
for (const { node, newLocation } of nodesToShift) {
|
|
161
|
+
this.store.setNode(newLocation, node);
|
|
162
|
+
if (node.leafIndex !== undefined) {
|
|
163
|
+
this.leafLocations[node.leafIndex] = newLocation;
|
|
164
|
+
}
|
|
169
165
|
}
|
|
170
|
-
|
|
171
|
-
return isLeaf;
|
|
172
166
|
}
|
|
173
167
|
|
|
174
|
-
private
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
const computeNewLocation = (index: number) => ({
|
|
180
|
-
level,
|
|
181
|
-
index: Math.floor(index / (groupSize * 2)) * groupSize + (index % groupSize),
|
|
182
|
-
});
|
|
168
|
+
private collectNodesToShift(from: TreeNodeLocation, to: TreeNodeLocation) {
|
|
169
|
+
const node = this.store.getNode(from);
|
|
170
|
+
if (!node) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
183
173
|
|
|
184
|
-
|
|
185
|
-
const isRightLeaf = this.shiftNodeUp(right, computeNewLocation(right.index));
|
|
174
|
+
let result = [{ node, newLocation: to }];
|
|
186
175
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
176
|
+
// If not a leaf, collect its children.
|
|
177
|
+
if (node.leafIndex === undefined) {
|
|
178
|
+
const [leftChild, rightChild] = this.store.getChildLocations(from);
|
|
179
|
+
const [leftChildNew, rightChildNew] = this.store.getChildLocations(to);
|
|
180
|
+
result = result
|
|
181
|
+
.concat(this.collectNodesToShift(leftChild, leftChildNew))
|
|
182
|
+
.concat(this.collectNodesToShift(rightChild, rightChildNew));
|
|
192
183
|
}
|
|
184
|
+
|
|
185
|
+
return result;
|
|
193
186
|
}
|
|
194
187
|
}
|