@aztec/foundation 2.1.0-rc.9 → 3.0.0-devnet.2
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/config/env_var.d.ts +1 -1
- package/dest/config/env_var.d.ts.map +1 -1
- package/dest/config/network_name.d.ts +1 -1
- package/dest/config/network_name.d.ts.map +1 -1
- package/dest/config/network_name.js +6 -2
- package/dest/crypto/aes128/index.d.ts.map +1 -1
- package/dest/crypto/aes128/index.js +23 -6
- package/dest/crypto/ecdsa/index.d.ts.map +1 -1
- package/dest/crypto/ecdsa/index.js +66 -48
- package/dest/crypto/grumpkin/index.d.ts.map +1 -1
- package/dest/crypto/grumpkin/index.js +64 -43
- package/dest/crypto/keys/index.js +9 -4
- package/dest/crypto/pedersen/pedersen.wasm.d.ts.map +1 -1
- package/dest/crypto/pedersen/pedersen.wasm.js +29 -13
- package/dest/crypto/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/poseidon/index.js +42 -17
- package/dest/crypto/schnorr/index.d.ts.map +1 -1
- package/dest/crypto/schnorr/index.js +35 -37
- package/dest/crypto/secp256k1/index.d.ts.map +1 -1
- package/dest/crypto/secp256k1/index.js +29 -18
- package/dest/crypto/secp256k1-signer/utils.d.ts +8 -0
- package/dest/crypto/secp256k1-signer/utils.d.ts.map +1 -1
- package/dest/crypto/secp256k1-signer/utils.js +14 -0
- package/dest/crypto/sync/index.js +3 -1
- package/dest/crypto/sync/pedersen/index.d.ts.map +1 -1
- package/dest/crypto/sync/pedersen/index.js +17 -10
- package/dest/crypto/sync/poseidon/index.d.ts.map +1 -1
- package/dest/crypto/sync/poseidon/index.js +27 -12
- package/dest/fields/bls12_point.d.ts +7 -7
- package/dest/fields/bls12_point.js +7 -7
- package/dest/fields/fields.d.ts.map +1 -1
- package/dest/fields/fields.js +9 -10
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/json-rpc/client/safe_json_rpc_client.d.ts.map +1 -1
- package/dest/json-rpc/client/safe_json_rpc_client.js +9 -0
- package/dest/log/pino-logger.d.ts.map +1 -1
- package/dest/log/pino-logger.js +0 -1
- package/dest/profiler/index.d.ts +2 -0
- package/dest/profiler/index.d.ts.map +1 -0
- package/dest/profiler/index.js +1 -0
- package/dest/profiler/profiler.d.ts +8 -0
- package/dest/profiler/profiler.d.ts.map +1 -0
- package/dest/profiler/profiler.js +97 -0
- package/dest/testing/formatting.d.ts +4 -0
- package/dest/testing/formatting.d.ts.map +1 -0
- package/dest/testing/formatting.js +3 -0
- package/dest/testing/index.d.ts +1 -0
- package/dest/testing/index.d.ts.map +1 -1
- package/dest/testing/index.js +1 -0
- package/dest/trees/unbalanced_merkle_tree.d.ts +0 -1
- package/dest/trees/unbalanced_merkle_tree.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree.js +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts +25 -22
- package/dest/trees/unbalanced_merkle_tree_calculator.d.ts.map +1 -1
- package/dest/trees/unbalanced_merkle_tree_calculator.js +124 -94
- package/dest/trees/unbalanced_tree_store.d.ts +1 -0
- package/dest/trees/unbalanced_tree_store.d.ts.map +1 -1
- package/dest/trees/unbalanced_tree_store.js +6 -0
- package/package.json +4 -3
- package/src/config/env_var.ts +2 -1
- package/src/config/network_name.ts +14 -3
- package/src/crypto/aes128/index.ts +19 -10
- package/src/crypto/ecdsa/index.ts +40 -37
- package/src/crypto/grumpkin/index.ts +29 -31
- package/src/crypto/keys/index.ts +5 -5
- package/src/crypto/pedersen/pedersen.wasm.ts +22 -18
- package/src/crypto/poseidon/index.ts +32 -24
- package/src/crypto/schnorr/index.ts +20 -17
- package/src/crypto/secp256k1/index.ts +15 -11
- package/src/crypto/secp256k1-signer/utils.ts +16 -0
- package/src/crypto/sync/index.ts +1 -1
- package/src/crypto/sync/pedersen/index.ts +16 -15
- package/src/crypto/sync/poseidon/index.ts +27 -22
- package/src/fields/bls12_point.ts +7 -7
- package/src/fields/fields.ts +5 -6
- package/src/index.ts +1 -0
- package/src/json-rpc/client/safe_json_rpc_client.ts +9 -0
- package/src/log/pino-logger.ts +0 -1
- package/src/profiler/index.ts +1 -0
- package/src/profiler/profiler.ts +125 -0
- package/src/testing/formatting.ts +3 -0
- package/src/testing/index.ts +1 -0
- package/src/trees/unbalanced_merkle_tree.ts +1 -1
- package/src/trees/unbalanced_merkle_tree_calculator.ts +140 -92
- package/src/trees/unbalanced_tree_store.ts +5 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BarretenbergSync
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Buffer } from 'buffer';
|
|
4
4
|
|
|
@@ -22,10 +22,15 @@ export class Aes128 {
|
|
|
22
22
|
paddingBuffer.fill(numPaddingBytes);
|
|
23
23
|
const input = Buffer.concat([data, paddingBuffer]);
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
26
|
+
const api = BarretenbergSync.getSingleton();
|
|
27
|
+
const response = api.aesEncrypt({
|
|
28
|
+
plaintext: input,
|
|
29
|
+
iv,
|
|
30
|
+
key,
|
|
31
|
+
length: input.length,
|
|
32
|
+
});
|
|
33
|
+
return Buffer.from(response.ciphertext);
|
|
29
34
|
}
|
|
30
35
|
|
|
31
36
|
/**
|
|
@@ -37,11 +42,15 @@ export class Aes128 {
|
|
|
37
42
|
* @returns Decrypted data.
|
|
38
43
|
*/
|
|
39
44
|
public async decryptBufferCBCKeepPadding(data: Uint8Array, iv: Uint8Array, key: Uint8Array): Promise<Buffer> {
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
46
|
+
const api = BarretenbergSync.getSingleton();
|
|
47
|
+
const response = api.aesDecrypt({
|
|
48
|
+
ciphertext: data,
|
|
49
|
+
iv,
|
|
50
|
+
key,
|
|
51
|
+
length: data.length,
|
|
52
|
+
});
|
|
53
|
+
return Buffer.from(response.plaintext);
|
|
45
54
|
}
|
|
46
55
|
|
|
47
56
|
/**
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
|
-
import { numToInt32BE } from '@aztec/foundation/serialize';
|
|
3
2
|
|
|
4
|
-
import { concatenateUint8Arrays } from '../serialize.js';
|
|
5
3
|
import { EcdsaSignature } from './signature.js';
|
|
6
4
|
|
|
7
5
|
export * from './signature.js';
|
|
@@ -18,11 +16,13 @@ export class Ecdsa {
|
|
|
18
16
|
* @returns A secp256k1 public key.
|
|
19
17
|
*/
|
|
20
18
|
public async computePublicKey(privateKey: Buffer): Promise<Buffer> {
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
20
|
+
const api = BarretenbergSync.getSingleton();
|
|
21
|
+
const response =
|
|
22
|
+
this.curve === 'secp256r1'
|
|
23
|
+
? api.ecdsaSecp256r1ComputePublicKey({ privateKey })
|
|
24
|
+
: api.ecdsaSecp256k1ComputePublicKey({ privateKey });
|
|
25
|
+
return Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
/**
|
|
@@ -32,16 +32,13 @@ export class Ecdsa {
|
|
|
32
32
|
* @returns An ECDSA signature of the form (r, s, v).
|
|
33
33
|
*/
|
|
34
34
|
public async constructSignature(msg: Uint8Array, privateKey: Buffer) {
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
const
|
|
38
|
-
.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
[32, 32, 1],
|
|
43
|
-
);
|
|
44
|
-
return new EcdsaSignature(Buffer.from(r), Buffer.from(s), Buffer.from(v));
|
|
35
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
36
|
+
const api = BarretenbergSync.getSingleton();
|
|
37
|
+
const response =
|
|
38
|
+
this.curve === 'secp256r1'
|
|
39
|
+
? api.ecdsaSecp256r1ConstructSignature({ message: msg, privateKey })
|
|
40
|
+
: api.ecdsaSecp256k1ConstructSignature({ message: msg, privateKey });
|
|
41
|
+
return new EcdsaSignature(Buffer.from(response.r), Buffer.from(response.s), Buffer.from([response.v]));
|
|
45
42
|
}
|
|
46
43
|
|
|
47
44
|
/**
|
|
@@ -51,16 +48,13 @@ export class Ecdsa {
|
|
|
51
48
|
* @returns The secp256k1 public key of the signer.
|
|
52
49
|
*/
|
|
53
50
|
public async recoverPublicKey(msg: Uint8Array, sig: EcdsaSignature): Promise<Buffer> {
|
|
54
|
-
|
|
55
|
-
const
|
|
56
|
-
const
|
|
57
|
-
.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
[64],
|
|
62
|
-
);
|
|
63
|
-
return Buffer.from(result);
|
|
51
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
52
|
+
const api = BarretenbergSync.getSingleton();
|
|
53
|
+
const response =
|
|
54
|
+
this.curve === 'secp256r1'
|
|
55
|
+
? api.ecdsaSecp256r1RecoverPublicKey({ message: msg, r: sig.r, s: sig.s, v: sig.v[0] })
|
|
56
|
+
: api.ecdsaSecp256k1RecoverPublicKey({ message: msg, r: sig.r, s: sig.s, v: sig.v[0] });
|
|
57
|
+
return Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]);
|
|
64
58
|
}
|
|
65
59
|
|
|
66
60
|
/**
|
|
@@ -71,15 +65,24 @@ export class Ecdsa {
|
|
|
71
65
|
* @returns True or false.
|
|
72
66
|
*/
|
|
73
67
|
public async verifySignature(msg: Uint8Array, pubKey: Buffer, sig: EcdsaSignature) {
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
68
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
69
|
+
const api = BarretenbergSync.getSingleton();
|
|
70
|
+
const response =
|
|
71
|
+
this.curve === 'secp256r1'
|
|
72
|
+
? api.ecdsaSecp256r1VerifySignature({
|
|
73
|
+
message: msg,
|
|
74
|
+
publicKey: { x: pubKey.subarray(0, 32), y: pubKey.subarray(32, 64) },
|
|
75
|
+
r: sig.r,
|
|
76
|
+
s: sig.s,
|
|
77
|
+
v: sig.v[0],
|
|
78
|
+
})
|
|
79
|
+
: api.ecdsaSecp256k1VerifySignature({
|
|
80
|
+
message: msg,
|
|
81
|
+
publicKey: { x: pubKey.subarray(0, 32), y: pubKey.subarray(32, 64) },
|
|
82
|
+
r: sig.r,
|
|
83
|
+
s: sig.s,
|
|
84
|
+
v: sig.v[0],
|
|
85
|
+
});
|
|
86
|
+
return response.verified;
|
|
84
87
|
}
|
|
85
88
|
}
|
|
@@ -28,9 +28,13 @@ export class Grumpkin {
|
|
|
28
28
|
* @returns Result of the multiplication.
|
|
29
29
|
*/
|
|
30
30
|
public async mul(point: Point, scalar: GrumpkinScalar): Promise<Point> {
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
31
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
32
|
+
const api = BarretenbergSync.getSingleton();
|
|
33
|
+
const response = api.grumpkinMul({
|
|
34
|
+
point: { x: point.x.toBuffer(), y: point.y.toBuffer() },
|
|
35
|
+
scalar: scalar.toBuffer(),
|
|
36
|
+
});
|
|
37
|
+
return Point.fromBuffer(Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]));
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
/**
|
|
@@ -40,9 +44,13 @@ export class Grumpkin {
|
|
|
40
44
|
* @returns Result of the addition.
|
|
41
45
|
*/
|
|
42
46
|
public async add(a: Point, b: Point): Promise<Point> {
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
47
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
48
|
+
const api = BarretenbergSync.getSingleton();
|
|
49
|
+
const response = api.grumpkinAdd({
|
|
50
|
+
pointA: { x: a.x.toBuffer(), y: a.y.toBuffer() },
|
|
51
|
+
pointB: { x: b.x.toBuffer(), y: b.y.toBuffer() },
|
|
52
|
+
});
|
|
53
|
+
return Point.fromBuffer(Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]));
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
/**
|
|
@@ -52,24 +60,14 @@ export class Grumpkin {
|
|
|
52
60
|
* @returns Points multiplied by the scalar.
|
|
53
61
|
*/
|
|
54
62
|
public async batchMul(points: Point[], scalar: GrumpkinScalar) {
|
|
55
|
-
|
|
63
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
64
|
+
const api = BarretenbergSync.getSingleton();
|
|
65
|
+
const response = api.grumpkinBatchMul({
|
|
66
|
+
points: points.map(p => ({ x: p.x.toBuffer(), y: p.y.toBuffer() })),
|
|
67
|
+
scalar: scalar.toBuffer(),
|
|
68
|
+
});
|
|
56
69
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const api = await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
|
|
60
|
-
const [result] = api
|
|
61
|
-
.getWasm()
|
|
62
|
-
.callWasmExport(
|
|
63
|
-
'ecc_grumpkin__batch_mul',
|
|
64
|
-
[concatenatedPoints, scalar.toBuffer(), points.length],
|
|
65
|
-
[pointsByteLength],
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
const parsedResult: Point[] = [];
|
|
69
|
-
for (let i = 0; i < pointsByteLength; i += 64) {
|
|
70
|
-
parsedResult.push(Point.fromBuffer(Buffer.from(result.subarray(i, i + 64))));
|
|
71
|
-
}
|
|
72
|
-
return parsedResult;
|
|
70
|
+
return response.points.map(p => Point.fromBuffer(Buffer.concat([Buffer.from(p.x), Buffer.from(p.y)])));
|
|
73
71
|
}
|
|
74
72
|
|
|
75
73
|
/**
|
|
@@ -77,9 +75,10 @@ export class Grumpkin {
|
|
|
77
75
|
* @returns Random field element.
|
|
78
76
|
*/
|
|
79
77
|
public async getRandomFr(): Promise<Fr> {
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
78
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
79
|
+
const api = BarretenbergSync.getSingleton();
|
|
80
|
+
const response = api.grumpkinGetRandomFr({ dummy: 0 });
|
|
81
|
+
return Fr.fromBuffer(Buffer.from(response.value));
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
/**
|
|
@@ -88,10 +87,9 @@ export class Grumpkin {
|
|
|
88
87
|
* @returns Buffer representation of the field element.
|
|
89
88
|
*/
|
|
90
89
|
public async reduce512BufferToFr(uint512Buf: Buffer): Promise<Fr> {
|
|
91
|
-
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return Fr.fromBuffer(Buffer.from(result));
|
|
90
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
91
|
+
const api = BarretenbergSync.getSingleton();
|
|
92
|
+
const response = api.grumpkinReduce512({ input: uint512Buf });
|
|
93
|
+
return Fr.fromBuffer(Buffer.from(response.value));
|
|
96
94
|
}
|
|
97
95
|
}
|
package/src/crypto/keys/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { BarretenbergSync
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../fields/fields.js';
|
|
4
4
|
|
|
5
5
|
export async function vkAsFieldsMegaHonk(input: Buffer): Promise<Fr[]> {
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
return
|
|
6
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
7
|
+
const api = BarretenbergSync.getSingleton();
|
|
8
|
+
const response = api.megaVkAsFields({ verificationKey: input });
|
|
9
|
+
return response.fields.map(field => Fr.fromBuffer(Buffer.from(field)));
|
|
10
10
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BarretenbergSync
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../fields/fields.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
|
|
@@ -12,14 +12,13 @@ export async function pedersenCommit(input: Buffer[], offset = 0) {
|
|
|
12
12
|
throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
|
|
13
13
|
}
|
|
14
14
|
input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
|
|
15
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
16
|
+
const api = BarretenbergSync.getSingleton();
|
|
17
|
+
const response = api.pedersenCommit({
|
|
18
|
+
inputs: input,
|
|
19
|
+
hashIndex: offset,
|
|
20
|
+
});
|
|
21
|
+
return [Buffer.from(response.point.x), Buffer.from(response.point.y)];
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
/**
|
|
@@ -30,19 +29,24 @@ export async function pedersenCommit(input: Buffer[], offset = 0) {
|
|
|
30
29
|
*/
|
|
31
30
|
export async function pedersenHash(input: Fieldable[], index = 0): Promise<Fr> {
|
|
32
31
|
const inputFields = serializeToFields(input);
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
33
|
+
const api = BarretenbergSync.getSingleton();
|
|
34
|
+
const response = api.pedersenHash({
|
|
35
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
36
|
+
hashIndex: index,
|
|
37
|
+
});
|
|
38
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* Create a pedersen hash from an arbitrary length buffer.
|
|
43
43
|
*/
|
|
44
44
|
export async function pedersenHashBuffer(input: Buffer, index = 0) {
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
45
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
46
|
+
const api = BarretenbergSync.getSingleton();
|
|
47
|
+
const response = api.pedersenHashBuffer({
|
|
48
|
+
input,
|
|
49
|
+
hashIndex: index,
|
|
50
|
+
});
|
|
51
|
+
return Buffer.from(response.hash);
|
|
48
52
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BarretenbergSync
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../fields/fields.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../serialize/serialize.js';
|
|
@@ -10,11 +10,12 @@ 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
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
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));
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -26,19 +27,22 @@ export async function poseidon2Hash(input: Fieldable[]): Promise<Fr> {
|
|
|
26
27
|
export async function poseidon2HashWithSeparator(input: Fieldable[], separator: number): Promise<Fr> {
|
|
27
28
|
const inputFields = serializeToFields(input);
|
|
28
29
|
inputFields.unshift(new Fr(separator));
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
inputFields.map(i =>
|
|
33
|
-
);
|
|
34
|
-
return Fr.fromBuffer(Buffer.from(hash
|
|
30
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
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));
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
export async function poseidon2HashAccumulate(input: Fieldable[]): Promise<Fr> {
|
|
38
39
|
const inputFields = serializeToFields(input);
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
40
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
41
|
+
const api = BarretenbergSync.getSingleton();
|
|
42
|
+
const response = api.poseidon2HashAccumulate({
|
|
43
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
44
|
+
});
|
|
45
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
42
46
|
}
|
|
43
47
|
|
|
44
48
|
/**
|
|
@@ -50,11 +54,14 @@ export async function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]> {
|
|
|
50
54
|
const inputFields = serializeToFields(input);
|
|
51
55
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
52
56
|
// assert(input.length === 4, 'Input state must be of size 4');
|
|
53
|
-
|
|
54
|
-
const
|
|
57
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
58
|
+
const api = BarretenbergSync.getSingleton();
|
|
59
|
+
const response = api.poseidon2Permutation({
|
|
60
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
61
|
+
});
|
|
55
62
|
// We'd like this assertion but it's not possible to use it in the browser.
|
|
56
|
-
// assert(
|
|
57
|
-
return
|
|
63
|
+
// assert(response.outputs.length === 4, 'Output state must be of size 4');
|
|
64
|
+
return response.outputs.map(o => Fr.fromBuffer(Buffer.from(o)));
|
|
58
65
|
}
|
|
59
66
|
|
|
60
67
|
export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
@@ -68,10 +75,11 @@ export async function poseidon2HashBytes(input: Buffer): Promise<Fr> {
|
|
|
68
75
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
78
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
79
|
+
const api = BarretenbergSync.getSingleton();
|
|
80
|
+
const response = api.poseidon2Hash({
|
|
81
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
82
|
+
});
|
|
75
83
|
|
|
76
|
-
return Fr.fromBuffer(Buffer.from(
|
|
84
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
77
85
|
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
import { type GrumpkinScalar, Point } from '@aztec/foundation/fields';
|
|
3
|
-
import { numToInt32BE } from '@aztec/foundation/serialize';
|
|
4
3
|
|
|
5
|
-
import { concatenateUint8Arrays } from '../serialize.js';
|
|
6
4
|
import { SchnorrSignature } from './signature.js';
|
|
7
5
|
|
|
8
6
|
export * from './signature.js';
|
|
@@ -17,9 +15,10 @@ export class Schnorr {
|
|
|
17
15
|
* @returns A grumpkin public key.
|
|
18
16
|
*/
|
|
19
17
|
public async computePublicKey(privateKey: GrumpkinScalar): Promise<Point> {
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
18
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
19
|
+
const api = BarretenbergSync.getSingleton();
|
|
20
|
+
const response = api.schnorrComputePublicKey({ privateKey: privateKey.toBuffer() });
|
|
21
|
+
return Point.fromBuffer(Buffer.concat([Buffer.from(response.publicKey.x), Buffer.from(response.publicKey.y)]));
|
|
23
22
|
}
|
|
24
23
|
|
|
25
24
|
/**
|
|
@@ -29,12 +28,13 @@ export class Schnorr {
|
|
|
29
28
|
* @returns A Schnorr signature of the form (s, e).
|
|
30
29
|
*/
|
|
31
30
|
public async constructSignature(msg: Uint8Array, privateKey: GrumpkinScalar) {
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
32
|
+
const api = BarretenbergSync.getSingleton();
|
|
33
|
+
const response = api.schnorrConstructSignature({
|
|
34
|
+
message: msg,
|
|
35
|
+
privateKey: privateKey.toBuffer(),
|
|
36
|
+
});
|
|
37
|
+
return new SchnorrSignature(Buffer.from([...response.s, ...response.e]));
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
@@ -45,11 +45,14 @@ export class Schnorr {
|
|
|
45
45
|
* @returns True or false.
|
|
46
46
|
*/
|
|
47
47
|
public async verifySignature(msg: Uint8Array, pubKey: Point, sig: SchnorrSignature) {
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
.
|
|
53
|
-
|
|
48
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
49
|
+
const api = BarretenbergSync.getSingleton();
|
|
50
|
+
const response = api.schnorrVerifySignature({
|
|
51
|
+
message: msg,
|
|
52
|
+
publicKey: { x: pubKey.x.toBuffer(), y: pubKey.y.toBuffer() },
|
|
53
|
+
s: sig.s,
|
|
54
|
+
e: sig.e,
|
|
55
|
+
});
|
|
56
|
+
return response.verified;
|
|
54
57
|
}
|
|
55
58
|
}
|
|
@@ -27,9 +27,13 @@ export class Secp256k1 {
|
|
|
27
27
|
* @returns Result of the multiplication.
|
|
28
28
|
*/
|
|
29
29
|
public async mul(point: Uint8Array, scalar: Uint8Array) {
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
30
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
31
|
+
const api = BarretenbergSync.getSingleton();
|
|
32
|
+
const response = api.secp256k1Mul({
|
|
33
|
+
point: { x: point.subarray(0, 32), y: point.subarray(32, 64) },
|
|
34
|
+
scalar,
|
|
35
|
+
});
|
|
36
|
+
return Buffer.concat([Buffer.from(response.point.x), Buffer.from(response.point.y)]);
|
|
33
37
|
}
|
|
34
38
|
|
|
35
39
|
/**
|
|
@@ -37,9 +41,10 @@ export class Secp256k1 {
|
|
|
37
41
|
* @returns Random field element.
|
|
38
42
|
*/
|
|
39
43
|
public async getRandomFr() {
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
44
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
45
|
+
const api = BarretenbergSync.getSingleton();
|
|
46
|
+
const response = api.secp256k1GetRandomFr({ dummy: 0 });
|
|
47
|
+
return Buffer.from(response.value);
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
/**
|
|
@@ -48,10 +53,9 @@ export class Secp256k1 {
|
|
|
48
53
|
* @returns Buffer representation of the field element.
|
|
49
54
|
*/
|
|
50
55
|
public async reduce512BufferToFr(uint512Buf: Buffer) {
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return Buffer.from(result);
|
|
56
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
57
|
+
const api = BarretenbergSync.getSingleton();
|
|
58
|
+
const response = api.secp256k1Reduce512({ input: uint512Buf });
|
|
59
|
+
return Buffer.from(response.value);
|
|
56
60
|
}
|
|
57
61
|
}
|
|
@@ -47,6 +47,7 @@ export function addressFromPrivateKey(privateKey: Buffer): EthAddress {
|
|
|
47
47
|
* @param hash - The hash to recover the address from.
|
|
48
48
|
* @param signature - The signature to recover the address from.
|
|
49
49
|
* @returns The address.
|
|
50
|
+
* @throws Error if signature recovery fails.
|
|
50
51
|
*/
|
|
51
52
|
export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress {
|
|
52
53
|
try {
|
|
@@ -59,6 +60,21 @@ export function recoverAddress(hash: Buffer32, signature: Signature): EthAddress
|
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
/**
|
|
64
|
+
* Safely attempts to recover an address from a hash and a signature.
|
|
65
|
+
* @param hash - The hash to recover the address from.
|
|
66
|
+
* @param signature - The signature to recover the address from.
|
|
67
|
+
* @returns The address if recovery succeeds, undefined otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export function tryRecoverAddress(hash: Buffer32, signature: Signature): EthAddress | undefined {
|
|
70
|
+
try {
|
|
71
|
+
const publicKey = recoverPublicKey(hash, signature);
|
|
72
|
+
return publicKeyToAddress(publicKey);
|
|
73
|
+
} catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
62
78
|
/**
|
|
63
79
|
* @attribution - viem
|
|
64
80
|
* Converts a yParityOrV value to a recovery bit.
|
package/src/crypto/sync/index.ts
CHANGED
|
@@ -3,4 +3,4 @@ import { BarretenbergSync } from '@aztec/bb.js';
|
|
|
3
3
|
export * from './poseidon/index.js';
|
|
4
4
|
export * from './pedersen/index.js';
|
|
5
5
|
|
|
6
|
-
await BarretenbergSync.initSingleton(process.env.BB_WASM_PATH);
|
|
6
|
+
await BarretenbergSync.initSingleton({ wasmPath: process.env.BB_WASM_PATH });
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BarretenbergSync
|
|
1
|
+
import { BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
|
|
3
3
|
import { Fr } from '../../../fields/fields.js';
|
|
4
4
|
import { type Fieldable, serializeToFields } from '../../../serialize/serialize.js';
|
|
@@ -12,13 +12,11 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
|
|
|
12
12
|
throw new Error('All Pedersen Commit input buffers must be <= 32 bytes.');
|
|
13
13
|
}
|
|
14
14
|
input = input.map(i => (i.length < 32 ? Buffer.concat([Buffer.alloc(32 - i.length, 0), i]) : i));
|
|
15
|
-
const
|
|
16
|
-
input
|
|
17
|
-
offset,
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
// TODO: rename toTypedArray()?
|
|
21
|
-
return [Buffer.from(point.x.toBuffer()), Buffer.from(point.y.toBuffer())];
|
|
15
|
+
const response = BarretenbergSync.getSingleton().pedersenCommit({
|
|
16
|
+
inputs: input,
|
|
17
|
+
hashIndex: offset,
|
|
18
|
+
});
|
|
19
|
+
return [Buffer.from(response.point.x), Buffer.from(response.point.y)];
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
/**
|
|
@@ -29,17 +27,20 @@ export function pedersenCommit(input: Buffer[], offset = 0) {
|
|
|
29
27
|
*/
|
|
30
28
|
export function pedersenHash(input: Fieldable[], index = 0): Fr {
|
|
31
29
|
const inputFields = serializeToFields(input);
|
|
32
|
-
const
|
|
33
|
-
inputFields.map(i =>
|
|
34
|
-
index,
|
|
35
|
-
);
|
|
36
|
-
return Fr.fromBuffer(Buffer.from(hash
|
|
30
|
+
const response = BarretenbergSync.getSingleton().pedersenHash({
|
|
31
|
+
inputs: inputFields.map(i => i.toBuffer()),
|
|
32
|
+
hashIndex: index,
|
|
33
|
+
});
|
|
34
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
/**
|
|
40
38
|
* Create a pedersen hash from an arbitrary length buffer.
|
|
41
39
|
*/
|
|
42
40
|
export function pedersenHashBuffer(input: Buffer, index = 0) {
|
|
43
|
-
const
|
|
44
|
-
|
|
41
|
+
const response = BarretenbergSync.getSingleton().pedersenHashBuffer({
|
|
42
|
+
input,
|
|
43
|
+
hashIndex: index,
|
|
44
|
+
});
|
|
45
|
+
return Buffer.from(response.hash);
|
|
45
46
|
}
|