zkjson 0.5.2 → 0.5.3

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.
package/encoder.js CHANGED
@@ -394,24 +394,19 @@ function decode(arr) {
394
394
  return json
395
395
  }
396
396
 
397
- const toIndex = str => {
398
- return (
399
- "1" +
400
- str
401
- .split("")
402
- .map(s => base64Map[s])
403
- .join("")
404
- )
397
+ function toIndex(rawStr) {
398
+ const b64url = Buffer.from(rawStr, "utf8").toString("base64url")
399
+ const bi = BigInt("0x" + Buffer.from(b64url, "base64url").toString("hex"))
400
+ return bi.toString(10)
405
401
  }
406
402
 
407
- const fromIndex = id => {
408
- let _id = id.toString().split("")
409
- _id.shift()
410
- return splitEvery(2, _id)
411
- .map(s => {
412
- return strMap[s.join("")]
413
- })
414
- .join("")
403
+ function fromIndex(idxStr) {
404
+ const bi = BigInt(idxStr)
405
+ let hex = bi.toString(16)
406
+ if (hex.length % 2) hex = "0" + hex
407
+ const buf = Buffer.from(hex, "hex")
408
+ const b64url = buf.toString("base64url")
409
+ return Buffer.from(b64url, "base64url").toString("utf8")
415
410
  }
416
411
 
417
412
  function toSignal(arr) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zkjson",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Zero Knowledge Provable JSON",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
package/decoder-v2.js DELETED
@@ -1,604 +0,0 @@
1
- const {
2
- strmap_rev,
3
- base64_rev,
4
- bits,
5
- tobits,
6
- strmap,
7
- base64,
8
- } = require("./utils.js")
9
-
10
- class decoder {
11
- constructor() {
12
- this.c = 0
13
- this.json = null
14
- }
15
- n(len) {
16
- let result = 0
17
- for (let i = 0; i < len; i++) {
18
- const bitPos = this.c + i
19
- const byteIndex = Math.floor(bitPos / 8)
20
- const bitIndex = 7 - (bitPos % 8)
21
- const bit = (this.o[byteIndex] >> bitIndex) & 1
22
- result = (result << 1) | bit
23
- }
24
- this.c += len
25
- return result
26
- }
27
- leb128() {
28
- let n = 0
29
- let i = 0
30
- let len_len = null
31
- do {
32
- len_len = this.n(8)
33
- n += Math.pow(128, i) * (len_len >= 128 ? len_len - 128 : len_len)
34
- i++
35
- } while (len_len >= 128)
36
- return n
37
- }
38
- decode(v) {
39
- this.o = v
40
- this.c = 0
41
- this.nc = 0
42
- this.bc = 0
43
- this.len = 0
44
- this.str_len = 0
45
- this.str = {}
46
- this.str_rev = {}
47
- this.key_length = 0
48
- this.num_cache = null
49
- this.vflags = []
50
- this.kflags = []
51
- this.bools = []
52
- this.krefs = []
53
- this.vrefs = []
54
- this.keylens = []
55
- this.types = []
56
- this.nums = []
57
- this.keys = []
58
- this.json = {}
59
- this.single = this.n(1) === 1
60
- if (this.single) this.getSingle()
61
- else {
62
- this.getLen()
63
- this.getVflags()
64
- this.getVlinks()
65
- this.getKflags()
66
- this.getKlinks()
67
- this.getKeyLens()
68
- this.getKeys()
69
- this.getTypes()
70
- this.getBools()
71
- this.getNums()
72
- this.build()
73
- }
74
- }
75
- getSingle() {
76
- const vals = [null, true, false, "", [], {}]
77
- const isNum = this.n(1)
78
- if (isNum) {
79
- const num = this.n(6)
80
- this.json = num
81
- if (num < 63) {
82
- } else {
83
- this.json += this.leb128()
84
- }
85
- } else {
86
- const code = this.n(6)
87
- if (code < 6) {
88
- this.json = vals[code]
89
- } else if (code < 9) {
90
- if (code === 7 || code === 8) {
91
- const moved = this.uint()
92
- const n = this.uint()
93
- const neg = code === 7 ? 1 : -1
94
- this.json = (n / Math.pow(10, moved)) * neg
95
- } else {
96
- const n = this.uint()
97
- this.json = -n
98
- }
99
- } else if (code < 61) {
100
- this.json = strmap_rev[(code - 9).toString()]
101
- } else if (code === 61) {
102
- this.json = String.fromCharCode(Number(this.leb128()))
103
- } else if (code === 62) {
104
- const len = this.short()
105
- this.json = ""
106
- for (let i = 0; i < len; i++) {
107
- this.json += base64_rev[this.n(6).toString()]
108
- }
109
- } else if (code === 63) {
110
- const len = this.short()
111
- this.json = ""
112
- for (let i = 0; i < len; i++) {
113
- this.json += String.fromCharCode(Number(this.leb128()))
114
- }
115
- }
116
- }
117
- }
118
-
119
- getLen() {
120
- this.len = this.short()
121
- }
122
-
123
- show() {
124
- console.log()
125
- console.log("len", this.len)
126
- console.log("krefs", this.krefs)
127
- console.log("vrefs", this.vrefs)
128
- console.log("keylens", this.keylens)
129
- console.log("types", this.types)
130
- console.log("nums", this.nums)
131
- console.log("keys", this.keys)
132
- console.log()
133
- }
134
-
135
- getVflags() {
136
- let i = 0
137
- while (i < this.len) {
138
- const flag = this.n(1)
139
- this.vflags.push(flag)
140
- i++
141
- }
142
- }
143
-
144
- getKflags() {
145
- let i = 0
146
- while (i < this.key_length - 1) {
147
- const flag = this.n(1)
148
- this.kflags.push(flag)
149
- i++
150
- }
151
- }
152
-
153
- getKlinks() {
154
- let i = 0
155
- let count = 1
156
- let prev = 0
157
- while (i < this.kflags.length) {
158
- if (this.kflags[i] === 1) {
159
- let val = this.n(3)
160
- if (val === 0) {
161
- let len = this.short()
162
- val = this.n(3)
163
- let i3 = i
164
- for (let i2 = 0; i2 < len; i2++) {
165
- const diff = this.kflags[i3 + i2]
166
- prev = this.addKlink(diff === 1, val, prev)
167
- i++
168
- }
169
- } else {
170
- prev = this.addKlink(true, val, prev)
171
- i++
172
- }
173
- } else {
174
- let val = 0
175
- do {
176
- val = this.n(count)
177
- if (val === 0) count += 1
178
- } while (val === 0)
179
-
180
- if (val === 0) {
181
- let len = this.short()
182
- val = this.n(count)
183
- let i3 = i
184
- for (let i2 = 0; i2 < len; i2++) {
185
- const diff = this.kflags[i3 + i2]
186
- prev = this.addKlink(diff === 1, val, prev)
187
- i++
188
- }
189
- } else {
190
- prev = this.addKlink(false, val, prev)
191
- i++
192
- }
193
- }
194
- }
195
- }
196
-
197
- addVlink(diff, val, prev) {
198
- val -= 1
199
- if (diff) {
200
- if (val > 3) val = prev - (val - 3)
201
- else val += prev
202
- }
203
- this.vrefs.push(val)
204
- if (this.key_length < val) this.key_length = val
205
- prev = val
206
- return prev
207
- }
208
-
209
- addKlink(diff, val, prev) {
210
- val -= 1
211
- if (diff) {
212
- if (val > 3) val = prev - (val - 3)
213
- else val += prev
214
- }
215
- this.krefs.push(val)
216
- prev = val
217
- return prev
218
- }
219
-
220
- getVlinks() {
221
- let i = 0
222
- let count = 1
223
- let prev = 0
224
- while (i < this.vflags.length) {
225
- if (this.vflags[i] === 1) {
226
- let val = this.n(3)
227
- if (val === 0) {
228
- let len = this.short()
229
- val = this.n(3)
230
- let i3 = i
231
- for (let i2 = 0; i2 < len; i2++) {
232
- const diff = this.vflags[i3 + i2]
233
- prev = this.addVlink(diff === 1, val, prev)
234
- i++
235
- }
236
- } else {
237
- prev = this.addVlink(true, val, prev)
238
- i++
239
- }
240
- } else {
241
- let val = 0
242
- do {
243
- val = this.n(count)
244
- if (val === 0) count += 1
245
- } while (val === 0)
246
- if (val === 0) {
247
- let len = this.short()
248
- val = this.n(count)
249
- let i3 = i
250
- for (let i2 = 0; i2 < len; i2++) {
251
- const diff = this.vflags[i3 + i2]
252
- prev = this.addVlink(diff === 1, val, prev)
253
- i++
254
- }
255
- } else {
256
- prev = this.addVlink(false, val, prev)
257
- i++
258
- }
259
- }
260
- }
261
- }
262
-
263
- getKeyLens() {
264
- if (this.krefs.length === 0 && this.len === 0) return
265
- for (let i = 0; i < this.krefs.length + 1; i++) {
266
- const type = this.n(2)
267
- if (type < 2) {
268
- this.keylens.push([type])
269
- } else {
270
- const int = this.short()
271
- this.keylens.push([type, int])
272
- }
273
- }
274
- }
275
-
276
- getTypes() {
277
- let i2 = -1
278
- let len = Math.max(1, this.vrefs.length)
279
- for (let i = 0; i < len; i++) {
280
- let type = this.n(3)
281
- if (type === 0) {
282
- const count = this.short()
283
- i += count - 1
284
- let type2 = this.n(3)
285
- for (let i2 = 0; i2 < count; i2++) this.types.push(type2)
286
- } else this.types.push(type)
287
- }
288
- }
289
- short() {
290
- const x = this.n(2)
291
- return x === 3 ? this.leb128() : this.n(x === 2 ? 4 : x === 1 ? 3 : 2)
292
- }
293
- uint() {
294
- const x = this.n(2)
295
- return x === 3 ? this.leb128() : this.n(x === 2 ? 6 : x === 1 ? 4 : 3)
296
- }
297
- dint(prev = 0) {
298
- if (this.num_cache !== null) {
299
- let n = this.num_cache.diff ? prev + this.num_cache.n : this.num_cache.n
300
- this.num_cache.len -= 1
301
- if (this.num_cache.len === 0) this.num_cache = null
302
- return n
303
- }
304
- const x = this.n(2)
305
- const diff = x === 0
306
- let num = x === 3 ? this.leb128() : this.n(x === 2 ? 6 : x === 1 ? 4 : 3)
307
- if (num === 7 && diff) {
308
- const len = this.short()
309
- const x2 = this.n(2)
310
- let diff = x2 === 0
311
- let n = null
312
- if (x2 === 3) n = this.leb128()
313
- else {
314
- const d = x2 === 0 ? 3 : x2 === 1 ? 4 : 6
315
- n = this.n(d)
316
- }
317
- if (diff) {
318
- if (n > 3) n = prev - (n - 3)
319
- else n = prev + n
320
- }
321
- num = n
322
- this.num_cache = { len: len - 1, n, diff }
323
- return n
324
- } else if (diff) {
325
- if (num > 3) num = prev - (num - 3)
326
- else num = prev + num
327
- }
328
- return num
329
- }
330
- getBools() {
331
- for (let v of this.types) {
332
- if (v === 3) this.bools.push(this.n(1) === 1)
333
- }
334
- }
335
- getNums() {
336
- let prev = 0
337
- for (let v of this.types) {
338
- if (v >= 4 && v <= 6) {
339
- let num = this.dint(prev)
340
- prev = num
341
- if (v === 4) this.nums.push(num)
342
- else if (v === 5) this.nums.push(-num)
343
- else if (v === 6) {
344
- if (num === 0 || num === 4) {
345
- const moved = this.dint(prev)
346
- prev = moved
347
- const int = this.dint(prev)
348
- prev = int
349
- const neg = num === 0 ? 1 : -1
350
- this.nums.push((int / Math.pow(10, moved - 1)) * neg)
351
- } else {
352
- const moved = num > 4 ? num - 4 : num
353
- const neg = num > 4 ? -1 : 1
354
- if (moved === 1) this.nums.push(neg === -1 ? {} : [])
355
- else {
356
- const int = this.dint(prev)
357
- prev = int
358
- this.nums.push((int / Math.pow(10, moved - 1)) * neg)
359
- }
360
- }
361
- }
362
- }
363
- }
364
- }
365
- getKeys() {
366
- let arr = 0
367
- let obj = 0
368
- for (let i = 0; i < this.keylens.length; i++) {
369
- const [type, len] = this.keylens[i]
370
- if (type < 2) {
371
- this.keys.push(type === 0 ? arr++ : obj++)
372
- } else {
373
- if (type === 2) {
374
- if (len === 0) {
375
- const code = this.short()
376
- this.keys.push([code])
377
- } else {
378
- let key = ""
379
- for (let i2 = 0; i2 < len - 1; i2++) key += base64_rev[this.n(6)]
380
- this.keys.push(key)
381
- }
382
- } else {
383
- if (len === 2) {
384
- this.keys.push("")
385
- } else {
386
- let key = ""
387
- for (let i2 = 0; i2 < len - 1; i2++) {
388
- key += String.fromCharCode(Number(this.leb128()))
389
- }
390
- this.keys.push(key)
391
- }
392
- }
393
- }
394
- }
395
- }
396
- bits() {
397
- console.log(this.c, tobits(this.o, this.c))
398
- }
399
- movePads() {
400
- if (this.c % 8 > 0) this.c += 8 - (this.c % 8)
401
- }
402
- getKey(i, keys) {
403
- const k = this.keys[i - 1]
404
- if (Array.isArray(k)) keys.unshift([2, k[0]])
405
- else if (typeof k === "number") keys.unshift([this.keylens[i - 1][0], k])
406
- else keys.unshift(k)
407
- if (i > 1) {
408
- const d = this.krefs[i - 2]
409
- if (d > 0) this.getKey(this.krefs[d - 1], keys)
410
- }
411
- let i2 = 0
412
- for (let k of keys) {
413
- if (typeof k === "string") {
414
- if (typeof this.str_rev[k] === "undefined") {
415
- const _key = (this.str_len++).toString()
416
- this.str[_key] = k
417
- this.str_rev[k] = _key
418
- }
419
- } else if (Array.isArray(k) && k[0] === 2) {
420
- keys[i2] = this.str[k[1].toString()]
421
- }
422
- i2++
423
- }
424
- }
425
- build() {
426
- const get = i => {
427
- const type = this.types[i]
428
- let val = null
429
- if (type === 7 || type === 2) {
430
- let len = this.short()
431
- if (type === 2 && len === 0) {
432
- const code = this.short().toString()
433
- val = this.str[code]
434
- } else {
435
- val = ""
436
- for (let i2 = 0; i2 < len; i2++) {
437
- if (type === 7) {
438
- val += String.fromCharCode(Number(this.leb128()))
439
- } else {
440
- val += base64_rev[this.n(6).toString()]
441
- }
442
- }
443
- if (typeof this.str_rev[val] === "undefined") {
444
- const _key = (this.str_len++).toString()
445
- this.str[_key] = val
446
- this.str_rev[val] = _key
447
- }
448
- }
449
- } else if (type === 4) {
450
- val = this.nums[this.nc++]
451
- } else if (type === 5) val = this.nums[this.nc++]
452
- else if (type === 6) val = this.nums[this.nc++]
453
- else if (type === 1) val = null
454
- else if (type === 3) val = this.bools[this.bc++]
455
- return val
456
- }
457
- if (this.vrefs.length === 0) return (this.json = get(0))
458
- this.json = null
459
- let i = 0
460
- let init = [[], []]
461
- let type = key => (typeof key === "string" ? 2 : key[0])
462
- let set = k => (init[k[0]][k[1]] = true)
463
- let ex = k => init[k[0]][k[1]] === true
464
- for (let v of this.vrefs) {
465
- let keys = []
466
- this.getKey(v, keys)
467
- const val = get(i)
468
- i++
469
- let json = this.json
470
- for (let i2 = 0; i2 < keys.length; i2++) {
471
- let k = keys[i2]
472
- if (json === null) {
473
- let t = type(k)
474
- set(k)
475
- if (t === 0) {
476
- this.json = []
477
- json = this.json
478
- if (i2 === keys.length - 1) {
479
- json[0] = val
480
- break
481
- }
482
- if (i2 === keys.length - 2) {
483
- const k2 = keys[i2 + 1]
484
- if (type(k2) === 0) {
485
- json.push([val])
486
- break
487
- }
488
- } else {
489
- const k2 = keys[i2 + 1]
490
- if (type(k2) === 0) {
491
- set(k2)
492
- json.push([])
493
- json = json[json.length - 1]
494
- } else if (type(k2) === 1) {
495
- set(k2)
496
- json.push({})
497
- json = json[json.length - 1]
498
- }
499
- }
500
- } else {
501
- this.json = {}
502
- json = this.json
503
- if (i2 === keys.length - 2) {
504
- const k2 = keys[i2 + 1]
505
- json[k2] = val
506
- break
507
- }
508
- }
509
- if (i2 !== keys.length - 2) continue
510
- } else if (i2 === 0) {
511
- const k2 = keys[i2 + 1]
512
- const t1 = type(k)
513
- if (t1 === 0) {
514
- if (keys.length === 1) {
515
- json.push(val)
516
- break
517
- } else if (keys.length === 2) {
518
- if (!ex(k2)) set(k2) && json.push([])
519
- json = json[json.length - 1]
520
- json.push(val)
521
- break
522
- }
523
- const t2 = type(k2)
524
- if (t2 === 0) {
525
- if (!ex(k2)) set(k2) && json.push([])
526
- json = json[json.length - 1]
527
- } else if (t2 === 1) {
528
- if (!ex(k2)) set(k2) && json.push({})
529
- json = json[json.length - 1]
530
- }
531
- } else if (t1 === 1) {
532
- if (keys.length === 2) {
533
- json[k2] = val
534
- break
535
- }
536
- }
537
- continue
538
- }
539
- if (i2 === keys.length - 2) {
540
- const jtype = Array.isArray(json) ? 0 : 1
541
- const ctype = type(k)
542
- const k2 = keys[i2 + 1]
543
- const ntype = type(k2)
544
- if (ctype === 0 && ntype === 0) {
545
- if (!ex(k2)) set(k2) && json.push([])
546
- json = json[json.length - 1]
547
- json.push(val)
548
- break
549
- } else if (ctype === 1 && ntype === 2) {
550
- json[k2] = val
551
- break
552
- } else if (jtype === 1 && ctype === 2) {
553
- if (ntype === 0) {
554
- json[k] ??= []
555
- json[k].push(val)
556
- }
557
- break
558
- } else {
559
- //console.log("impossible 4")
560
- }
561
- } else {
562
- const jtype = Array.isArray(json) ? 0 : 1
563
- const ctype = type(k)
564
- const k2 = keys[i2 + 1]
565
- const ntype = type(k2)
566
- if (jtype === 1 && ctype === 2) {
567
- if (ntype === 0) {
568
- json[k] ??= []
569
- json = json[k]
570
- } else if (ntype === 1) {
571
- json[k] ??= {}
572
- json = json[k]
573
- } else {
574
- //console.log("impossible 5")
575
- }
576
- } else if (jtype === 0 && ctype === 1) {
577
- // console.log("impossible 6")
578
- } else if (jtype === 0 && ctype === 0) {
579
- if (ntype === 0) {
580
- if (!ex(k2)) set(k2) && json.push([])
581
- json = json[json.length - 1]
582
- } else if (ntype === 1) {
583
- if (!ex(k2)) set(k2) && json.push({})
584
- json = json[json.length - 1]
585
- } else {
586
- // console.log("impossible 7")
587
- }
588
- } else if (jtype === 1 && ctype === 1) {
589
- // nothing
590
- } else {
591
- //console.log("impossible 8")
592
- }
593
- }
594
- }
595
- }
596
- }
597
- }
598
-
599
- function decode_x(v, d) {
600
- d.decode(v)
601
- return d.json
602
- }
603
-
604
- module.exports = { decoder, decode_x }