simplito / elliptic-php

Fast, general Elliptic Curve Cryptography library. Supports curves used in Bitcoin, Ethereum and other cryptocurrencies (secp256k1, ed25519, ..)
MIT License
215 stars 52 forks source link

Lib failed comparison agains trusty native Curve25519 #18

Open AsenOsen opened 4 years ago

AsenOsen commented 4 years ago

I`ve decided to compare basic calculation of public key based on private key against Curve25519:

$secureRandom = openssl_random_pseudo_bytes ( 32);
// native curve25519.so
$trusty_private = curve25519_private($secureRandom);
$trusty_public = curve25519_public($trusty_private);

// this library
$ec = new EC('curve25519');
$untrusty_keypair = $ec->keyFromPrivate(bin2hex($trusty_private));
$untrusty_public = $untrusty_keypair->getPublic(true, 'hex');

echo $untrusty_public . PHP_EOL;
echo bin2hex($trusty_public) . PHP_EOL;

and this check has failed:

6aa466621807d91ec5a6777544a6796fde5adf8b72a842b23c25f0c479f3aa71
d323bc8387b836dca1cc42fb8f53cb533ff5eaaf2461adc8ef698640bd614a4b
ldudzsim commented 4 years ago

In ECC public key is a point. In the Montgomery version of curve25519 in our lib we return public key in normalized form, it means that second coordinate is always 1, so only x is returned and it is returned in big endian order and that’s it. When you use the edward version of curve (ed25519) you get a standard formatted public key with the beginning 0x02/0x03 for compact and 0x04 for the full version. I don't know in what form your lib returns the public key, but it does not match with our lib for sure. Finally, I mention that our lib is port of elliptic-js, so you can also compare your lib with it, but the results will be similar.