Coder Social home page Coder Social logo

ecsol's People

Contributors

fulldecent avatar jbaylina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ecsol's Issues

Bug in EC points addition

04
af80b90d25145da28c583359beb47b21796b2fe1a23c1511e443e7a64dfdb27d
7434c380f0aa4c500e220aa1a9d068514b1ff4d5019e624e7ba1efe82b340a59
+
04
310958696132fdb8c276d755d40280c72107adcc9fc5c854e5384a1e57144320
77976693b8c4fa28b876c8e9dd5a66e3f6fe660538fdf5057ce9587bb7740f3c

Your _jAdd gives:

04
e9263029255cc76d19b497888879cafe8c081bcfd5946e7a37f14b2fed2d1fef
f7e5bc9eef682baad54c6a9265650b235011d0ac08f137b5c51edecb0716bfb4

But right answer is:

04
f3b45b265230a38684b1b623cf67a2ba108f14a8b9474aeac2bd48bcabca483b
3a2b5e15542c479d3a33aea1c7c53a3d7acba44fb1ab69420fc0312d177c216a

My code produces right answer, but consumes +20K gas:

function addXY(uint256 x1, uint256 y1, uint256 x2, uint256 y2) public pure returns(uint256 x3, uint256 y3) {
    uint256 anti = invmod(submod(x1, x2, m), m);
    uint256 alpha = mulmod(submod(y1, y2, m), anti, m);
    x3 = submod(submod(mulmod(alpha, alpha, m), x1, m), x2, m);
    y3 = submod(mulmod(alpha, submod(x2, x3, m), m), y2, m);
}

function submod(uint256 a, uint256 b, uint256 p) public pure returns (uint256) {
    return addmod(a, p - b, p);
}

function invmod(uint256 a, uint256 p) public pure returns (uint256) {
    int256 t1 = 0;
    int256 t2 = 1;
    uint256 r1 = p;
    uint256 r2 = a;
    uint256 q;
    while (r2 != 0) {
        q = r1 / r2;
        (t1, t2, r1, r2) = (t2, t1 - int(q) * t2, r2, r1 - q * r2);
    }

    return t1 < 0 ? p - uint256(-t1) : uint256(t1);
}

Your current result point is not even valid for the EC curve: y^2 = x^3 + 7 (mod m)

You can also verify this example in online EC summator: https://gobittest.appspot.com/VanitySum

Please help me to understand

  1. Describe a bit why you have _jAdd and _ecAdd?
  2. Why are computations so complicated? Are they optimized?

I am going to implement my own, as I think simpler solution:

function add(uint256 x1, uint256 y1, uint256 x2, uint256 y2) public pure returns(uint256 x3, uint256 y3) {
    uint256 m = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f;
    uint256 anti = invmod(submod(x2, x1, m), m);
    uint256 alpha = mulmod(submod(y2, y1, m), anti, m);
    x3 = submod(submod(mulmod(alpha, alpha, m), x2, m), x1, m);
    y3 = submod(mulmod(alpha, submod(x1, x3, m), m), y1, m);
}

function mul(uint256 x1, uint256 y1, uint256 privateKey) public pure returns(uint256 x3, uint256 y3) {
    for (uint i = 0; i < 256; i++) {
        if (((privateKey >> i) & 1) == 1) {
            if (x3 == 0 && y3 == 0) {
                (x3,y3) = (x1,y1);
            }
            else {
                (x3,y3) = addXY(x3,y3, x1,y1);
            }
        }
        (x1,y1) = addXY(x1,y1, x1,y1);
    }
}

function publicKey(uint256 privateKey) public pure returns(uint256 x, uint256 y) {
    uint256 gx = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798;
    uint256 gy = 0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8;
    return mul(gx, gy, privateKey);
}

Method add has a few tests and works fine, but publicKey gives the wrong result. Can you tell me why it is wrong and is it true, that your complicated solution has better performance (require less gas)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.