@aztec/foundation 4.2.0-nightly.20260320 → 4.2.0-nightly.20260322
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/branded-types/slot.d.ts +4 -1
- package/dest/branded-types/slot.d.ts.map +1 -1
- package/dest/branded-types/slot.js +3 -0
- 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/package.json +2 -2
- package/src/branded-types/slot.ts +5 -0
- package/src/crypto/poseidon/index.ts +42 -34
|
@@ -31,6 +31,9 @@ export declare namespace SlotNumber {
|
|
|
31
31
|
export declare namespace SlotNumber {
|
|
32
32
|
var isValid: (value: unknown) => value is SlotNumber;
|
|
33
33
|
}
|
|
34
|
+
export declare namespace SlotNumber {
|
|
35
|
+
var add: (sn: SlotNumber, increment: number) => SlotNumber;
|
|
36
|
+
}
|
|
34
37
|
export declare namespace SlotNumber {
|
|
35
38
|
var ZERO: SlotNumber;
|
|
36
39
|
}
|
|
@@ -39,4 +42,4 @@ export declare namespace SlotNumber {
|
|
|
39
42
|
* Accepts numbers, bigints, or strings and coerces them to SlotNumber.
|
|
40
43
|
*/
|
|
41
44
|
export declare const SlotNumberSchema: z.ZodEffects<z.ZodPipeline<z.ZodUnion<[z.ZodNumber, z.ZodBigInt, z.ZodString]>, z.ZodNumber>, SlotNumber, string | number | bigint>;
|
|
42
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
45
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic2xvdC5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2JyYW5kZWQtdHlwZXMvc2xvdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsQ0FBQyxFQUFFLE1BQU0sS0FBSyxDQUFDO0FBRXhCLE9BQU8sS0FBSyxFQUFFLE9BQU8sRUFBRSxNQUFNLFlBQVksQ0FBQztBQUUxQzs7Ozs7Ozs7Ozs7OztHQWFHO0FBQ0gsTUFBTSxNQUFNLFVBQVUsR0FBRyxPQUFPLENBQUMsTUFBTSxFQUFFLFlBQVksQ0FBQyxDQUFDO0FBRXZEOzs7OztHQUtHO0FBQ0gsd0JBQWdCLFVBQVUsQ0FBQyxLQUFLLEVBQUUsTUFBTSxHQUFHLFVBQVUsQ0FRcEQ7Ozs7Ozs7Ozs7Ozs7Ozs7QUFtREQ7OztHQUdHO0FBQ0gsZUFBTyxNQUFNLGdCQUFnQixxSUFHVyxDQUFDIn0=
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"slot.d.ts","sourceRoot":"","sources":["../../src/branded-types/slot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAQpD
|
|
1
|
+
{"version":3,"file":"slot.d.ts","sourceRoot":"","sources":["../../src/branded-types/slot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAE1C;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;AAEvD;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAQpD;;;;;;;;;;;;;;;;AAmDD;;;GAGG;AACH,eAAO,MAAM,gBAAgB,qIAGW,CAAC"}
|
|
@@ -46,6 +46,9 @@ import { z } from 'zod';
|
|
|
46
46
|
*/ SlotNumber.isValid = function(value) {
|
|
47
47
|
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
48
48
|
};
|
|
49
|
+
/** Increments a SlotNumber by a given value. */ SlotNumber.add = function(sn, increment) {
|
|
50
|
+
return SlotNumber(sn + increment);
|
|
51
|
+
};
|
|
49
52
|
/**
|
|
50
53
|
* The zero slot value.
|
|
51
54
|
*/ SlotNumber.ZERO = SlotNumber(0);
|
|
@@ -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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/foundation",
|
|
3
|
-
"version": "4.2.0-nightly.
|
|
3
|
+
"version": "4.2.0-nightly.20260322",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dest/index.js",
|
|
6
6
|
"types": "./dest/index.d.ts",
|
|
@@ -144,7 +144,7 @@
|
|
|
144
144
|
"testEnvironment": "../../foundation/src/jest/env.mjs"
|
|
145
145
|
},
|
|
146
146
|
"dependencies": {
|
|
147
|
-
"@aztec/bb.js": "4.2.0-nightly.
|
|
147
|
+
"@aztec/bb.js": "4.2.0-nightly.20260322",
|
|
148
148
|
"@koa/cors": "^5.0.0",
|
|
149
149
|
"@noble/curves": "=1.7.0",
|
|
150
150
|
"@noble/hashes": "^1.6.1",
|
|
@@ -73,6 +73,11 @@ SlotNumber.isValid = function (value: unknown): value is SlotNumber {
|
|
|
73
73
|
return typeof value === 'number' && Number.isInteger(value) && value >= 0;
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
+
/** Increments a SlotNumber by a given value. */
|
|
77
|
+
SlotNumber.add = function (sn: SlotNumber, increment: number): SlotNumber {
|
|
78
|
+
return SlotNumber(sn + increment);
|
|
79
|
+
};
|
|
80
|
+
|
|
76
81
|
/**
|
|
77
82
|
* The zero slot value.
|
|
78
83
|
*/
|
|
@@ -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
|
}
|