text
stringlengths 1
2.05k
|
---|
183147628
);
vk[56] = G1Point(
9090845515311106600436846379065946318058952289413948072431786810720329894277,
1722460393286107233579238876508677840936009861747420874009138738174760258850
);
vk[57] = G1Point(
2213889945202636137630217627265100992986775825571420924028508761827862915086,
1141968364604334451889887522589191128529733915583920463787937023339932630842
);
vk[58] = G1Point(
20456251219609929297591094856316762598797449654417795771607076592173119770834,
8505554830751982244469586969491963764193166354899055209208744116625949769223
);
vk[59] = G1Point(
20764552363708182108437044609302144079216651782533258856357771881993888180849,
5921191061717335495895332279770326686451945894157952000771270982128757623433
);
vk[60] = G1Point(
15200491309187033689701632744609921588687822717049972773860607965049839358498,
963174101797405264376426772342024356994744317274258835260867706713519099190
);
vk[61] = G1Point(
16907409359263295645490014611417427808286586585673567545549370374423687270932,
19965738920935732212165618198627456481706426962709668717428158398175019622633
);
vk[62] = G1Point(
8525733762824884009931940933269975990468556742128693668478069420206268288958,
17680957562382256477839242533381992422893557640970411982748854729672918653339
);
vk[63] = G1Point(
11543924252775971085638087262239886261896444790986575032898538165024986094767,
18295533857221598255085058623508918462384641096084692938748885507727999045589
);
vk[64] = G1Point(
7650073211562746195287504984432880599500134080888190390185023908018545159212,
8926007449865916826503243560207850774231542132502497737708140518754674515195
);
vk[65] = G1Point(
1091003011419753460066156476 |
8248291622559208820070544459540583031788280438050,
2161200733196553722076963828666276486998396660698119820390811182345471844390
);
vk[66] = G1Point(
5619991943329231023586342563071292156848936940252243397784818979776452294394,
17086471386430636827044781369055262728948850562979233008000676670453119931905
);
vk[67] = G1Point(
20029721922638353437616765565750954147967465635811408466336662374931690558864,
12157045576250721417202657406507371059403669196756053696479579894919277155409
);
vk[68] = G1Point(
21135518903999037687700383159674705128503559373593305877591081795645434677800,
14826149418250868867665516729312927912183357232820965249219250101690877051719
);
vk[69] = G1Point(
12716235266612547965522432659702796588728652520165953576885109254764760946605,
1502656029161667595612192784104946048748456206287753606896784110697937038273
);
vk[70] = G1Point(
1049220917141553398666899775374455498324054642642897994229873531044866400312,
20769086632475471411761783937841264352491499614880433868847982280973141672413
);
vk[71] = G1Point(
3614350552551352780806550106639402451345056206997206243944277215031513049373,
5206057445524765927002106886059500476391837098700968541446631114025985784910
);
vk[72] = G1Point(
8055783365824181166702825600356820312061099690523284725576656663176679044116,
1638222926585075350767574412771496589388909043259378375319066944648316994837
);
vk[73] = G1Point(
4057792612019156099527886469601168926861003507392647059213084047169560834646,
6594931307015609958146395288390214967001676005571978717602974701376929545008
);
vk[74] = G1Point(
9950382393274396072720404713158345851200057195628611055661986956843175756293,
7282922667960477094585696 |
814153673802022441822983967842678841595733604007174
);
vk[75] = G1Point(
4586216621225205252031099476870849094841015192052367266638267710491870499101,
4604017147526254722438115051976438786550914352568100746360878795783694704167
);
vk[76] = G1Point(
16799214131908640765084658268392840479345866505817700863114604240164943164257,
15354212741684672046178028260202360583039504621090458373615159126939692802706
);
vk[77] = G1Point(
13138609772016813027991609032179065209970990415242714114775376639090712079350,
15380517677991831032858870866819856499419492092909311846600193251951859042321
);
vk[78] = G1Point(
12681723977387824731895989023500526405308466858542903103055928573501887506887,
11370573709343602505230460979583593651754855121852100216138643349056073782952
);
vk[79] = G1Point(
4200058302555642333283692750835107019807983821991387347796012866346514302605,
14357085568398294016842996105655116738370071081936618062005085850396798772802
);
vk[80] = G1Point(
14137699264377957975776528918800596987631257455548836008761386515677706582563,
2877943046106495432464489171942097254021067591451481943864022445799990140117
);
vk[81] = G1Point(
7480145966790209514588805951103723897773954176759764819231387108562986050867,
12404751866229336820648708250938384384013582100169058004847207735622575043993
);
vk[82] = G1Point(
12970817937144745309785994864230789483629807994776973410040274401977769841052,
5825762304110705990197138035992052772838196210579994473399600004016677423488
);
vk[83] = G1Point(
14988453290426221989095533870965847942554723567967755183069083157996373748616,
8648840510009197738716543586800373429604948580372256694893314143136379863664
);
vk[84] = G1P |
oint(
15145542077656252793870557125161247488411025860428855768932504599780138755577,
2178169336494538462372002990799200948579620951049701898691482757493703042642
);
vk[85] = G1Point(
11135789569088525124827536278606171249701996905898972803869975950803968697579,
3104837136515107621506029430582483708481393523526438371726824112484239803581
);
vk[86] = G1Point(
17066149863746730614554018679517110986144152654411463722880426514835413500149,
11885136744232152755409190843781868227764042786753547405780615738418310637923
);
vk[87] = G1Point(
8848015573236418075663743497372404662739214749989826392286123012148059889680,
1972243942447596180922716596695612444433272877968270983679827686933514965206
);
vk[88] = G1Point(
14865752525354582126252517599134593966808867282046342796694614702712521564336,
18116025354261037812375363008053160294289201308729119292420655212966763428629
);
vk[89] = G1Point(
20008653757824978308869797085409493811497783305061398226809054512392049926827,
2023221323067464241928858421807576285617952900431861729331205253026094553028
);
vk[90] = G1Point(
9427479375857197202287179331720188202666358779957538977247547218804648021559,
13103256830144414831969119769186006894217432078739958285698595391380496268010
);
vk[91] = G1Point(
3022753493095593546667513964979418729795168926723981739728213991436890865173,
15651100209468844976455723098156772118319496522347044594278603917566715835964
);
vk[92] = G1Point(
11863153566289362104600364540490616631416480033499697509671034253106023069686,
6263106079901584198635057982958449515247366171335121668407272647131625665359
);
vk[93] = G1Point(
1632268454934283210866357082989656996613161609804593296738987685077 |
804217796,
8943206385844065158715717104595797380028345892524924749292670404417492092532
);
vk[94] = G1Point(
2514580143298917949844157611116376945011648866679103488807951227637293425228,
1570539622709169759643018064245830768717632289525346051035788970161633534650
);
vk[95] = G1Point(
14995127663646685519705792243009870088473684011447124089592875697835776461681,
10011280883107051614601335912182178771718155707859435453848946557263718614275
);
vk[96] = G1Point(
10837864859142056917623769978173380007817338460373139706119502408994471525015,
5688212466311380080002109574884673535981843432406127069039894776083240717165
);
vk[97] = G1Point(
8184241099722337872655100621160316867350497097484481073516270737365644965184,
8370899682353048184295715026039653844162707028675185918829983378235273090579
);
vk[98] = G1Point(
14336699639296594061188773829570477118517654937378475865402690828519678655979,
18962321517991296846137966567405441567624457545084441947313953041676115679070
);
vk[99] = G1Point(
9460029977525714962867496691244424070473524798482965710433684902814580254803,
13773860226345636507739066933383044706264742752335080543835025360890117637789
);
vk[100] = G1Point(
19391891390609481429849774145579503985374912055370046060106347963180567737648,
7728129834219170358551601609946089408272789276196019291377633982082964264714
);
}
} |
pragma solidity ^0.8.17;
library Pairing { |
struct G1Point {
uint X;
uint Y;
} |
struct G2Point {
uint[2] X;
uint[2] Y;
}
function P1() internal pure returns (G1Point memory) {
return G1Point(1, 2);
}
function P2() internal pure returns (G2Point memory) {
return G2Point(
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
10857046999023057135944570762232829481370756359578518086990519993285655852781],
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
8495653923123431417604973247489272438418190587263600148770280649306958101930]
);
/*
return G2Point(
[10857046999023057135944570762232829481370756359578518086990519993285655852781,
11559732032986387107991004021392285783925812861821192530917403151452391805634],
[8495653923123431417604973247489272438418190587263600148770280649306958101930,
4082367875863433681332203403145435568316851327593401208105741076214120093531]
);
*/
}
function negate(G1Point memory p) internal pure returns (G1Point memory r) {
uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
if (p.X == 0 && p.Y == 0)
return G1Point(0, 0);
return G1Point(p.X, q - (p.Y % q));
}
function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) {
uint[4] memory input;
input[0] = p1.X;
input[1] = p1.Y;
input[2] = p2.X;
input[3] = p2.Y;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60)
switch success case 0 { invalid() }
}
require(success,"pairing-add-failed");
}
function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) {
uint[3] memory input;
input[0] = p.X;
input[1] = p.Y;
i |
nput[2] = s;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60)
switch success case 0 { invalid() }
} |
require (success,"pairing-mul-failed");
}
function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) {
require(p1.length == p2.length,"pairing-lengths-failed");
uint elements = p1.length;
uint inputSize = elements * 6;
uint[] memory input = new uint[](inputSize);
for (uint i = 0; i < elements; i++)
{
input[i * 6 + 0] = p1[i].X;
input[i * 6 + 1] = p1[i].Y;
input[i * 6 + 2] = p2[i].X[0];
input[i * 6 + 3] = p2[i].X[1];
input[i * 6 + 4] = p2[i].Y[0];
input[i * 6 + 5] = p2[i].Y[1];
}
uint[1] memory out;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
switch success case 0 { invalid() }
}
require(success,"pairing-opcode-failed");
return out[0] != 0;
}
function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](2);
G2Point[] memory p2 = new G2Point[](2);
p1[0] = a1;
p1[1] = b1;
p2[0] = a2;
p2[1] = b2;
return pairing(p1, p2);
}
function pairingProd3(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](3);
G2Point[] memory p2 = new G2Point[](3);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
return pairing(p1, p2);
}
function pairingProd4(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2,
G1Point memory d1, G2P |
oint memory d2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](4);
G2Point[] memory p2 = new G2Point[](4);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p1[3] = d1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
p2[3] = d2;
return pairing(p1, p2);
}
}
contract CircuitVerifier {
using Pairing for *; |
struct VerifyingKey {
Pairing.G1Point alfa1;
Pairing.G2Point beta2;
Pairing.G2Point gamma2;
Pairing.G2Point delta2;
Pairing.G1Point[] IC;
} |
struct Proof {
Pairing.G1Point A;
Pairing.G2Point B;
Pairing.G1Point C;
}
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
vk.alfa1 = Pairing.G1Point(
20491192805390485299153009773594534940189261866228447918068658471970481763042,
9383485363053290200918347156157836566562967994039712273449902621266178545958
);
vk.beta2 = Pairing.G2Point(
[4252822878758300859123897981450591353533073413197771768651442665752259397132,
6375614351688725206403948262868962793625744043794305715222011528459656738731],
[21847035105528745403288232691147584728191162732299865338377159692350059136679,
10505242626370262277552901082094356697409835680220590971873171140371331206856]
);
vk.gamma2 = Pairing.G2Point(
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
10857046999023057135944570762232829481370756359578518086990519993285655852781],
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
8495653923123431417604973247489272438418190587263600148770280649306958101930]
);
vk.delta2 = Pairing.G2Point(
[21223509572149436009383842546644857332549415106982951130166482247313305552407,
3651919070680602585131326454611744313033427008208909328852558006475401815457],
[18868338637417310549300806688818271707183649362616203659593245255731372694149,
12639385737564052598655546142232001975674170132048529941115373366821228560987]
);
vk.IC = new Pairing.G1Point[](32);
vk.IC[0] = Pairing.G1Point(
19285368940731314812277377326380883472569493771921601523645257990686666718389,
1372693327773405640347273163919379519555169290873089668969290615157317568035
);
vk.IC[1] = Pairing.G1Point(
20563536359277927673 |
211382785955880671029020113525488954738694785575739831195,
11622487325429485635036204798963062119327899165409993719814798109467539711521
);
vk.IC[2] = Pairing.G1Point(
8875816761637054206953475440341751666786638952753385218037925061132134808360,
10670771010820437935149234172619692113592674470313124621792870855604924174724
);
vk.IC[3] = Pairing.G1Point(
16310041769337439549216324037724832358058235392896021682362617270538374194950,
19324449383987862762752536963119465502812921099286174351059525673474911225532
);
vk.IC[4] = Pairing.G1Point(
5783563821028270213576609919720027429769938502424460091302825153121542109354,
1470669597353590816746365817778990659877634944571654698518112762825899872953
);
vk.IC[5] = Pairing.G1Point(
914937451350736032348203387680959396512999207088712230002930452486647275486,
13278992517431057944065874304895401817561900836300928790090187392043172986310
);
vk.IC[6] = Pairing.G1Point(
18097596007339529406692800143852776601000937260850591264898128684789643908212,
9153937710318033405384133751664845960297229722568264885716854633955663625536
);
vk.IC[7] = Pairing.G1Point(
6193249348722695043944500703095878319595334835001600074904795240215318098962,
7853680886918860231577585366958076401174476205187153074482747182486269572453
);
vk.IC[8] = Pairing.G1Point(
15270422438445823922593250494709654463789987296743911250277775444865091824612,
41021829069415812333047291763917661413146830708911148 |
01586417028043961885657
);
vk.IC[9] = Pairing.G1Point(
1302681630257233329077408872587714433119006291453962064536810133248742069768,
16311982814639644121944335458064770134173125251073766459777997299727849450385
);
vk.IC[10] = Pairing.G1Point(
2547973719675613033586687516381437235939306144752695939764438305531826924266,
18707555548721531003785445481648633019475010768441520378552149383179479150717
);
vk.IC[11] = Pairing.G1Point(
4428496179622071166051755585560830941147382314141057626874137147454947858555,
20313942796395280130218843132871495899105990153866314823037951753327298389440
);
vk.IC[12] = Pairing.G1Point(
21090886813683041019220427702428409885856117842016306648411480480763064485622,
2904286936290379856261715681207382922806286474730092451626811072360471392706
);
vk.IC[13] = Pairing.G1Point(
16953207110836848412348418809752737780820855895491067079125814117205539343156,
1818176631812756401799820926733888831130235415626052739389592258254689714069
);
vk.IC[14] = Pairing.G1Point(
14275611219479745388903161313834529621377028751523300114715470306855103627468,
11395634342520831379817961559538472855919256807836266605671005070285381756811
);
vk.IC[15] = Pairing.G1Point(
9309985145070020350527688966527437090410653959124451822044308620974043195469,
5088783600811643090890999487942869959512873512301353706146318021257882291855
);
vk.IC[16] = Pairing.G1Point |
(
13845291475527603009053139388380092079280298146548334122387682777257704819701,
19983427718412885469630659039041222500384101588665734536304699212006998692472
);
vk.IC[17] = Pairing.G1Point(
13231858176024723276494898879866156922615784394666546818166548789657182161333,
11199542500735857507253092815482494011222825605932956765796942807430804932442
);
vk.IC[18] = Pairing.G1Point(
15155380983211082488641924827916379578962497681571449333022148252969777764037,
4146224505674490292116190239156754729027337668003337499925105327877886686419
);
vk.IC[19] = Pairing.G1Point(
8169038537569741399573329096561615641148655375190982634441976478639056198693,
11963650259756467569328883816276529262045940085129455596849074710225723789976
);
vk.IC[20] = Pairing.G1Point(
5768155153658025058286442157394530016577292835139355355291835326382757604073,
20084350319318171708307617210896946548033127857641151424695801209275721178484
);
vk.IC[21] = Pairing.G1Point(
12703197821467744408272824365427240188730432752459024271595948028914355362762,
10123119830081479418463659963407526854356320634595089810637209343214392456651
);
vk.IC[22] = Pairing.G1Point(
18202154060412128906014642267610638621848197350141370475801967787541536447902,
7172856910555396735982105446089210786857646719326360443646152381651817923139
);
vk.IC[23] = Pairing.G1Point(
17170777702061064354926492758896433985012177585871248144609849735893537428237,
4129984 |
23308992625938675915107301585996363349849877844235609364273086873580
);
vk.IC[24] = Pairing.G1Point(
3545350639001990923140538415427584236524735576470412437640669325012183149004,
20188177885143613106665205433040400257378338431586890569517744733352415194844
);
vk.IC[25] = Pairing.G1Point(
21431297364806809809353204501967336223370018667834067200289961618421550463576,
6224434139824781014957254620472227483320631913991355808517618408479036778646
);
vk.IC[26] = Pairing.G1Point(
20280682591691563582384585973340321812285845652705934582770347952936885495314,
12208275392540746880779008886418423836514168579437691486870715982767835559268
);
vk.IC[27] = Pairing.G1Point(
10015165244416857193592336278992467819961744116081081647783642516266955540912,
20130961332116514755984061027376120975717399625907757330731812572615020936707
);
vk.IC[28] = Pairing.G1Point(
8336371214105638859634154554031929460603615856662424524205559804248139721705,
15877135363481174608851919525400781895600679056573072501733758948126246943151
);
vk.IC[29] = Pairing.G1Point(
14076177816885665971918033670006882703355155560752256957969673368434759377466,
4110228092353627263550493253063698850130425422586685028497387964159506802444
);
vk.IC[30] = Pairing.G1Point(
21779324301473364824100454032265775775351274040721207181034336488931253729090,
501953779072995894027107266033339049457352284371355668744583435564783383240
); |
vk.IC[31] = Pairing.G1Point(
17060488929845011250862811192805763333332019172161699169052857724884424599208,
8629028379209702408634728188414144519665064163607186328886948451932454722462
);
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {
uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
VerifyingKey memory vk = verifyingKey();
require(input.length + 1 == vk.IC.length,"verifier-bad-input");
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
for (uint i = 0; i < input.length; i++) {
require(input[i] < snark_scalar_field,"verifier-gte-snark-scalar-field");
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(vk.IC[i + 1], input[i]));
}
vk_x = Pairing.addition(vk_x, vk.IC[0]);
if (!Pairing.pairingProd4(
Pairing.negate(proof.A), proof.B,
vk.alfa1, vk.beta2,
vk_x, vk.gamma2,
proof.C, vk.delta2
)) return 1;
return 0;
}
function verifyProof(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[] memory input
) public view returns (bool r) {
Proof memory proof;
proof.A = Pairing.G1Point(a[0], a[1]);
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
proof.C = Pairing.G1Point(c[0], c[1]);
if (verify(input, proof) == 0) {
return true;
} else {
return false;
}
}
} |
pragma solidity ^0.8.17; |
import "./Pairing100.sol"; |
import "./Pairing200.sol"; |
import "./Pairing300.sol"; |
import "./Pairing400.sol"; |
import "./Pairing500.sol"; |
import "./Pairing600.sol"; |
import "./Pairing700.sol"; |
import "./Pairing800.sol"; |
import "./Pairing900.sol"; |
import "./Pairing1000.sol";
library Pairing { |
struct G1Point {
uint X;
uint Y;
} |
struct G2Point {
uint[2] X;
uint[2] Y;
}
function P1() internal pure returns (G1Point memory) {
return G1Point(1, 2);
}
function P2() internal pure returns (G2Point memory) {
return G2Point(
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
10857046999023057135944570762232829481370756359578518086990519993285655852781],
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
8495653923123431417604973247489272438418190587263600148770280649306958101930]
);
/*
return G2Point(
[10857046999023057135944570762232829481370756359578518086990519993285655852781,
11559732032986387107991004021392285783925812861821192530917403151452391805634],
[8495653923123431417604973247489272438418190587263600148770280649306958101930,
4082367875863433681332203403145435568316851327593401208105741076214120093531]
);
*/
}
function negate(G1Point memory p) internal pure returns (G1Point memory r) {
uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583;
if (p.X == 0 && p.Y == 0)
return G1Point(0, 0);
return G1Point(p.X, q - (p.Y % q));
}
function addition(G1Point memory p1, G1Point memory p2) internal view returns (G1Point memory r) {
uint[4] memory input;
input[0] = p1.X;
input[1] = p1.Y;
input[2] = p2.X;
input[3] = p2.Y;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60)
switch success case 0 { invalid() }
}
require(success,"pairing-add-failed");
}
function scalar_mul(G1Point memory p, uint s) internal view returns (G1Point memory r) {
uint[3] memory input;
input[0] = p.X;
input[1] = p.Y;
i |
nput[2] = s;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60)
switch success case 0 { invalid() }
} |
require (success,"pairing-mul-failed");
}
function pairing(G1Point[] memory p1, G2Point[] memory p2) internal view returns (bool) {
require(p1.length == p2.length,"pairing-lengths-failed");
uint elements = p1.length;
uint inputSize = elements * 6;
uint[] memory input = new uint[](inputSize);
for (uint i = 0; i < elements; i++)
{
input[i * 6 + 0] = p1[i].X;
input[i * 6 + 1] = p1[i].Y;
input[i * 6 + 2] = p2[i].X[0];
input[i * 6 + 3] = p2[i].X[1];
input[i * 6 + 4] = p2[i].Y[0];
input[i * 6 + 5] = p2[i].Y[1];
}
uint[1] memory out;
bool success;
assembly {
success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
switch success case 0 { invalid() }
}
require(success,"pairing-opcode-failed");
return out[0] != 0;
}
function pairingProd2(G1Point memory a1, G2Point memory a2, G1Point memory b1, G2Point memory b2) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](2);
G2Point[] memory p2 = new G2Point[](2);
p1[0] = a1;
p1[1] = b1;
p2[0] = a2;
p2[1] = b2;
return pairing(p1, p2);
}
function pairingProd3(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](3);
G2Point[] memory p2 = new G2Point[](3);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
return pairing(p1, p2);
}
function pairingProd4(
G1Point memory a1, G2Point memory a2,
G1Point memory b1, G2Point memory b2,
G1Point memory c1, G2Point memory c2,
G1Point memory d1, G2P |
oint memory d2
) internal view returns (bool) {
G1Point[] memory p1 = new G1Point[](4);
G2Point[] memory p2 = new G2Point[](4);
p1[0] = a1;
p1[1] = b1;
p1[2] = c1;
p1[3] = d1;
p2[0] = a2;
p2[1] = b2;
p2[2] = c2;
p2[3] = d2;
return pairing(p1, p2);
}
}
contract EncryptionVerifier {
using Pairing for *;
address private pairing100;
address private pairing200;
address private pairing300;
address private pairing400;
address private pairing500;
address private pairing600;
address private pairing700;
address private pairing800;
address private pairing900;
address private pairing1000; |
struct VerifyingKey {
Pairing.G1Point alfa1;
Pairing.G2Point beta2;
Pairing.G2Point gamma2;
Pairing.G2Point delta2;
Pairing.G1Point[] IC;
} |
struct Proof {
Pairing.G1Point A;
Pairing.G2Point B;
Pairing.G1Point C;
}
constructor(
address add100,
address add200,
address add300,
address add400,
address add500,
address add600,
address add700,
address add800,
address add900,
address add1000
) {
pairing100 = add100;
pairing200 = add200;
pairing300 = add300;
pairing400 = add400;
pairing500 = add500;
pairing600 = add600;
pairing700 = add700;
pairing800 = add800;
pairing900 = add900;
pairing1000 = add1000;
}
function verifyingKey() internal pure returns (VerifyingKey memory vk) {
vk.alfa1 = Pairing.G1Point(
20491192805390485299153009773594534940189261866228447918068658471970481763042,
9383485363053290200918347156157836566562967994039712273449902621266178545958
);
vk.beta2 = Pairing.G2Point(
[4252822878758300859123897981450591353533073413197771768651442665752259397132,
6375614351688725206403948262868962793625744043794305715222011528459656738731],
[21847035105528745403288232691147584728191162732299865338377159692350059136679,
10505242626370262277552901082094356697409835680220590971873171140371331206856]
);
vk.gamma2 = Pairing.G2Point(
[11559732032986387107991004021392285783925812861821192530917403151452391805634,
10857046999023057135944570762232829481370756359578518086990519993285655852781],
[4082367875863433681332203403145435568316851327593401208105741076214120093531,
8495653923123431417604973247489272438418190587263600148770280649306958101930]
);
vk.delta2 = Pairing.G2Point(
[6653259084928985759212025249045143654297075953578382745064964143441455343356,
8160999132074616195727858211272521546858233416802944087099739007663370791684],
[5005417252240 |
310256132733574742221180631042236600616922722331817516271580377,
535375283190531279782443189967329266724259036468032022432462508232591886570]
);
vk.IC = new Pairing.G1Point[](1006);
vk.IC[0] = Pairing.G1Point(
3913146479250333998622309518342179854776388675763500448325061276047871385028,
185902641878167853786013541387769876019822308496137446718496175008088134965
);
}
function verify(uint[] memory input, Proof memory proof) internal view returns (uint) {
uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
VerifyingKey memory vk = verifyingKey();
require(input.length + 1 == 1006,"verifier-bad-input");
Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0);
for (uint i = 0; i < 100; i++) {
require(input[i] < snark_scalar_field,"verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing100(pairing100).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 100] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing200(pairing200).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 100]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 200] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing300(pairing300).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 200]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 300] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing400(pairing400).vk(i + 1);
vk_x = Pairing.addition(vk_x, |
Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 300]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 400] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing500(pairing500).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 400]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 500] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing600(pairing600).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 500]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 600] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing700(pairing700).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 600]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 700] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing800(pairing800).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 700]));
}
for (uint i = 0; i < 100; i++) {
require(input[i + 800] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing900(pairing900).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 800]));
}
for (uint i = 0; i < 105; i++) {
require(input[i + 900] < snark_scalar_field, "verifier-gte-snark-scalar-field");
(uint X, uint Y) = Pairing1000(pairing1000).vk(i + 1);
vk_x = Pairing.addition(vk_x, Pairing.scalar_mul(Pairing.G1Point(X, Y), input[i + 900]));
}
vk_x = Pairing.addition(vk_x, vk.IC[0]);
if (!Pairing.pairingProd4(
Pairing.negat |
e(proof.A), proof.B,
vk.alfa1, vk.beta2,
vk_x, vk.gamma2,
proof.C, vk.delta2
)) return 1;
return 0;
}
function verifyProof(
uint[2] memory a,
uint[2][2] memory b,
uint[2] memory c,
uint[1005] memory input
) public view returns (bool r) {
Proof memory proof;
proof.A = Pairing.G1Point(a[0], a[1]);
proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]);
proof.C = Pairing.G1Point(c[0], c[1]);
uint[] memory inputValues = new uint[](input.length);
for(uint i = 0; i < input.length; i++){
inputValues[i] = input[i];
}
if (verify(inputValues, proof) == 0) {
return true;
} else {
return false;
}
}
} |
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
const pairing100 = await deploy('Pairing100', {
from: deployer,
log: true
});
const pairing200 = await deploy('Pairing200', {
from: deployer,
log: true
});
const pairing300 = await deploy('Pairing300', {
from: deployer,
log: true
});
const pairing400 = await deploy('Pairing400', {
from: deployer,
log: true
});
const pairing500 = await deploy('Pairing500', {
from: deployer,
log: true
});
const pairing600 = await deploy('Pairing600', {
from: deployer,
log: true
});
const pairing700 = await deploy('Pairing700', {
from: deployer,
log: true
});
const pairing800 = await deploy('Pairing800', {
from: deployer,
log: true
});
const pairing900 = await deploy('Pairing900', {
from: deployer,
log: true
});
const pairing1000 = await deploy('Pairing1000', {
from: deployer,
log: true
});
const encryptionVerifier = await deploy('EncryptionVerifier', {
from: deployer,
log: true,
args: [pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address]
});
await deploy('BountyFactory', {
from: deployer,
log: true,
args: [encryptionVerifier.address]
});
await deploy('CircuitVerifier', {
from: deployer,
log: true
});
};
module.exports.tags = ['complete'];
|
require("@nomicfoundation/hardhat-toolbox");
require("@nomiclabs/hardhat-ethers");
require("hardhat-deploy");
require("hardhat-contract-sizer");
require("hardhat-gas-reporter");
require("dotenv").config();
// const { PRIVATE_KEY: GOERLI_PRIVATE_KEY, ALCHEMY_API_KEY } = process.env;
module.exports = {
solidity: {
version: "0.8.17",
optimizer: {
enabled: true,
runs: 1000,
}
},
networks: {
hardhat: {
chainId: 1337,
allowUnlimitedContractSize: true,
gas: 100000000,
blockGasLimit: 0x1fffffffffffff,
},
// goerli: {
// allowUnlimitedContractSize: true,
// gas: 100000000,
// blockGasLimit: 0x1fffffffffffff,
// url: `https://eth-goerli.alchemyapi.io/v2/${ALCHEMY_API_KEY}`,
// accounts: [`${GOERLI_PRIVATE_KEY}`],
// },
},
namedAccounts: {
deployer: 0,
},
paths: {
deploy: "deploy",
deployments: "deployments",
},
mocha: {
timeout: 1000000
}
};
|
const fs = require("fs");
const solidityRegex = /pragma solidity \^\d+\.\d+\.\d+/
const inputRegex = /uint\[\d+\] memory input(?!;)/
const contentRegex = /proof\.C = Pairing\.G1Point\(c\[0\], c\[1\]\);([\s\S]*?)if \(verify\(inputValues, proof\) == 0\)/
const verifierRegex = /contract Verifier/
let content = fs.readFileSync("./contracts/circuitVerifier.sol", { encoding: 'utf-8' });
let bumped = content.replace(solidityRegex, 'pragma solidity ^0.8.17');
bumped = bumped.replace(inputRegex, 'uint[] memory input');
bumped = bumped.replace(contentRegex, 'proof.C = Pairing.G1Point(c[0], c[1]);\n\n if (verify(input, proof) == 0)');
let renamed = bumped.replace(verifierRegex, 'contract CircuitVerifier');
fs.writeFileSync("./contracts/circuitVerifier.sol", renamed);
content = fs.readFileSync("./contracts/encryptionVerifier.sol", { encoding: 'utf-8' });
bumped = content.replace(solidityRegex, 'pragma solidity ^0.8.17');
// bumped = bumped.replace(inputRegex, 'uint[] memory input');
// bumped = bumped.replace(contentRegex, 'proof.C = Pairing.G1Point(c[0], c[1]);\n\n if (verify(input, proof) == 0)');
renamed = bumped.replace(verifierRegex, 'contract EncryptionVerifier');
fs.writeFileSync("./contracts/encryptionVerifier.sol", renamed);
|
require("dotenv").config();
const lighthouse = require('@lighthouse-web3/sdk');
const fs = require('fs');
const apiKey = process.env.API_KEY;
async function main() {
const hashes = [];
for (let i = 0; i < 1000; i++) {
const path = "assets/" + i + ".pgm"; //Give path to the file
const response = await lighthouse.uploadFileRaw(path, apiKey);
console.log(response.data.Hash);
hashes.push(response.data.Hash);
if (i == 0) {
console.assert(response.data.Hash == "bafkreig42jyiawthjkmskza765hn6krgqs7uk7cmtjmggb6mgnjql7dqje");
}
console.assert(response.data.Size == "797");
}
fs.writeFileSync("assets/cid.txt", hashes.join("\r"));
}
main().then(() => process.exit(0)).catch(error => {
console.error(error);
process.exit(1);
}); |
const { expect } = require("chai");
const { ethers } = require("hardhat");
require("dotenv").config();
const lighthouse = require('@lighthouse-web3/sdk');
const { recoverShards } = require("@lighthouse-web3/kavach");
const fs = require("fs");
const privateKey = process.env.PRIVATE_KEY;
const publicKey = process.env.PUBLIC_KEY;
const privateKeyBob = process.env.PRIVATE_KEY_BOB;
const publicKeyBob = process.env.PUBLIC_KEY_BOB;
const apiKey = process.env.API_KEY;
const path = "assets/0.pgm";
const sign_auth_message = async (publicKey, privateKey) => {
const provider = new ethers.providers.JsonRpcProvider();
const signer = new ethers.Wallet(privateKey, provider);
const messageRequested = (await lighthouse.getAuthMessage(publicKey)).data.message;
const signedMessage = await signer.signMessage(messageRequested);
return (signedMessage)
}
describe("Lighthouse SDK test", function () {
it("Should upload file in raw to lighthouse", async function () {
const response = await lighthouse.uploadFileRaw(path, apiKey);
expect(response.data.Name).equal("0.pgm");
expect(response.data.Hash).equal("bafkreig42jyiawthjkmskza765hn6krgqs7uk7cmtjmggb6mgnjql7dqje");
expect(response.data.Size).equal("797")
});
it("Should upload file to lighthouse", async function () {
const response = await lighthouse.upload(path, apiKey);
expect(response.data.Name).equal("0.pgm");
expect(response.data.Hash).equal("QmevD3vQfg6Nf9RYWGCrNdfmT49BhLS714sxGkVifqmYg9");
expect(response.data.Size).equal("808")
});
let cid;
it("Should upload encrypted file to lighthouse", async function () {
const signed_message = await sign_auth_message(publicKey, privateKey);
const response = await lighthouse.uploadEncrypted(
path,
apiKey,
publicKey,
signed_message,
);
expect(response.data.Name).equal("0.pgm");
expect(response.data.Size).equal("852");
cid = |
response.data.Hash;
});
it("Should download decrypted file from lighthouse", async function () {
const signed_message = await sign_auth_message(publicKey, privateKey);
const fileEncryptionKey = await lighthouse.fetchEncryptionKey(
cid,
publicKey,
signed_message
);
const decrypted = await lighthouse.decryptFile(
cid,
fileEncryptionKey.data.key
);
expect(decrypted.byteLength).equal(797);
});
it("Should share private file with another user", async function () {
const signed_message = await sign_auth_message(publicKey, privateKey);
const response = await lighthouse.shareFile(
publicKey,
[publicKeyBob],
cid,
signed_message,
);
expect(response.data.cid).equal(cid);
expect(response.data.status).equal("Success");
expect(response.data.shareTo[0]).equal(publicKeyBob);
});
it("Should download decrypted file from lighthouse shared with another user", async function () {
const signed_message = await sign_auth_message(publicKeyBob, privateKeyBob);
const fileEncryptionKey = await lighthouse.fetchEncryptionKey(
cid,
publicKeyBob,
signed_message
);
const decrypted = await lighthouse.decryptFile(
cid,
fileEncryptionKey.data.key
);
expect(decrypted.byteLength).equal(797);
});
it("Should revoke access to private file from another user", async function () {
const signed_message = await sign_auth_message(publicKey, privateKey);
const response = await lighthouse.revokeFileAccess(
publicKey,
[publicKeyBob],
cid,
signed_message,
);
expect(response.data.cid).equal(cid);
expect(response.data.status).equal("Success");
expect(response.data.revokeTo[0]).equal(publicKeyBob);
});
it("Should not |
be able to download decrypted file from lighthouse shared with another user", async function () {
const signed_message = await sign_auth_message(publicKeyBob, privateKeyBob);
const { error, shards } = await recoverShards(
publicKeyBob,
cid,
signed_message,
5
);
expect(shards.length).equal(0);
expect(error?.message).equal("you don't have access");
});
}); |
const { expect, assert } = require("chai");
const { ethers } = require("hardhat");
const { groth16 } = require("snarkjs");
const fs = require("fs");
const crypto = require("crypto");
const base32 = require("base32.js");
const F1Field = require("ffjavascript").F1Field;
const Scalar = require("ffjavascript").Scalar;
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
const Fr = new F1Field(exports.p);
const { Keypair } = require("circomlib-ml/test/modules/maci-domainobjs");
const { decrypt } = require("circomlib-ml/test/modules/maci-crypto");
const json = require("./circuit.json");
const labels = require("../assets/labels.json");
const cids = fs.readFileSync("assets/cid.txt").toString().split("\r");
const idx = 567;
let modelHash;
describe("circuit.circom test", function () {
let INPUT = {};
for (const [key, value] of Object.entries(json)) {
if (Array.isArray(value)) {
let tmpArray = [];
for (let i = 0; i < value.flat().length; i++) {
tmpArray.push(Fr.e(value.flat()[i]));
}
INPUT[key] = tmpArray;
} else {
INPUT[key] = Fr.e(value);
}
}
let Verifier;
let verifier;
let a, b, c, Input;
const digests = [];
before(async function () {
Verifier = await ethers.getContractFactory("CircuitVerifier");
verifier = await Verifier.deploy();
await verifier.deployed();
const input = [];
for (let i = 0; i < 10; i++) {
const bytes = fs.readFileSync("assets/" + (idx + i) + ".pgm");
const binary = [...bytes].map((b) => b.toString(2).padStart(8, "0").split("")).flat();
input.push(binary);
const hash = crypto.createHash('sha256');
hash.update(bytes);
digests.push(hash.digest('hex'));
}
INPUT["in"] = input.flat();
const { proof, publicSignals } = await groth16.fullProve(INPUT, "circuits/bu |
ild/circuit_js/circuit.wasm", "circuits/build/circuit_final.zkey");
const calldata = await groth16.exportSolidityCallData(proof, publicSignals);
const argv = calldata.replace(/["[\]\s]/g, "").split(',').map(x => BigInt(x).toString());
a = [argv[0], argv[1]];
b = [[argv[2], argv[3]], [argv[4], argv[5]]];
c = [argv[6], argv[7]];
Input = argv.slice(8);
console.log(labels.slice(idx, idx + 10));
console.log(Input.slice(0,10));
const calldataJson = {
"a": a,
"b": b,
"c": c,
"input": Input,
}
fs.writeFileSync("./test/circuitCalldata.json", JSON.stringify(calldataJson));
modelHash = Input[30];
});
it("Check circuit output", async () => {
for (let i = 0; i < 10; i++) {
const digest1 = Fr.e(digests[i].slice(0,32), 16);
const digest2 = Fr.e(digests[i].slice(32,64), 16);
assert(Fr.eq(Fr.e(Input[i*2+10]),Fr.e(digest1)));
assert(Fr.eq(Fr.e(Input[i*2+11]),Fr.e(digest2)));
}
});
it("Verifier should return true for correct proofs", async function () {
expect(await verifier.verifyProof(a, b, c, Input)).to.be.true;
});
it("Verifier should return false for invalid proof", async function () {
let a = [0, 0];
let b = [[0, 0], [0, 0]];
let c = [0, 0];
expect(await verifier.verifyProof(a, b, c, Input)).to.be.false;
});
it("CIDv1 should match that from IPFS", async function () {
const cid_version = 1;
const cid_codec = 85;
const hash_function_code = 18;
const length = 32;
for (let i = 0; i < 10; i++) {
const cidraw = cid_version.toString(16).padStart(2, "0") + cid_codec.toString(16).padStart(2, "0") + hash_function_code.toString(16).padStart(2, "0") + length.toString(16).padStart(2, "0") + digests[i];
const buf = Buffer.from(cidraw, 'hex');
const encoder = new base |
32.Encoder();
const cid = encoder.write(buf).finalize().toLowerCase();
expect("b" + cid).equal(cids[idx + i]);
}
});
});
describe("encryption.circom test", function () {
let input = [];
for (const [key, value] of Object.entries(json)) {
if (Array.isArray(value)) {
let tmpArray = [];
for (let i = 0; i < value.flat().length; i++) {
tmpArray.push(Fr.e(value.flat()[i]));
}
input = [...input, ...tmpArray];
} else {
input.push(value);
}
}
for (let i = input.length; i< 1000; i++) {
input.push(0);
}
let Verifier;
let verifier;
let a, b, c, Input;
let keypair;
let keypair2;
let ecdhSharedKey;
let INPUT = {};
before(async function () {
keypair = new Keypair();
keypair2 = new Keypair();
ecdhSharedKey = Keypair.genEcdhSharedKey(
keypair.privKey,
keypair2.pubKey,
);
INPUT = {
'private_key': keypair.privKey.asCircuitInputs(),
'public_key': keypair2.pubKey.asCircuitInputs(),
'in': input,
}
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairing200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing400.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await p |
airing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy();
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairing1000 = await Pairing1000.deploy();
await pairing1000.deployed();
Verifier = await ethers.getContractFactory("EncryptionVerifier");
verifier = await Verifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await verifier.deployed();
const { proof, publicSignals } = await groth16.fullProve(INPUT, "circuits/build/encryption_js/encryption.wasm", "circuits/build/encryption_final.zkey");
const calldata = await groth16.exportSolidityCallData(proof, publicSignals);
const argv = calldata.replace(/["[\]\s]/g, "").split(',').map(x => BigInt(x).toString());
a = [argv[0], argv[1]];
b = [[argv[2], argv[3]], [argv[4], argv[5]]];
c = [argv[6], argv[7]];
Input = argv.slice(8);
const calldataJson = {
"a": a,
"b": b,
"c": c,
"input": Input,
}
const keysJson = [
{
"private_key": keypair.privKey.asCircuitInputs(),
"public_key": keypair.pubKey.asCircuitInputs(),
}, |
{
"private_key": keypair2.privKey.asCircuitInputs(),
"public_key": keypair2.pubKey.asCircuitInputs(),
},
]
fs.writeFileSync("./test/keys.json", JSON.stringify(keysJson));
fs.writeFileSync("./test/encryptionCalldata.json", JSON.stringify(calldataJson));
});
it("Check circuit output", async () => {
assert(Fr.eq(Fr.e(Input[0]),Fr.e(modelHash)));
assert(Fr.eq(Fr.e(Input[1]),Fr.e(ecdhSharedKey)));
assert(Fr.eq(Fr.e(Input[1003]),Fr.e(INPUT['public_key'][0])));
assert(Fr.eq(Fr.e(Input[1004]),Fr.e(INPUT['public_key'][1])));
});
it("Verifier should return true for correct proofs", async function () {
expect(await verifier.verifyProof(a, b, c, Input)).to.be.true;
});
it("Verifier should return false for invalid proof", async function () {
let a = [0, 0];
let b = [[0, 0], [0, 0]];
let c = [0, 0];
expect(await verifier.verifyProof(a, b, c, Input)).to.be.false;
});
it("Decryption should match", async function () {
const ciphertext = {
iv: Input[2],
data: Input.slice(3,1004),
}
const decrypted = decrypt(ciphertext, ecdhSharedKey);
for (let i = 0; i < 1000; i++) {
assert(Fr.eq(Fr.e(decrypted[i]), Fr.e(input[i])));
}
});
}); |
const { expect } = require("chai");
const { ethers } = require("hardhat");
const fs = require("fs");
const base32 = require("base32.js");
const labels = require("../assets/labels.json");
const circuitCalldata = require("./circuitCalldata.json");
const encryptionCalldata = require("./encryptionCalldata.json");
const cids = fs.readFileSync("assets/cid.txt").toString().split("\r");
const idx = 567;
const _labels = [];
describe("Bounty contract test", function () {
const cidraw = [];
let encryptionVerifierAddress;
describe("Tx 1: Creating a bounty", function () {
before(async function () {
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairing200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing400.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await pairing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy(); |
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairing1000 = await Pairing1000.deploy();
await pairing1000.deployed();
const EncyrptionVerifier = await ethers.getContractFactory("EncryptionVerifier");
const encryptionVerifier = await EncyrptionVerifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await encryptionVerifier.deployed();
encryptionVerifierAddress = encryptionVerifier.address;
for (let i = 0; i < 10; i++) {
const decoder = new base32.Decoder();
cidraw.push(decoder.write(cids[idx + i].slice(1)).finalize());
_labels.push(labels[idx + i]);
}
fs.writeFileSync("./test/cidraw.json", JSON.stringify(cidraw.map((cid) => '0x' + cid.toString('hex'))));
fs.writeFileSync("./test/labels.json", JSON.stringify(_labels));
});
it("Should reject deploying the contract is msg.value is 0", async function () {
const owner = await ethers.provider.getSigner(0).getAddress();
const Bounty = await ethers.getContractFactory("Bounty");
const bounty = await Bounty.deploy();
await bounty.deployed();
await expect(bounty.initialize(
owner,
"Bounty 1",
"This is the first bounty",
cidraw,
_labels,
70,
encryptionVerifierAddress,
)).to.be.revertedWith("Bounty reward must be greater than 0");
});
it("Should de |
ploy the contract and set owner to first input", async function () {
const owner = await ethers.provider.getSigner(0).getAddress();
const Bounty = await ethers.getContractFactory("Bounty");
const bounty = await Bounty.deploy();
await bounty.deployed();
const tx = await bounty.initialize(
owner,
"Bounty 1",
"This is the first bounty",
cidraw,
_labels,
70,
encryptionVerifierAddress,
{ value: ethers.utils.parseEther("1") }
);
await tx.wait();
expect(await bounty.name()).to.equal("Bounty 1");
expect(await bounty.description()).to.equal("This is the first bounty");
for (let i = 0; i < 10; i++) {
expect(await bounty.dataCIDs(i)).to.equal('0x' + cidraw[i].toString('hex'));
expect(await bounty.labels(i)).to.equal(_labels[i]);
}
expect(await bounty.owner()).to.equal(owner);
expect(await bounty.completedStep()).to.equal(1);
});
});
let a, b, c, Input;
describe("Tx 2: Submitting a bounty", function () {
let bounty, verifier, encryptionVerifierAddress;
const digests = [];
before(async function () {
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairing200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing4 |
00.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await pairing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy();
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairing1000 = await Pairing1000.deploy();
await pairing1000.deployed();
const EncyrptionVerifier = await ethers.getContractFactory("EncryptionVerifier");
const encryptionVerifier = await EncyrptionVerifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await encryptionVerifier.deployed();
encryptionVerifierAddress = encryptionVerifier.address;
const owner = await ethers.provider.getSigner(0).getAddress();
const Bounty = await ethers.getContractFactory("Bounty");
bounty = await Bounty.deploy();
await bounty.deployed();
const tx = await bounty.initialize(
owner,
"Bounty 1",
"This is the first bounty",
cidraw,
_ |
labels,
70,
encryptionVerifierAddress,
{ value: ethers.utils.parseEther("1") }
);
await tx.wait();
const Verifier = await ethers.getContractFactory("CircuitVerifier");
verifier = await Verifier.deploy();
await verifier.deployed();
a = circuitCalldata.a;
b = circuitCalldata.b;
c = circuitCalldata.c;
Input = circuitCalldata.input;
});
it("Should reject a bounty with wrong proof", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
[0, 0],
[[0, 0], [0, 0]],
[0, 0],
Input
)).to.be.revertedWith("Invalid proof");
});
it("Should reject a bounty with wrong verifier", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
ethers.constants.AddressZero,
a,
b,
c,
Input
)).to.be.revertedWith("Invalid verifier address");
});
it("Should reject a bounty with wrong input", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
[..._labels, ...Array(21).keys()]
)).to.be.revertedWith("Data CID mismatch");
});
it("Should reject a bounty with low accuracy", async function () {
await e |
xpect(bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
[...Array(31).keys()]
)).to.be.revertedWith("Accuracy threshold not met");
});
it("Should submit a bounty", async function () {
const submitter = await ethers.provider.getSigner(1).getAddress();
const tx = await bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
Input
);
await tx.wait();
expect(tx).to.emit(bounty, "BountySubmitted");
expect(await bounty.bountyHunter()).to.equal(submitter);
expect(await bounty.completedStep()).to.equal(2);
});
it("Should reject submitting a submitted bounty", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
Input
)).to.be.revertedWith("Bounty already submitted");
});
});
let publicKeys;
describe("Tx 3: Releasing a bounty", function () {
let bounty, verifier, encryptionVerifierAddress;
before(async function () {
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairi |
ng200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing400.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await pairing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy();
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairing1000 = await Pairing1000.deploy();
await pairing1000.deployed();
const EncyrptionVerifier = await ethers.getContractFactory("EncryptionVerifier");
const encryptionVerifier = await EncyrptionVerifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await encryptionVerifier.deployed();
encryptionVerifierAddress = encryptionVerifier.address;
const owner = await ethers.provi |
der.getSigner(0).getAddress();
const Bounty = await ethers.getContractFactory("Bounty");
bounty = await Bounty.deploy();
await bounty.deployed();
const tx = await bounty.initialize(
owner,
"Bounty 1",
"This is the first bounty",
cidraw,
_labels,
70,
encryptionVerifierAddress,
{ value: ethers.utils.parseEther("1") }
);
await tx.wait();
const Verifier = await ethers.getContractFactory("CircuitVerifier");
verifier = await Verifier.deploy();
await verifier.deployed();
publicKeys = [encryptionCalldata.input[1003], encryptionCalldata.input[1004]];
});
it("Should reject releasing an unsubmitted bounty", async function () {
await expect(bounty.releaseBounty(publicKeys)).to.be.revertedWith("Bounty hunter has not submitted proof");
});
it("Should reject releasing a bounty by non-owner", async function () {
const tx = await bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
Input
);
await tx.wait();
await expect(bounty.connect(ethers.provider.getSigner(1)).releaseBounty(publicKeys)).to.be.revertedWith("Ownable: caller is not the owner");
});
it("Should release a bounty", async function () {
const tx = await bounty.releaseBounty(publicKeys);
await tx.wait();
expect(tx).to.emit(bounty, "BountyReleased");
expect(await bounty.isComplete()).to.equal(true);
expect(await bounty.completedStep()).to.equal(3);
});
it("Should reject releasing a bounty twice", asyn |
c function () {
await expect(bounty.releaseBounty(publicKeys)).to.be.revertedWith("Bounty is already complete");
});
});
describe("Tx 4: Claiming a bounty", function () {
let bounty, verifier, encryptionVerifierAddress;
before(async function () {
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairing200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing400.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await pairing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy();
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairin |
g1000 = await Pairing1000.deploy();
await pairing1000.deployed();
const EncyrptionVerifier = await ethers.getContractFactory("EncryptionVerifier");
const encryptionVerifier = await EncyrptionVerifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await encryptionVerifier.deployed();
encryptionVerifierAddress = encryptionVerifier.address;
const owner = await ethers.provider.getSigner(0).getAddress();
const Bounty = await ethers.getContractFactory("Bounty");
bounty = await Bounty.deploy();
await bounty.deployed();
let tx = await bounty.initialize(
owner,
"Bounty 1",
"This is the first bounty",
cidraw,
_labels,
70,
encryptionVerifierAddress,
{ value: ethers.utils.parseEther("1") }
);
await tx.wait();
const Verifier = await ethers.getContractFactory("CircuitVerifier");
verifier = await Verifier.deploy();
await verifier.deployed();
tx = await bounty.connect(ethers.provider.getSigner(1)).
submitBounty(
0x0,
0x0,
0x0,
verifier.address,
a,
b,
c,
Input
);
await tx.wait();
a = encryptionCalldata.a;
b = encryptionCalldata.b;
c = encryptionCalldata.c;
Input = encryptionCalldata.input;
});
it("Should reject claiming a bounty by non bounty hunter", async function () {
await expect(bounty.connect(ethers.provider.getSigner(2))
.claimBounty(a, b, |
c, Input)).to.be.revertedWith("Only bounty hunter can claim bounty");
});
it("Should reject claiming an uncompleted bounty", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1))
.claimBounty(a, b, c, Input))
.to.be.revertedWith("Bounty is not complete");
});
it("Should claim a bounty", async function () {
const bountyHunter = await ethers.provider.getSigner(1).getAddress();
const balance = await ethers.provider.getBalance(bountyHunter);
let tx = await bounty.releaseBounty(publicKeys);
await tx.wait();
tx = await bounty.connect(ethers.provider.getSigner(1))
.claimBounty(a, b, c, Input);
await tx.wait();
expect(tx).to.emit(bounty, "BountyClaimed");
expect(await ethers.provider.getBalance(bountyHunter)).to.greaterThan(balance);
expect(await bounty.completedStep()).to.equal(4);
});
it("Should reject claiming a claimed bounty", async function () {
await expect(bounty.connect(ethers.provider.getSigner(1))
.claimBounty(a, b, c, Input))
.to.be.revertedWith("Bounty already claimed");
});
});
}); |
const { expect } = require("chai");
const { ethers } = require("hardhat");
const fs = require("fs");
const base32 = require("base32.js");
const labels = require("../assets/labels.json");
const cids = fs.readFileSync("assets/cid.txt").toString().split("\r");
const idx = 567;
const _labels = [];
describe("BountyFactory test", function () {
let factory;
let Bounty;
const cidraw = [];
before(async function () {
const Pairing100 = await ethers.getContractFactory("Pairing100");
const pairing100 = await Pairing100.deploy();
await pairing100.deployed();
const Pairing200 = await ethers.getContractFactory("Pairing200");
const pairing200 = await Pairing200.deploy();
await pairing200.deployed();
const Pairing300 = await ethers.getContractFactory("Pairing300");
const pairing300 = await Pairing300.deploy();
await pairing300.deployed();
const Pairing400 = await ethers.getContractFactory("Pairing400");
const pairing400 = await Pairing400.deploy();
await pairing400.deployed();
const Pairing500 = await ethers.getContractFactory("Pairing500");
const pairing500 = await Pairing500.deploy();
await pairing500.deployed();
const Pairing600 = await ethers.getContractFactory("Pairing600");
const pairing600 = await Pairing600.deploy();
await pairing600.deployed();
const Pairing700 = await ethers.getContractFactory("Pairing700");
const pairing700 = await Pairing700.deploy();
await pairing700.deployed();
const Pairing800 = await ethers.getContractFactory("Pairing800");
const pairing800 = await Pairing800.deploy();
await pairing800.deployed();
const Pairing900 = await ethers.getContractFactory("Pairing900");
const pairing900 = await Pairing900.deploy();
await pairing900.deployed();
const Pairing1000 = await ethers.getContractFactory("Pairing1000");
const pairing1000 = await |
Pairing1000.deploy();
await pairing1000.deployed();
const EncyrptionVerifier = await ethers.getContractFactory("EncryptionVerifier");
const encryptionVerifier = await EncyrptionVerifier.deploy(pairing100.address, pairing200.address, pairing300.address, pairing400.address, pairing500.address, pairing600.address, pairing700.address, pairing800.address, pairing900.address, pairing1000.address);
await encryptionVerifier.deployed();
const BountyFactory = await ethers.getContractFactory("BountyFactory");
factory = await BountyFactory.deploy(encryptionVerifier.address);
await factory.deployed();
Bounty = await ethers.getContractFactory("Bounty");
for (let i = 0; i < 10; i++) {
const decoder = new base32.Decoder();
cidraw.push(decoder.write(cids[idx + i].slice(1)).finalize());
_labels.push(labels[idx + i]);
}
});
it("Should create a new bounty", async function () {
const tx = await factory.createBounty(
"Bounty 1",
"This is the first bounty",
cidraw,
_labels,
70,
{ value: ethers.utils.parseEther("1") }
);
await tx.wait();
const bounty = await Bounty.attach(await factory.bounties(0));
expect(await bounty.owner()).to.equal(await ethers.provider.getSigner(0).getAddress());
expect(await ethers.provider.getBalance(bounty.address)).to.equal(ethers.utils.parseEther("1"));
expect(await bounty.name()).to.equal("Bounty 1");
expect(await bounty.description()).to.equal("This is the first bounty");
expect(await factory.bountyCount()).to.equal(1);
});
it("Should calculate future bounty address", async function () {
const nonce = await ethers.provider.getTransactionCount(factory.address);
const futureAddress = ethers.utils.getContractAddress({ from: factory.address, nonce: nonce });
await expect(factory.createBou |
nty(
"Bounty 2",
"This is the second bounty",
cidraw,
_labels,
70,
{ value: ethers.utils.parseEther("1") }
)).to.emit(factory, "BountyCreated").withArgs(futureAddress);
expect(await factory.bountyCount()).to.equal(2);
});
}); |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from tensorflow import keras\n",
"from PIL import Image\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1000):\n",
" image = Image.fromarray(x_train[i], mode='L')\n",
" image.save('./hardhat/assets/'+str(i)+'.pgm')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"with open('./hardhat/assets/labels.json', 'w') as f:\n",
" json.dump(y_train[:1000].tolist(), f)\n",
" f.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "keras2circom",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|