@aztec/foundation 0.0.1-commit.cbf2c2d5d → 0.0.1-commit.ce4f8c4f2
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/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/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/package.json +2 -2
- package/src/crypto/poseidon/index.ts +42 -34
- package/src/trees/indexed_merkle_tree_calculator.ts +5 -1
|
@@ -20,4 +20,4 @@ export declare function poseidon2HashWithSeparator(input: Fieldable[], separator
|
|
|
20
20
|
*/
|
|
21
21
|
export declare function poseidon2Permutation(input: Fieldable[]): Promise<Fr[]>;
|
|
22
22
|
export declare function poseidon2HashBytes(input: Buffer): Promise<Fr>;
|
|
23
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
23
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9jcnlwdG8vcG9zZWlkb24vaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBRUEsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLDZCQUE2QixDQUFDO0FBQ2pELE9BQU8sRUFBRSxLQUFLLFNBQVMsRUFBcUIsTUFBTSw4QkFBOEIsQ0FBQztBQXNCakY7Ozs7R0FJRztBQUNILHdCQUFnQixhQUFhLENBQUMsS0FBSyxFQUFFLFNBQVMsRUFBRSxHQUFHLE9BQU8sQ0FBQyxFQUFFLENBQUMsQ0FFN0Q7QUFFRDs7Ozs7R0FLRztBQUNILHdCQUFnQiwwQkFBMEIsQ0FBQyxLQUFLLEVBQUUsU0FBUyxFQUFFLEVBQUUsU0FBUyxFQUFFLE1BQU0sR0FBRyxPQUFPLENBQUMsRUFBRSxDQUFDLENBSTdGO0FBRUQ7Ozs7R0FJRztBQUNILHdCQUFzQixvQkFBb0IsQ0FBQyxLQUFLLEVBQUUsU0FBUyxFQUFFLEdBQUcsT0FBTyxDQUFDLEVBQUUsRUFBRSxDQUFDLENBaUI1RTtBQUVELHdCQUFnQixrQkFBa0IsQ0FBQyxLQUFLLEVBQUUsTUFBTSxHQUFHLE9BQU8sQ0FBQyxFQUFFLENBQUMsQ0FZN0QifQ==
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crypto/poseidon/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,6BAA6B,CAAC;AACjD,OAAO,EAAE,KAAK,SAAS,EAAqB,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/crypto/poseidon/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,EAAE,EAAE,MAAM,6BAA6B,CAAC;AACjD,OAAO,EAAE,KAAK,SAAS,EAAqB,MAAM,8BAA8B,CAAC;AAsBjF;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC,CAE7D;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS,EAAE,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,CAI7F;AAED;;;;GAIG;AACH,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC,EAAE,EAAE,CAAC,CAiB5E;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,CAY7D"}
|
|
@@ -1,33 +1,40 @@
|
|
|
1
|
-
import { Barretenberg } from '@aztec/bb.js';
|
|
1
|
+
import { Barretenberg, BarretenbergSync } from '@aztec/bb.js';
|
|
2
2
|
import { Fr } from '../../curves/bn254/field.js';
|
|
3
3
|
import { serializeToFields } from '../../serialize/serialize.js';
|
|
4
|
+
const IS_BROWSER = typeof self !== 'undefined';
|
|
5
|
+
async function poseidon2HashFields(inputFields) {
|
|
6
|
+
if (IS_BROWSER) {
|
|
7
|
+
await BarretenbergSync.initSingleton();
|
|
8
|
+
const api = BarretenbergSync.getSingleton();
|
|
9
|
+
const response = api.poseidon2Hash({
|
|
10
|
+
inputs: inputFields.map((i)=>i.toBuffer())
|
|
11
|
+
});
|
|
12
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
13
|
+
} else {
|
|
14
|
+
await Barretenberg.initSingleton();
|
|
15
|
+
const api = Barretenberg.getSingleton();
|
|
16
|
+
const response = await api.poseidon2Hash({
|
|
17
|
+
inputs: inputFields.map((i)=>i.toBuffer())
|
|
18
|
+
});
|
|
19
|
+
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
4
22
|
/**
|
|
5
23
|
* Create a poseidon hash (field) from an array of input fields.
|
|
6
24
|
* @param input - The input fields to hash.
|
|
7
25
|
* @returns The poseidon hash.
|
|
8
|
-
*/ export
|
|
9
|
-
|
|
10
|
-
await Barretenberg.initSingleton();
|
|
11
|
-
const api = Barretenberg.getSingleton();
|
|
12
|
-
const response = await api.poseidon2Hash({
|
|
13
|
-
inputs: inputFields.map((i)=>i.toBuffer())
|
|
14
|
-
});
|
|
15
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
26
|
+
*/ export function poseidon2Hash(input) {
|
|
27
|
+
return poseidon2HashFields(serializeToFields(input));
|
|
16
28
|
}
|
|
17
29
|
/**
|
|
18
30
|
* Create a poseidon hash (field) from an array of input fields and a domain separator.
|
|
19
31
|
* @param input - The input fields to hash.
|
|
20
32
|
* @param separator - The domain separator.
|
|
21
33
|
* @returns The poseidon hash.
|
|
22
|
-
*/ export
|
|
34
|
+
*/ export function poseidon2HashWithSeparator(input, separator) {
|
|
23
35
|
const inputFields = serializeToFields(input);
|
|
24
36
|
inputFields.unshift(new Fr(separator));
|
|
25
|
-
|
|
26
|
-
const api = Barretenberg.getSingleton();
|
|
27
|
-
const response = await api.poseidon2Hash({
|
|
28
|
-
inputs: inputFields.map((i)=>i.toBuffer())
|
|
29
|
-
});
|
|
30
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
37
|
+
return poseidon2HashFields(inputFields);
|
|
31
38
|
}
|
|
32
39
|
/**
|
|
33
40
|
* Runs a Poseidon2 permutation.
|
|
@@ -35,18 +42,23 @@ import { serializeToFields } from '../../serialize/serialize.js';
|
|
|
35
42
|
* @returns the output state, size 4.
|
|
36
43
|
*/ export async function poseidon2Permutation(input) {
|
|
37
44
|
const inputFields = serializeToFields(input);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (IS_BROWSER) {
|
|
46
|
+
await BarretenbergSync.initSingleton();
|
|
47
|
+
const api = BarretenbergSync.getSingleton();
|
|
48
|
+
const response = api.poseidon2Permutation({
|
|
49
|
+
inputs: inputFields.map((i)=>i.toBuffer())
|
|
50
|
+
});
|
|
51
|
+
return response.outputs.map((o)=>Fr.fromBuffer(Buffer.from(o)));
|
|
52
|
+
} else {
|
|
53
|
+
await Barretenberg.initSingleton();
|
|
54
|
+
const api = Barretenberg.getSingleton();
|
|
55
|
+
const response = await api.poseidon2Permutation({
|
|
56
|
+
inputs: inputFields.map((i)=>i.toBuffer())
|
|
57
|
+
});
|
|
58
|
+
return response.outputs.map((o)=>Fr.fromBuffer(Buffer.from(o)));
|
|
59
|
+
}
|
|
48
60
|
}
|
|
49
|
-
export
|
|
61
|
+
export function poseidon2HashBytes(input) {
|
|
50
62
|
const inputFields = [];
|
|
51
63
|
for(let i = 0; i < input.length; i += 31){
|
|
52
64
|
const fieldBytes = Buffer.alloc(32, 0);
|
|
@@ -55,10 +67,5 @@ export async function poseidon2HashBytes(input) {
|
|
|
55
67
|
fieldBytes.reverse();
|
|
56
68
|
inputFields.push(Fr.fromBuffer(fieldBytes));
|
|
57
69
|
}
|
|
58
|
-
|
|
59
|
-
const api = Barretenberg.getSingleton();
|
|
60
|
-
const response = await api.poseidon2Hash({
|
|
61
|
-
inputs: inputFields.map((i)=>i.toBuffer())
|
|
62
|
-
});
|
|
63
|
-
return Fr.fromBuffer(Buffer.from(response.hash));
|
|
70
|
+
return poseidon2HashFields(inputFields);
|
|
64
71
|
}
|
|
@@ -18,4 +18,4 @@ export declare class IndexedMerkleTreeCalculator<T extends IndexedTreeLeafPreima
|
|
|
18
18
|
computeTreeRoot(leaves?: Buffer[]): Promise<Buffer>;
|
|
19
19
|
}
|
|
20
20
|
export {};
|
|
21
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
21
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXhlZF9tZXJrbGVfdHJlZV9jYWxjdWxhdG9yLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHJlZXMvaW5kZXhlZF9tZXJrbGVfdHJlZV9jYWxjdWxhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUVBLE9BQU8sS0FBSyxFQUFFLHVCQUF1QixFQUFFLE1BQU0seUJBQXlCLENBQUM7QUFFdkUsT0FBTyxLQUFLLEVBQUUsV0FBVyxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBQy9DLE9BQU8sRUFBRSxpQkFBaUIsRUFBRSxNQUFNLDBCQUEwQixDQUFDO0FBRTdELFVBQVUsbUJBQW1CLENBQUMsQ0FBQyxTQUFTLHVCQUF1QjtJQUM3RCxV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AW,EACnB,OAAO,EAAE,mBAAmB,CAAC,CAAC,CAAC,EAC/B,QAAQ,sBAAmB,8CAO5B;IAEK,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAyCpE;IAEK,eAAe,CAAC,MAAM,GAAE,MAAM,EAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAkB5D;CACF"}
|
|
@@ -34,7 +34,11 @@ import { IndexedMerkleTree } from './indexed_merkle_tree.js';
|
|
|
34
34
|
const sorted = values.map((v, i)=>({
|
|
35
35
|
value: v,
|
|
36
36
|
index: i
|
|
37
|
-
})).sort((a, b)=>
|
|
37
|
+
})).sort((a, b)=>{
|
|
38
|
+
const aBigInt = toBigIntBE(a.value);
|
|
39
|
+
const bBigInt = toBigIntBE(b.value);
|
|
40
|
+
return aBigInt < bBigInt ? 1 : aBigInt > bBigInt ? -1 : 0;
|
|
41
|
+
});
|
|
38
42
|
const indexedLeaves = sorted.map((item, i)=>({
|
|
39
43
|
leaf: this.factory.fromBuffer(Buffer.concat([
|
|
40
44
|
item.value,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.ce4f8c4f2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dest/index.js",
|
|
6
6
|
"types": "./dest/index.d.ts",
|
|
@@ -145,7 +145,7 @@
|
|
|
145
145
|
"testEnvironment": "../../foundation/src/jest/env.mjs"
|
|
146
146
|
},
|
|
147
147
|
"dependencies": {
|
|
148
|
-
"@aztec/bb.js": "0.0.1-commit.
|
|
148
|
+
"@aztec/bb.js": "0.0.1-commit.ce4f8c4f2",
|
|
149
149
|
"@koa/cors": "^5.0.0",
|
|
150
150
|
"@noble/curves": "=1.7.0",
|
|
151
151
|
"@noble/hashes": "^1.6.1",
|
|
@@ -1,21 +1,35 @@
|
|
|
1
|
-
import { Barretenberg } 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 Barretenberg.initSingleton();
|
|
14
|
-
const api = Barretenberg.getSingleton();
|
|
15
|
-
const response = await 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 = Barretenberg.getSingleton();
|
|
32
|
-
const response = await 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 = Barretenberg.getSingleton();
|
|
70
|
-
const response = await 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
|
}
|
|
@@ -40,7 +40,11 @@ export class IndexedMerkleTreeCalculator<T extends IndexedTreeLeafPreimage, N ex
|
|
|
40
40
|
}
|
|
41
41
|
const sorted = values
|
|
42
42
|
.map((v, i) => ({ value: v, index: i }))
|
|
43
|
-
.sort((a, b)
|
|
43
|
+
.sort((a, b): -1 | 0 | 1 => {
|
|
44
|
+
const aBigInt = toBigIntBE(a.value);
|
|
45
|
+
const bBigInt = toBigIntBE(b.value);
|
|
46
|
+
return aBigInt < bBigInt ? 1 : aBigInt > bBigInt ? -1 : 0;
|
|
47
|
+
});
|
|
44
48
|
const indexedLeaves = sorted.map((item, i) => ({
|
|
45
49
|
leaf: this.factory.fromBuffer(
|
|
46
50
|
Buffer.concat([
|