Small fixes and optimizations

This commit is contained in:
Jordi Baylina 2019-04-09 14:55:59 +02:00
parent 26c2389800
commit ff5b1760c2
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
4 changed files with 16 additions and 41 deletions

View File

@ -31,5 +31,9 @@ exports.groth = {
exports.bigInt = require("./src/bigint.js");
exports.ZqField = require("./src/zqfield.js");
exports.stringifyBigInts = require("./src/stringifybigint.js").stringifyBigInts;
exports.unstringifyBigInts = require("./src/stringifybigint.js").unstringifyBigInts;
const Bn128 = require("./src/bn128.js");
exports.bn128 = new Bn128();

View File

@ -30,7 +30,7 @@ class BN128 {
this.q = bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
this.r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
this.g1 = [ bigInt(1), bigInt(2) ];
this.g1 = [ bigInt(1), bigInt(2), bigInt(1)];
this.g2 = [
[
bigInt("10857046999023057135944570762232829481370756359578518086990519993285655852781"),
@ -39,6 +39,10 @@ class BN128 {
[
bigInt("8495653923123431417604973247489272438418190587263600148770280649306958101930"),
bigInt("4082367875863433681332203403145435568316851327593401208105741076214120093531")
],
[
bigInt("1"),
bigInt("0")
]
];
@ -219,7 +223,7 @@ class BN128 {
}
if (this.loopCountNef)
if (this.loopCountNeg)
{
f = this.F12.inverse(f);
}

View File

@ -64,8 +64,8 @@ function calculateWitness(circuit, inputSignals, log) {
}
log(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
}
// return ctx.witness.slice(0, circuit.nVars);
return ctx.witness;
return ctx.witness.slice(0, circuit.nVars);
// return ctx.witness;
}
class RTCtx {

View File

@ -69,9 +69,7 @@ module.exports = function genProof(vk_proof, witness) {
pib1 = G1.add( pib1, vk_proof.vk_beta_1 );
pib1 = G1.add( pib1, G1.mulScalar( vk_proof.vk_delta_1, s ));
const h = calculateH(vk_proof, witness, PolF.F.zero, PolF.F.zero, PolF.F.zero);
// console.log(h.length + "/" + vk_proof.hExps.length);
const h = calculateH(vk_proof, witness);
for (let i = 0; i < h.length; i++) {
proof.pi_c = G1.add( proof.pi_c, G1.mulScalar( vk_proof.hExps[i], h[i]));
@ -92,10 +90,11 @@ module.exports = function genProof(vk_proof, witness) {
proof.protocol = "groth";
return {proof, publicSignals};
};
function calculateH(vk_proof, witness, d1, d2, d3) {
function calculateH(vk_proof, witness) {
const F = PolF.F;
const m = vk_proof.domainSize;
@ -124,39 +123,7 @@ function calculateH(vk_proof, witness, d1, d2, d3) {
const polABC_S = PolF.sub(polAB_S, polC_S);
const polZ_S = new Array(m+1).fill(F.zero);
polZ_S[m] = F.one;
polZ_S[0] = F.neg(F.one);
let H_S = PolF.div(polABC_S, polZ_S);
/*
const H2S = PolF.mul(H_S, polZ_S);
if (PolF.equals(H2S, polABC_S)) {
console.log("Is Divisible!");
} else {
console.log("ERROR: Not divisible!");
}
*/
/* add coefficients of the polynomial (d2*A + d1*B - d3) + d1*d2*Z */
H_S = PolF.extend(H_S, m+1);
for (let i=0; i<m; i++) {
const d2A = PolF.F.mul(d2, polA_S[i]);
const d1B = PolF.F.mul(d1, polB_S[i]);
H_S[i] = PolF.F.add(H_S[i], PolF.F.add(d2A, d1B));
}
H_S[0] = PolF.F.sub(H_S[0], d3);
// Z = x^m -1
const d1d2 = PolF.F.mul(d1, d2);
H_S[m] = PolF.F.add(H_S[m], d1d2);
H_S[0] = PolF.F.sub(H_S[0], d1d2);
H_S = PolF.reduce(PolF.affine(H_S));
const H_S = polABC_S.slice(m);
return H_S;
}