write code in stream mode

This commit is contained in:
Jordi Baylina
2020-01-27 13:08:11 +07:00
parent 20058a38d6
commit 3a9766a008
9 changed files with 108 additions and 45 deletions

View File

@@ -35,7 +35,7 @@ async function testField(prime, test) {
` ${path.join(dir.path, "fr.o")}` + ` ${path.join(dir.path, "fr.o")}` +
` ${path.join(dir.path, "fr.c")}` + ` ${path.join(dir.path, "fr.c")}` +
` -o ${path.join(dir.path, "tester")}` + ` -o ${path.join(dir.path, "tester")}` +
" -lgmp" " -lgmp -g"
); );
const inLines = []; const inLines = [];

View File

@@ -63,7 +63,6 @@ char *<%=name%>_element2str(P<%=name%>Element pE) {
} else { } else {
mpz_init_set_si(r, pE->shortVal); mpz_init_set_si(r, pE->shortVal);
mpz_add(r, r, q); mpz_add(r, r, q);
mpz_clear(q);
} }
} else { } else {
<%=name%>_toNormal(pE); <%=name%>_toNormal(pE);

View File

@@ -8,8 +8,8 @@
#define Fr_LONGMONTGOMERY 0xC0000000 #define Fr_LONGMONTGOMERY 0xC0000000
typedef struct __attribute__((__packed__)) { typedef struct __attribute__((__packed__)) {
int32_t shortVal; int32_t shortVal;
u_int32_t type; uint32_t type;
u_int64_t longVal[Fr_N64]; uint64_t longVal[Fr_N64];
} FrElement; } FrElement;
typedef FrElement *PFrElement; typedef FrElement *PFrElement;
extern FrElement Fr_q; extern FrElement Fr_q;

View File

@@ -8,8 +8,8 @@
#define <%=name%>_LONGMONTGOMERY 0xC0000000 #define <%=name%>_LONGMONTGOMERY 0xC0000000
typedef struct __attribute__((__packed__)) { typedef struct __attribute__((__packed__)) {
int32_t shortVal; int32_t shortVal;
u_int32_t type; uint32_t type;
u_int64_t longVal[<%=name%>_N64]; uint64_t longVal[<%=name%>_N64];
} <%=name%>Element; } <%=name%>Element;
typedef <%=name%>Element *P<%=name%>Element; typedef <%=name%>Element *P<%=name%>Element;
extern <%=name%>Element <%=name%>_q; extern <%=name%>Element <%=name%>_q;

View File

@@ -5,6 +5,7 @@
#include <iomanip> #include <iomanip>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h>
#include <thread> #include <thread>
#include "calcwit.h" #include "calcwit.h"
#include "utils.h" #include "utils.h"

View File

@@ -22,6 +22,7 @@ const bigInt = require("big-integer");
const utils = require("./utils"); const utils = require("./utils");
const gen = require("./c_gen").gen; const gen = require("./c_gen").gen;
const createRefs = require("./c_gen").createRefs; const createRefs = require("./c_gen").createRefs;
const streamFromMultiArray = require("./stream_from_multiarray");
module.exports = buildC; module.exports = buildC;
@@ -55,17 +56,18 @@ function buildC(ctx) {
const wit2Sig = buildWit2Sig(ctx); const wit2Sig = buildWit2Sig(ctx);
const circuitVar = buildCircuitVar(ctx); const circuitVar = buildCircuitVar(ctx);
return "" + return streamFromMultiArray([
headder + "\n" + headder , "\n" ,
sizes + "\n" + sizes , "\n" ,
constants + "\n" + constants , "\n" ,
entryTables + "\n" + entryTables , "\n" ,
functions + "\n" + functions , "\n" ,
code + "\n" + code , "\n" ,
compnentsArray + "\n" + compnentsArray , "\n" ,
mapIsInput + "\n" + mapIsInput , "\n" ,
wit2Sig +"\n" + wit2Sig , "\n" ,
circuitVar; circuitVar
]);
} }
function buildEntryTables(ctx) { function buildEntryTables(ctx) {
@@ -96,13 +98,14 @@ function buildEntryTables(ctx) {
} }
return "" + return [
"// HashMaps\n" + "// HashMaps\n" ,
codes_hashMaps.join("\n") + "\n" + codes_hashMaps , "\n" ,
"\n" + "\n" ,
"// Component Entries\n" + "// Component Entries\n" ,
codes_componentEntries.join("\n") + "\n" + codes_componentEntries , "\n" ,
"\n"; "\n"
];
function addHashTable(cIdx) { function addHashTable(cIdx) {
const keys = Object.keys(ctx.components[cIdx].names.o); const keys = Object.keys(ctx.components[cIdx].names.o);
@@ -198,12 +201,18 @@ function buildCode(ctx) {
ctx.components[i].fnName = fName; ctx.components[i].fnName = fName;
} }
return fnComponents.join("\n"); return fnComponents;
} }
function buildFuncFunctions(ctx) { function buildFuncFunctions(ctx) {
return "// Functions\n" + if (ctx.functionCodes) {
ctx.functionCodes.join("\n"); return ["// Functions\n" ,
ctx.functionCodes
];
} else {
return "";
}
} }
function buildComponentsArray(ctx) { function buildComponentsArray(ctx) {
@@ -225,12 +234,11 @@ function buildComponentsArray(ctx) {
ccodes.push(`{${ctx.components[i].htName},${ctx.components[i].etName},${ctx.components[i].fnName}, ${ctx.components[i].nInSignals}, ${newThread}}\n`); ccodes.push(`{${ctx.components[i].htName},${ctx.components[i].etName},${ctx.components[i].fnName}, ${ctx.components[i].nInSignals}, ${newThread}}\n`);
} }
ccodes.push("};\n"); ccodes.push("};\n");
const codeComponents = ccodes.join("");
return "" + return [
"// Components\n" + "// Components\n" ,
codeComponents + ccodes , "\n"
"\n"; ];
} }
@@ -247,8 +255,10 @@ function buildHeader(ctx) {
} }
function buildSizes(ctx) { function buildSizes(ctx) {
return "// Sizes\n" + return [
ctx.codes_sizes.join("\n"); "// Sizes\n" ,
ctx.codes_sizes
];
} }
function buildConstants(ctx) { function buildConstants(ctx) {
@@ -256,14 +266,14 @@ function buildConstants(ctx) {
const R = bigInt.one.shiftLeft(n64*64); const R = bigInt.one.shiftLeft(n64*64);
const lines = []; const lines = [];
lines.push("// Constants"); lines.push("// Constants\n");
lines.push(`FrElement _constants[${ctx.constants.length}] = {`); lines.push(`FrElement _constants[${ctx.constants.length}] = {\n`);
for (let i=0; i<ctx.constants.length; i++) { for (let i=0; i<ctx.constants.length; i++) {
lines.push((i>0 ? "," : " ") + "{" + number2Code(ctx.constants[i]) + "}"); lines.push((i>0 ? "," : " ") + "{" + number2Code(ctx.constants[i]) + "}\n");
} }
lines.push("};"); lines.push("};\n");
return lines.join("\n"); return lines;
function number2Code(n) { function number2Code(n) {
if (n.lt(bigInt("80000000", 16)) ) { if (n.lt(bigInt("80000000", 16)) ) {
@@ -343,10 +353,11 @@ function buildMapIsInput(ctx) {
arr.push(line); arr.push(line);
} }
return "// mapIsArray\n" + return ["// mapIsArray\n" ,
`u32 _mapIsInput[${Math.floor((ctx.signals.length-1) / 32)+1}] = {\n`+ `u32 _mapIsInput[${Math.floor((ctx.signals.length-1) / 32)+1}] = {\n`,
arr.join("\n") + "\n" + arr, "\n" ,
"};\n"; "};\n"
];
function toHex(number) { function toHex(number) {
if (number < 0) number = 0xFFFFFFFF + number + 1; if (number < 0) number = 0xFFFFFFFF + number + 1;
@@ -393,7 +404,7 @@ function buildWit2Sig(ctx) {
if (code != "") codes.push(code + "\n"); if (code != "") codes.push(code + "\n");
codes.push("};\n"); codes.push("};\n");
return codes.join(""); return codes;
} }

View File

@@ -83,7 +83,8 @@ async function compile(srcFile, options) {
if (options.cSourceWriteStream) { if (options.cSourceWriteStream) {
const cSrc = buildC(ctx); const cSrc = buildC(ctx);
options.cSourceWriteStream.write(cSrc); cSrc.pipe(options.cSourceWriteStream);
await new Promise(fulfill => options.cSourceWriteStream.on("finish", fulfill));
} }
// const mainCode = gen(ctx,ast); // const mainCode = gen(ctx,ast);

View File

@@ -0,0 +1,50 @@
const Readable = require("stream").Readable;
module.exports = function streamFromMultiarray(ma) {
const rs = Readable();
let curIndex = getFirstIdx(ma);
rs._read = function() {
let res;
do {
res = objFromIdx(ma, curIndex);
curIndex = nextIdx(curIndex);
} while (res==="");
rs.push(res);
};
return rs;
function getFirstIdx(ma) {
if (!Array.isArray(ma)) return [];
return [0, ...getFirstIdx(ma[0])];
}
function nextIdx(idx) {
if (idx == null) return null;
if (idx.length == 0) return null;
const parentIdx = idx.slice(0,-1);
const itObj = objFromIdx(ma, parentIdx);
const newLastIdx = idx[idx.length-1]+1;
if (newLastIdx < itObj.length) {
const resIdx = idx.slice();
resIdx[resIdx.length-1] = newLastIdx;
return [...resIdx, ...getFirstIdx(itObj[newLastIdx])];
} else {
return nextIdx(parentIdx);
}
}
function objFromIdx(ma, idx) {
if (idx == null) return null;
if (idx.length == 0) return ma;
if (ma.length == 0) return "";
return objFromIdx(ma[idx[0]], idx.slice(1));
}
};

View File

@@ -17,6 +17,7 @@ describe("field asm test", function () {
const tv = buildTestVector2(bn128r, "add"); const tv = buildTestVector2(bn128r, "add");
await tester(bn128r, tv); await tester(bn128r, tv);
}); });
it("secp256k1q add", async () => { it("secp256k1q add", async () => {
const tv = buildTestVector2(secp256k1q, "add"); const tv = buildTestVector2(secp256k1q, "add");
await tester(secp256k1q, tv); await tester(secp256k1q, tv);