Fix zkey export json

This commit is contained in:
Jordi Baylina 2021-01-28 21:55:41 +01:00
parent 8dab91063a
commit 24fe89f8db
No known key found for this signature in database
GPG Key ID: 7480C80C1BE43112
7 changed files with 448 additions and 111 deletions

@ -4499,19 +4499,21 @@ async function writeG2(fd, curve, p) {
await fd.write(buff);
}
async function readG1(fd, curve) {
async function readG1(fd, curve, toObject) {
const buff = await fd.read(curve.G1.F.n8*2);
return curve.G1.fromRprLEM(buff, 0);
const res = curve.G1.fromRprLEM(buff, 0);
return toObject ? curve.G1.toObject(res) : res;
}
async function readG2(fd, curve) {
async function readG2(fd, curve, toObject) {
const buff = await fd.read(curve.G2.F.n8*2);
return curve.G2.fromRprLEM(buff, 0);
const res = curve.G2.fromRprLEM(buff, 0);
return toObject ? curve.G2.toObject(res) : res;
}
async function readHeader(fd, sections, protocol) {
async function readHeader(fd, sections, protocol, toObject) {
if (protocol != "groth16") throw new Error("Protocol not supported: "+protocol);
const zkey = {};
@ -4541,36 +4543,36 @@ async function readHeader(fd, sections, protocol) {
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2(zkey.domainSize);
zkey.vk_alpha_1 = await readG1(fd, curve);
zkey.vk_beta_1 = await readG1(fd, curve);
zkey.vk_beta_2 = await readG2(fd, curve);
zkey.vk_gamma_2 = await readG2(fd, curve);
zkey.vk_delta_1 = await readG1(fd, curve);
zkey.vk_delta_2 = await readG2(fd, curve);
zkey.vk_alpha_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_2 = await readG2(fd, curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, curve, toObject);
zkey.vk_delta_1 = await readG1(fd, curve, toObject);
zkey.vk_delta_2 = await readG2(fd, curve, toObject);
await endReadSection(fd);
return zkey;
}
async function readZKey(fileName) {
async function readZKey(fileName, toObject) {
const {fd, sections} = await readBinFile(fileName, "zkey", 1);
const zkey = await readHeader(fd, sections, "groth16");
const zkey = await readHeader(fd, sections, "groth16", toObject);
const Fr = new ffjavascript.F1Field(zkey.r);
const Rr = ffjavascript.Scalar.mod(ffjavascript.Scalar.shl(1, zkey.n8r*8), zkey.r);
const Rri = Fr.inv(Rr);
const Rri2 = Fr.mul(Rri, Rri);
let curve = getCurveFromQ(zkey.q);
let curve = await getCurveFromQ(zkey.q);
// Read IC Section
///////////
await startReadUniqueSection(fd, sections, 3);
zkey.IC = [];
for (let i=0; i<= zkey.nPublic; i++) {
const P = await readG1(fd, curve);
const P = await readG1(fd, curve, toObject);
zkey.IC.push(P);
}
await endReadSection(fd);
@ -4600,7 +4602,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 5);
zkey.A = [];
for (let i=0; i<zkey.nVars; i++) {
const A = await readG1(fd, curve);
const A = await readG1(fd, curve, toObject);
zkey.A[i] = A;
}
await endReadSection(fd);
@ -4611,7 +4613,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 6);
zkey.B1 = [];
for (let i=0; i<zkey.nVars; i++) {
const B1 = await readG1(fd, curve);
const B1 = await readG1(fd, curve, toObject);
zkey.B1[i] = B1;
}
@ -4623,7 +4625,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 7);
zkey.B2 = [];
for (let i=0; i<zkey.nVars; i++) {
const B2 = await readG2(fd, curve);
const B2 = await readG2(fd, curve, toObject);
zkey.B2[i] = B2;
}
await endReadSection(fd);
@ -4634,7 +4636,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 8);
zkey.C = [];
for (let i=zkey.nPublic+1; i<zkey.nVars; i++) {
const C = await readG1(fd, curve);
const C = await readG1(fd, curve, toObject);
zkey.C[i] = C;
}
@ -4646,7 +4648,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 9);
zkey.hExps = [];
for (let i=0; i<zkey.domainSize; i++) {
const H = await readG1(fd, curve);
const H = await readG1(fd, curve, toObject);
zkey.hExps.push(H);
}
await endReadSection(fd);
@ -4655,7 +4657,7 @@ async function readZKey(fileName) {
return zkey;
async function readFr2() {
async function readFr2(toObject) {
const n = await readBigInt(fd, zkey.n8r);
return Fr.mul(n, Rri2);
}
@ -4663,12 +4665,12 @@ async function readZKey(fileName) {
}
async function readContribution$1(fd, curve) {
async function readContribution$1(fd, curve, toObject) {
const c = {delta:{}};
c.deltaAfter = await readG1(fd, curve);
c.delta.g1_s = await readG1(fd, curve);
c.delta.g1_sx = await readG1(fd, curve);
c.delta.g2_spx = await readG2(fd, curve);
c.deltaAfter = await readG1(fd, curve, toObject);
c.delta.g1_s = await readG1(fd, curve, toObject);
c.delta.g1_sx = await readG1(fd, curve, toObject);
c.delta.g2_spx = await readG2(fd, curve, toObject);
c.transcript = await fd.read(64);
c.type = await fd.readULE32();
@ -5680,7 +5682,7 @@ async function beacon$1(zkeyNameOld, zkeyNameNew, name, beaconHashStr, numIterat
async function zkeyExportJson(zkeyFileName, verbose) {
const zKey = await readZKey(zkeyFileName);
const zKey = await readZKey(zkeyFileName, true);
return zKey;
}

@ -1213,19 +1213,21 @@ async function writeG2(fd, curve, p) {
await fd.write(buff);
}
async function readG1(fd, curve) {
async function readG1(fd, curve, toObject) {
const buff = await fd.read(curve.G1.F.n8*2);
return curve.G1.fromRprLEM(buff, 0);
const res = curve.G1.fromRprLEM(buff, 0);
return toObject ? curve.G1.toObject(res) : res;
}
async function readG2(fd, curve) {
async function readG2(fd, curve, toObject) {
const buff = await fd.read(curve.G2.F.n8*2);
return curve.G2.fromRprLEM(buff, 0);
const res = curve.G2.fromRprLEM(buff, 0);
return toObject ? curve.G2.toObject(res) : res;
}
async function readHeader(fd, sections, protocol) {
async function readHeader(fd, sections, protocol, toObject) {
if (protocol != "groth16") throw new Error("Protocol not supported: "+protocol);
const zkey = {};
@ -1255,36 +1257,36 @@ async function readHeader(fd, sections, protocol) {
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2(zkey.domainSize);
zkey.vk_alpha_1 = await readG1(fd, curve);
zkey.vk_beta_1 = await readG1(fd, curve);
zkey.vk_beta_2 = await readG2(fd, curve);
zkey.vk_gamma_2 = await readG2(fd, curve);
zkey.vk_delta_1 = await readG1(fd, curve);
zkey.vk_delta_2 = await readG2(fd, curve);
zkey.vk_alpha_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_2 = await readG2(fd, curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, curve, toObject);
zkey.vk_delta_1 = await readG1(fd, curve, toObject);
zkey.vk_delta_2 = await readG2(fd, curve, toObject);
await endReadSection(fd);
return zkey;
}
async function readZKey(fileName) {
async function readZKey(fileName, toObject) {
const {fd, sections} = await readBinFile(fileName, "zkey", 1);
const zkey = await readHeader(fd, sections, "groth16");
const zkey = await readHeader(fd, sections, "groth16", toObject);
const Fr = new ffjavascript.F1Field(zkey.r);
const Rr = ffjavascript.Scalar.mod(ffjavascript.Scalar.shl(1, zkey.n8r*8), zkey.r);
const Rri = Fr.inv(Rr);
const Rri2 = Fr.mul(Rri, Rri);
let curve = getCurveFromQ(zkey.q);
let curve = await getCurveFromQ(zkey.q);
// Read IC Section
///////////
await startReadUniqueSection(fd, sections, 3);
zkey.IC = [];
for (let i=0; i<= zkey.nPublic; i++) {
const P = await readG1(fd, curve);
const P = await readG1(fd, curve, toObject);
zkey.IC.push(P);
}
await endReadSection(fd);
@ -1314,7 +1316,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 5);
zkey.A = [];
for (let i=0; i<zkey.nVars; i++) {
const A = await readG1(fd, curve);
const A = await readG1(fd, curve, toObject);
zkey.A[i] = A;
}
await endReadSection(fd);
@ -1325,7 +1327,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 6);
zkey.B1 = [];
for (let i=0; i<zkey.nVars; i++) {
const B1 = await readG1(fd, curve);
const B1 = await readG1(fd, curve, toObject);
zkey.B1[i] = B1;
}
@ -1337,7 +1339,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 7);
zkey.B2 = [];
for (let i=0; i<zkey.nVars; i++) {
const B2 = await readG2(fd, curve);
const B2 = await readG2(fd, curve, toObject);
zkey.B2[i] = B2;
}
await endReadSection(fd);
@ -1348,7 +1350,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 8);
zkey.C = [];
for (let i=zkey.nPublic+1; i<zkey.nVars; i++) {
const C = await readG1(fd, curve);
const C = await readG1(fd, curve, toObject);
zkey.C[i] = C;
}
@ -1360,7 +1362,7 @@ async function readZKey(fileName) {
await startReadUniqueSection(fd, sections, 9);
zkey.hExps = [];
for (let i=0; i<zkey.domainSize; i++) {
const H = await readG1(fd, curve);
const H = await readG1(fd, curve, toObject);
zkey.hExps.push(H);
}
await endReadSection(fd);
@ -1369,7 +1371,7 @@ async function readZKey(fileName) {
return zkey;
async function readFr2() {
async function readFr2(toObject) {
const n = await readBigInt(fd, zkey.n8r);
return Fr.mul(n, Rri2);
}
@ -1377,12 +1379,12 @@ async function readZKey(fileName) {
}
async function readContribution(fd, curve) {
async function readContribution(fd, curve, toObject) {
const c = {delta:{}};
c.deltaAfter = await readG1(fd, curve);
c.delta.g1_s = await readG1(fd, curve);
c.delta.g1_sx = await readG1(fd, curve);
c.delta.g2_spx = await readG2(fd, curve);
c.deltaAfter = await readG1(fd, curve, toObject);
c.delta.g1_s = await readG1(fd, curve, toObject);
c.delta.g1_sx = await readG1(fd, curve, toObject);
c.delta.g2_spx = await readG2(fd, curve, toObject);
c.transcript = await fd.read(64);
c.type = await fd.readULE32();
@ -5884,7 +5886,7 @@ async function beacon$1(zkeyNameOld, zkeyNameNew, name, beaconHashStr, numIterat
async function zkeyExportJson(zkeyFileName, verbose) {
const zKey = await readZKey(zkeyFileName);
const zKey = await readZKey(zkeyFileName, true);
return zKey;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

329
circuit_final.zkey.json Normal file

@ -0,0 +1,329 @@
{
"protocol": "groth16",
"n8q": 32,
"q": "21888242871839275222246405745257275088696311157297823662689037894645226208583",
"n8r": 32,
"r": "21888242871839275222246405745257275088548364400416034343698204186575808495617",
"nVars": 6,
"nPublic": 2,
"domainSize": 8,
"power": 3,
"vk_alpha_1": [
"1",
"2",
"1"
],
"vk_beta_1": [
"1",
"2",
"1"
],
"vk_beta_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_gamma_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"vk_delta_1": [
"1",
"2",
"1"
],
"vk_delta_2": [
[
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
],
[
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
],
[
"1",
"0"
]
],
"IC": [
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"1",
"2",
"1"
]
],
"ccoefs": [
{
"matrix": 0,
"constraint": 1,
"signal": 4,
"value": "1"
},
{
"matrix": 1,
"constraint": 1,
"signal": 2,
"value": "1"
},
{
"matrix": 0,
"constraint": 3,
"signal": 0,
"value": "1"
},
{
"matrix": 0,
"constraint": 4,
"signal": 1,
"value": "1"
},
{
"matrix": 0,
"constraint": 5,
"signal": 2,
"value": "1"
}
],
"A": [
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
]
],
"B1": [
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
]
],
"B2": [
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
],
[
[
"0",
"0"
],
[
"1",
"0"
],
[
"0",
"0"
]
]
],
"C": [
null,
null,
null,
[
"1",
"21888242871839275222246405745257275088696311157297823662689037894645226208581",
"1"
],
[
"1",
"21888242871839275222246405745257275088696311157297823662689037894645226208581",
"1"
],
[
"0",
"1",
"0"
]
],
"hExps": [
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
],
[
"0",
"1",
"0"
]
]
}

@ -2,7 +2,7 @@ import { readZKey as readZKey } from "./zkey_utils.js";
export default async function zkeyExportJson(zkeyFileName, verbose) {
const zKey = await readZKey(zkeyFileName);
const zKey = await readZKey(zkeyFileName, true);
return zKey;
}

@ -172,19 +172,21 @@ async function writeG2(fd, curve, p) {
await fd.write(buff);
}
async function readG1(fd, curve) {
async function readG1(fd, curve, toObject) {
const buff = await fd.read(curve.G1.F.n8*2);
return curve.G1.fromRprLEM(buff, 0);
const res = curve.G1.fromRprLEM(buff, 0);
return toObject ? curve.G1.toObject(res) : res;
}
async function readG2(fd, curve) {
async function readG2(fd, curve, toObject) {
const buff = await fd.read(curve.G2.F.n8*2);
return curve.G2.fromRprLEM(buff, 0);
const res = curve.G2.fromRprLEM(buff, 0);
return toObject ? curve.G2.toObject(res) : res;
}
export async function readHeader(fd, sections, protocol) {
export async function readHeader(fd, sections, protocol, toObject) {
if (protocol != "groth16") throw new Error("Protocol not supported: "+protocol);
const zkey = {};
@ -214,36 +216,36 @@ export async function readHeader(fd, sections, protocol) {
zkey.nPublic = await fd.readULE32();
zkey.domainSize = await fd.readULE32();
zkey.power = log2(zkey.domainSize);
zkey.vk_alpha_1 = await readG1(fd, curve);
zkey.vk_beta_1 = await readG1(fd, curve);
zkey.vk_beta_2 = await readG2(fd, curve);
zkey.vk_gamma_2 = await readG2(fd, curve);
zkey.vk_delta_1 = await readG1(fd, curve);
zkey.vk_delta_2 = await readG2(fd, curve);
zkey.vk_alpha_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_1 = await readG1(fd, curve, toObject);
zkey.vk_beta_2 = await readG2(fd, curve, toObject);
zkey.vk_gamma_2 = await readG2(fd, curve, toObject);
zkey.vk_delta_1 = await readG1(fd, curve, toObject);
zkey.vk_delta_2 = await readG2(fd, curve, toObject);
await binFileUtils.endReadSection(fd);
return zkey;
}
export async function readZKey(fileName) {
export async function readZKey(fileName, toObject) {
const {fd, sections} = await binFileUtils.readBinFile(fileName, "zkey", 1);
const zkey = await readHeader(fd, sections, "groth16");
const zkey = await readHeader(fd, sections, "groth16", toObject);
const Fr = new F1Field(zkey.r);
const Rr = Scalar.mod(Scalar.shl(1, zkey.n8r*8), zkey.r);
const Rri = Fr.inv(Rr);
const Rri2 = Fr.mul(Rri, Rri);
let curve = getCurve(zkey.q);
let curve = await getCurve(zkey.q);
// Read IC Section
///////////
await binFileUtils.startReadUniqueSection(fd, sections, 3);
zkey.IC = [];
for (let i=0; i<= zkey.nPublic; i++) {
const P = await readG1(fd, curve);
const P = await readG1(fd, curve, toObject);
zkey.IC.push(P);
}
await binFileUtils.endReadSection(fd);
@ -258,7 +260,7 @@ export async function readZKey(fileName) {
const m = await fd.readULE32();
const c = await fd.readULE32();
const s = await fd.readULE32();
const v = await readFr2();
const v = await readFr2(toObject);
zkey.ccoefs.push({
matrix: m,
constraint: c,
@ -273,7 +275,7 @@ export async function readZKey(fileName) {
await binFileUtils.startReadUniqueSection(fd, sections, 5);
zkey.A = [];
for (let i=0; i<zkey.nVars; i++) {
const A = await readG1(fd, curve);
const A = await readG1(fd, curve, toObject);
zkey.A[i] = A;
}
await binFileUtils.endReadSection(fd);
@ -284,7 +286,7 @@ export async function readZKey(fileName) {
await binFileUtils.startReadUniqueSection(fd, sections, 6);
zkey.B1 = [];
for (let i=0; i<zkey.nVars; i++) {
const B1 = await readG1(fd, curve);
const B1 = await readG1(fd, curve, toObject);
zkey.B1[i] = B1;
}
@ -296,7 +298,7 @@ export async function readZKey(fileName) {
await binFileUtils.startReadUniqueSection(fd, sections, 7);
zkey.B2 = [];
for (let i=0; i<zkey.nVars; i++) {
const B2 = await readG2(fd, curve);
const B2 = await readG2(fd, curve, toObject);
zkey.B2[i] = B2;
}
await binFileUtils.endReadSection(fd);
@ -307,7 +309,7 @@ export async function readZKey(fileName) {
await binFileUtils.startReadUniqueSection(fd, sections, 8);
zkey.C = [];
for (let i=zkey.nPublic+1; i<zkey.nVars; i++) {
const C = await readG1(fd, curve);
const C = await readG1(fd, curve, toObject);
zkey.C[i] = C;
}
@ -319,7 +321,7 @@ export async function readZKey(fileName) {
await binFileUtils.startReadUniqueSection(fd, sections, 9);
zkey.hExps = [];
for (let i=0; i<zkey.domainSize; i++) {
const H = await readG1(fd, curve);
const H = await readG1(fd, curve, toObject);
zkey.hExps.push(H);
}
await binFileUtils.endReadSection(fd);
@ -328,7 +330,7 @@ export async function readZKey(fileName) {
return zkey;
async function readFr2() {
async function readFr2(toObject) {
const n = await binFileUtils.readBigInt(fd, zkey.n8r);
return Fr.mul(n, Rri2);
}
@ -336,12 +338,12 @@ export async function readZKey(fileName) {
}
async function readContribution(fd, curve) {
async function readContribution(fd, curve, toObject) {
const c = {delta:{}};
c.deltaAfter = await readG1(fd, curve);
c.delta.g1_s = await readG1(fd, curve);
c.delta.g1_sx = await readG1(fd, curve);
c.delta.g2_spx = await readG2(fd, curve);
c.deltaAfter = await readG1(fd, curve, toObject);
c.delta.g1_s = await readG1(fd, curve, toObject);
c.delta.g1_sx = await readG1(fd, curve, toObject);
c.delta.g2_spx = await readG2(fd, curve, toObject);
c.transcript = await fd.read(64);
c.type = await fd.readULE32();