Adapt the way to connect mimcs

This commit is contained in:
Jordi Baylina 2019-04-28 12:03:15 +01:00
parent 7792887216
commit 58f758d5ad
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
7 changed files with 53 additions and 50 deletions

@ -58,6 +58,7 @@ template EdDSAMiMCVerifier() {
hash.in[2] <== Ax; hash.in[2] <== Ax;
hash.in[3] <== Ay; hash.in[3] <== Ay;
hash.in[4] <== M; hash.in[4] <== M;
hash.k <== 0;
component h2bits = Num2Bits_strict(); component h2bits = Num2Bits_strict();
h2bits.in <== hash.out; h2bits.in <== hash.out;

@ -137,18 +137,19 @@ template MiMC7(nrounds) {
template MultiMiMC7(nInputs, nRounds) { template MultiMiMC7(nInputs, nRounds) {
signal input in[nInputs]; signal input in[nInputs];
signal input k;
signal output out; signal output out;
signal r[nInputs +1];
component mims[nInputs]; component mims[nInputs];
r[0] <== k;
for (var i=0; i<nInputs; i++) { for (var i=0; i<nInputs; i++) {
mims[i] = MiMC7(nRounds); mims[i] = MiMC7(nRounds);
if (i==0) { mims[i].x_in <== in[i];
mims[i].x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361; mims[i].k <== r[i];
} else { r[i+1] <== r[i] + in[i] + mims[i].out;
mims[i].x_in <== mims[i-1].out;
}
mims[i].k <== in[i];
} }
out <== mims[nInputs-1].out; out <== r[nInputs];
} }

@ -29,19 +29,12 @@ template SMTHash1() {
signal input value; signal input value;
signal output out; signal output out;
component h1 = MiMC7(91); // Constant component h = MultiMiMC7(2, 91); // Constant
h1.x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361; h.in[0] <== key;
h1.k <== 1; h.in[1] <== value;
h.k <== 1;
component h2 = MiMC7(91); out <== h.out;
h2.x_in <== h1.out;
h2.k <== key;
component h3 = MiMC7(91);
h3.x_in <== h2.out;
h3.k <== value;
out <== h3.out;
} }
/* /*
@ -55,13 +48,10 @@ template SMTHash2() {
signal input R; signal input R;
signal output out; signal output out;
component h1 = MiMC7(91); component h = MultiMiMC7(2, 91); // Constant
h1.x_in <== 15021630795539610737508582392395901278341266317943626182700664337106830745361; h.in[0] <== L;
h1.k <== L; h.in[1] <== R;
h.k <== 0;
component h2 = MiMC7(91); out <== h.out;
h2.x_in <== h1.out;
h2.k <== R;
out <== h2.out;
} }

@ -44,10 +44,21 @@ exports.hash = (_x_in, _k) =>{
return F.affine(F.add(r, k)); return F.affine(F.add(r, k));
}; };
exports.multiHash = (arr) => { exports.multiHash = (arr, key) => {
let r = exports.getIV(); let r;
for (let i=0; i<arr.length; i++) { if (typeof(key) === "undefined") {
r = exports.hash(r, bigInt(arr[i])); r = F.zero;
} else {
r = key;
} }
return r; for (let i=0; i<arr.length; i++) {
r = F.add(
F.add(
r,
arr[i]
),
exports.hash(bigInt(arr[i]), r)
);
}
return F.affine(r);
}; };

@ -46,8 +46,8 @@ class SMT {
const ins = []; const ins = [];
const dels = []; const dels = [];
let rtOld = mimc7.multiHash([1, key, resFind.foundValue]); let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one);
let rtNew = mimc7.multiHash([1, key, newValue]); let rtNew = mimc7.multiHash([key, newValue], bigInt.one);
ins.push([rtNew, [1, key, newValue ]]); ins.push([rtNew, [1, key, newValue ]]);
dels.push(rtOld); dels.push(rtOld);
@ -59,11 +59,11 @@ class SMT {
oldNode = [sibling, rtOld]; oldNode = [sibling, rtOld];
newNode = [sibling, rtNew]; newNode = [sibling, rtNew];
} else { } else {
oldNode = [rtOld, sibling, ]; oldNode = [rtOld, sibling];
newNode = [rtNew, sibling, ]; newNode = [rtNew, sibling];
} }
rtOld = mimc7.multiHash(oldNode); rtOld = mimc7.multiHash(oldNode, bigInt.zero);
rtNew = mimc7.multiHash(newNode); rtNew = mimc7.multiHash(newNode, bigInt.zero);
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([1, key, resFind.foundValue]); let rtOld = mimc7.multiHash([key, resFind.foundValue], bigInt.one);
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]); rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero);
} else { } else {
rtOld = mimc7.multiHash([rtOld, oldSibling]); rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero);
} }
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); rtNew = mimc7.multiHash(newNode, bigInt.zero);
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([1, resFind.notFoundKey, resFind.notFoundValue]); rtOld = mimc7.multiHash([resFind.notFoundKey, resFind.notFoundValue], bigInt.one);
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([1, key, value]); let rt = mimc7.multiHash([key, value], bigInt.one);
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]); rtOld = mimc7.multiHash([oldSibling, rtOld], bigInt.zero);
} else { } else {
rtOld = mimc7.multiHash([rtOld, oldSibling]); rtOld = mimc7.multiHash([rtOld, oldSibling], bigInt.zero);
} }
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]); newRt = mimc7.multiHash([res.siblings[i], rt], bigInt.zero);
inserts.push([newRt,[res.siblings[i], rt]] ); inserts.push([newRt,[res.siblings[i], rt]] );
} else { } else {
newRt = mimc7.multiHash([rt, res.siblings[i]]); newRt = mimc7.multiHash([rt, res.siblings[i]], bigInt.zero);
inserts.push([newRt,[rt, res.siblings[i]]] ); inserts.push([newRt,[rt, res.siblings[i]]] );
} }
rt = newRt; rt = newRt;

@ -9,7 +9,7 @@ const assert = chai.assert;
const bigInt = snarkjs.bigInt; const bigInt = snarkjs.bigInt;
describe("EdDSA test", function () { describe("EdDSA MiMC test", function () {
let circuit; let circuit;
this.timeout(100000); this.timeout(100000);