This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircuit.ts
52 lines (46 loc) · 1.53 KB
/
circuit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import crypto from "crypto";
// @ts-ignore
import { groth16 } from "snarkjs";
// @ts-ignore
import circomlib from "circomlib";
/** Generate random buffer of specified byte length */
const rbuffer = (nbytes: number) => crypto.randomBytes(nbytes);
/** Compute pedersen hash */
function pedersenHash(data: Buffer): BigInt {
return circomlib.babyJub.unpackPoint(circomlib.pedersenHash.hash(data))[0];
}
async function genProofArgs(proof: any, pub: any) {
proof = unstringifyBigInts(proof);
pub = unstringifyBigInts(pub);
const calldata = await groth16.exportSolidityCallData(proof, pub);
const args = JSON.parse("[" + calldata + "]");
return args;
}
// source: https://github.com/iden3/ffjavascript/blob/master/src/utils_bigint.js
function unstringifyBigInts(o: any): any {
if (typeof o == "string" && /^[0-9]+$/.test(o)) {
return BigInt(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
} else if (typeof o == "object") {
const res: any = {};
const keys = Object.keys(o);
keys.forEach(k => {
res[k] = unstringifyBigInts(o[k]);
});
return res;
} else {
return o;
}
}
// source: https://github.com/no2chem/bigint-buffer/blob/c4d61b5c4fcaab36c55130840e906c162dfce646/src/index.ts#L25
function toBigIntLE(buf: Buffer) {
const reversed = Buffer.from(buf);
reversed.reverse();
const hex = reversed.toString("hex");
if (hex.length === 0) {
return BigInt(0);
}
return BigInt(`0x${hex}`);
}
export { genProofArgs, unstringifyBigInts, toBigIntLE, rbuffer, pedersenHash, groth16 };