@aztec/foundation 0.55.1 → 0.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/abi/decoder.d.ts +6 -6
- package/dest/abi/decoder.d.ts.map +1 -1
- package/dest/abi/decoder.js +23 -24
- package/dest/buffer/buffer32.d.ts +9 -0
- package/dest/buffer/buffer32.d.ts.map +1 -1
- package/dest/buffer/buffer32.js +18 -1
- package/dest/config/env_var.d.ts +1 -1
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/crypto/index.d.ts +1 -0
- package/dest/crypto/index.d.ts.map +1 -1
- package/dest/crypto/index.js +2 -1
- package/dest/crypto/secp256k1-signer/index.d.ts +3 -0
- package/dest/crypto/secp256k1-signer/index.d.ts.map +1 -0
- package/dest/crypto/secp256k1-signer/index.js +3 -0
- package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts +24 -0
- package/dest/crypto/secp256k1-signer/secp256k1_signer.d.ts.map +1 -0
- package/dest/crypto/secp256k1-signer/secp256k1_signer.js +31 -0
- package/dest/crypto/secp256k1-signer/utils.d.ts +39 -0
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -0
- package/dest/crypto/secp256k1-signer/utils.js +89 -0
- package/dest/crypto/sha256/index.d.ts +15 -3
- package/dest/crypto/sha256/index.d.ts.map +1 -1
- package/dest/crypto/sha256/index.js +110 -5
- package/dest/eth-signature/eth_signature.d.ts +54 -0
- package/dest/eth-signature/eth_signature.d.ts.map +1 -0
- package/dest/eth-signature/eth_signature.js +69 -0
- package/dest/eth-signature/index.d.ts +2 -0
- package/dest/eth-signature/index.d.ts.map +1 -0
- package/dest/eth-signature/index.js +2 -0
- package/dest/fields/point.d.ts.map +1 -1
- package/dest/fields/point.js +3 -2
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/serialize/buffer_reader.d.ts +9 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -1
- package/dest/serialize/buffer_reader.js +18 -1
- package/dest/serialize/serialize.js +2 -2
- package/dest/serialize/types.d.ts +1 -1
- package/dest/serialize/types.d.ts.map +1 -1
- package/dest/serialize/types.js +1 -1
- package/dest/testing/index.d.ts +1 -0
- package/dest/testing/index.d.ts.map +1 -1
- package/dest/testing/index.js +2 -1
- package/dest/testing/port_allocator.d.ts +10 -0
- package/dest/testing/port_allocator.d.ts.map +1 -0
- package/dest/testing/port_allocator.js +32 -0
- package/dest/trees/index.d.ts +1 -0
- package/dest/trees/index.d.ts.map +1 -1
- package/dest/trees/index.js +2 -2
- package/dest/trees/unbalanced_merkle_root.d.ts +12 -0
- package/dest/trees/unbalanced_merkle_root.d.ts.map +1 -0
- package/dest/trees/unbalanced_merkle_root.js +52 -0
- package/package.json +5 -3
- package/src/abi/decoder.ts +24 -25
- package/src/buffer/buffer32.ts +18 -0
- package/src/config/env_var.ts +1 -1
- package/src/crypto/index.ts +1 -0
- package/src/crypto/secp256k1-signer/index.ts +2 -0
- package/src/crypto/secp256k1-signer/secp256k1_signer.ts +38 -0
- package/src/crypto/secp256k1-signer/utils.ts +99 -0
- package/src/crypto/sha256/index.ts +137 -4
- package/src/eth-signature/eth_signature.ts +90 -0
- package/src/eth-signature/index.ts +1 -0
- package/src/fields/point.ts +2 -1
- package/src/index.ts +1 -0
- package/src/serialize/buffer_reader.ts +20 -0
- package/src/serialize/serialize.ts +1 -1
- package/src/serialize/types.ts +1 -1
- package/src/testing/index.ts +1 -0
- package/src/testing/port_allocator.ts +31 -0
- package/src/trees/index.ts +2 -0
- package/src/trees/unbalanced_merkle_root.ts +52 -0
package/src/index.ts
CHANGED
|
@@ -70,6 +70,26 @@ export class BufferReader {
|
|
|
70
70
|
return result as Tuple<number, N>;
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Reads a 256-bit unsigned integer from the buffer at the current index position.
|
|
75
|
+
* Updates the index position by 32 bytes after reading the number.
|
|
76
|
+
*
|
|
77
|
+
* Assumes the number is stored in big-endian format.
|
|
78
|
+
*
|
|
79
|
+
* @returns The read 256 bit value as a bigint.
|
|
80
|
+
*/
|
|
81
|
+
public readUInt256(): bigint {
|
|
82
|
+
this.#rangeCheck(32);
|
|
83
|
+
|
|
84
|
+
let result = BigInt(0);
|
|
85
|
+
for (let i = 0; i < 32; i++) {
|
|
86
|
+
result = (result << BigInt(8)) | BigInt(this.buffer[this.index + i]);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this.index += 32;
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
73
93
|
/**
|
|
74
94
|
* Reads a 16-bit unsigned integer from the buffer at the current index position.
|
|
75
95
|
* Updates the index position by 2 bytes after reading the number.
|
|
@@ -242,7 +242,7 @@ export function toFriendlyJSON(obj: object): string {
|
|
|
242
242
|
).toFriendlyJSON
|
|
243
243
|
) {
|
|
244
244
|
return value.toFriendlyJSON();
|
|
245
|
-
} else if (value && value.type && ['Fr', 'Fq', 'AztecAddress'].includes(value.type)) {
|
|
245
|
+
} else if (value && value.type && ['Fr', 'Fq', 'AztecAddress', 'EthAddress'].includes(value.type)) {
|
|
246
246
|
return value.value;
|
|
247
247
|
} else {
|
|
248
248
|
return value;
|
package/src/serialize/types.ts
CHANGED
|
@@ -35,6 +35,6 @@ type MapTuple<T extends any[], F extends (item: any) => any> = {
|
|
|
35
35
|
* @see https://github.com/microsoft/TypeScript/issues/29841.
|
|
36
36
|
* @param array - A tuple array.
|
|
37
37
|
*/
|
|
38
|
-
export function mapTuple<T extends any[], F extends (item:
|
|
38
|
+
export function mapTuple<T extends any[], F extends (item: T[number]) => any>(tuple: T, fn: F): MapTuple<T, F> {
|
|
39
39
|
return tuple.map(fn) as MapTuple<T, F>;
|
|
40
40
|
}
|
package/src/testing/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Get a random port that is free to use.
|
|
5
|
+
* Returns undefined if it fails to get a port.
|
|
6
|
+
*
|
|
7
|
+
* @attribution: adapted from https://stackoverflow.com/a/71178451
|
|
8
|
+
*
|
|
9
|
+
* @returns a random port that is free to use.
|
|
10
|
+
*/
|
|
11
|
+
export function getRandomPort(): Promise<number | undefined> {
|
|
12
|
+
return new Promise(resolve => {
|
|
13
|
+
const server = net.createServer();
|
|
14
|
+
server.listen(0, () => {
|
|
15
|
+
const address = server.address();
|
|
16
|
+
if (address && typeof address === 'object' && 'port' in address) {
|
|
17
|
+
const port = address.port;
|
|
18
|
+
server.close(() => {
|
|
19
|
+
resolve(port);
|
|
20
|
+
});
|
|
21
|
+
} else {
|
|
22
|
+
server.close(() => {
|
|
23
|
+
resolve(undefined);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
server.on('error', () => {
|
|
28
|
+
resolve(undefined);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
}
|
package/src/trees/index.ts
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { padArrayEnd } from '@aztec/foundation/collection';
|
|
2
|
+
import { sha256Trunc } from '@aztec/foundation/crypto';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Computes the merkle root for an unbalanced tree.
|
|
6
|
+
*
|
|
7
|
+
* @dev Adapted from proving-state.ts -> findMergeLevel and unbalanced_tree.ts.
|
|
8
|
+
* Calculates the tree upwards layer by layer until we reach the root.
|
|
9
|
+
* The L1 calculation instead computes the tree from right to left (slightly cheaper gas).
|
|
10
|
+
* TODO: A more thorough investigation of which method is cheaper, then use that method everywhere.
|
|
11
|
+
*/
|
|
12
|
+
export function computeUnbalancedMerkleRoot(leaves: Buffer[], emptyLeaf?: Buffer, hasher = sha256Trunc): Buffer {
|
|
13
|
+
// Pad leaves to 2
|
|
14
|
+
if (leaves.length < 2) {
|
|
15
|
+
if (emptyLeaf === undefined) {
|
|
16
|
+
throw new Error('Cannot compute a Merkle root with less than 2 leaves');
|
|
17
|
+
} else {
|
|
18
|
+
leaves = padArrayEnd(leaves, emptyLeaf, 2);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const depth = Math.ceil(Math.log2(leaves.length));
|
|
23
|
+
let [layerWidth, nodeToShift] =
|
|
24
|
+
leaves.length & 1 ? [leaves.length - 1, leaves[leaves.length - 1]] : [leaves.length, Buffer.alloc(0)];
|
|
25
|
+
// Allocate this layer's leaves and init the next layer up
|
|
26
|
+
let thisLayer = leaves.slice(0, layerWidth);
|
|
27
|
+
let nextLayer = [];
|
|
28
|
+
for (let i = 0; i < depth; i++) {
|
|
29
|
+
for (let j = 0; j < layerWidth; j += 2) {
|
|
30
|
+
// Store the hash of each pair one layer up
|
|
31
|
+
nextLayer[j / 2] = hasher(Buffer.concat([thisLayer[j], thisLayer[j + 1]]));
|
|
32
|
+
}
|
|
33
|
+
layerWidth /= 2;
|
|
34
|
+
if (layerWidth & 1) {
|
|
35
|
+
if (nodeToShift.length) {
|
|
36
|
+
// If the next layer has odd length, and we have a node that needs to be shifted up, add it here
|
|
37
|
+
nextLayer.push(nodeToShift);
|
|
38
|
+
layerWidth += 1;
|
|
39
|
+
nodeToShift = Buffer.alloc(0);
|
|
40
|
+
} else {
|
|
41
|
+
// If we don't have a node waiting to be shifted, store the next layer's final node to be shifted
|
|
42
|
+
layerWidth -= 1;
|
|
43
|
+
nodeToShift = nextLayer[layerWidth];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
// reset the layers
|
|
47
|
+
thisLayer = nextLayer;
|
|
48
|
+
nextLayer = [];
|
|
49
|
+
}
|
|
50
|
+
// return the root
|
|
51
|
+
return thisLayer[0];
|
|
52
|
+
}
|