|
@ -1,7 +1,7 @@ |
|
|
const bigInt = require("snarkjs").bigInt; |
|
|
const bigInt = require("snarkjs").bigInt; |
|
|
|
|
|
|
|
|
const SMTMemDB = require("./smt_memdb"); |
|
|
const SMTMemDB = require("./smt_memdb"); |
|
|
const mimc7 = require("./mimc7"); |
|
|
|
|
|
|
|
|
const {hash0, hash1} = require("./smt_hashes_poseidon"); |
|
|
|
|
|
|
|
|
class SMT { |
|
|
class SMT { |
|
|
|
|
|
|
|
@ -46,8 +46,8 @@ class SMT { |
|
|
const ins = []; |
|
|
const ins = []; |
|
|
const dels = []; |
|
|
const dels = []; |
|
|
|
|
|
|
|
|
let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one); |
|
|
|
|
|
let rtNew = mimc7.multiHash([key, newValue], bigInt.one); |
|
|
|
|
|
|
|
|
let rtOld = hash1(key, resFind.foundValue); |
|
|
|
|
|
let rtNew = hash1(key, newValue); |
|
|
ins.push([rtNew, [1, key, newValue ]]); |
|
|
ins.push([rtNew, [1, key, newValue ]]); |
|
|
dels.push(rtOld); |
|
|
dels.push(rtOld); |
|
|
|
|
|
|
|
@ -62,8 +62,8 @@ class SMT { |
|
|
oldNode = [rtOld, sibling]; |
|
|
oldNode = [rtOld, sibling]; |
|
|
newNode = [rtNew, sibling]; |
|
|
newNode = [rtNew, sibling]; |
|
|
} |
|
|
} |
|
|
rtOld = mimc7.multiHash(oldNode, bigInt.zero); |
|
|
|
|
|
rtNew = mimc7.multiHash(newNode, bigInt.zero); |
|
|
|
|
|
|
|
|
rtOld = hash0(oldNode[0], oldNode[1]); |
|
|
|
|
|
rtNew = hash0(newNode[0], newNode[1]); |
|
|
dels.push(rtOld); |
|
|
dels.push(rtOld); |
|
|
ins.push([rtNew, newNode]); |
|
|
ins.push([rtNew, newNode]); |
|
|
} |
|
|
} |
|
@ -92,7 +92,7 @@ class SMT { |
|
|
|
|
|
|
|
|
const dels = []; |
|
|
const dels = []; |
|
|
const ins = []; |
|
|
const ins = []; |
|
|
let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one); |
|
|
|
|
|
|
|
|
let rtOld = hash1(key, resFind.foundValue); |
|
|
let rtNew; |
|
|
let rtNew; |
|
|
dels.push(rtOld); |
|
|
dels.push(rtOld); |
|
|
|
|
|
|
|
@ -130,9 +130,9 @@ class SMT { |
|
|
} |
|
|
} |
|
|
const oldSibling = resFind.siblings[level]; |
|
|
const oldSibling = resFind.siblings[level]; |
|
|
if (keyBits[level]) { |
|
|
if (keyBits[level]) { |
|
|
rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero); |
|
|
|
|
|
|
|
|
rtOld = hash0(oldSibling, rtOld); |
|
|
} else { |
|
|
} else { |
|
|
rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero); |
|
|
|
|
|
|
|
|
rtOld = hash0(rtOld, oldSibling); |
|
|
} |
|
|
} |
|
|
dels.push(rtOld); |
|
|
dels.push(rtOld); |
|
|
if (!newSibling.isZero()) { |
|
|
if (!newSibling.isZero()) { |
|
@ -147,7 +147,7 @@ class SMT { |
|
|
} else { |
|
|
} else { |
|
|
newNode = [rtNew, newSibling]; |
|
|
newNode = [rtNew, newSibling]; |
|
|
} |
|
|
} |
|
|
rtNew = mimc7.multiHash(newNode, bigInt.zero); |
|
|
|
|
|
|
|
|
rtNew = hash0(newNode[0], newNode[1]); |
|
|
ins.push([rtNew, newNode]); |
|
|
ins.push([rtNew, newNode]); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -185,7 +185,7 @@ class SMT { |
|
|
for (let i= res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) { |
|
|
for (let i= res.siblings.length; oldKeyits[i] == newKeyBits[i]; i++) { |
|
|
res.siblings.push(bigInt.zero); |
|
|
res.siblings.push(bigInt.zero); |
|
|
} |
|
|
} |
|
|
rtOld = mimc7.multiHash([resFind.notFoundKey, resFind.notFoundValue], bigInt.one); |
|
|
|
|
|
|
|
|
rtOld = hash1(resFind.notFoundKey, resFind.notFoundValue); |
|
|
res.siblings.push(rtOld); |
|
|
res.siblings.push(rtOld); |
|
|
addedOne = true; |
|
|
addedOne = true; |
|
|
mixed = false; |
|
|
mixed = false; |
|
@ -197,7 +197,7 @@ class SMT { |
|
|
const inserts = []; |
|
|
const inserts = []; |
|
|
const dels = []; |
|
|
const dels = []; |
|
|
|
|
|
|
|
|
let rt = mimc7.multiHash([key, value], bigInt.one); |
|
|
|
|
|
|
|
|
let rt = hash1(key, value); |
|
|
inserts.push([rt,[1, key, value]] ); |
|
|
inserts.push([rt,[1, key, value]] ); |
|
|
|
|
|
|
|
|
for (let i=res.siblings.length-1; i>=0; i--) { |
|
|
for (let i=res.siblings.length-1; i>=0; i--) { |
|
@ -207,9 +207,9 @@ class SMT { |
|
|
if (mixed) { |
|
|
if (mixed) { |
|
|
const oldSibling = resFind.siblings[i]; |
|
|
const oldSibling = resFind.siblings[i]; |
|
|
if (newKeyBits[i]) { |
|
|
if (newKeyBits[i]) { |
|
|
rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero); |
|
|
|
|
|
|
|
|
rtOld = hash0(oldSibling, rtOld); |
|
|
} else { |
|
|
} else { |
|
|
rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero); |
|
|
|
|
|
|
|
|
rtOld = hash0(rtOld, oldSibling); |
|
|
} |
|
|
} |
|
|
dels.push(rtOld); |
|
|
dels.push(rtOld); |
|
|
} |
|
|
} |
|
@ -217,10 +217,10 @@ class SMT { |
|
|
|
|
|
|
|
|
let newRt; |
|
|
let newRt; |
|
|
if (newKeyBits[i]) { |
|
|
if (newKeyBits[i]) { |
|
|
newRt = mimc7.multiHash([res.siblings[i], rt], bigInt.zero); |
|
|
|
|
|
|
|
|
newRt = hash0(res.siblings[i], rt); |
|
|
inserts.push([newRt,[res.siblings[i], rt]] ); |
|
|
inserts.push([newRt,[res.siblings[i], rt]] ); |
|
|
} else { |
|
|
} else { |
|
|
newRt = mimc7.multiHash([rt, res.siblings[i]], bigInt.zero); |
|
|
|
|
|
|
|
|
newRt = hash0(rt, res.siblings[i]); |
|
|
inserts.push([newRt,[rt, res.siblings[i]]] ); |
|
|
inserts.push([newRt,[rt, res.siblings[i]]] ); |
|
|
} |
|
|
} |
|
|
rt = newRt; |
|
|
rt = newRt; |
|
|