jbaylina / ecsol Goto Github PK
View Code? Open in Web Editor NEWElliptic Curve Implementation in Solidity
License: GNU General Public License v3.0
Elliptic Curve Implementation in Solidity
License: GNU General Public License v3.0
I wish to have _ecSub
method, is it possible to build it upon _ecAdd
?
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
I have listed you here https://github.com/fulldecent/ethereum-libraries
Could you please submit a PR there to confirm the maturity level of this library?
Please publish a lower and upper bound for gas cost.
_jAdd
and _ecAdd
?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)?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.