indutny / elliptic

Fast Elliptic Curve Cryptography in plain javascript
1.68k stars 373 forks source link

Problem with Ed25519 #268

Open kot-begemot opened 2 years ago

kot-begemot commented 2 years ago

I wonder if I am doing everything correctly, but I can't quite get why I am keep receiving different points for ed25519 depending on the way I am multiplying points by scalar. I compared same setup it with secp256k1 curve of this lib and got exactly same points. Can someone explain if this me that that is missing something or this is some kind of bug. I haven't tried to verify this results with any other lib yet. Here is my code and the result output:

const BN = require('bn.js');
const _elliptic = require('elliptic');

let curve, x1, x2, x3;

const s1 = 5;
const s2 = 7;

curve = _elliptic.curves.secp256k1.curve;

x1 = curve.g.mul(new BN(s1 * s2)).x.toString();
x2 = curve.g.mul(new BN(s1)).mul(new BN(s2)).x.toString();
x3 = curve.g.mul(new BN(s2)).mul(new BN(s1)).x.toString();

console.log('Secp256k1');
console.log('x1', x1);
console.log('x2', x2);
console.log('x3', x3);

if ( x1 !== x2 || x2 !== x3 ) throw "Secp256k1 is not working";

curve = _elliptic.curves.ed25519.curve;

x1 = curve.g.mul(new BN(s1 * s2)).x.toString();
x2 = curve.g.mul(new BN(s1)).mul(new BN(s2)).x.toString();
x3 = curve.g.mul(new BN(s2)).mul(new BN(s1)).x.toString();

console.log('Ed25519');
console.log('x1', x1);
console.log('x2', x2);
console.log('x3', x3);

if ( x1 !== x2 || x2 !== x3 ) throw "Ed25519 is not working";

Output

Secp256k1
x1 43584328072464330665967763306297595761508151294385275883849271528835646125177
x2 43584328072464330665967763306297595761508151294385275883849271528835646125177
x3 43584328072464330665967763306297595761508151294385275883849271528835646125177
Ed25519
x1 55273943480971088995773479464066134207989308286022701864858601428259859655314
x2 51135981228776515221048757123224307152351546728503975207995010098383940295622
x3 22416939968309503026374457194513247018475167317131809640455674209551101296110

/home/elliptic.js:33
if ( x1 !== x2 || x2 !== x3 ) throw "Ed25519 is not working";
                              ^
Ed25519 is not working