From c5cd8223502bf32a62fa0925ec200fd4478b0e9d Mon Sep 17 00:00:00 2001 From: adriamb Date: Fri, 14 Jun 2019 16:42:23 +0200 Subject: [PATCH 1/3] sol 0.5.0, remove warnings, add require messages --- templates/verifier_groth.sol | 67 +++++++++++++++----------- templates/verifier_original.sol | 85 ++++++++++++++++----------------- 2 files changed, 80 insertions(+), 72 deletions(-) diff --git a/templates/verifier_groth.sol b/templates/verifier_groth.sol index d1dd1c2..ed890d9 100644 --- a/templates/verifier_groth.sol +++ b/templates/verifier_groth.sol @@ -3,7 +3,13 @@ // 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. -pragma solidity ^0.4.17; +// +// 2019 OKIMS +// ported to solidity 0.5 +// fixed linter warnings +// added requiere error messages +// +pragma solidity ^0.5.0; library Pairing { struct G1Point { uint X; @@ -15,11 +21,11 @@ library Pairing { uint[2] Y; } /// @return the generator of G1 - function P1() pure internal returns (G1Point) { + function P1() internal pure returns (G1Point memory) { return G1Point(1, 2); } /// @return the generator of G2 - function P2() pure internal returns (G2Point) { + function P2() internal pure returns (G2Point memory) { // Original code point return G2Point( [11559732032986387107991004021392285783925812861821192530917403151452391805634, @@ -39,7 +45,7 @@ library Pairing { */ } /// @return the negation of p, i.e. p.addition(p.negate()) should be zero. - function negate(G1Point p) pure internal returns (G1Point) { + 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) @@ -47,41 +53,43 @@ library Pairing { return G1Point(p.X, q - (p.Y % q)); } /// @return the sum of two points of G1 - function addition(G1Point p1, G1Point p2) view internal returns (G1Point r) { + 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); + require(success,"pairing-add-failed"); } /// @return 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 p, uint s) view internal returns (G1Point r) { + 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); + 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[] p1, G2Point[] p2) view internal returns (bool) { - require(p1.length == p2.length); + 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); @@ -96,16 +104,17 @@ library Pairing { } 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); + require(success,"pairing-opcode-failed"); return out[0] != 0; } /// Convenience method for a pairing check for two pairs. - function pairingProd2(G1Point a1, G2Point a2, G1Point b1, G2Point b2) view internal returns (bool) { + 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; @@ -116,10 +125,10 @@ library Pairing { } /// Convenience method for a pairing check for three pairs. function pairingProd3( - G1Point a1, G2Point a2, - G1Point b1, G2Point b2, - G1Point c1, G2Point c2 - ) view internal returns (bool) { + 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; @@ -132,11 +141,11 @@ library Pairing { } /// Convenience method for a pairing check for four pairs. function pairingProd4( - G1Point a1, G2Point a2, - G1Point b1, G2Point b2, - G1Point c1, G2Point c2, - G1Point d1, G2Point d2 - ) view internal returns (bool) { + 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; @@ -164,7 +173,7 @@ contract Verifier { Pairing.G2Point B; Pairing.G1Point C; } - function verifyingKey() pure internal returns (VerifyingKey vk) { + function verifyingKey() internal pure returns (VerifyingKey memory vk) { vk.alfa1 = Pairing.G1Point(<%vk_alfa1%>); vk.beta2 = Pairing.G2Point(<%vk_beta2%>); vk.gamma2 = Pairing.G2Point(<%vk_gamma2%>); @@ -172,9 +181,9 @@ contract Verifier { vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } - function verify(uint[] input, Proof proof) view internal returns (uint) { + function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { VerifyingKey memory vk = verifyingKey(); - require(input.length + 1 == vk.IC.length); + 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); for (uint i = 0; i < input.length; i++) @@ -189,11 +198,11 @@ contract Verifier { return 0; } function verifyProof( - uint[2] a, - uint[2][2] b, - uint[2] c, - uint[<%vk_input_length%>] input - ) view public returns (bool r) { + uint[2] memory a, + uint[2][2] memory b, + uint[2] memory c, + uint[<%vk_input_length%>] 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]]); diff --git a/templates/verifier_original.sol b/templates/verifier_original.sol index 4157287..addc9b0 100644 --- a/templates/verifier_original.sol +++ b/templates/verifier_original.sol @@ -3,7 +3,13 @@ // 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. -pragma solidity ^0.4.17; +// +// 2019 OKIMS +// ported to solidity 0.5 +// fixed linter warnings +// added requiere error messages +// +pragma solidity ^0.5.0; library Pairing { struct G1Point { uint X; @@ -15,11 +21,11 @@ library Pairing { uint[2] Y; } /// @return the generator of G1 - function P1() pure internal returns (G1Point) { + function P1() internal pure returns (G1Point memory) { return G1Point(1, 2); } /// @return the generator of G2 - function P2() pure internal returns (G2Point) { + function P2() internal pure returns (G2Point memory) { // Original code point return G2Point( [11559732032986387107991004021392285783925812861821192530917403151452391805634, @@ -27,19 +33,9 @@ library Pairing { [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 p) pure internal returns (G1Point) { + 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) @@ -47,41 +43,43 @@ library Pairing { return G1Point(p.X, q - (p.Y % q)); } /// @return the sum of two points of G1 - function addition(G1Point p1, G1Point p2) view internal returns (G1Point r) { + 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); + require(success,"pairing-add-failed"); } /// @return 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 p, uint s) view internal returns (G1Point r) { + 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); + 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[] p1, G2Point[] p2) view internal returns (bool) { - require(p1.length == p2.length); + 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); @@ -96,16 +94,17 @@ library Pairing { } 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); + require(success,"pairing-opcode-failed"); return out[0] != 0; } /// Convenience method for a pairing check for two pairs. - function pairingProd2(G1Point a1, G2Point a2, G1Point b1, G2Point b2) view internal returns (bool) { + 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; @@ -116,10 +115,10 @@ library Pairing { } /// Convenience method for a pairing check for three pairs. function pairingProd3( - G1Point a1, G2Point a2, - G1Point b1, G2Point b2, - G1Point c1, G2Point c2 - ) view internal returns (bool) { + 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; @@ -132,11 +131,11 @@ library Pairing { } /// Convenience method for a pairing check for four pairs. function pairingProd4( - G1Point a1, G2Point a2, - G1Point b1, G2Point b2, - G1Point c1, G2Point c2, - G1Point d1, G2Point d2 - ) view internal returns (bool) { + 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; @@ -172,7 +171,7 @@ contract Verifier { Pairing.G1Point K; Pairing.G1Point H; } - function verifyingKey() pure internal returns (VerifyingKey vk) { + function verifyingKey() internal pure returns (VerifyingKey memory vk) { vk.A = Pairing.G2Point(<%vk_a%>); vk.B = Pairing.G1Point(<%vk_b%>); vk.C = Pairing.G2Point(<%vk_c%>); @@ -183,9 +182,9 @@ contract Verifier { vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } - function verify(uint[] input, Proof proof) view internal returns (uint) { + function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { VerifyingKey memory vk = verifyingKey(); - require(input.length + 1 == vk.IC.length); + 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); for (uint i = 0; i < input.length; i++) @@ -207,15 +206,15 @@ contract Verifier { return 0; } function verifyProof( - uint[2] a, - uint[2] a_p, - uint[2][2] b, - uint[2] b_p, - uint[2] c, - uint[2] c_p, - uint[2] h, - uint[2] k, - uint[<%vk_input_length%>] input + uint[2] memory a, + uint[2] memory a_p, + uint[2][2] memory b, + uint[2] memory b_p, + uint[2] memory c, + uint[2] memory c_p, + uint[2] memory h, + uint[2] memory k, + uint[<%vk_input_length%>] memory input ) view public returns (bool r) { Proof memory proof; proof.A = Pairing.G1Point(a[0], a[1]); From 22a5b7bc46a0a1a69019af9feb99fe3cdbe1411b Mon Sep 17 00:00:00 2001 From: adriamb Date: Tue, 18 Jun 2019 16:33:14 +0200 Subject: [PATCH 2/3] Generate contract name by its name output --- cli.js | 17 ++++-- templates/verifier_groth.sol | 59 ++++++++------------ templates/verifier_original.sol | 97 ++++++++++++++++----------------- 3 files changed, 83 insertions(+), 90 deletions(-) diff --git a/cli.js b/cli.js index 0d92abf..5c846f2 100755 --- a/cli.js +++ b/cli.js @@ -318,12 +318,14 @@ try { } else if (argv._[0].toUpperCase() == "GENERATEVERIFIER") { const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); + let contractName = path.parse(verifierName).name + contractName = contractName.charAt(0).toUpperCase() + contractName.slice(1) let verifierCode; if (verificationKey.protocol == "original") { - verifierCode = generateVerifier_original(verificationKey); + verifierCode = generateVerifier_original(contractName,verificationKey); } else if (verificationKey.protocol == "groth") { - verifierCode = generateVerifier_groth(verificationKey); + verifierCode = generateVerifier_groth(contractName,verificationKey); } else { throw new Error("InvalidProof"); } @@ -374,9 +376,11 @@ try { } -function generateVerifier_original(verificationKey) { +function generateVerifier_original(contractName, verificationKey) { let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_original.sol"), "utf-8"); + template = template.replace("<%contractName%>", contractName); + const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+ `${verificationKey.vk_a[0][0].toString()}], `+ `[${verificationKey.vk_a[1][1].toString()},` + @@ -422,7 +426,7 @@ function generateVerifier_original(verificationKey) { let vi = ""; for (let i=0; i", vi); @@ -431,9 +435,10 @@ function generateVerifier_original(verificationKey) { } -function generateVerifier_groth(verificationKey) { +function generateVerifier_groth(contractName,verificationKey) { let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8"); + template = template.replace("<%contractName%>", contractName); const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+ `${verificationKey.vk_alfa_1[1].toString()}`; @@ -464,7 +469,7 @@ function generateVerifier_groth(verificationKey) { let vi = ""; for (let i=0; i", vi); diff --git a/templates/verifier_groth.sol b/templates/verifier_groth.sol index ed890d9..8c0efa1 100644 --- a/templates/verifier_groth.sol +++ b/templates/verifier_groth.sol @@ -10,7 +10,8 @@ // added requiere error messages // pragma solidity ^0.5.0; -library Pairing { +contract <%contractName%> { + struct G1Point { uint X; uint Y; @@ -33,16 +34,6 @@ library Pairing { [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) { @@ -158,39 +149,37 @@ library Pairing { p2[3] = d2; return pairing(p1, p2); } -} -contract Verifier { - using Pairing for *; + struct VerifyingKey { - Pairing.G1Point alfa1; - Pairing.G2Point beta2; - Pairing.G2Point gamma2; - Pairing.G2Point delta2; - Pairing.G1Point[] IC; + G1Point alfa1; + G2Point beta2; + G2Point gamma2; + G2Point delta2; + G1Point[] IC; } struct Proof { - Pairing.G1Point A; - Pairing.G2Point B; - Pairing.G1Point C; + G1Point A; + G2Point B; + G1Point C; } function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.alfa1 = Pairing.G1Point(<%vk_alfa1%>); - vk.beta2 = Pairing.G2Point(<%vk_beta2%>); - vk.gamma2 = Pairing.G2Point(<%vk_gamma2%>); - vk.delta2 = Pairing.G2Point(<%vk_delta2%>); - vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); + vk.alfa1 = G1Point(<%vk_alfa1%>); + vk.beta2 = G2Point(<%vk_beta2%>); + vk.gamma2 = G2Point(<%vk_gamma2%>); + vk.delta2 = G2Point(<%vk_delta2%>); + vk.IC = new G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { 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); + G1Point memory vk_x = G1Point(0, 0); for (uint i = 0; i < input.length; i++) - 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_x = addition(vk_x, scalar_mul(vk.IC[i + 1], input[i])); + vk_x = addition(vk_x, vk.IC[0]); + if (!pairingProd4( + negate(proof.A), proof.B, vk.alfa1, vk.beta2, vk_x, vk.gamma2, proof.C, vk.delta2 @@ -204,9 +193,9 @@ contract Verifier { uint[<%vk_input_length%>] 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 = G1Point(a[0], a[1]); + proof.B = G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); + proof.C = 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/templates/verifier_original.sol b/templates/verifier_original.sol index addc9b0..c1972b4 100644 --- a/templates/verifier_original.sol +++ b/templates/verifier_original.sol @@ -10,7 +10,8 @@ // added requiere error messages // pragma solidity ^0.5.0; -library Pairing { +contract <%contractName%> { + struct G1Point { uint X; uint Y; @@ -148,60 +149,58 @@ library Pairing { p2[3] = d2; return pairing(p1, p2); } -} -contract Verifier { - using Pairing for *; + struct VerifyingKey { - Pairing.G2Point A; - Pairing.G1Point B; - Pairing.G2Point C; - Pairing.G2Point gamma; - Pairing.G1Point gammaBeta1; - Pairing.G2Point gammaBeta2; - Pairing.G2Point Z; - Pairing.G1Point[] IC; + G2Point A; + G1Point B; + G2Point C; + G2Point gamma; + G1Point gammaBeta1; + G2Point gammaBeta2; + G2Point Z; + G1Point[] IC; } struct Proof { - Pairing.G1Point A; - Pairing.G1Point A_p; - Pairing.G2Point B; - Pairing.G1Point B_p; - Pairing.G1Point C; - Pairing.G1Point C_p; - Pairing.G1Point K; - Pairing.G1Point H; + G1Point A; + G1Point A_p; + G2Point B; + G1Point B_p; + G1Point C; + G1Point C_p; + G1Point K; + G1Point H; } function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.A = Pairing.G2Point(<%vk_a%>); - vk.B = Pairing.G1Point(<%vk_b%>); - vk.C = Pairing.G2Point(<%vk_c%>); - vk.gamma = Pairing.G2Point(<%vk_g%>); - vk.gammaBeta1 = Pairing.G1Point(<%vk_gb1%>); - vk.gammaBeta2 = Pairing.G2Point(<%vk_gb2%>); - vk.Z = Pairing.G2Point(<%vk_z%>); - vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); + vk.A = G2Point(<%vk_a%>); + vk.B = G1Point(<%vk_b%>); + vk.C = G2Point(<%vk_c%>); + vk.gamma = G2Point(<%vk_g%>); + vk.gammaBeta1 = G1Point(<%vk_gb1%>); + vk.gammaBeta2 = G2Point(<%vk_gb2%>); + vk.Z = G2Point(<%vk_z%>); + vk.IC = new G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { 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); + G1Point memory vk_x = G1Point(0, 0); for (uint i = 0; i < input.length; i++) - 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.pairingProd2(proof.A, vk.A, Pairing.negate(proof.A_p), Pairing.P2())) return 1; - if (!Pairing.pairingProd2(vk.B, proof.B, Pairing.negate(proof.B_p), Pairing.P2())) return 2; - if (!Pairing.pairingProd2(proof.C, vk.C, Pairing.negate(proof.C_p), Pairing.P2())) return 3; - if (!Pairing.pairingProd3( + vk_x = addition(vk_x, scalar_mul(vk.IC[i + 1], input[i])); + vk_x = addition(vk_x, vk.IC[0]); + if (!pairingProd2(proof.A, vk.A, negate(proof.A_p), P2())) return 1; + if (!pairingProd2(vk.B, proof.B, negate(proof.B_p), P2())) return 2; + if (!pairingProd2(proof.C, vk.C, negate(proof.C_p), P2())) return 3; + if (!pairingProd3( proof.K, vk.gamma, - Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.A, proof.C))), vk.gammaBeta2, - Pairing.negate(vk.gammaBeta1), proof.B + negate(addition(vk_x, addition(proof.A, proof.C))), vk.gammaBeta2, + negate(vk.gammaBeta1), proof.B )) return 4; - if (!Pairing.pairingProd3( - Pairing.addition(vk_x, proof.A), proof.B, - Pairing.negate(proof.H), vk.Z, - Pairing.negate(proof.C), Pairing.P2() + if (!pairingProd3( + addition(vk_x, proof.A), proof.B, + negate(proof.H), vk.Z, + negate(proof.C), P2() )) return 5; return 0; } @@ -217,14 +216,14 @@ contract Verifier { uint[<%vk_input_length%>] memory input ) view public returns (bool r) { Proof memory proof; - proof.A = Pairing.G1Point(a[0], a[1]); - proof.A_p = Pairing.G1Point(a_p[0], a_p[1]); - proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); - proof.B_p = Pairing.G1Point(b_p[0], b_p[1]); - proof.C = Pairing.G1Point(c[0], c[1]); - proof.C_p = Pairing.G1Point(c_p[0], c_p[1]); - proof.H = Pairing.G1Point(h[0], h[1]); - proof.K = Pairing.G1Point(k[0], k[1]); + proof.A = G1Point(a[0], a[1]); + proof.A_p = G1Point(a_p[0], a_p[1]); + proof.B = G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); + proof.B_p = G1Point(b_p[0], b_p[1]); + proof.C = G1Point(c[0], c[1]); + proof.C_p = G1Point(c_p[0], c_p[1]); + proof.H = G1Point(h[0], h[1]); + proof.K = G1Point(k[0], k[1]); uint[] memory inputValues = new uint[](input.length); for(uint i = 0; i < input.length; i++){ inputValues[i] = input[i]; From d2b3be19ef66d8ee8a68efea2765336e620bac54 Mon Sep 17 00:00:00 2001 From: adriamb Date: Tue, 18 Jun 2019 17:07:11 +0200 Subject: [PATCH 3/3] Revert "Generate contract name by its name output" This reverts commit 22a5b7bc46a0a1a69019af9feb99fe3cdbe1411b. --- cli.js | 17 ++---- templates/verifier_groth.sol | 59 ++++++++++++-------- templates/verifier_original.sol | 97 +++++++++++++++++---------------- 3 files changed, 90 insertions(+), 83 deletions(-) diff --git a/cli.js b/cli.js index 5c846f2..0d92abf 100755 --- a/cli.js +++ b/cli.js @@ -318,14 +318,12 @@ try { } else if (argv._[0].toUpperCase() == "GENERATEVERIFIER") { const verificationKey = unstringifyBigInts(JSON.parse(fs.readFileSync(verificationKeyName, "utf8"))); - let contractName = path.parse(verifierName).name - contractName = contractName.charAt(0).toUpperCase() + contractName.slice(1) let verifierCode; if (verificationKey.protocol == "original") { - verifierCode = generateVerifier_original(contractName,verificationKey); + verifierCode = generateVerifier_original(verificationKey); } else if (verificationKey.protocol == "groth") { - verifierCode = generateVerifier_groth(contractName,verificationKey); + verifierCode = generateVerifier_groth(verificationKey); } else { throw new Error("InvalidProof"); } @@ -376,11 +374,9 @@ try { } -function generateVerifier_original(contractName, verificationKey) { +function generateVerifier_original(verificationKey) { let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_original.sol"), "utf-8"); - template = template.replace("<%contractName%>", contractName); - const vka_str = `[${verificationKey.vk_a[0][1].toString()},`+ `${verificationKey.vk_a[0][0].toString()}], `+ `[${verificationKey.vk_a[1][1].toString()},` + @@ -426,7 +422,7 @@ function generateVerifier_original(contractName, verificationKey) { let vi = ""; for (let i=0; i", vi); @@ -435,10 +431,9 @@ function generateVerifier_original(contractName, verificationKey) { } -function generateVerifier_groth(contractName,verificationKey) { +function generateVerifier_groth(verificationKey) { let template = fs.readFileSync(path.join( __dirname, "templates", "verifier_groth.sol"), "utf-8"); - template = template.replace("<%contractName%>", contractName); const vkalfa1_str = `${verificationKey.vk_alfa_1[0].toString()},`+ `${verificationKey.vk_alfa_1[1].toString()}`; @@ -469,7 +464,7 @@ function generateVerifier_groth(contractName,verificationKey) { let vi = ""; for (let i=0; i", vi); diff --git a/templates/verifier_groth.sol b/templates/verifier_groth.sol index 8c0efa1..ed890d9 100644 --- a/templates/verifier_groth.sol +++ b/templates/verifier_groth.sol @@ -10,8 +10,7 @@ // added requiere error messages // pragma solidity ^0.5.0; -contract <%contractName%> { - +library Pairing { struct G1Point { uint X; uint Y; @@ -34,6 +33,16 @@ contract <%contractName%> { [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) { @@ -149,37 +158,39 @@ contract <%contractName%> { p2[3] = d2; return pairing(p1, p2); } - +} +contract Verifier { + using Pairing for *; struct VerifyingKey { - G1Point alfa1; - G2Point beta2; - G2Point gamma2; - G2Point delta2; - G1Point[] IC; + Pairing.G1Point alfa1; + Pairing.G2Point beta2; + Pairing.G2Point gamma2; + Pairing.G2Point delta2; + Pairing.G1Point[] IC; } struct Proof { - G1Point A; - G2Point B; - G1Point C; + Pairing.G1Point A; + Pairing.G2Point B; + Pairing.G1Point C; } function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.alfa1 = G1Point(<%vk_alfa1%>); - vk.beta2 = G2Point(<%vk_beta2%>); - vk.gamma2 = G2Point(<%vk_gamma2%>); - vk.delta2 = G2Point(<%vk_delta2%>); - vk.IC = new G1Point[](<%vk_ic_length%>); + vk.alfa1 = Pairing.G1Point(<%vk_alfa1%>); + vk.beta2 = Pairing.G2Point(<%vk_beta2%>); + vk.gamma2 = Pairing.G2Point(<%vk_gamma2%>); + vk.delta2 = Pairing.G2Point(<%vk_delta2%>); + vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.IC.length,"verifier-bad-input"); // Compute the linear combination vk_x - G1Point memory vk_x = G1Point(0, 0); + Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); for (uint i = 0; i < input.length; i++) - vk_x = addition(vk_x, scalar_mul(vk.IC[i + 1], input[i])); - vk_x = addition(vk_x, vk.IC[0]); - if (!pairingProd4( - negate(proof.A), proof.B, + 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 @@ -193,9 +204,9 @@ contract <%contractName%> { uint[<%vk_input_length%>] memory input ) public view returns (bool r) { Proof memory proof; - proof.A = G1Point(a[0], a[1]); - proof.B = G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); - proof.C = G1Point(c[0], c[1]); + 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]; diff --git a/templates/verifier_original.sol b/templates/verifier_original.sol index c1972b4..addc9b0 100644 --- a/templates/verifier_original.sol +++ b/templates/verifier_original.sol @@ -10,8 +10,7 @@ // added requiere error messages // pragma solidity ^0.5.0; -contract <%contractName%> { - +library Pairing { struct G1Point { uint X; uint Y; @@ -149,58 +148,60 @@ contract <%contractName%> { p2[3] = d2; return pairing(p1, p2); } - +} +contract Verifier { + using Pairing for *; struct VerifyingKey { - G2Point A; - G1Point B; - G2Point C; - G2Point gamma; - G1Point gammaBeta1; - G2Point gammaBeta2; - G2Point Z; - G1Point[] IC; + Pairing.G2Point A; + Pairing.G1Point B; + Pairing.G2Point C; + Pairing.G2Point gamma; + Pairing.G1Point gammaBeta1; + Pairing.G2Point gammaBeta2; + Pairing.G2Point Z; + Pairing.G1Point[] IC; } struct Proof { - G1Point A; - G1Point A_p; - G2Point B; - G1Point B_p; - G1Point C; - G1Point C_p; - G1Point K; - G1Point H; + Pairing.G1Point A; + Pairing.G1Point A_p; + Pairing.G2Point B; + Pairing.G1Point B_p; + Pairing.G1Point C; + Pairing.G1Point C_p; + Pairing.G1Point K; + Pairing.G1Point H; } function verifyingKey() internal pure returns (VerifyingKey memory vk) { - vk.A = G2Point(<%vk_a%>); - vk.B = G1Point(<%vk_b%>); - vk.C = G2Point(<%vk_c%>); - vk.gamma = G2Point(<%vk_g%>); - vk.gammaBeta1 = G1Point(<%vk_gb1%>); - vk.gammaBeta2 = G2Point(<%vk_gb2%>); - vk.Z = G2Point(<%vk_z%>); - vk.IC = new G1Point[](<%vk_ic_length%>); + vk.A = Pairing.G2Point(<%vk_a%>); + vk.B = Pairing.G1Point(<%vk_b%>); + vk.C = Pairing.G2Point(<%vk_c%>); + vk.gamma = Pairing.G2Point(<%vk_g%>); + vk.gammaBeta1 = Pairing.G1Point(<%vk_gb1%>); + vk.gammaBeta2 = Pairing.G2Point(<%vk_gb2%>); + vk.Z = Pairing.G2Point(<%vk_z%>); + vk.IC = new Pairing.G1Point[](<%vk_ic_length%>); <%vk_ic_pts%> } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.IC.length,"verifier-bad-input"); // Compute the linear combination vk_x - G1Point memory vk_x = G1Point(0, 0); + Pairing.G1Point memory vk_x = Pairing.G1Point(0, 0); for (uint i = 0; i < input.length; i++) - vk_x = addition(vk_x, scalar_mul(vk.IC[i + 1], input[i])); - vk_x = addition(vk_x, vk.IC[0]); - if (!pairingProd2(proof.A, vk.A, negate(proof.A_p), P2())) return 1; - if (!pairingProd2(vk.B, proof.B, negate(proof.B_p), P2())) return 2; - if (!pairingProd2(proof.C, vk.C, negate(proof.C_p), P2())) return 3; - if (!pairingProd3( + 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.pairingProd2(proof.A, vk.A, Pairing.negate(proof.A_p), Pairing.P2())) return 1; + if (!Pairing.pairingProd2(vk.B, proof.B, Pairing.negate(proof.B_p), Pairing.P2())) return 2; + if (!Pairing.pairingProd2(proof.C, vk.C, Pairing.negate(proof.C_p), Pairing.P2())) return 3; + if (!Pairing.pairingProd3( proof.K, vk.gamma, - negate(addition(vk_x, addition(proof.A, proof.C))), vk.gammaBeta2, - negate(vk.gammaBeta1), proof.B + Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.A, proof.C))), vk.gammaBeta2, + Pairing.negate(vk.gammaBeta1), proof.B )) return 4; - if (!pairingProd3( - addition(vk_x, proof.A), proof.B, - negate(proof.H), vk.Z, - negate(proof.C), P2() + if (!Pairing.pairingProd3( + Pairing.addition(vk_x, proof.A), proof.B, + Pairing.negate(proof.H), vk.Z, + Pairing.negate(proof.C), Pairing.P2() )) return 5; return 0; } @@ -216,14 +217,14 @@ contract <%contractName%> { uint[<%vk_input_length%>] memory input ) view public returns (bool r) { Proof memory proof; - proof.A = G1Point(a[0], a[1]); - proof.A_p = G1Point(a_p[0], a_p[1]); - proof.B = G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); - proof.B_p = G1Point(b_p[0], b_p[1]); - proof.C = G1Point(c[0], c[1]); - proof.C_p = G1Point(c_p[0], c_p[1]); - proof.H = G1Point(h[0], h[1]); - proof.K = G1Point(k[0], k[1]); + proof.A = Pairing.G1Point(a[0], a[1]); + proof.A_p = Pairing.G1Point(a_p[0], a_p[1]); + proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); + proof.B_p = Pairing.G1Point(b_p[0], b_p[1]); + proof.C = Pairing.G1Point(c[0], c[1]); + proof.C_p = Pairing.G1Point(c_p[0], c_p[1]); + proof.H = Pairing.G1Point(h[0], h[1]); + proof.K = Pairing.G1Point(k[0], k[1]); uint[] memory inputValues = new uint[](input.length); for(uint i = 0; i < input.length; i++){ inputValues[i] = input[i];