Skip to content

Commit

Permalink
added support for BigUint64Array and fixed type resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
cf committed Oct 15, 2023
1 parent 5d01b9d commit 9e53cf3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ The permutation function exposed by the Poseidon2 class is one-to-one compatible
npm install poseidon2
```

## Usage For Goldilocks Field, T = 12
#### Note For TypeScript Users:
If you see the error "Cannot find module 'poseidon2/goldilocks-12' or its corresponding type declarations.ts(2307)", update your tsconfig.json to set "moduleResolution": "NodeNext".



## Usage For Goldilocks Field, T = 12
### Permute

```javascript
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "poseidon2",
"license": "MIT",
"author": "Open Asset Standards",
"version": "0.4.1",
"version": "0.4.2",
"description": "A zero dependency TypeScript implementation of the Poseidon2 Hash Function",
"main": "./dist/poseidon2.cjs",
"module": "./dist/poseidon2.mjs",
Expand All @@ -20,7 +20,7 @@
},
"scripts": {
"dev": "vite --host",
"build": "rimraf ./dist && tsc && vite build && dts-bundle-generator --config ./dts-bundle-generator.config.ts && tsc -p ./tsconfig.types.json",
"build": "rimraf ./dist && tsc && vite build && dts-bundle-generator --config ./dts-bundle-generator.config.ts",
"test": "vitest",
"test:coverage": "vitest --coverage",
"lint:scripts": "eslint . --ext .ts",
Expand Down
27 changes: 14 additions & 13 deletions src/goldilocks-12/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const SPONGE_RATE = 8;
const SPONGE_WIDTH = 12;
const ZERO = BigInt(0);
const F = Poseidon2Goldilocks12.primeField;

function hashNToMNoPad(inputsBase: bigint[], numOutputs: number): bigint[] {
const inputs: bigint[] = inputsBase.map((x: any) => F.e(x));
function hashNToMNoPad(
inputs: bigint[] | BigUint64Array,
numOutputs: number
): bigint[] {
const inputsLength = inputs.length;

let state: bigint[] = [
Expand All @@ -27,14 +28,14 @@ function hashNToMNoPad(inputsBase: bigint[], numOutputs: number): bigint[] {
];
const nChunks = Math.floor(inputsLength / SPONGE_RATE);
for (let i = 0; i < nChunks; i++) {
state[0] = inputs[i * 8 + 0];
state[1] = inputs[i * 8 + 1];
state[2] = inputs[i * 8 + 2];
state[3] = inputs[i * 8 + 3];
state[4] = inputs[i * 8 + 4];
state[5] = inputs[i * 8 + 5];
state[6] = inputs[i * 8 + 6];
state[7] = inputs[i * 8 + 7];
state[0] = F.e(inputs[i * 8]);
state[1] = F.e(inputs[i * 8 + 1]);
state[2] = F.e(inputs[i * 8 + 2]);
state[3] = F.e(inputs[i * 8 + 3]);
state[4] = F.e(inputs[i * 8 + 4]);
state[5] = F.e(inputs[i * 8 + 5]);
state[6] = F.e(inputs[i * 8 + 6]);
state[7] = F.e(inputs[i * 8 + 7]);
const n_state = Poseidon2Goldilocks12.permute(state);
state[0] = n_state[0];
state[1] = n_state[1];
Expand All @@ -53,7 +54,7 @@ function hashNToMNoPad(inputsBase: bigint[], numOutputs: number): bigint[] {
const remaining = inputsLength - start;
if (remaining > 0 && remaining < state.length) {
for (let i = 0; i < remaining; i++) {
state[i] = inputs[start + i];
state[i] = F.e(inputs[start + i]);
}
const n_state = Poseidon2Goldilocks12.permute(state);
state[0] = n_state[0];
Expand Down Expand Up @@ -93,7 +94,7 @@ function hashPad(input: bigint[]) {
paddedInput.push(BigInt(1));
return hashNToMNoPad(paddedInput, 4).slice(0, 4);
}
function hashNoPad(input: bigint[]): IHashOut {
function hashNoPad(input: bigint[] | BigUint64Array): IHashOut {
const output = hashNToMNoPad(input, 4);
return [output[0], output[1], output[2], output[3]];
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"moduleResolution": "NodeNext",
"strict": true,
"sourceMap": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 9e53cf3

Please sign in to comment.