zkjson 0.1.23 → 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/nft.js +131 -0
  2. package/package.json +1 -1
  3. package/uint.js +3 -0
package/nft.js ADDED
@@ -0,0 +1,131 @@
1
+ const crypto = require("crypto")
2
+ const snarkjs = require("snarkjs")
3
+ const { query, pad, path, val } = require("./encoder")
4
+ const { push, arr } = require("./uint")
5
+ const { parse } = require("./parse")
6
+
7
+ function coerce(o) {
8
+ if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") return o
9
+ if (o instanceof ArrayBuffer) return new Uint8Array(o)
10
+ if (ArrayBuffer.isView(o)) {
11
+ return new Uint8Array(o.buffer, o.byteOffset, o.byteLength)
12
+ }
13
+ throw new Error("Unknown type, must be binary type")
14
+ }
15
+
16
+ const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
17
+ const BASE = ALPHABET.length
18
+ const LEADER = ALPHABET.charAt(0)
19
+ const FACTOR = Math.log(BASE) / Math.log(256)
20
+ const iFACTOR = Math.log(256) / Math.log(BASE)
21
+
22
+ function toCID(source) {
23
+ if (source instanceof Uint8Array);
24
+ else if (ArrayBuffer.isView(source)) {
25
+ source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)
26
+ } else if (Array.isArray(source)) {
27
+ source = Uint8Array.from(source)
28
+ }
29
+ if (!(source instanceof Uint8Array)) {
30
+ throw new TypeError("Expected Uint8Array")
31
+ }
32
+ if (source.length === 0) return ""
33
+
34
+ var zeroes = 0
35
+ var length = 0
36
+ var pbegin = 0
37
+ var pend = source.length
38
+ while (pbegin !== pend && source[pbegin] === 0) {
39
+ pbegin++
40
+ zeroes++
41
+ }
42
+
43
+ var size = ((pend - pbegin) * iFACTOR + 1) >>> 0
44
+ var b58 = new Uint8Array(size)
45
+ while (pbegin !== pend) {
46
+ var carry = source[pbegin]
47
+ var i = 0
48
+ for (
49
+ var it1 = size - 1;
50
+ (carry !== 0 || i < length) && it1 !== -1;
51
+ it1--, i++
52
+ ) {
53
+ carry += (256 * b58[it1]) >>> 0
54
+ b58[it1] = carry % BASE >>> 0
55
+ carry = (carry / BASE) >>> 0
56
+ }
57
+ if (carry !== 0) {
58
+ throw new Error("Non-zero carry")
59
+ }
60
+ length = i
61
+ pbegin++
62
+ }
63
+ var it2 = size - length
64
+ while (it2 !== size && b58[it2] === 0) {
65
+ it2++
66
+ }
67
+ var str = LEADER.repeat(zeroes)
68
+ for (; it2 < size; ++it2) {
69
+ str += ALPHABET.charAt(b58[it2])
70
+ }
71
+ return str
72
+ }
73
+
74
+ class NFT {
75
+ constructor({
76
+ size_val = 5,
77
+ size_path = 5,
78
+ size_json = 256,
79
+ nBlocks = 10,
80
+ wasm,
81
+ zkey,
82
+ json,
83
+ }) {
84
+ this.json = json
85
+ this.wasm = wasm
86
+ this.zkey = zkey
87
+ this.nBlocks = nBlocks
88
+ this.size_val = size_val
89
+ this.size_path = size_path
90
+ this.size_json = size_json
91
+ }
92
+ path(pth) {
93
+ return pad(path(pth), this.size_path)
94
+ }
95
+ val(pth) {
96
+ return pad(val(this.json[pth]), this.size_val)
97
+ }
98
+ query(pth, cond) {
99
+ return pad(query(cond), this.size_val)
100
+ }
101
+
102
+ async zkp(pth, cond) {
103
+ const str = new TextEncoder().encode(JSON.stringify(this.json))
104
+ let encoded = arr(256)
105
+ for (let v of Array.from(str))
106
+ encoded = push(encoded, this.size_json, 76, v)
107
+ const enc = parse(encoded, 256)
108
+ const _path = this.path(pth)
109
+ const _val = Array.isArray(cond) ? this.query(pth, cond) : this.val(pth)
110
+ const inputs = { path: _path, val: _val, encoded }
111
+ const { proof, publicSignals } = await snarkjs.groth16.fullProve(
112
+ inputs,
113
+ this.wasm,
114
+ this.zkey,
115
+ )
116
+ return [
117
+ ...proof.pi_a.slice(0, 2),
118
+ ...proof.pi_b[0].slice(0, 2).reverse(),
119
+ ...proof.pi_b[1].slice(0, 2).reverse(),
120
+ ...proof.pi_c.slice(0, 2),
121
+ ...publicSignals,
122
+ ]
123
+ }
124
+ cid() {
125
+ const str = new TextEncoder().encode(JSON.stringify(this.json))
126
+ const hash = coerce(crypto.createHash("sha256").update(str).digest())
127
+ return toCID(new Uint8Array([18, hash.length, ...Array.from(hash)]))
128
+ }
129
+ }
130
+
131
+ module.exports = NFT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
package/uint.js CHANGED
@@ -464,6 +464,9 @@ function push(json, size, digit, c, overflow = 8) {
464
464
  }
465
465
  return json
466
466
  }
467
+ function str(arr) {
468
+ return arr.map(n => n.toString())
469
+ }
467
470
  function bn(arr) {
468
471
  if (typeof arr == "number") return BigInt(arr)
469
472
  if (!Array.isArray(arr)) return arr