@javakha77/circomlibjs-hinkal-fork 0.0.13 → 0.0.14
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/build/main.cjs +24 -29
- package/main.js +1 -1
- package/package.json +1 -1
- package/src/EddsaRN.js +2 -3
- package/src/poseidonRN.js +23 -43
- package/src/protocol.constants.js +1 -3
- package/src/serialize.utils.js +5 -5
package/build/main.cjs
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var ffjavascript = require('ffjavascript');
|
|
4
4
|
var blake1_js = require('@noble/hashes/blake1.js');
|
|
5
|
-
require('ethers');
|
|
6
5
|
var poseidonLite = require('poseidon-lite');
|
|
7
6
|
|
|
8
7
|
function _interopNamespaceDefault(e) {
|
|
@@ -25641,6 +25640,22 @@ class BabyJubRN {
|
|
|
25641
25640
|
}
|
|
25642
25641
|
}
|
|
25643
25642
|
|
|
25643
|
+
const toBigInt = (v) => {
|
|
25644
|
+
switch (typeof v) {
|
|
25645
|
+
case 'bigint': return v;
|
|
25646
|
+
case 'boolean': return v ? 1n : 0n;
|
|
25647
|
+
case 'number':
|
|
25648
|
+
if (!Number.isInteger(v)) throw new TypeError(`Poseidon: non-integer Number ${v}`);
|
|
25649
|
+
return BigInt(v);
|
|
25650
|
+
case 'string': {
|
|
25651
|
+
try { return BigInt(v.trim()); }
|
|
25652
|
+
catch { throw new TypeError(`Poseidon: cannot parse string "${v}" as an integer`); }
|
|
25653
|
+
}
|
|
25654
|
+
default:
|
|
25655
|
+
throw new TypeError(`Poseidon.F: unsupported value of type ${typeof v}`);
|
|
25656
|
+
}
|
|
25657
|
+
};
|
|
25658
|
+
|
|
25644
25659
|
// poseidon-lite exposes `poseidon1`..`poseidon16`; wrap them in a `buildPoseidon`-shaped
|
|
25645
25660
|
// factory (matching the WASM reference) so callers — and the existing
|
|
25646
25661
|
// `poseidon.F.toString(...)` pattern — keep working on RN.
|
|
@@ -25648,7 +25663,7 @@ const buildPoseidon = () => {
|
|
|
25648
25663
|
const poseidon = (inputs, initState = 0, nOut = 1) => {
|
|
25649
25664
|
const fn = poseidonLite__namespace[`poseidon${inputs.length}`];
|
|
25650
25665
|
if (!fn) throw new Error(`Poseidon: arity ${inputs.length} not supported (1..16)`);
|
|
25651
|
-
const formattedInputs = inputs.map((v) =>
|
|
25666
|
+
const formattedInputs = inputs.map((v) => toBigInt(v));
|
|
25652
25667
|
|
|
25653
25668
|
if (nOut > 1) {
|
|
25654
25669
|
const results = fn(formattedInputs, nOut);
|
|
@@ -25658,33 +25673,14 @@ const buildPoseidon = () => {
|
|
|
25658
25673
|
const res = fn(formattedInputs);
|
|
25659
25674
|
return BigInt(res);
|
|
25660
25675
|
};
|
|
25661
|
-
poseidon.F = {
|
|
25662
|
-
|
|
25663
|
-
|
|
25664
|
-
|
|
25676
|
+
poseidon.F = {
|
|
25677
|
+
toString: (v) => toBigInt(v).toString(),
|
|
25678
|
+
e: (v) => toBigInt(v),
|
|
25679
|
+
eq: (a, b) => toBigInt(a) === toBigInt(b),
|
|
25680
|
+
};
|
|
25665
25681
|
return poseidon;
|
|
25666
25682
|
};
|
|
25667
25683
|
|
|
25668
|
-
class PoseidonHolder {
|
|
25669
|
-
poseidon = undefined;
|
|
25670
|
-
|
|
25671
|
-
async init() {
|
|
25672
|
-
if (this.poseidon) return;
|
|
25673
|
-
this.poseidon = buildPoseidon();
|
|
25674
|
-
return this.poseidon;
|
|
25675
|
-
}
|
|
25676
|
-
|
|
25677
|
-
getPoseidon() {
|
|
25678
|
-
return this.poseidon;
|
|
25679
|
-
}
|
|
25680
|
-
}
|
|
25681
|
-
|
|
25682
|
-
const PoseidonRN = new PoseidonHolder();
|
|
25683
|
-
|
|
25684
|
-
/**
|
|
25685
|
-
* @typedef {(...args: unknown[]) => string} PoseidonHasher
|
|
25686
|
-
*/
|
|
25687
|
-
|
|
25688
25684
|
/* eslint-disable no-bitwise */
|
|
25689
25685
|
// React Native port of circomlibjs-hinkal-fork/src/eddsa.js (Poseidon EdDSA over BabyJubJub).
|
|
25690
25686
|
|
|
@@ -25755,14 +25751,13 @@ class EddsaRN {
|
|
|
25755
25751
|
}
|
|
25756
25752
|
|
|
25757
25753
|
const buildEddsaRN = async () => {
|
|
25758
|
-
|
|
25759
|
-
return new EddsaRN(new BabyJubRN(), PoseidonRN.getPoseidon());
|
|
25754
|
+
return new EddsaRN(new BabyJubRN(), buildPoseidon());
|
|
25760
25755
|
};
|
|
25761
25756
|
|
|
25762
25757
|
exports.BabyJubRN = BabyJubRN;
|
|
25763
|
-
exports.PoseidonRN = PoseidonRN;
|
|
25764
25758
|
exports.buildBabyjub = buildBabyJub;
|
|
25765
25759
|
exports.buildEddsa = buildEddsa;
|
|
25766
25760
|
exports.buildEddsaRN = buildEddsaRN;
|
|
25767
25761
|
exports.buildPoseidon = buildPoseidon$1;
|
|
25762
|
+
exports.buildPoseidonRN = buildPoseidon;
|
|
25768
25763
|
exports.buildPoseidonWasm = buildPoseidonWasm;
|
package/main.js
CHANGED
package/package.json
CHANGED
package/src/EddsaRN.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// React Native port of circomlibjs-hinkal-fork/src/eddsa.js (Poseidon EdDSA over BabyJubJub).
|
|
3
3
|
import { blake512 } from '@noble/hashes/blake1.js';
|
|
4
4
|
import { BabyJubRN } from './babyjubRN.js';
|
|
5
|
-
import {
|
|
5
|
+
import { buildPoseidon as buildPoseidonRN } from './poseidonRN.js';
|
|
6
6
|
import { mod } from './bigint-math.utils.js';
|
|
7
7
|
|
|
8
8
|
const BABYJUB_ORDER = 21888242871839275222246405745257275088614511777268538073601725287587578984328n;
|
|
@@ -72,6 +72,5 @@ export class EddsaRN {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
export const buildEddsaRN = async () => {
|
|
75
|
-
|
|
76
|
-
return new EddsaRN(new BabyJubRN(), PoseidonRN.getPoseidon());
|
|
75
|
+
return new EddsaRN(new BabyJubRN(), buildPoseidonRN());
|
|
77
76
|
};
|
package/src/poseidonRN.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
|
-
import { ethers } from 'ethers';
|
|
2
1
|
import * as poseidonLite from 'poseidon-lite';
|
|
3
|
-
|
|
2
|
+
|
|
3
|
+
const toBigInt = (v) => {
|
|
4
|
+
switch (typeof v) {
|
|
5
|
+
case 'bigint': return v;
|
|
6
|
+
case 'boolean': return v ? 1n : 0n;
|
|
7
|
+
case 'number':
|
|
8
|
+
if (!Number.isInteger(v)) throw new TypeError(`Poseidon: non-integer Number ${v}`);
|
|
9
|
+
return BigInt(v);
|
|
10
|
+
case 'string': {
|
|
11
|
+
try { return BigInt(v.trim()); }
|
|
12
|
+
catch { throw new TypeError(`Poseidon: cannot parse string "${v}" as an integer`); }
|
|
13
|
+
}
|
|
14
|
+
default:
|
|
15
|
+
throw new TypeError(`Poseidon.F: unsupported value of type ${typeof v}`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
4
18
|
|
|
5
19
|
// poseidon-lite exposes `poseidon1`..`poseidon16`; wrap them in a `buildPoseidon`-shaped
|
|
6
20
|
// factory (matching the WASM reference) so callers — and the existing
|
|
@@ -9,7 +23,7 @@ export const buildPoseidon = () => {
|
|
|
9
23
|
const poseidon = (inputs, initState = 0, nOut = 1) => {
|
|
10
24
|
const fn = poseidonLite[`poseidon${inputs.length}`];
|
|
11
25
|
if (!fn) throw new Error(`Poseidon: arity ${inputs.length} not supported (1..16)`);
|
|
12
|
-
const formattedInputs = inputs.map((v) =>
|
|
26
|
+
const formattedInputs = inputs.map((v) => toBigInt(v));
|
|
13
27
|
|
|
14
28
|
if (nOut > 1) {
|
|
15
29
|
const results = fn(formattedInputs, nOut);
|
|
@@ -19,44 +33,10 @@ export const buildPoseidon = () => {
|
|
|
19
33
|
const res = fn(formattedInputs);
|
|
20
34
|
return BigInt(res);
|
|
21
35
|
};
|
|
22
|
-
poseidon.F = {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
36
|
+
poseidon.F = {
|
|
37
|
+
toString: (v) => toBigInt(v).toString(),
|
|
38
|
+
e: (v) => toBigInt(v),
|
|
39
|
+
eq: (a, b) => toBigInt(a) === toBigInt(b),
|
|
40
|
+
};
|
|
26
41
|
return poseidon;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
class PoseidonHolder {
|
|
30
|
-
poseidon = undefined;
|
|
31
|
-
|
|
32
|
-
async init() {
|
|
33
|
-
if (this.poseidon) return;
|
|
34
|
-
this.poseidon = buildPoseidon();
|
|
35
|
-
return this.poseidon;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
getPoseidon() {
|
|
39
|
-
return this.poseidon;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export const PoseidonRN = new PoseidonHolder();
|
|
44
|
-
|
|
45
|
-
export function poseidonFunction(...args) {
|
|
46
|
-
const poseidon = PoseidonRN.getPoseidon();
|
|
47
|
-
return toBigInt(poseidon.F.toString(poseidon(args)));
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* hashing algorithm implementation
|
|
52
|
-
* @param args rest parameter to take unlimited number of arguments
|
|
53
|
-
* @returns poseidon hash in base 16
|
|
54
|
-
*/
|
|
55
|
-
export const poseidonHash = (...args) => {
|
|
56
|
-
const poseidon = PoseidonRN.getPoseidon();
|
|
57
|
-
return ethers.toBeHex(poseidon.F.toString(poseidon([...args])));
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* @typedef {(...args: unknown[]) => string} PoseidonHasher
|
|
62
|
-
*/
|
|
42
|
+
};
|
|
@@ -1,3 +1 @@
|
|
|
1
|
-
export const CIRCOM_P = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
|
2
|
-
export const CIRCOM_P_HALF = CIRCOM_P / 2n;
|
|
3
|
-
export const CIRCOM_MERKLE_LENGTH = 25;
|
|
1
|
+
export const CIRCOM_P = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
|
package/src/serialize.utils.js
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
// Safe JSON.stringify wrapper. Converts BigInt -> string so JSON serialization never throws.
|
|
6
|
-
|
|
6
|
+
export const safeJsonStringify = (value, replacer = null, space) =>
|
|
7
7
|
JSON.stringify(
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
value,
|
|
9
|
+
(key, nestedValue) => {
|
|
10
10
|
const next = typeof replacer === 'function' ? replacer(key, nestedValue) : nestedValue;
|
|
11
11
|
return typeof next === 'bigint' ? next.toString() : next;
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
},
|
|
13
|
+
space,
|
|
14
14
|
);
|