Please help me to understand
k06a opened this issue · 3 comments
k06a commented
- Describe a bit why you have
_jAdd
and_ecAdd
? - 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)?
k06a commented
Looks like:
_jAdd(x1,z1, x2,z2)
is x1/z1 + x2/z2 => x3/z3
_ecAdd(x1,y1,z1, x2,y2,z2)
is (x1,y1)/z1 + (x2,y2)/z2 => (x3,y3)/z3
k06a commented
Also my method addXY
do not works fine for doubling (when (x1,y1) == (x2,y2)
), so line (x1,y1) = addXY(x1,y1, x1,y1);
of mul
method was incorrect.