0

This is Junaid and I have been working on incorporating ZK SNARKs within a voting application. I am facing some issues regarding the deployment of the contracts.

I am sharing the link for the repo that I have been working on. https://github.com/Blockchain-E-Voting/EthzkSNARK-Voting/tree/master/contracts

First I am facing the issue in the 'verifier.sol' file for the switch statement. When I tried to resolve it by replacing the switch statement with if/else statement but then the compiler didn't able to identify the 'call' function and threw error 'Call' function not identified. I am using the solc (solidity compiler) version 0.4.23.

Original code:

/// @return the sum of two points of G1
function addition(G1Point p1, G1Point p2) internal returns (G1Point 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 := call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60)
            // Use "invalid" to make gas estimation work 
            switch success case 0 { invalid() }
        }
        require(success);
    }

Now what I tried was:

/// @return the sum of two points of G1
     function addition(G1Point p1, G1Point p2) internal returns (G1Point 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 = call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60);
            // Use "invalid" to make gas estimation work     
        //switch success case 0 { invalid() }
        //require(success);
        if (success == 0) {
            invalid();
        }
        else{
             require(success);
    }
   }
}

It would be great if anyone would help me in getting this application run properly.

Looking forward to your contribution.

Best Regards,

The whole code of this file is,

// This file is MIT Licensed.
//
// 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.

