|
@ -22,13 +22,11 @@ const bigInt = require("./bigint.js"); |
|
|
const BN128 = require("./bn128.js"); |
|
|
const BN128 = require("./bn128.js"); |
|
|
const PolField = require("./polfield.js"); |
|
|
const PolField = require("./polfield.js"); |
|
|
const ZqField = require("./zqfield.js"); |
|
|
const ZqField = require("./zqfield.js"); |
|
|
const RatField = require("./ratfield.js"); |
|
|
|
|
|
|
|
|
|
|
|
const bn128 = new BN128(); |
|
|
const bn128 = new BN128(); |
|
|
const G1 = bn128.G1; |
|
|
const G1 = bn128.G1; |
|
|
const G2 = bn128.G2; |
|
|
const G2 = bn128.G2; |
|
|
const PolF = new PolField(new ZqField(bn128.r)); |
|
|
const PolF = new PolField(new ZqField(bn128.r)); |
|
|
const RatPolF = new PolField(new RatField(new ZqField(bn128.r))); |
|
|
|
|
|
const F = new ZqField(bn128.r); |
|
|
const F = new ZqField(bn128.r); |
|
|
|
|
|
|
|
|
module.exports = function setup(circuit) { |
|
|
module.exports = function setup(circuit) { |
|
@ -121,18 +119,21 @@ function calculateValuesAtT(setup, circuit) { |
|
|
function calculateEncriptedValuesAtT(setup, circuit) { |
|
|
function calculateEncriptedValuesAtT(setup, circuit) { |
|
|
|
|
|
|
|
|
const v = calculateValuesAtT(setup, circuit); |
|
|
const v = calculateValuesAtT(setup, circuit); |
|
|
setup.vk_proof.A = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.B = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.C = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.Ap = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.Bp = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.Cp = new Array(circuit.nVars); |
|
|
|
|
|
setup.vk_proof.Kp = new Array(circuit.nVars); |
|
|
|
|
|
|
|
|
setup.vk_proof.A = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.B = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.C = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.Ap = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.Bp = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.Cp = new Array(circuit.nVars+1); |
|
|
|
|
|
setup.vk_proof.Kp = new Array(circuit.nVars+3); |
|
|
setup.vk_verifier.A = new Array(circuit.nVars); |
|
|
setup.vk_verifier.A = new Array(circuit.nVars); |
|
|
|
|
|
|
|
|
setup.toxic.ka = F.random(); |
|
|
setup.toxic.ka = F.random(); |
|
|
setup.toxic.kb = F.random(); |
|
|
setup.toxic.kb = F.random(); |
|
|
setup.toxic.kc = F.random(); |
|
|
setup.toxic.kc = F.random(); |
|
|
|
|
|
setup.toxic.ra = F.random(); |
|
|
|
|
|
setup.toxic.rb = F.random(); |
|
|
|
|
|
setup.toxic.rc = F.mul(setup.toxic.ra, setup.toxic.rb); |
|
|
setup.toxic.kbeta = F.random(); |
|
|
setup.toxic.kbeta = F.random(); |
|
|
setup.toxic.kgamma = F.random(); |
|
|
setup.toxic.kgamma = F.random(); |
|
|
|
|
|
|
|
@ -148,7 +149,8 @@ function calculateEncriptedValuesAtT(setup, circuit) { |
|
|
for (let s=0; s<circuit.nVars; s++) { |
|
|
for (let s=0; s<circuit.nVars; s++) { |
|
|
|
|
|
|
|
|
// A[i] = G1 * polA(t)
|
|
|
// A[i] = G1 * polA(t)
|
|
|
const A = G1.affine(G1.mulScalar(G1.g, v.a_t[s])); |
|
|
|
|
|
|
|
|
const raat = F.mul(setup.toxic.ra, v.a_t[s]); |
|
|
|
|
|
const A = G1.affine(G1.mulScalar(G1.g, raat)); |
|
|
|
|
|
|
|
|
setup.vk_proof.A[s] = A; |
|
|
setup.vk_proof.A[s] = A; |
|
|
|
|
|
|
|
@ -158,30 +160,32 @@ function calculateEncriptedValuesAtT(setup, circuit) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// B1[i] = G1 * polB(t)
|
|
|
// B1[i] = G1 * polB(t)
|
|
|
const B1 = G1.affine(G1.mulScalar(G1.g, v.b_t[s])); |
|
|
|
|
|
|
|
|
const rbbt = F.mul(setup.toxic.rb, v.b_t[s]); |
|
|
|
|
|
const B1 = G1.affine(G1.mulScalar(G1.g, rbbt)); |
|
|
|
|
|
|
|
|
// B2[i] = G2 * polB(t)
|
|
|
// B2[i] = G2 * polB(t)
|
|
|
const B2 = G2.affine(G2.mulScalar(G2.g, v.b_t[s])); |
|
|
|
|
|
|
|
|
const B2 = G2.affine(G2.mulScalar(G2.g, rbbt)); |
|
|
|
|
|
|
|
|
setup.vk_proof.B[s]=B2; |
|
|
setup.vk_proof.B[s]=B2; |
|
|
|
|
|
|
|
|
// C[i] = G1 * polC(t)
|
|
|
// C[i] = G1 * polC(t)
|
|
|
const C = G1.affine(G1.mulScalar( G1.g, v.c_t[s])); |
|
|
|
|
|
|
|
|
const rcct = F.mul(setup.toxic.rc, v.c_t[s]); |
|
|
|
|
|
const C = G1.affine(G1.mulScalar( G1.g, rcct)); |
|
|
setup.vk_proof.C[s] =C; |
|
|
setup.vk_proof.C[s] =C; |
|
|
|
|
|
|
|
|
// K = G1 * (A+B+C)
|
|
|
// K = G1 * (A+B+C)
|
|
|
|
|
|
|
|
|
const kt = F.affine(F.add(F.add(v.a_t[s], v.b_t[s]), v.c_t[s])); |
|
|
|
|
|
|
|
|
const kt = F.affine(F.add(F.add(raat, rbbt), rcct)); |
|
|
const K = G1.affine(G1.mulScalar( G1.g, kt)); |
|
|
const K = G1.affine(G1.mulScalar( G1.g, kt)); |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
// Comment this lines to improve the process
|
|
|
|
|
|
const Ktest = G1.affine(G1.add(G1.add(A, B1), C)); |
|
|
|
|
|
|
|
|
// Comment this lines to improve the process
|
|
|
|
|
|
const Ktest = G1.affine(G1.add(G1.add(A, B1), C)); |
|
|
|
|
|
|
|
|
|
|
|
if (!G1.equals(K, Ktest)) { |
|
|
|
|
|
console.log ("=====FAIL======"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!G1.equals(K, Ktest)) { |
|
|
|
|
|
console.log ("=====FAIL======"); |
|
|
|
|
|
} |
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setup.vk_proof.Ap[s] = G1.affine(G1.mulScalar(A, setup.toxic.ka)); |
|
|
setup.vk_proof.Ap[s] = G1.affine(G1.mulScalar(A, setup.toxic.ka)); |
|
@ -190,14 +194,35 @@ function calculateEncriptedValuesAtT(setup, circuit) { |
|
|
setup.vk_proof.Kp[s] = G1.affine(G1.mulScalar(K, setup.toxic.kbeta)); |
|
|
setup.vk_proof.Kp[s] = G1.affine(G1.mulScalar(K, setup.toxic.kbeta)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Extra coeficients
|
|
|
|
|
|
const A = G1.mulScalar( G1.g, F.mul(setup.toxic.ra, v.z_t)); |
|
|
|
|
|
setup.vk_proof.A[circuit.nVars] = G1.affine(A); |
|
|
|
|
|
setup.vk_proof.Ap[circuit.nVars] = G1.affine(G1.mulScalar(A, setup.toxic.ka)); |
|
|
|
|
|
|
|
|
|
|
|
const B1 = G1.mulScalar( G1.g, F.mul(setup.toxic.rb, v.z_t)); |
|
|
|
|
|
const B2 = G2.mulScalar( G2.g, F.mul(setup.toxic.rb, v.z_t)); |
|
|
|
|
|
setup.vk_proof.B[circuit.nVars] = G2.affine(B2); |
|
|
|
|
|
setup.vk_proof.Bp[circuit.nVars] = G1.affine(G1.mulScalar(B1, setup.toxic.kb)); |
|
|
|
|
|
|
|
|
|
|
|
const C = G1.mulScalar( G1.g, F.mul(setup.toxic.rc, v.z_t)); |
|
|
|
|
|
setup.vk_proof.C[circuit.nVars] = G1.affine(C); |
|
|
|
|
|
setup.vk_proof.Cp[circuit.nVars] = G1.affine(G1.mulScalar(C, setup.toxic.kc)); |
|
|
|
|
|
|
|
|
|
|
|
setup.vk_proof.Kp[circuit.nVars ] = G1.affine(G1.mulScalar(A, setup.toxic.kbeta)); |
|
|
|
|
|
setup.vk_proof.Kp[circuit.nVars+1] = G1.affine(G1.mulScalar(B1, setup.toxic.kbeta)); |
|
|
|
|
|
setup.vk_proof.Kp[circuit.nVars+2] = G1.affine(G1.mulScalar(C, setup.toxic.kbeta)); |
|
|
|
|
|
|
|
|
|
|
|
// setup.vk_verifier.A[0] = G1.affine(G1.add(setup.vk_verifier.A[0], setup.vk_proof.A[circuit.nVars]));
|
|
|
|
|
|
|
|
|
|
|
|
// vk_z
|
|
|
setup.vk_verifier.vk_z = G2.affine(G2.mulScalar( |
|
|
setup.vk_verifier.vk_z = G2.affine(G2.mulScalar( |
|
|
G2.g, |
|
|
G2.g, |
|
|
v.z_t)); |
|
|
|
|
|
|
|
|
F.mul(setup.toxic.rc, v.z_t))); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function calculateHexps(setup, circuit) { |
|
|
|
|
|
|
|
|
function calculateHexps(setup) { |
|
|
|
|
|
|
|
|
const maxH = setup.vk_proof.domainSize; |
|
|
|
|
|
|
|
|
const maxH = setup.vk_proof.domainSize+1; |
|
|
|
|
|
|
|
|
setup.vk_proof.hExps = new Array(maxH); |
|
|
setup.vk_proof.hExps = new Array(maxH); |
|
|
setup.vk_proof.hExps[0] = G1.g; |
|
|
setup.vk_proof.hExps[0] = G1.g; |
|
|