-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
155 lines (134 loc) · 3.87 KB
/
cli.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* eslint-disable no-console */
import clProcessor from './src/clprocessor.js';
import * as zkey from './src/zkey.js';
import { Decoder } from './src/decode.js';
import Logger from 'logplease';
import fs from 'fs'
import { readFileSync } from "fs";
import os from 'os';
const logger = Logger.create('UniGro16js', {showTimestamp: false});
Logger.setLogLevel('INFO');
const commands = [
{
cmd: 'setup [paramName] [RSName] [QAPName]',
description: 'setup phase',
alias: ['st'],
options: "-verbose|v",
action: setup,
},
{
cmd: 'decode [circuitName] [instanceId]',
description: 'decode phase',
alias: ['st'],
options: "-verbose|v",
action: decode,
},
{
cmd: 'derive [RSName] [cRSName] [circuitName] [QAPName]',
description: 'derive phase',
alias: ['dr'],
options: "-verbose|v",
action: derive,
},
{
cmd: 'prove [qapName] [cRSName] [proofName] [circuitName] [instanceId]',
description: 'prove phase',
alias: ['pr'],
options: "-verbose|v",
action: groth16Prove,
},
{
cmd: 'verify [proofName] [cRSName] [circuitName] [instanceId]',
description: 'verify phase',
alias: ['vr'],
options: "-verbose|v",
action: groth16Verify,
},
{
cmd: 'qap-all [curveName] [s_D] [min_s_max]',
description: 'build all qap',
alias: ['qa'],
options: "-verbose|v",
action: buildQAP,
},
{
cmd: 'qap-single [paramName] [id]',
description: 'build a single qap',
alias: ['qs'],
options: "-verbose|v",
action: buildSingleQAP,
},
];
clProcessor(commands).then( (res) => {
process.exit(res);
}, (err) => {
logger.error(err);
process.exit(1);
});
async function buildQAP(params, options) {
const curveName = params[0];
const sD = params[1];
const minSMax = params[2];
if (options.verbose) Logger.setLogLevel("DEBUG");
return zkey.buildQAP(curveName, sD, minSMax, logger);
}
async function buildSingleQAP(params, options) {
const paramName = params[0];
const id = params[1];
if (options.verbose) Logger.setLogLevel("DEBUG");
return zkey.buildSingleQAP(paramName, id, logger);
}
async function setup(params, options) {
const paramName = params[0];
const RSName = params[1];
const QAPName = params[2];
if (options.verbose) Logger.setLogLevel("DEBUG");
return zkey.setup(paramName, RSName, QAPName, logger);
}
async function decode(params, options) {
const circuitName = params[0]
const instanceid = params[1]
const system = os.platform()
const slash = system === 'darwin' ? '/' : '\\'
const json = fs.readFileSync(`${circuitName}/config.json`, 'utf8')
const jsonData = JSON.parse(json);
const { config, code } = jsonData
const decode = new Decoder()
if (options.verbose) Logger.setLogLevel("DEBUG");
return decode.runCode(
Buffer.from(code.join(''), 'hex'),
config,
circuitName,
instanceid
)
}
async function derive(params, options) {
const RSName = params[0];
const cRSName = params[1];
const circuitName = params[2];
const QAPName = params[3];
if (options.verbose) Logger.setLogLevel("DEBUG");
return zkey.derive(RSName, cRSName, circuitName, QAPName, logger);
}
async function groth16Prove(params, options) {
const qapName = params[0]
const cRSName = params[1];
const proofName = params[2];
const circuitName = params[3];
const instanceId = params[4];
if (options.verbose) Logger.setLogLevel("DEBUG");
return zkey.groth16Prove(qapName, cRSName, proofName, circuitName, instanceId, logger);
}
async function groth16Verify(params, options) {
const proofName = params[0];
const cRSName = params[1];
const circuitName = params[2];
const instanceId = params[3] || '1';
if (options.verbose) Logger.setLogLevel("DEBUG");
const isValid = await zkey.groth16Verify(proofName, cRSName, circuitName, instanceId, logger);
if (isValid === true) {
console.log('VALID')
} else {
console.log('INVALID')
}
}