@javakha77/circomlibjs-hinkal-fork 0.0.11

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.
@@ -0,0 +1,63 @@
1
+ import { ethers } from 'ethers';
2
+ import * as poseidonLite from 'poseidon-lite';
3
+ import { toBigInt } from './amounts.utils.js';
4
+
5
+ // poseidon-lite exposes `poseidon1`..`poseidon16`; wrap them in a `buildPoseidon`-shaped
6
+ // factory (matching the WASM reference) so callers — and the existing
7
+ // `poseidon.F.toString(...)` pattern — keep working on RN.
8
+ export const buildPoseidon = () => {
9
+ const poseidon = (inputs, initState = 0, nOut = 1) => {
10
+ const fn = poseidonLite[`poseidon${inputs.length}`];
11
+ if (!fn) throw new Error(`Poseidon: arity ${inputs.length} not supported (1..16)`);
12
+ const formattedInputs = inputs.map((v) => BigInt(v));
13
+
14
+ if (nOut > 1) {
15
+ const results = fn(formattedInputs, nOut);
16
+ return results.map(v => BigInt(v));
17
+ }
18
+
19
+ const res = fn(formattedInputs);
20
+ return BigInt(res);
21
+ };
22
+
23
+ poseidon.F = { toString: (v) => BigInt(v).toString(),
24
+ e: (v) => BigInt(v),
25
+ eq: (a, b) => BigInt(a) === BigInt(b)
26
+ };
27
+ return poseidon;
28
+ };
29
+
30
+ class PoseidonHolder {
31
+ poseidon = undefined;
32
+
33
+ async init() {
34
+ if (this.poseidon) return;
35
+ this.poseidon = buildPoseidon();
36
+ return this.poseidon;
37
+ }
38
+
39
+ getPoseidon() {
40
+ return this.poseidon;
41
+ }
42
+ }
43
+
44
+ export const PoseidonRN = new PoseidonHolder();
45
+
46
+ export function poseidonFunction(...args) {
47
+ const poseidon = PoseidonRN.getPoseidon();
48
+ return toBigInt(poseidon.F.toString(poseidon(args)));
49
+ }
50
+
51
+ /**
52
+ * hashing algorithm implementation
53
+ * @param args rest parameter to take unlimited number of arguments
54
+ * @returns poseidon hash in base 16
55
+ */
56
+ export const poseidonHash = (...args) => {
57
+ const poseidon = PoseidonRN.getPoseidon();
58
+ return ethers.toBeHex(poseidon.F.toString(poseidon([...args])));
59
+ };
60
+
61
+ /**
62
+ * @typedef {(...args: unknown[]) => string} PoseidonHasher
63
+ */