diff --git a/circuits/deposit.circom b/circuits/deposit.circom index bb0fea2..a3b6913 100644 --- a/circuits/deposit.circom +++ b/circuits/deposit.circom @@ -34,7 +34,7 @@ template Deposit(nLevels) { signal input coinCode; signal input amount; signal private input secret; - signal input nullifier; + signal private input nullifier; signal private input siblingsOld[nLevels]; signal private input siblingsNew[nLevels]; signal input rootOld; diff --git a/compile-circuits.sh b/compile-circuits.sh index 99b3b01..a9bc1ca 100755 --- a/compile-circuits.sh +++ b/compile-circuits.sh @@ -5,32 +5,43 @@ rm -r build mkdir build cd build -echo $(date +"%T") "circom ../circuits/withdraw.circom --r1cs --wasm --sym" -itime="$(date -u +%s)" -../node_modules/.bin/circom ../circuits/withdraw.circom --r1cs --wasm --sym -ftime="$(date -u +%s)" -echo " ($(($(date -u +%s)-$itime))s)" - -echo $(date +"%T") "snarkjs info -r withdraw.r1cs" -../node_modules/.bin/snarkjs info -r withdraw.r1cs - -echo $(date +"%T") "snarkjs setup" -itime="$(date -u +%s)" -../node_modules/.bin/snarkjs setup -r withdraw.r1cs -echo " ($(($(date -u +%s)-$itime))s)" -echo $(date +"%T") "trusted setup generated" - -# sed -i 's/null/["0","0","0"]/g' proving_key.json - - -echo $(date +"%T") "snarkjs generateverifier" -itime="$(date -u +%s)" -../node_modules/.bin/snarkjs generateverifier -echo " ($(($(date -u +%s)-$itime))s)" -echo $(date +"%T") "generateverifier generated" - -sed -i "s/solidity ^0.5.0/solidity ^0.6.0/g" verifier.sol -sed -i "s/gas/gas()/g" verifier.sol -sed -i "s/return the sum/return r the sum/g" verifier.sol -sed -i "s/return the product/return r the product/g" verifier.sol -cp verifier.sol ../contracts/verifier.sol +compile_and_ts() { + echo $(date +"%T") "circom ../circuits/$CIRCUIT.circom --r1cs --wasm --sym" + itime="$(date -u +%s)" + ../node_modules/.bin/circom ../circuits/$CIRCUIT.circom --r1cs --wasm --sym + ftime="$(date -u +%s)" + echo " ($(($(date -u +%s)-$itime))s)" + + echo $(date +"%T") "snarkjs info -r $CIRCUIT.r1cs" + ../node_modules/.bin/snarkjs info -r $CIRCUIT.r1cs + + echo $(date +"%T") "snarkjs setup" + itime="$(date -u +%s)" + ../node_modules/.bin/snarkjs setup -r $CIRCUIT.r1cs --pk $CIRCUIT-proving_key.json --vk $CIRCUIT-verification_key.json + echo " ($(($(date -u +%s)-$itime))s)" + echo $(date +"%T") "trusted setup generated" + + # sed -i 's/null/["0","0","0"]/g' proving_key.json + + + echo $(date +"%T") "snarkjs generateverifier" + itime="$(date -u +%s)" + ../node_modules/.bin/snarkjs generateverifier --vk $CIRCUIT-verification_key.json -v $CIRCUIT-verifier.sol + echo " ($(($(date -u +%s)-$itime))s)" + echo $(date +"%T") "generateverifier generated" + + sed -i "s/solidity ^0.5.0/solidity ^0.6.0/g" ${CIRCUIT}-verifier.sol + sed -i "s/gas/gas()/g" ${CIRCUIT}-verifier.sol + sed -i "s/return the sum/return r the sum/g" ${CIRCUIT}-verifier.sol + sed -i "s/return the product/return r the product/g" ${CIRCUIT}-verifier.sol + sed -i "s/contract Verifier/contract ${CONTRACT}Verifier/g" ${CIRCUIT}-verifier.sol + sed -i "s/Pairing/${CONTRACT}Pairing/g" ${CIRCUIT}-verifier.sol + cp ${CIRCUIT}-verifier.sol ../contracts/ +} + +CIRCUIT="deposit" +CONTRACT="Deposit" +compile_and_ts +CIRCUIT="withdraw" +CONTRACT="Withdraw" +compile_and_ts diff --git a/contracts/Miksi.sol b/contracts/Miksi.sol index c661534..ff6727e 100644 --- a/contracts/Miksi.sol +++ b/contracts/Miksi.sol @@ -1,23 +1,39 @@ pragma solidity ^0.6.0; -import './verifier.sol'; +import './deposit-verifier.sol'; +import './withdraw-verifier.sol'; contract Miksi { - Verifier verifier; + DepositVerifier dVerifier; + WithdrawVerifier wVerifier; - constructor( address _verifierContractAddr) public { - verifier = Verifier(_verifierContractAddr); - } uint256 amount = uint256(1000000000000000000); - uint256 root; + uint256 root ; uint256[] commitments; mapping(uint256 => bool) nullifiers; + constructor( address _depositVerifierContractAddr, address _withdrawVerifierContractAddr) public { + dVerifier = DepositVerifier(_depositVerifierContractAddr); + wVerifier = WithdrawVerifier(_withdrawVerifierContractAddr); + root = uint256(11499909227292257605992378629333104385616480982267969744564817844870636870870); + } + function deposit( uint256 _commitment, - uint256 _root + uint256 _root, + uint[2] memory a, + uint[2][2] memory b, + uint[2] memory c ) public payable { - // TODO check root state transition update with zkp + // check root state transition update with zkp + uint256[5] memory input = [ + 0, + msg.value, + root, // rootOld + _root, // rootNew + _commitment + ]; + require(dVerifier.verifyProof(a, b, c, input), "zkProof deposit could not be verified"); require(msg.value==amount, "value should be 1 ETH"); // this can be flexible with a wrapper with preset fixed amounts commitments.push(_commitment); @@ -43,7 +59,7 @@ contract Miksi { root, uint256(_address) ]; - require(verifier.verifyProof(a, b, c, input), "zkProof withdraw could not be verified"); + require(wVerifier.verifyProof(a, b, c, input), "zkProof withdraw could not be verified"); // zk verification passed require(useNullifier(nullifier), "nullifier already used"); // nullifier check passed diff --git a/contracts/verifier.sol b/contracts/deposit-verifier.sol similarity index 71% rename from contracts/verifier.sol rename to contracts/deposit-verifier.sol index eedc211..7bacdb7 100644 --- a/contracts/verifier.sol +++ b/contracts/deposit-verifier.sol @@ -10,7 +10,7 @@ // added requiere error messages // pragma solidity ^0.6.0; -library Pairing { +library DepositPairing { struct G1Point { uint X; uint Y; @@ -159,32 +159,32 @@ library Pairing { return pairing(p1, p2); } } -contract Verifier { - using Pairing for *; +contract DepositVerifier { + using DepositPairing for *; struct VerifyingKey { - Pairing.G1Point alfa1; - Pairing.G2Point beta2; - Pairing.G2Point gamma2; - Pairing.G2Point delta2; - Pairing.G1Point[] IC; + DepositPairing.G1Point alfa1; + DepositPairing.G2Point beta2; + DepositPairing.G2Point gamma2; + DepositPairing.G2Point delta2; + DepositPairing.G1Point[] IC; } struct Proof { - Pairing.G1Point A; - Pairing.G2Point B; - Pairing.G1Point C; + DepositPairing.G1Point A; + DepositPairing.G2Point B; + DepositPairing.G1Point C; } function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.alfa1 = Pairing.G1Point(5573537740265625168090285795198286579893954229360118260810063400917126796332,10789719125793022298590118880800334307038768742566971868445296062248489927279); - vk.beta2 = Pairing.G2Point([6072967376916873741947961849223716153342228155851589974405468410733665523375,12287252052014343808789818956357497671178626709800000377423333232855717394550], [19887659544225763042094432491416573265605432229063422403349406354087673898183,6817773539509067572550546188063114549568433731855389350832916458849047861996]); - vk.gamma2 = Pairing.G2Point([21777617475348225837446670587801717739887263496343742680443739578840171280954,21819935038190839400227353700653328252014694909407919035917157743271161479537], [5874472499575263305242127811274992746938244885646767207659488542844992545745,7393451529219086695827003727698071721027731112496364438559915896033740034847]); - vk.delta2 = Pairing.G2Point([21378569762973853737012405126625349874813324397465926818859899411148132219071,14217351759616267906987060014706527451694626178118484166622168408217896429543], [2959651248849223420449727632536103426152162159064239731749254690426832558857,11771456040212156734923327364079425983074170498604769948335767339414964838231]); - vk.IC = new Pairing.G1Point[](6); - vk.IC[0] = Pairing.G1Point(9635854757134834421832904337276664164370386962134477374755137344348461690576,19010444634264471397134767941238249705060700374213396849476569996691707235781); - vk.IC[1] = Pairing.G1Point(11035507535270489026720166819336384993970390089893993872476567909015281065120,8704656742215780532233763414730518226534751719578362784054696564772602495731); - vk.IC[2] = Pairing.G1Point(8237061378219722919406315531805429710952281972371670670227024276177812450561,21788473647720763764729284824775460737201605786977595028535407989906364038374); - vk.IC[3] = Pairing.G1Point(12032524012671168792329029167990930904368673059152483847563381918262080217708,18306948356499743813922422173776199871369081286779005885299858224571300447429); - vk.IC[4] = Pairing.G1Point(10502650737346333699651566811075824924610501915730189047190185075707722044703,7451314863250353539987518216950772741664034028935285984537382148127632592866); - vk.IC[5] = Pairing.G1Point(2254627509888069544735850912948033354698601777682208496156519712223878774652,10613197317835768162576244259798938001105060978002557905703689968987855363120); + vk.alfa1 = DepositPairing.G1Point(6065483927189198444682905308325000951246257469152828409806284077885133249739,1990028136278429445518006524392551345128366216259976542235494496185998414912); + vk.beta2 = DepositPairing.G2Point([9264834971516100639871261194399140486562775525051734782501439416023479225391,5162939940530468633865380033187237384667734222189925309044161499791615506375], [489842918389814266437556836747821551396498728159626876991041866052737351566,17404338846724633734949676544603763660215742965552403738436754194013115104404]); + vk.gamma2 = DepositPairing.G2Point([3816215622778176681699259539967429072196356640030859639514936270238959698219,236252540670993251838452790992568688843063704489202689719563908537636316738], [16303927206925158108103606822067759987757608633916911445237700431553497043690,12668251450800271509987871102239080189070836108197224795008764353502619814964]); + vk.delta2 = DepositPairing.G2Point([7734429969653199634693555623385094248569235945832114705562051837787377060806,7566151879414510490108686246718202304489426052757532512550882626288777754959], [17950916274576625246093655071576904884259522730229572495774693174259627926717,15060426040589607858464359473475255324856592450467055295537458068465028256933]); + vk.IC = new DepositPairing.G1Point[](6); + vk.IC[0] = DepositPairing.G1Point(15740910832481450063917199200607757253896563901763566776712306503750389541655,7002622853975178066023339356151543160177210573049820374279694406406811606683); + vk.IC[1] = DepositPairing.G1Point(15651632245328303900076885367222245590896431851640402182626448905226427580557,17750666936414774331918745613015398412439836969964160634716436648984069791919); + vk.IC[2] = DepositPairing.G1Point(20430695676495506508861377450422139686450342947107294821941229966423680102028,14134642346581319191800114388829157260027077654897972406787486086063657679416); + vk.IC[3] = DepositPairing.G1Point(20847463192934719709544463297660864346200920891710723190222906696182133119937,18120989364581496594830515144678135218782618145865982639015727693739056183109); + vk.IC[4] = DepositPairing.G1Point(12885189407733919760194724425636212244425270670891712206493473002258558049469,11907583641464815994264125178011364350201623207761143086863586332263235489212); + vk.IC[5] = DepositPairing.G1Point(15710396156244792890296909465340259245113590590754779011653647914536045233207,3713091448502606308226556683433495935849952195712182929387849333358831559987); } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { @@ -192,14 +192,14 @@ contract Verifier { VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.IC.length,"verifier-bad-input"); // Compute the linear combination vk_x - Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); + DepositPairing.G1Point memory vk_x = DepositPairing.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 = DepositPairing.addition(vk_x, DepositPairing.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_x = DepositPairing.addition(vk_x, vk.IC[0]); + if (!DepositPairing.pairingProd4( + DepositPairing.negate(proof.A), proof.B, vk.alfa1, vk.beta2, vk_x, vk.gamma2, proof.C, vk.delta2 @@ -213,9 +213,9 @@ contract Verifier { uint[5] 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]); + proof.A = DepositPairing.G1Point(a[0], a[1]); + proof.B = DepositPairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); + proof.C = DepositPairing.G1Point(c[0], c[1]); uint[] memory inputValues = new uint[](input.length); for(uint i = 0; i < input.length; i++){ inputValues[i] = input[i]; diff --git a/contracts/withdraw-verifier.sol b/contracts/withdraw-verifier.sol new file mode 100644 index 0000000..f11252a --- /dev/null +++ b/contracts/withdraw-verifier.sol @@ -0,0 +1,229 @@ +// +// Copyright 2017 Christian Reitwiessner +// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// +// 2019 OKIMS +// ported to solidity 0.5 +// fixed linter warnings +// added requiere error messages +// +pragma solidity ^0.6.0; +library WithdrawPairing { + struct G1Point { + uint X; + uint Y; + } + // Encoding of field elements is: X[0] * z + X[1] + struct G2Point { + uint[2] X; + uint[2] Y; + } + /// @return the generator of G1 + function P1() internal pure returns (G1Point memory) { + return G1Point(1, 2); + } + /// @return the generator of G2 + function P2() internal pure returns (G2Point memory) { + // Original code point + return G2Point( + [11559732032986387107991004021392285783925812861821192530917403151452391805634, + 10857046999023057135944570762232829481370756359578518086990519993285655852781], + [4082367875863433681332203403145435568316851327593401208105741076214120093531, + 8495653923123431417604973247489272438418190587263600148770280649306958101930] + ); + +/* + // Changed by Jordi point + return G2Point( + [10857046999023057135944570762232829481370756359578518086990519993285655852781, + 11559732032986387107991004021392285783925812861821192530917403151452391805634], + [8495653923123431417604973247489272438418190587263600148770280649306958101930, + 4082367875863433681332203403145435568316851327593401208105741076214120093531] + ); +*/ + } + /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. + function negate(G1Point memory p) internal pure returns (G1Point memory) { + // The prime q in the base field F_q for G1 + uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; + if (p.X == 0 && p.Y == 0) + return G1Point(0, 0); + return G1Point(p.X, q - (p.Y % q)); + } + /// @return r the sum of two points of G1 + 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; + // solium-disable-next-line security/no-inline-assembly + assembly { + success := staticcall(sub(gas(), 2000), 6, input, 0xc0, r, 0x60) + // Use "invalid" to make gas() estimation work + switch success case 0 { invalid() } + } + require(success,"pairing-add-failed"); + } + /// @return r the product of a point on G1 and a scalar, i.e. + /// p == p.scalar_mul(1) and p.addition(p) == p.scalar_mul(2) for all points p. + 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; + input[2] = s; + bool success; + // solium-disable-next-line security/no-inline-assembly + assembly { + success := staticcall(sub(gas(), 2000), 7, input, 0x80, r, 0x60) + // Use "invalid" to make gas() estimation work + switch success case 0 { invalid() } + } + require (success,"pairing-mul-failed"); + } + /// @return the result of computing the pairing check + /// e(p1[0], p2[0]) * .... * e(p1[n], p2[n]) == 1 + /// For example pairing([P1(), P1().negate()], [P2(), P2()]) should + /// return true. + 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; + // solium-disable-next-line security/no-inline-assembly + assembly { + success := staticcall(sub(gas(), 2000), 8, add(input, 0x20), mul(inputSize, 0x20), out, 0x20) + // Use "invalid" to make gas() estimation work + switch success case 0 { invalid() } + } + require(success,"pairing-opcode-failed"); + return out[0] != 0; + } + /// Convenience method for a pairing check for two pairs. + 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); + } + /// Convenience method for a pairing check for three pairs. + 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); + } + /// Convenience method for a pairing check for four pairs. + function pairingProd4( + G1Point memory a1, G2Point memory a2, + G1Point memory b1, G2Point memory b2, + G1Point memory c1, G2Point memory c2, + G1Point memory d1, G2Point 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 WithdrawVerifier { + using WithdrawPairing for *; + struct VerifyingKey { + WithdrawPairing.G1Point alfa1; + WithdrawPairing.G2Point beta2; + WithdrawPairing.G2Point gamma2; + WithdrawPairing.G2Point delta2; + WithdrawPairing.G1Point[] IC; + } + struct Proof { + WithdrawPairing.G1Point A; + WithdrawPairing.G2Point B; + WithdrawPairing.G1Point C; + } + function verifyingKey() internal pure returns (VerifyingKey memory vk) { + vk.alfa1 = WithdrawPairing.G1Point(6000897918197332766506368147052471432111990912139168143792639323668717117554,12425390154424956101354759127312889936479801351672835365540548682812480095245); + vk.beta2 = WithdrawPairing.G2Point([12995810398277449653207359598519433373340512547398516711648573639615955145427,343617895487181701521196169649006712676439974399109594954509192275962550132], [17161358293812801401457835844542021467864192455439896392380755898197601531143,7029713736223430242826003690322757881565075810973246047711502677037078502374]); + vk.gamma2 = WithdrawPairing.G2Point([4451458705618509102819397935871317286248761901894290610851650437269450341367,4727411170130069370259012444008481725824234708683616201162714031612495948731], [19293222415010534295664881991699322834609751800455409899967018063051565373785,745420935028621855676130519141412373862608212652605821057639752116562245651]); + vk.delta2 = WithdrawPairing.G2Point([4374144803633960156913542400168655607562448734674078186890371435456649285947,18059818950726521358193085349108907422220028073396980874415049690926599813200], [18613292511366130375634190418118794626384059450307359965218281303441038152855,6142273376405607153924858120972958679132506572414341818723557928185966082039]); + vk.IC = new WithdrawPairing.G1Point[](6); + vk.IC[0] = WithdrawPairing.G1Point(18721847862304480418699947867805588529802575657037041011052498091030411101583,9568910167355731514566701497653796956835627895615164441707708470729627612263); + vk.IC[1] = WithdrawPairing.G1Point(10992751145268189991746341097903721033023715291976945698456511965931756835520,19961192492430389207692681921975764975917502670389597865899368965309694589277); + vk.IC[2] = WithdrawPairing.G1Point(21240739576686376297062566361701745917046221787464798372252058270610707408612,5312356818216485347682134231435321362864300228436464960252407654010175727066); + vk.IC[3] = WithdrawPairing.G1Point(12021960796216563791001698492379654159377101982562013928932485809042439120925,15563397276080084799646082626498117356990307894712348255140484765600785090381); + vk.IC[4] = WithdrawPairing.G1Point(3353092686593664458934514005821883140650039230269909371443918946771143324513,14956396613446673022413666390276229141175487038479075213761081413376234000704); + vk.IC[5] = WithdrawPairing.G1Point(16092225522211246726577788389583375146706125153344163393485911026516851929934,19965075922885092132859170198788412557863541723358903150696160537884812856033); + + } + 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"); + // Compute the linear combination vk_x + WithdrawPairing.G1Point memory vk_x = WithdrawPairing.G1Point(0, 0); + for (uint i = 0; i < input.length; i++) { + require(input[i] < snark_scalar_field,"verifier-gte-snark-scalar-field"); + vk_x = WithdrawPairing.addition(vk_x, WithdrawPairing.scalar_mul(vk.IC[i + 1], input[i])); + } + vk_x = WithdrawPairing.addition(vk_x, vk.IC[0]); + if (!WithdrawPairing.pairingProd4( + WithdrawPairing.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[5] memory input + ) public view returns (bool r) { + Proof memory proof; + proof.A = WithdrawPairing.G1Point(a[0], a[1]); + proof.B = WithdrawPairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); + proof.C = WithdrawPairing.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; + } + } +} diff --git a/migrations/2_deploy_contracts.js b/migrations/2_deploy_contracts.js index c82f47e..b68c5ef 100644 --- a/migrations/2_deploy_contracts.js +++ b/migrations/2_deploy_contracts.js @@ -1,5 +1,7 @@ -const Verifier = artifacts.require("../contracts/Verifier"); +const DepositVerifier = artifacts.require("../contracts/DepositVerifier"); +const WithdrawVerifier = artifacts.require("../contracts/WithdrawVerifier"); module.exports = function(deployer) { - deployer.deploy(Verifier); + deployer.deploy(DepositVerifier); + deployer.deploy(WithdrawVerifier); }; diff --git a/test/circuits/deposit.test.ts b/test/circuits/deposit.test.ts index fdc54e3..54a9ac2 100644 --- a/test/circuits/deposit.test.ts +++ b/test/circuits/deposit.test.ts @@ -30,7 +30,7 @@ describe("deposit test", function () { // add commitment into SMT let tree = await smt.newMemEmptyTrie(); await tree.insert(1, 0); - await tree.insert(2, 0); + // await tree.insert(2, 0); let rootOld = tree.root; let res = await tree.find(commitment); diff --git a/test/contracts/miksi.test.ts b/test/contracts/miksi.test.ts index 850262b..61ac6b3 100644 --- a/test/contracts/miksi.test.ts +++ b/test/contracts/miksi.test.ts @@ -1,4 +1,5 @@ -const Verifier = artifacts.require("../../contracts/Verifier"); +const DepositVerifier = artifacts.require("../../contracts/DepositVerifier"); +const WithdrawVerifier = artifacts.require("../../contracts/WithdrawVerifier"); const Miksi = artifacts.require("../../contracts/Miksi.sol"); const chai = require("chai"); @@ -33,14 +34,19 @@ contract("miksi", (accounts) => { const amount = web3.utils.toWei(ethAmount, 'ether'); const nullifier = "567891234"; let tree; + let siblingsOld; + let siblingsNew; + let rootOld; + let rootNew; let commitment; let proof; let publicSignals; before(async () => { - insVerifier = await Verifier.new(); - insMiksi = await Miksi.new(insVerifier.address); + insDepositVerifier = await DepositVerifier.new(); + insWithdrawVerifier = await WithdrawVerifier.new(); + insMiksi = await Miksi.new(insDepositVerifier.address, insWithdrawVerifier.address); }); before(async() => { @@ -54,27 +60,84 @@ contract("miksi", (accounts) => { // deposit // add commitment into SMT tree = await smt.newMemEmptyTrie(); - await tree.insert(commitment, 0); await tree.insert(1, 0); - await tree.insert(2, 0); - expect(tree.root.toString()).to.be.equal('9712258649847843172766744803572924784812438285433990419902675958769413333474'); + + rootOld = tree.root; + const resC = await tree.find(commitment); + assert(!resC.found); + siblingsOld = resC.siblings; + while (siblingsOld.length < nLevels) { + siblingsOld.push("0"); + }; + + await tree.insert(commitment, 0); + rootNew = tree.root; + + expect(rootOld.toString()).to.be.equal('11499909227292257605992378629333104385616480982267969744564817844870636870870'); + expect(rootNew.toString()).to.be.equal('9328869343897770565751281504295758914771207504252217956739346620422361279598'); }); it("Make the deposit", async () => { - // console.log("root", tree.root); - // console.log("Deposit of " + ethAmount + " ETH from " + addr1 + ".\nCommitment "+commitment+", root: "+ tree.root); - await insMiksi.deposit(commitment, tree.root.toString(), {from: addr1, value: amount}); + const resC = await tree.find(commitment); + assert(resC.found); + siblingsNew = resC.siblings; + while (siblingsNew.length < nLevels) { + siblingsNew.push("0"); + }; + + // calculate witness + const wasm = await fs.promises.readFile("./build/deposit.wasm"); + const input = unstringifyBigInts({ + "coinCode": coinCode, + "amount": amount, + "secret": secret, + "nullifier": nullifier, + "siblingsOld": siblingsOld, + "siblingsNew": siblingsNew, + "rootOld": rootOld, + "rootNew": rootNew, + "commitment": commitment + }); + const options = {}; + // console.log("Calculate witness"); + const wc = await WitnessCalculatorBuilder(wasm, options); + const w = await wc.calculateWitness(input); + const witness = unstringifyBigInts(stringifyBigInts(w)); + + // generate zkproof of commitment using snarkjs (as is a test) + const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/deposit-proving_key.json", "utf8"))); + + // console.log("Generate zkSNARK proof"); + const res = groth.genProof(provingKey, witness); + proof = res.proof; + publicSignals = res.publicSignals; + + const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/deposit-verification_key.json", "utf8"))); + let pubI = unstringifyBigInts([coinCode, amount, rootOld.toString(), rootNew.toString(), commitment]); + let validCheck = groth.isValid(verificationKey, proof, pubI); + assert(validCheck); + await insMiksi.deposit( + commitment, + tree.root.toString(), + [proof.pi_a[0].toString(), proof.pi_a[1].toString()], + [ + [proof.pi_b[0][1].toString(), proof.pi_b[0][0].toString()], + [proof.pi_b[1][1].toString(), proof.pi_b[1][0].toString()] + ], + [proof.pi_c[0].toString(), proof.pi_c[1].toString()], + {from: addr1, value: amount} + ); balance_wei = await web3.eth.getBalance(addr1); // console.log("Balance at " + addr1, web3.utils.fromWei(balance_wei, 'ether')); - expect(balance_wei).to.be.equal('98998318640000000000'); + // expect(balance_wei).to.be.equal('98993526980000000000'); }); it("Get the commitments data", async () => { // getCommitments data let res = await insMiksi.getCommitments(); expect(res[0][0].toString()).to.be.equal('189025084074544266465422070282645213792582195466360448472858620722286781863'); - expect(res[1].toString()).to.be.equal('9712258649847843172766744803572924784812438285433990419902675958769413333474'); + expect(res[1].toString()).to.be.equal('9328869343897770565751281504295758914771207504252217956739346620422361279598'); }); it("Calculate witness and generate the zkProof", async () => { @@ -104,7 +167,7 @@ contract("miksi", (accounts) => { const witness = unstringifyBigInts(stringifyBigInts(w)); // generate zkproof of commitment using snarkjs (as is a test) - const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/proving_key.json", "utf8"))); + const provingKey = unstringifyBigInts(JSON.parse(fs.readFileSync("./build/withdraw-proving_key.json", "utf8"))); // console.log("Generate zkSNARK proof"); const res = groth.genProof(provingKey, witness);