mirror of
https://github.com/arnaucube/websnark.git
synced 2026-02-27 21:46:42 +01:00
Initial commit
This commit is contained in:
147
test/bn128.js
Normal file
147
test/bn128.js
Normal file
@@ -0,0 +1,147 @@
|
||||
const assert = require("assert");
|
||||
const refBn128 = require("snarkjs").bn128;
|
||||
const refBigInt = require("snarkjs").bigInt;
|
||||
|
||||
const buildBn128 = require("../index.js").buildBn128;
|
||||
|
||||
describe("Basic tests for g1 in bn128", () => {
|
||||
it("It should do a basic point doubling G1", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G1.double(refBn128.g1);
|
||||
|
||||
const p1 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p1, p1);
|
||||
bn128.g1_double(p1, p1);
|
||||
bn128.g1_fromMontgomery(p1, p1);
|
||||
const d = bn128.g1_getPoint(p1);
|
||||
|
||||
for (let i=0; i<3; i++) {
|
||||
d[i] = refBigInt(d[i].toString());
|
||||
}
|
||||
|
||||
assert(refBn128.G1.equals(d, refD));
|
||||
});
|
||||
it("It should add two points G1", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G1.affine(refBn128.G1.mulScalar(refBn128.g1, 3));
|
||||
|
||||
const p1 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p1, p1);
|
||||
bn128.g1_double(p1, p1);
|
||||
const p2 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p2, p2);
|
||||
bn128.g1_add(p1, p2, p2);
|
||||
bn128.g1_affine(p2, p2);
|
||||
bn128.g1_fromMontgomery(p2, p2);
|
||||
const d = bn128.g1_getPoint(p2);
|
||||
|
||||
d[0] = refBigInt(d[0].toString());
|
||||
d[1] = refBigInt(d[1].toString());
|
||||
d[2] = refBigInt(d[2].toString());
|
||||
|
||||
|
||||
assert(d[0].equals(refD[0]));
|
||||
assert(d[1].equals(refD[1]));
|
||||
assert(d[2].equals(1));
|
||||
|
||||
assert(refBn128.G1.equals(d, refD));
|
||||
});
|
||||
it("It should timesScalar G1", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G1.mulScalar(refBn128.g1, 55);
|
||||
|
||||
const p1 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p1, p1);
|
||||
|
||||
const s = bn128.allocInt(55);
|
||||
bn128.g1_timesScalar(p1, s, 32, p1);
|
||||
|
||||
bn128.g1_fromMontgomery(p1, p1);
|
||||
const d = bn128.g1_getPoint(p1);
|
||||
|
||||
for (let i=0; i<3; i++) {
|
||||
d[i] = refBigInt(d[i].toString());
|
||||
}
|
||||
|
||||
assert(refBn128.G1.equals(d, refD));
|
||||
});
|
||||
it("G1n == 0", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const p1 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p1, p1);
|
||||
|
||||
const s = bn128.allocInt(bn128.r);
|
||||
bn128.g1_timesScalar(p1, s, 32, p1);
|
||||
|
||||
bn128.g1_fromMontgomery(p1, p1);
|
||||
|
||||
assert(bn128.g1_isZero(p1));
|
||||
});
|
||||
it("It should do a test", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const t = bn128.test_AddG1(100000);
|
||||
|
||||
console.log(t);
|
||||
}).timeout(10000000);
|
||||
it("It should validate the test", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G1.mulScalar(refBn128.g1, 100000);
|
||||
|
||||
const p1 = bn128.g1_allocPoint(refBn128.g1);
|
||||
bn128.g1_toMontgomery(p1, p1);
|
||||
const p2 = bn128.g1_allocPoint();
|
||||
bn128.testAddG1(100000, p1, p2);
|
||||
bn128.g1_fromMontgomery(p2, p2);
|
||||
const d = bn128.g1_getPoint(p2);
|
||||
|
||||
for (let i=0; i<3; i++) {
|
||||
d[i] = refBigInt(d[i].toString());
|
||||
}
|
||||
|
||||
assert(refBn128.G1.equals(d, refD));
|
||||
|
||||
}).timeout(10000000);
|
||||
it("It should do a basic point doubling in G2", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G2.double(refBn128.g2);
|
||||
|
||||
const p1 = bn128.g2_allocPoint(refBn128.g2);
|
||||
bn128.g2_toMontgomery(p1, p1);
|
||||
bn128.g2_double(p1, p1);
|
||||
bn128.g2_fromMontgomery(p1, p1);
|
||||
const d = bn128.g2_getPoint(p1);
|
||||
|
||||
for (let i=0; i<3; i++) {
|
||||
for (let j=0; j<2; j++) {
|
||||
d[i][j] = refBigInt(d[i][j].toString());
|
||||
}
|
||||
}
|
||||
|
||||
assert(refBn128.G2.equals(d, refD));
|
||||
});
|
||||
it("It should add two points in G2", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
const refD = refBn128.G2.affine(refBn128.G2.mulScalar(refBn128.g2, 3));
|
||||
|
||||
const p1 = bn128.g2_allocPoint(refBn128.g2);
|
||||
bn128.g2_toMontgomery(p1, p1);
|
||||
bn128.g2_double(p1, p1);
|
||||
const p2 = bn128.g2_allocPoint(refBn128.g2);
|
||||
bn128.g2_toMontgomery(p2, p2);
|
||||
bn128.g2_add(p1, p2, p2);
|
||||
bn128.g2_affine(p2, p2);
|
||||
bn128.g2_fromMontgomery(p2, p2);
|
||||
const d = bn128.g2_getPoint(p2);
|
||||
|
||||
for (let i=0; i<3; i++) {
|
||||
for (let j=0; j<2; j++) {
|
||||
d[i][j] = refBigInt(d[i][j].toString());
|
||||
assert(d[i][j].equals(refD[i][j]));
|
||||
}
|
||||
}
|
||||
|
||||
assert(refBn128.G2.equals(d, refD));
|
||||
});
|
||||
});
|
||||
BIN
test/data/proving_key.bin
Normal file
BIN
test/data/proving_key.bin
Normal file
Binary file not shown.
60
test/data/public.json
Normal file
60
test/data/public.json
Normal file
@@ -0,0 +1,60 @@
|
||||
[
|
||||
"12195095808573067990042857686504557510035235683154016308243419405358680169250",
|
||||
"7149014917815960042505969439971619119991011354574443484106856202048948095881",
|
||||
"9164435831827345487378393454304824441756195871900421654673163382659437536500",
|
||||
"1234",
|
||||
"0",
|
||||
"16374068974774336469776493892198386626211184005839336945221081336304141111920",
|
||||
"1",
|
||||
"2610057752638682202795145288373380503107623443963127956230801721756904484787",
|
||||
"16617171478497210597712478520507818259149717466230047843969353176573634386897",
|
||||
"1790107960388390971897835168022684452552187545968846840523968533783427112708",
|
||||
"13303187477790464289185372704934741905530397009889514067884373165312221183226",
|
||||
"20934614636390406120326149903842640726858112697509676494085990398714931055996",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"11968552468543593335149856751038426364722479921732335754114707699811978359681",
|
||||
"9614565467070354290860829491102892458030262142650662112245199748688182033133",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"0",
|
||||
"3900887248494684663431642341114683844061898198331562653906267847513911890751",
|
||||
"17279342286447729775991865155392847976865724336068569228942936646576478041635",
|
||||
"0"
|
||||
]
|
||||
378
test/data/verification_key.json
Normal file
378
test/data/verification_key.json
Normal file
@@ -0,0 +1,378 @@
|
||||
{
|
||||
"protocol": "groth",
|
||||
"nPublic": 58,
|
||||
"IC": [
|
||||
[
|
||||
"19526011409366579990588025301030213951715906703577073975290871624466881592401",
|
||||
"19704225241808120619466308438935447262963873917264064669080753233318969572957",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"7218481415766897306762916767110075847697992625643495798846732092896344771677",
|
||||
"1810734562057506037864970683351135446505342561419524855305385566525430554608",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1640189422982581804826644035748296239719141803014116052080598186211151031797",
|
||||
"7781696247496343071100255748895449136227964112761638882773761539066568268925",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1019783362851718589135884871584846731091007700702318392055072177004648561589",
|
||||
"18812499958741900484687753662765966120897029726834689506634674431261159346840",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"15561759052567336872552966722770915260975101273887034098005539514081530527925",
|
||||
"11730428660167585547193351712992338394459044787769016092386316685070099352472",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1024450630178286178450853989597618731194658350885429138953206234739921258345",
|
||||
"7948069502311904001229660361963340784026126140723442378626252475516635024822",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"8134630773713074896857037173371019670883865326581932178449629660512993310425",
|
||||
"6162538535380093935174079566719686610557565288806461488942825301577983962127",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"8713144302139572077210052963377568333718037671811574593525902475107121219071",
|
||||
"13618940145565575426081729012812756247313835929620966248623806311505567102432",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"2208161957288886407409949471646360867727094427710349204075749603229409811551",
|
||||
"19165194795626512061280419859788136098199019550268978813401285308580039446381",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1481335695502173099248239238900758768801091349137030249276629661318408132871",
|
||||
"5156686038021439018308366275250160506883007416398589471786874665591845100113",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"20337511777673950886185756823107347060969023608337968294278846656718023200150",
|
||||
"3997644781553846440387918107564571745476924483850609244466307282628877247562",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"10588095668159724333142658097242748201084821311539505166654240692525635653949",
|
||||
"1183100960889281823631938371556943968916169482989513163517955652814147785270",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"14899200894902404684338252010495693209614589615201102355683265977943244426004",
|
||||
"6733181143166489149612223364873851869451680106085133952329639843716177370522",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1683537073912953978091278971997049690818520143499563010072565648446561661056",
|
||||
"19419929908654053003650694876848111465754667348310947518133436565700017002483",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"14657090907982846377445922373510406393589381865954917955559100596313207100861",
|
||||
"13642650140642299314216565478747411703974469867765487295597972222030646618581",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"16529622819344543978903257968152672100303712811187008674706734962518660200534",
|
||||
"9047308189580806381060024072651025924757275727793326189939157176373582276423",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"3080248660073481279234987580633467131838554544589313213304320336447912874502",
|
||||
"12178549914181501174961289691127530013023904264051503031309740303517490947766",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"15487788123318013174142216619036794699452999597075149645621900875574186695610",
|
||||
"1626752354094836838615855864490581912181205603023556119206923761362325487488",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"1666930804642622665542570167075210228582250288184353798587314779395900191926",
|
||||
"19705035825701732447104451708888835197574105722528610005429830999713513944135",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"6864231653145100847184101019813757441375546934931929553137150578511457821468",
|
||||
"11030717897717091434566238005756773466216938437645365639326676041449665933687",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"17708960029000737859399187469088839058910680034659542429747423649838721586318",
|
||||
"17966398620544733014948000033194587396780241478697380962648049866838729264654",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"13757192009142934986497800326740625614298535990393743419508862715438850214278",
|
||||
"21608544818365956429804089572501400134306590793845664759121305528719014468539",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"2387150367018947723453720841613212674042625869787597370012597855542196564772",
|
||||
"11695602459063663755646871470499162454273937998667584326916784819522675676030",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"3969932184785511719481056173532036069678557655561039720091342741570026295152",
|
||||
"17993913767992706963197080864583362193024219058878156591482644926890948868797",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"9423946557684255938323840651123017285324730158168043839780806631003806918939",
|
||||
"20601140822008955908584760520762447153677611366925313301286794723328845538334",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"12962476762153853692961359313770195651287320370991012193410923729342519400410",
|
||||
"16790095546018287000758153406357558316483550179182040426959851702714662233350",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"11513038453371099732062533711296648120504101464641839310671168933784980314882",
|
||||
"19906375207605336857368669180394946464921182728087574346715161382734582996091",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"2121497929168641193320430463750735744335423634358969475213438628839145775097",
|
||||
"14279619451032406093176776946256910631214549125624592712768709224713246159330",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"11226191782273968706267268899222700962751661296528346044234995849296626637818",
|
||||
"13010144704231455483130550798892383439128876102102878476813890284532924199082",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"11110550860460015317490388108081233932145530993142470924675507899413835184448",
|
||||
"16564127121244024114702603123214129039853156994745132309985985728814892932482",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"16778941937031321722070777716071699346818608459977560423095851882699057661238",
|
||||
"4697439800149822368300068885116892100711854565071104448591753229703512600800",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"15872083856166627537790395064677676672858055702693032209281782235450963069960",
|
||||
"10359824654894233948940829295414182326916813602184261612555323269593953713959",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"15247415861798118260310199809217153579347967502938862593203253520242564857778",
|
||||
"17943608471215894498734622964827265729455290707752355804444644415538765187171",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"19910646054904277798554639079332859903830938020106054338361835817749036204289",
|
||||
"14251173391661908313942511660900252216342610385205017933107964019360045794603",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"15617270263010441782314109350958570541215852291212818016021084008333418208875",
|
||||
"21881089065537370063139940701535262435555591536651612422900371838614353113281",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"6421115626779256473944138173048502514066702768381496596650324103983916504835",
|
||||
"3477316365965567489844246613122076671753354745444827471590577962440540253825",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"16473272305165140318293304721756072118952895979824993476741656445985532109687",
|
||||
"11966737459889574739163354519745663782810240444604837406970095410044504857114",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"5372377827028613665959777806978819222622231879950628237059135069445403406617",
|
||||
"10754772058946143812874307806006613255761709338906946759757401504859407645417",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"14186175579895909616185878057026760618866356046925560400399807036820346884046",
|
||||
"4815071274998802335087493250322971182140859271153573841178680570745042169758",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"18587428576009373156679145442597168345936336951096103073893982856175630643693",
|
||||
"16551515971501720092878042838225840381462545412649977468782450446861560095079",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"20092704064518140906336001291719220397791993391927049447238747943557979137332",
|
||||
"14309044514703625249681667739033504979034764749598333744088213698477089190167",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"18094211252427269277727737867758525438768651774457808013501513791100524400111",
|
||||
"1953616651202224570087320348179078176455230140050934991264156573541656440177",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"8306150994125890187737385803981541016404020628778896850769772941676552580695",
|
||||
"9692340394713555790690111046383485596714503094426824962694563185658648842716",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"2310757642461495319988919139078520159193510139788049016019877513576089825606",
|
||||
"8889867579822094460826499233108459267599315072851707155589562022968565836877",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"18308698969834252376687927943697318806874330259437519073367865794809196122421",
|
||||
"14739033668305736376862695711443548284923118719280017423642214528572384515934",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"21281456929816658128965712205802466600645879143675783709584584463978062814070",
|
||||
"11318407702446502058218573109267233739777220569151759825244543584735350643466",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"7520703426805357030477495741143669754535245601456768825659455316560615971189",
|
||||
"11716692431794868904945130966329488889062393077100622718344498934379412336473",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"12755699673150967843636678152611549820209710341861685811272938968227849444490",
|
||||
"8041405236201023920708302321965742245298279037603394500679202870952036944455",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"4767324308822656869298992811825434492721876454715254600244690065028848020971",
|
||||
"7698254074025215726826011606321771959580174521108656914865701606041868015929",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"5069108519819643877425272525791845153259463918796090785858826854873469677567",
|
||||
"19441624293073788344676145862916585919555281266024685026467295031220552700054",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"19615829997657139098357071858533416653704257439734430213792369647973566826347",
|
||||
"20354691099796661979686973753889173391948033275751613338970065504168017804337",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"11027190822371723497281530686863090859613245206871440157541315964572815271179",
|
||||
"10675136011261330036038351905981966853190049667679786632322902689639273096714",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"6073908302198369305254211170927314788182979554648782100497505455266731015204",
|
||||
"230783342774856919928214058753287925592697137855334446779967352493825837423",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"8065349080015031289241881658385531453814503979517539346801510330252327420241",
|
||||
"14735126201189209265547932564409552262152445686850481557202138107408115327464",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"6452037663171386579712621793513191966255625190206245575651250486849182460440",
|
||||
"7209242914697571385995884156916495284148841850843926922191745144563603631497",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"10593401732655136290887720645830905766352213200230343334980873391309958939320",
|
||||
"666955912278201937315337912832661869841839611440234361211929441370598456858",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"3479342154509588972525995110657563238207085236375361439444008581380409197198",
|
||||
"2842082156517941562270865475681450943187927072014071620218399830230257691351",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"692434019399694517146046655769177702432905351223416354821366078192182076820",
|
||||
"3395628146877374571895195099601711702304342467654447561346689703803124051395",
|
||||
"1"
|
||||
],
|
||||
[
|
||||
"897877668313362010234827860073421630587020756077760567259587493004095744184",
|
||||
"7566955279869906474734064013485588345675973359196540621993138844162613375198",
|
||||
"1"
|
||||
]
|
||||
],
|
||||
"vk_alfa_1": [
|
||||
"8354125022402491146096524632602954406606496454803779043544940201207527912027",
|
||||
"719066282644331645717102069170608708195162774370272260015202086661089311234",
|
||||
"1"
|
||||
],
|
||||
"vk_beta_2": [
|
||||
[
|
||||
"15819609270299917787101227421057497064555252095398692710896314899171935449228",
|
||||
"12410557311433316645833461472858428751547879560031808599488331029313135860054"
|
||||
],
|
||||
[
|
||||
"5915247915713704640408458318654604569396223348462269801166279014325877172920",
|
||||
"3151765353208434115856893997427239125947497909307782934497369635848305207349"
|
||||
],
|
||||
[
|
||||
"1",
|
||||
"0"
|
||||
]
|
||||
],
|
||||
"vk_gamma_2": [
|
||||
[
|
||||
"20998635465704765944234729848213271190298703821739579245252166965359275379567",
|
||||
"18569698049261699868049342823691041461765011065833511061731150982712719058931"
|
||||
],
|
||||
[
|
||||
"19643309731014428269860044007048729642835975708160558919248553748448226867008",
|
||||
"4057552944219884903616160090555822269203226833340888130271232201855723185188"
|
||||
],
|
||||
[
|
||||
"1",
|
||||
"0"
|
||||
]
|
||||
],
|
||||
"vk_delta_2": [
|
||||
[
|
||||
"1699455723545013724352264641481223849638169293269636387255739145105164643572",
|
||||
"10951443100057856372062056768155654835091940387457226546452206121970064287769"
|
||||
],
|
||||
[
|
||||
"18091804447302921360663814931698141642678950451776128240905512956330370630584",
|
||||
"11051171602925343050692043383444760908270199727214620699742880132687007451610"
|
||||
],
|
||||
[
|
||||
"1",
|
||||
"0"
|
||||
]
|
||||
],
|
||||
"vk_alfabeta_12": [
|
||||
[
|
||||
[
|
||||
"18723835367546013216669396584879552615309554640417855595864851968454850794686",
|
||||
"2751734539677780406895433411352652269903002350864560518448916000577298711044"
|
||||
],
|
||||
[
|
||||
"6091631998457053869677482825687515224140938572474416320681319181954212930660",
|
||||
"14750817718913847167640471421588191103071235885472070890891565424818957616506"
|
||||
],
|
||||
[
|
||||
"1503045325833070525242109471593215150873992909765587341806427485400509901763",
|
||||
"19595263023959996895029566929430291188201738108321510303592033282761719078927"
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
"14243067455511068097745434183222546746125426041491764346913202956845188552019",
|
||||
"17462170864881388598981702392557723387786493323913878401432726985509236575485"
|
||||
],
|
||||
[
|
||||
"15670136540447067653418982806709348681767811130611329178707663135867906047157",
|
||||
"8632459998104744185409696090239906876607793844814496641442049837459287937900"
|
||||
],
|
||||
[
|
||||
"1147927280145543088837981995583243716796018112475088793287724642814036528129",
|
||||
"15939953021942181393804832630429756290981160876828402349069996652097158880389"
|
||||
]
|
||||
]
|
||||
]
|
||||
}
|
||||
BIN
test/data/witness.bin
Normal file
BIN
test/data/witness.bin
Normal file
Binary file not shown.
485
test/f1.js
Normal file
485
test/f1.js
Normal file
@@ -0,0 +1,485 @@
|
||||
const assert = require("assert");
|
||||
const bigInt = require("big-integer");
|
||||
|
||||
const buildF1 = require("../index.js").buildF1;
|
||||
|
||||
describe("Basic tests for Zq", () => {
|
||||
it("It should do a basic addition", async () => {
|
||||
const f1 = await buildF1(101);
|
||||
|
||||
const pA = f1.allocInt(3);
|
||||
const pB = f1.allocInt(4);
|
||||
const pC = f1.allocInt();
|
||||
f1.f1_add(pA, pB, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
assert.equal(c, 7);
|
||||
});
|
||||
it("Should add with 2 chunks", async () => {
|
||||
const f1 = await buildF1(bigInt("100000000000000000001", 16));
|
||||
|
||||
const pA = f1.allocInt(bigInt("FFFFFFFFFFFFFFFF", 16));
|
||||
const pB = f1.allocInt(1);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_add(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
assert(c.equals(bigInt("10000000000000000", 16)));
|
||||
});
|
||||
it("Should add with 2 chunks overflow", async () => {
|
||||
const q = bigInt("10000000000000001", 16);
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt(bigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16).mod(q));
|
||||
const pB = f1.allocInt(1);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_add(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
assert(c.equals(1));
|
||||
});
|
||||
it("Should add with double overflow", async () => {
|
||||
const q = bigInt(1).shiftLeft(255).add(1);
|
||||
const a = bigInt(1).shiftLeft(256).minus(1).mod(q);
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt(a);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_add(pA, pA, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
assert(c.equals(a.add(a).mod(q)));
|
||||
});
|
||||
it("It should do a basic substraction", async () => {
|
||||
const f1 = await buildF1(101);
|
||||
|
||||
const pA = f1.allocInt(5);
|
||||
const pB = f1.allocInt(3);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_sub(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
assert.equal(c, 2);
|
||||
});
|
||||
it("It should do a basic substraction with negative result", async () => {
|
||||
const f1 = await buildF1(101);
|
||||
|
||||
const pA = f1.allocInt(3);
|
||||
const pB = f1.allocInt(5);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_sub(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
assert.equal(c.mod(101), 99);
|
||||
});
|
||||
it("Should substract with 2 chunks overflow", async () => {
|
||||
const q = bigInt("10000000000000001", 16);
|
||||
const a = bigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16).mod(q);
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt(1);
|
||||
const pB = f1.allocInt(a);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_sub(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
let d = bigInt.one.minus(a).mod(q);
|
||||
if (d.isNegative()) d = d.add(q);
|
||||
|
||||
assert(c.equals(d));
|
||||
});
|
||||
it("It should Substract a big number", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const a = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const b = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808234523");
|
||||
|
||||
const pA = f1.allocInt(a);
|
||||
const pB = f1.allocInt(b);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_sub(pA, pB, pC);
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
let cc = a.minus(b).mod(q);
|
||||
if (cc.isNegative()) cc = cc.add(q);
|
||||
assert(cc.equals(c.mod(q)));
|
||||
|
||||
|
||||
const pAA = f1.allocInt(b);
|
||||
const pBB = f1.allocInt(a);
|
||||
const pCC = f1.allocInt();
|
||||
|
||||
f1.f1_sub(pAA, pBB, pCC);
|
||||
const d = f1.getInt(pCC);
|
||||
|
||||
let dd = b.minus(a).mod(q);
|
||||
if (dd.isNegative()) dd = dd.add(q);
|
||||
assert(dd.equals(d.mod(q)));
|
||||
});
|
||||
|
||||
it("It should do a basic multiplication", async () => {
|
||||
const f1 = await buildF1(101);
|
||||
|
||||
const pA = f1.allocInt(3);
|
||||
const pB = f1.allocInt(4);
|
||||
const pC = f1.allocInt2();
|
||||
|
||||
f1.int_mul(pA, pB, pC);
|
||||
const c = f1.getInt2(pC);
|
||||
|
||||
assert.equal(c, 12);
|
||||
});
|
||||
|
||||
it("It should do a basic division", async () => {
|
||||
const f1 = await buildF1(101);
|
||||
|
||||
const pA = f1.allocInt(12);
|
||||
const pB = f1.allocInt(6);
|
||||
const pC = f1.allocInt();
|
||||
f1.int_div(pA, pB, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
assert.equal(c, 2);
|
||||
});
|
||||
it("It should do a more complex division", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt(bigInt("FFFF00000000", 16));
|
||||
const pB = f1.allocInt(bigInt("100000000", 16));
|
||||
const pC = f1.allocInt();
|
||||
f1.int_div(pA, pB, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
assert(c.equals(bigInt("FFFF", 16)));
|
||||
});
|
||||
it("It should do a division by zero", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt(bigInt("FFFF00000000", 16));
|
||||
const pB = f1.allocInt(0);
|
||||
const pC = f1.allocInt();
|
||||
try {
|
||||
f1.int_div(pA, pB, pC);
|
||||
assert(false, "Didn't throw...");
|
||||
} catch (err) {
|
||||
assert.equal(err.toString(), "RuntimeError: divide by zero");
|
||||
}
|
||||
});
|
||||
|
||||
it("It should do a various division", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
|
||||
const v= [
|
||||
bigInt.zero,
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(bigInt("F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0F0", 16)),
|
||||
bigInt(bigInt("10101010101010101010101010101010", 16)),
|
||||
bigInt(bigInt("FF00FF00FF00FF00FF00FF00FF00FF00", 16)),
|
||||
bigInt(bigInt("11001100110011001100110011001100", 16)),
|
||||
bigInt(bigInt("F0F0F0F0F0F0F0F0", 16)),
|
||||
bigInt(bigInt("1010101010101010", 16)),
|
||||
bigInt(bigInt("FF00FF00FF00FF00", 16)),
|
||||
bigInt(bigInt("1100110011001100", 16)),
|
||||
bigInt(2),
|
||||
bigInt.one,
|
||||
];
|
||||
|
||||
const pA = f1.allocInt();
|
||||
const pB = f1.allocInt();
|
||||
const pC = f1.allocInt();
|
||||
const pR = f1.allocInt();
|
||||
for (let i=0; i<v.length; i++) {
|
||||
for (let j=1; j<v.length; j++) {
|
||||
const expected_c = v[i].divide(v[j]);
|
||||
const expected_r = v[i].mod(v[j]);
|
||||
|
||||
f1.putInt(pA, v[i]);
|
||||
f1.putInt(pB, v[j]);
|
||||
|
||||
f1.int_div(pA, pB, pC, pR);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
const r = f1.getInt(pR);
|
||||
|
||||
assert(expected_r.equals(r));
|
||||
assert(expected_c.equals(c));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
it("It should do a basic reduction 1", async () => {
|
||||
const f1 = await buildF1(bigInt("FFFFFFFFFFFFFFFF",16));
|
||||
|
||||
const pA = f1.allocInt2(bigInt(0x10000000000000000));
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1m_mReduct(pA, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
|
||||
assert.equal(c, 1);
|
||||
});
|
||||
it("It should do a basic reduction 2", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const a = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const b = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808234523");
|
||||
|
||||
const pA = f1.allocInt(a);
|
||||
const pB = f1.allocInt(b);
|
||||
const pC = f1.allocInt2();
|
||||
const pD = f1.allocInt();
|
||||
|
||||
f1.int_mul(pA, pB, pC);
|
||||
const c = f1.getInt2(pC);
|
||||
|
||||
f1.f1m_mReduct(pC, pD);
|
||||
const d = f1.getInt(pD);
|
||||
|
||||
const r = bigInt.one.shiftLeft(256).mod(q);
|
||||
const r2 = r.times(r).mod(q);
|
||||
|
||||
const pR2 = f1.allocInt(r2);
|
||||
|
||||
|
||||
const pE = f1.allocInt2();
|
||||
f1.int_mul(pD, pR2, pE);
|
||||
|
||||
const pF = f1.allocInt();
|
||||
f1.f1m_mReduct(pE, pF);
|
||||
|
||||
const f = f1.getInt2(pF);
|
||||
|
||||
assert(a.times(b).mod(q).equals(f.mod(q)));
|
||||
});
|
||||
it("It should do a basic reduction 3", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const a = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const b = bigInt("10000242871839275222246405745257275088548364400416034343698204186575808234523");
|
||||
|
||||
const pA = f1.allocInt(a);
|
||||
const pB = f1.allocInt(b);
|
||||
const pC = f1.allocInt();
|
||||
|
||||
f1.f1_mul(pA, pB, pC);
|
||||
|
||||
const c = f1.getInt2(pC);
|
||||
assert(a.times(b).mod(q).equals(c.mod(q)));
|
||||
});
|
||||
it("It should do various test in zq Snarks modules", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
|
||||
const f1 = await buildF1(q);
|
||||
const v= [
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(2),
|
||||
bigInt.one,
|
||||
bigInt.zero
|
||||
];
|
||||
|
||||
const pA = f1.allocInt();
|
||||
const pB = f1.allocInt();
|
||||
const pC = f1.allocInt();
|
||||
let c;
|
||||
|
||||
for (let i=0; i<v.length; i++) {
|
||||
for (let j=0; j<5; j++) {
|
||||
|
||||
f1.putInt(pA, v[i]);
|
||||
f1.putInt(pB, v[j]);
|
||||
|
||||
// eq
|
||||
assert.equal( f1.int_eq(pA,pB), (i==j));
|
||||
|
||||
// add
|
||||
f1.f1_add(pA, pB, pC);
|
||||
c = f1.getInt2(pC);
|
||||
assert(c.equals(v[i].add(v[j]).mod(q)));
|
||||
|
||||
// sub
|
||||
f1.f1_sub(pA, pB, pC);
|
||||
c = f1.getInt2(pC);
|
||||
|
||||
let s = v[i].minus(v[j]).mod(q);
|
||||
if (s.isNegative()) s=s.add(q);
|
||||
assert(c.equals(s));
|
||||
|
||||
// mul
|
||||
f1.f1_mul(pA, pB, pC);
|
||||
c = f1.getInt2(pC);
|
||||
assert(c.equals(v[i].times(v[j]).mod(q)));
|
||||
}
|
||||
}
|
||||
});
|
||||
it("It should do a test", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const t = f1.test_F1(1000000);
|
||||
|
||||
console.log(t);
|
||||
|
||||
}).timeout(10000000);
|
||||
it("Should test to montgomery", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const r = bigInt(11);
|
||||
|
||||
const pR = f1.allocInt(r);
|
||||
const pRes = f1.allocInt();
|
||||
const pRes2 = f1.allocInt();
|
||||
|
||||
f1.f1m_toMontgomery(pR, pRes);
|
||||
const res = f1.getInt(pRes);
|
||||
|
||||
f1.f1m_fromMontgomery(pRes, pRes2);
|
||||
const res2 = f1.getInt(pRes2);
|
||||
|
||||
assert(res.equals(r.times( bigInt.one.shiftLeft(256)).mod(q)));
|
||||
assert(res2.equals(r));
|
||||
});
|
||||
it("Should convert back and forth montgomery", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const v= [
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(2),
|
||||
bigInt.one,
|
||||
bigInt.zero
|
||||
];
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt();
|
||||
|
||||
for (let i=0; i<v.length; i++) {
|
||||
f1.putInt(pA, v[i]);
|
||||
|
||||
f1.f1m_toMontgomery(pA, pA);
|
||||
f1.f1m_fromMontgomery(pA, pA);
|
||||
|
||||
const a = f1.getInt(pA);
|
||||
assert(v[i].equals(a));
|
||||
}
|
||||
});
|
||||
it("Should do inverse", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const v= [
|
||||
bigInt.one,
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(2),
|
||||
];
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt();
|
||||
const pB = f1.allocInt();
|
||||
const pQ = f1.allocInt();
|
||||
|
||||
f1.putInt(pQ, q);
|
||||
|
||||
for (let i=0; i<v.length; i++) {
|
||||
f1.putInt(pA, v[i]);
|
||||
|
||||
f1.int_inverseMod(pA, pQ, pB);
|
||||
|
||||
const b = f1.getInt(pB);
|
||||
assert(b.equals(v[i].modInv(q)));
|
||||
}
|
||||
});
|
||||
it("Should do inverse in montgomery", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const v= [
|
||||
bigInt.one,
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(2),
|
||||
];
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt();
|
||||
const pB = f1.allocInt();
|
||||
const pC = f1.allocInt();
|
||||
|
||||
for (let i=0; i<v.length; i++) {
|
||||
f1.putInt(pA, v[i]);
|
||||
|
||||
f1.f1m_toMontgomery(pA, pA);
|
||||
f1.f1m_inverse(pA, pB);
|
||||
f1.f1m_mul(pA, pB, pC);
|
||||
f1.f1m_fromMontgomery(pC, pC);
|
||||
|
||||
const c = f1.getInt(pC);
|
||||
assert(c.equals(1));
|
||||
}
|
||||
});
|
||||
it("Test Neg", async () => {
|
||||
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const v= [
|
||||
bigInt.one,
|
||||
q.minus(1),
|
||||
q.minus(2),
|
||||
q.minus(1).shiftRight(1),
|
||||
q.minus(1).shiftRight(1).add(1),
|
||||
q.minus(1).shiftRight(1).add(2),
|
||||
q.minus(1).shiftRight(1).minus(1),
|
||||
q.minus(1).shiftRight(1).minus(2),
|
||||
bigInt(2),
|
||||
];
|
||||
const f1 = await buildF1(q);
|
||||
|
||||
const pA = f1.allocInt();
|
||||
|
||||
for (let i=0; i<v.length; i++) {
|
||||
f1.putInt(pA, v[i]);
|
||||
|
||||
f1.f1m_neg(pA);
|
||||
f1.f1m_neg(pA);
|
||||
|
||||
const a = f1.getInt(pA);
|
||||
assert(a.equals(v[i]));
|
||||
}
|
||||
});
|
||||
});
|
||||
123
test/fft.js
Normal file
123
test/fft.js
Normal file
@@ -0,0 +1,123 @@
|
||||
const assert = require("assert");
|
||||
const refBn128 = require("snarkjs").bn128;
|
||||
const refBigInt = require("snarkjs").bigInt;
|
||||
|
||||
const buildBn128 = require("../index.js").buildBn128;
|
||||
|
||||
describe("FFT tests", () => {
|
||||
it("create a basic FFT", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const N=4;
|
||||
|
||||
const p = bn128.alloc(32*N);
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(p+i*32, i);
|
||||
}
|
||||
|
||||
bn128.fft_toMontgomeryN(p, p, N);
|
||||
bn128.fft_fft(p, N);
|
||||
bn128.fft_ifft(p, N);
|
||||
bn128.fft_fromMontgomeryN(p, p, N);
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
const a = bn128.getInt(p+i*32);
|
||||
assert.equal(a,i);
|
||||
}
|
||||
});
|
||||
|
||||
it("create a do it reverselly FFT", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const N=1024;
|
||||
|
||||
const p = bn128.alloc(32*N);
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(p+i*32, i);
|
||||
}
|
||||
|
||||
bn128.fft_toMontgomeryN(p, p, N);
|
||||
bn128.fft_ifft(p, N, 0);
|
||||
bn128.fft_fft(p, N, 0);
|
||||
bn128.fft_fromMontgomeryN(p, p, N);
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
const a = bn128.getInt(p+i*32);
|
||||
assert.equal(a,i);
|
||||
}
|
||||
});
|
||||
it("test with zeros", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const N=1024;
|
||||
|
||||
const p = bn128.alloc(32*N);
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(p+i*32, (i%2 == 0)? 0 : 1);
|
||||
}
|
||||
|
||||
bn128.fft_toMontgomeryN(p, p, N);
|
||||
bn128.fft_ifft(p, N, 0);
|
||||
bn128.fft_fft(p, N, 0);
|
||||
bn128.fft_fromMontgomeryN(p, p, N);
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
const a = bn128.getInt(p+i*32);
|
||||
assert.equal(a,(i%2 == 0)? 0 : 1);
|
||||
}
|
||||
});
|
||||
it("test interleaved", async () => {
|
||||
const bn128 = await buildBn128();
|
||||
|
||||
const N=1024;
|
||||
|
||||
const p = bn128.alloc(32*N);
|
||||
const pr1 = bn128.alloc(32*N*2);
|
||||
const pr2 = bn128.alloc(32*N*2);
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(p+i*32, i);
|
||||
}
|
||||
bn128.fft_toMontgomeryN(p, p, N);
|
||||
bn128.fft_fft(p, N, 0);
|
||||
bn128.fft_copyNInterleaved(p, pr1, N);
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(p+i*32, i);
|
||||
}
|
||||
bn128.fft_toMontgomeryN(p, p, N);
|
||||
bn128.fft_fft(p, N, 1);
|
||||
bn128.fft_copyNInterleaved(p, pr1+32, N);
|
||||
|
||||
bn128.fft_fromMontgomeryN(pr1, pr1, N*2);
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
bn128.putInt(pr2+i*32, i);
|
||||
}
|
||||
for (let i=N; i<N*2; i++) {
|
||||
bn128.putInt(pr2+i*32, 0);
|
||||
}
|
||||
bn128.fft_toMontgomeryN(pr2, pr2, N*2);
|
||||
bn128.fft_fft(pr2, N*2, 0);
|
||||
bn128.fft_fromMontgomeryN(pr2, pr2, N*2);
|
||||
|
||||
for (let i=0; i<N*2; i++) {
|
||||
const a = bn128.getInt(pr1+i*32, 1);
|
||||
const b = bn128.getInt(pr2+i*32, 1);
|
||||
assert(a.equals(b));
|
||||
}
|
||||
|
||||
bn128.fft_toMontgomeryN(pr1, pr1, N*2);
|
||||
bn128.fft_ifft(pr1, N*2, 0);
|
||||
bn128.fft_fromMontgomeryN(pr1, pr1, N*2);
|
||||
for (let i=0; i<N; i++) {
|
||||
const a = bn128.getInt(pr1+i*32, 1);
|
||||
assert.equal(a,i);
|
||||
}
|
||||
for (let i=N; i<N*2; i++) {
|
||||
const a = bn128.getInt(pr1+i*32, 1);
|
||||
assert.equal(a,0);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
25
test/groth16.js
Normal file
25
test/groth16.js
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
const assert = require("assert");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const snarkjs = require("snarkjs");
|
||||
|
||||
const buildGroth16 = require("../index.js").buildGroth16;
|
||||
|
||||
describe("Basic tests for groth16 proof generator", () => {
|
||||
it("It should do a basic point doubling G1", async () => {
|
||||
const groth16 = await buildGroth16();
|
||||
|
||||
const signals = fs.readFileSync(path.join(__dirname, "data", "witness.bin"));
|
||||
const provingKey = fs.readFileSync(path.join(__dirname, "data", "proving_key.bin"));
|
||||
const proofS = await groth16.proof(signals.buffer, provingKey.buffer);
|
||||
|
||||
const proof = snarkjs.unstringifyBigInts(proofS);
|
||||
const verifierKey = snarkjs.unstringifyBigInts(JSON.parse(fs.readFileSync(path.join(__dirname, "data", "verification_key.json"), "utf8")));
|
||||
const pub = snarkjs.unstringifyBigInts(JSON.parse(fs.readFileSync(path.join(__dirname, "data", "public.json"), "utf8")));
|
||||
|
||||
assert(snarkjs.groth.isValid(verifierKey, proof, pub));
|
||||
|
||||
groth16.terminate();
|
||||
}).timeout(10000000);
|
||||
});
|
||||
Reference in New Issue
Block a user