-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
56 lines (46 loc) · 2.15 KB
/
main.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
51
52
53
54
55
56
/**
* This script verifies the proofs generated by the script in `example/script`.
*
* It loads json files in `example/json` and verifies them using the wasm bindings
* in `example/verifier/pkg/sp1_wasm_verifier.js`.
*/
import * as wasm from "../../verifier/pkg/sp1_wasm_verifier.js"
import fs from 'node:fs'
import path from 'node:path'
import assert from 'node:assert'
// Convert a hexadecimal string to a Uint8Array
export const fromHexString = (hexString) =>
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const files = fs.readdirSync("../json");
// Iterate through each file in the data directory
for (const file of files) {
try {
// Read and parse the JSON content of the file
const fileContent = fs.readFileSync(path.join("../json", file), 'utf8');
const proof_json = JSON.parse(fileContent);
// Determine the ZKP type (Groth16 or Plonk) based on the filename
const zkpType = file.toLowerCase().includes('groth16') ? 'groth16' : 'plonk';
const proof = fromHexString(proof_json.proof);
const public_inputs = fromHexString(proof_json.public_inputs);
const vkey_hash = proof_json.vkey_hash;
// Get the values using DataView.
const view = new DataView(public_inputs.buffer);
// Read each 32-bit (4 byte) integer as little-endian
const n = view.getUint32(0, true);
const a = view.getUint32(4, true);
const b = view.getUint32(8, true);
console.log(`n: ${n}`);
console.log(`a: ${a}`);
console.log(`b: ${b}`);
// Select the appropriate verification function and verification key based on ZKP type
const verifyFunction = zkpType === 'groth16' ? wasm.verify_groth16 : wasm.verify_plonk;
const startTime = performance.now();
const result = verifyFunction(proof, public_inputs, vkey_hash);
const endTime = performance.now();
console.log(`${zkpType} verification took ${endTime - startTime}ms`);
assert(result);
console.log(`Proof in ${file} is valid.`);
} catch (error) {
console.error(`Error processing ${file}: ${error.message}`);
}
}