-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathct.js
50 lines (38 loc) · 1.31 KB
/
ct.js
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
const snarkjs = require("snarkjs");
const fs = require("fs");
// Example circuit for verifying a simple confidential transaction
const circuitDef = `
template ConfidentialTransaction() {
signal input v; // Transaction amount
signal input r; // Blinding factor
signal input C; // Commitment
signal output valid;
// Pedersen commitment check
C === v * H + r * G;
// Range proof (simplified for illustration)
valid <== (v >= 0) && (v < MAX_VALUE);
}
component main = ConfidentialTransaction();
`;
async function main() {
// Compile the circuit
const { r1cs, wasm } = await snarkjs.compile(circuitDef);
// Example inputs
const inputs = {
v: 5,
r: 3,
C: 5 * H + 3 * G // Example commitment
};
// Generate the witness
const witness = await snarkjs.wtns.calculate(r1cs, inputs, wasm);
// Setup phase (trusted setup)
const { zkey } = await snarkjs.groth16.setup(r1cs);
// Generate proof
const { proof, publicSignals } = await snarkjs.groth16.prove(zkey, witness);
// Verification key
const vkey = await snarkjs.zKey.exportVerificationKey(zkey);
// Verify proof
const isValid = await snarkjs.groth16.verify(vkey, publicSignals, proof);
console.log("Proof is valid:", isValid);
}
main().catch(console.error);