zkjson 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. package/doc.js +41 -3
  2. package/package.json +1 -1
package/doc.js CHANGED
@@ -1,16 +1,54 @@
1
+ const snarkjs = require("snarkjs")
2
+ const { resolve } = require("path")
1
3
  const { pad, toSignal, encode, encodePath, encodeVal } = require("./encoder")
2
4
 
3
5
  module.exports = class Doc {
4
- constructor({ size_val = 5, size_path = 5, size_json = 256 }) {
6
+ constructor({ size_val = 5, size_path = 5, size_json = 256, wasm, zkey }) {
5
7
  this.size_val = size_val
6
8
  this.size_path = size_path
7
9
  this.size_json = size_json
10
+ this.wasm = wasm
11
+ this.zkey = zkey
8
12
  }
9
- async getInputs({ json, path, val }) {
13
+ async getInputs({ json, path }) {
10
14
  return {
11
15
  json: pad(toSignal(encode(json)), this.size_json),
12
16
  path: pad(toSignal(encodePath(path)), this.size_path),
13
- val: pad(toSignal(encodeVal(val)), this.size_val),
17
+ val: pad(toSignal(encodeVal(this.getVal(json, path))), this.size_val),
14
18
  }
15
19
  }
20
+ _getVal(j, p) {
21
+ if (p.length === 0) {
22
+ return j
23
+ } else {
24
+ const sp = p[0].split("[")
25
+ for (let v of sp) {
26
+ if (/]$/.test(v)) {
27
+ j = j[v.replace(/]$/, "") * 1]
28
+ } else {
29
+ j = j[v]
30
+ }
31
+ }
32
+ return this._getVal(j, p.slice(1))
33
+ }
34
+ }
35
+ getVal(j, p) {
36
+ if (p === "") return j
37
+ return this._getVal(j, p.split("."))
38
+ }
39
+ async genProof(json, path) {
40
+ const inputs = await this.getInputs(json, path)
41
+ const { proof, publicSignals } = await snarkjs.groth16.fullProve(
42
+ inputs,
43
+ this.wasm,
44
+ this.zkey
45
+ )
46
+ return [
47
+ ...proof.pi_a.slice(0, 2),
48
+ ...proof.pi_b[0].slice(0, 2).reverse(),
49
+ ...proof.pi_b[1].slice(0, 2).reverse(),
50
+ ...proof.pi_c.slice(0, 2),
51
+ ...publicSignals,
52
+ ]
53
+ }
16
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "main": "index.js",
6
6
  "license": "MIT",