pragma solidity ^0.4.23;
library Pairing {
    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() pure internal returns (G1Point) {
        return G1Point(1, 2);
    }
    /// @return the generator of G2
    function P2() pure internal returns (G2Point) {
        return G2Point(
            [11559732032986387107991004021392285783925812861821192530917403151452391805634,
             10857046999023057135944570762232829481370756359578518086990519993285655852781],
            [4082367875863433681332203403145435568316851327593401208105741076214120093531,
             8495653923123431417604973247489272438418190587263600148770280649306958101930]
        );
    }
    /// @return the negation of p, i.e. p.addition(p.negate()) should be zero.
    function negate(G1Point p) internal pure  returns (G1Point) {
        // 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 the sum of two points of G1
    function addition(G1Point p1, G1Point p2) internal returns (G1Point r) {
        uint[4] memory input;
        input[0] = p1.X;
        input[1] = p1.Y;
        input[2] = p2.X;
        input[3] = p2.Y;
        bool success;
            success = call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60);
            // Use "invalid" to make gas estimation work     
        //switch success case 0 { invalid() }
        //require(success);
    //}
    if iszero(success) {
    revert(0, 0)
}
    /// @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) internal returns (G1Point r) {
        uint[3] memory input;
        input[0] = p.X;
        input[1] = p.Y;
        input[2] = s;
        bool success;
        assembly 
        {
            success := call(sub(gas, 2000), 7, 0, input, 0x80, r, 0x60)
            // Use "invalid" to make gas estimation work
            switch success case 0 { invalid() }
        }
        require (success);
    }
    /// @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) internal returns (bool) {
        require(p1.length == p2.length);
        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 := call(sub(gas, 2000), 8, 0, add(input, 0x20), mul(inputSize, 0x20), out, 0x20)
            // Use "invalid" to make gas estimation work
            switch success case 0 { invalid() }
        }
        require(success);
        return out[0] != 0;
    }
    /// Convenience method for a pairing check for two pairs.
    function pairingProd2(G1Point a1, G2Point a2, G1Point b1, G2Point b2) internal 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 a1, G2Point a2,
            G1Point b1, G2Point b2,
            G1Point c1, G2Point c2
    ) internal 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 a1, G2Point a2,
            G1Point b1, G2Point b2,
            G1Point c1, G2Point c2,
            G1Point d1, G2Point d2
    ) internal 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 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;
    }
    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;
    }
    function verifyingKey() pure internal returns (VerifyingKey vk) {
        vk.A = Pairing.G2Point([0x33de61db4f5934f8646980bdbb09ba19ff68983c38d0967196b01b972a50fb5, 0x167096f00a6c7a01607c19b1351cd9b1c50bcfaec5fed77ad98e7fe876511374], [0x2751b1f30775dd81454dee862b4d02e91ad15756d6224756aedbe58ca06c216, 0x26a09b5bbd5cdb912b8f09d707f1741a9ac48d6e0f73e862236f051b73d53b55]);
        vk.B = Pairing.G1Point(0x18f8de99ee586e675b3f9fea80f25f3283d9c03a13b26107a6d883aee82b5c73, 0x21f33c90cd3fb9d1bc8ea131bfc7b7850846d5fffb70ce66a2dceb7799b8d99a);
        vk.C = Pairing.G2Point([0x2f5e4b788b4a2c2174e8c4d97bbae932484eb5535aec9fe311bf4f9387aa1d01, 0x2cdf2769a28bc1d11fc1de2372925cfefab1e560b08cc719cff37d99b7e87091], [0x18fbcdeaae46d19281d396f5f641e3b67af55b3f77cd2ec671606587ee1a6b42, 0x239ded808e68ea3781235d66a136c938c968bca62f8aa002379db00225f1c5ca]);
        vk.gamma = Pairing.G2Point([0x615bf5be61b937ea94d053bc81eebba8ab2effe55783f81c39d1c95ab7a957e, 0x10c8ed52c3f5c843c968d5c09acde4840734b4c9d723d0cb6ea0a7e3a5d85dca], [0x640498f9bc8bb0e2336c502aad129c0a1ee9ba61e8f44ad39b1f13f36ce9076, 0x2f698085427044680920d514f67e26ff984824f1464a27fee5b83e5d27513c46]);
        vk.gammaBeta1 = Pairing.G1Point(0x2a99bc15b1edfa1c86a9fd7ed333d5cb0d35282b0e907d5baa644ce6d2049851, 0x1e289523f69aacdb76ba5747367454b7594b133baf97d8eb6d8780d74e29b9ec);
        vk.gammaBeta2 = Pairing.G2Point([0x21041f7250d4dc2b96e166d86130b6f3466d59b75658c1aabe8a208275c5fe04, 0x2e11b8c7f4a97d9c2f8fccef3760b5ccf3a11ed9f9c38159a450534aa8f35195], [0x29e0bf2a6723a9ba6ca0f687338d7eb9450e0a4d6cceb91e54347296b18a26da, 0x23fc872cff5f984f77e78f68039a263be7b40768518c92ae4dd81ff66b06fa46]);
        vk.Z = Pairing.G2Point([0x235d9e4193d28bebc47c55d58ce05b8d7f2cb26f09b06b8c85c9dd70139dba0e, 0xd6395535f6adbc98bcfc87ae2d56950e266f445de3c1e9287b022d63585ea87], [0x124d45c1d43d9406886ddfa86c5be4d0a2840ce061fdfa1910c58d715b50fbaa, 0x40395fdc52a1ca64779871db11b78e98f97ee85df07c7ebee711e3c7ca9f7d3]);
        vk.IC = new Pairing.G1Point[](2);
        vk.IC[0] = Pairing.G1Point(0x134b88b827ad466340562ce6ae375307bfecd1fbed4dd78a3bba7742c5eb45f1, 0x18fa4f8997558930b456921ebecefbeadcd875b0298a27c9ba69d9eea45e8eea);
        vk.IC[1] = Pairing.G1Point(0x1d694a79a0570423a77d5415ffbf690443a9877b396a88286d54146550c3c178, 0xc4e475824a8dcf1ddc0eb5b2bcacf60401123a68892ea8dae0d69956e84c214);
    }
    function verify(uint[] input, Proof proof) internal returns (uint) {
        VerifyingKey memory vk = verifyingKey();
        require(input.length + 1 == vk.IC.length);
        // Compute the linear combination vk_x
        Pairing.G1Point memory vk_x = Pairing.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(
            proof.K, vk.gamma,
            Pairing.negate(Pairing.addition(vk_x, Pairing.addition(proof.A, proof.C))), vk.gammaBeta2,
            Pairing.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()
        )) return 5;
        return 0;
    }
    event Verified(string s);
    function verifyTx(
            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[1] input
        ) 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]);
        uint[] memory inputValues = new uint[](input.length);
        for(uint i = 0; i < input.length; i++){
            inputValues[i] = input[i];
        }
        if (verify(inputValues, proof) == 0) {
            emit Verified("Transaction successfully verified.");
            return true;
        } else {
            return false;
        }
    }
}
8
  • "I tried to resolve it by replacing the switch statement with if/else statement": 1. Please share the relevant piece of code (don't make us jump to your link and search for it). 2. Please show how you've changed it (how exactly are we supposed to determine the reason for your compilation issue otherwise???). Feb 26, 2020 at 13:08
  • function addition(G1Point p1, G1Point p2) internal returns (G1Point r) { uint[4] memory input; input[0] = p1.X; input[1] = p1.Y; input[2] = p2.X; input[3] = p2.Y; bool success; { success = call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60); // Use "invalid" to make gas estimation work if (success == 0){ invalid(); } else{ require(success); } }
    – user58235
    Feb 28, 2020 at 6:20
  • Post it properly formatted as part of your question please. Feb 28, 2020 at 6:25
  • this is what I tried to resolve the issue. The original code was, bool success; success = call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60); switch success case 0 { invalid() } require(success); }
    – user58235
    Feb 28, 2020 at 6:28
  • Properly formatted as part of your question (not in the comment section)! Feb 28, 2020 at 6:32

1 Answer 1

1

I believe that you can simply change if (success == 0) to if iszero(success).

However, you may also try changing this:

if (success == 0) {
    invalid();
}
else {
    require(success);
}

To this:

if iszero(success) {
    revert(0, 0) // assuming that there are 2 fields in the `G1Point` structure
}
14
  • showing an error "expected token Lparen got identifier"
    – user58235
    Feb 28, 2020 at 6:46
  • @user58235: On which line of code? Feb 28, 2020 at 6:47
  • On iszero function. this line exactly: if iszero(success) {
    – user58235
    Feb 28, 2020 at 6:50
  • @user58235: Are you kidding me??? Where is the assembly from the original code? Why did you remove it in your question??? Feb 28, 2020 at 6:59
  • So are you trying to say that I should go like this? bool success; assembly { success = call(sub(gas, 2000), 6, 0, input, 0xc0, r, 0x60); // Use "invalid" to make gas estimation work //switch success case 0 { invalid() } //require(success); if iszero(success) { revert(0, 0) } }
    – user58235
    Feb 28, 2020 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.