starkbank / ecdsa-dotnet

A lightweight and fast pure C# ECDSA library
https://starkbank.com
Other
49 stars 20 forks source link

Support Curves.AddCurve() to add other curves dynamically #28

Closed softwarekamal closed 2 years ago

softwarekamal commented 2 years ago

Hello starbank, @rcmstark, and other lovely team. Your project are very awesome and strong enough. Please make more flexibility to allow us to add custom curves or standard one easily to library via Curves.AddCurve() method or something, So we can initialize all curves only once at runtime.

For example: Someone need to use Ed25519, Curve25519 from https://neuromancer.sk/std/other/Ed25519

He will need to clone github project to modify starbank/ecdsa-dotnet, The Curves.cs

image

There's tons of curves that we need to play with it: image

Please accept my apologies my dear :)

softwarekamal commented 2 years ago

Am also try to inject the default selected curve. It worked. But library doesn't support P1363 signature format.

There's always extra 6-7 bytes!, Will you support P1363 sign,verify? Its like Bouncy Castle SHA256WithPlain-ECDSA

        BigInteger P = EllipticCurve.Utils.BinaryAscii.numberFromHex("fffffffffffffffffffffffffffffffeffffffffffffffff");
        BigInteger A = EllipticCurve.Utils.BinaryAscii.numberFromHex("fffffffffffffffffffffffffffffffefffffffffffffffc");
        BigInteger B = EllipticCurve.Utils.BinaryAscii.numberFromHex("64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1");
        BigInteger Gx = EllipticCurve.Utils.BinaryAscii.numberFromHex("188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012");
        BigInteger Gy = EllipticCurve.Utils.BinaryAscii.numberFromHex("07192b95ffc8da78631011ed6b24cdd573f977a11e794811");
        BigInteger N = EllipticCurve.Utils.BinaryAscii.numberFromHex("ffffffffffffffffffffffff99def836146bc9b1b4d22831");
        BigInteger H = EllipticCurve.Utils.BinaryAscii.numberFromHex("1");  // I don't find H parameter....
        EllipticCurve.Curves.secp256k1 = new EllipticCurve.CurveFp(A, B, P, N, Gx, Gy, EllipticCurve.Curves.secp256k1.name, new int[] { 1, 0 });

        EllipticCurve.PrivateKey privateKey = new EllipticCurve.PrivateKey();   // Generate random key
        EllipticCurve.PublicKey publicKey = privateKey.publicKey();

        var signature = EllipticCurve.Ecdsa.sign("hello dear", privateKey);
        var sigInBytes = signature.toDer();
        var isVerify = EllipticCurve.Ecdsa.verify("hello dear", signature, publicKey);

Also where is Cofactor (H) value in CurveFpclass?

leandro-stark commented 2 years ago

Hi, @softwarekamal!

Currently our ECDSA only allows you to add more curves by forking or monkey patching it to manually add the missing curves to the CurveFp class in curve.cs. While we do plan to implement the function to add curves dynamically, it might be some time before we can prioritize this...

Also, the current implementation assumes all curve cofactors are 1. Therefore, they have been omitted from the code. We do not support cofactors that are different from 1 right now.

In case you cannot wait for these features, we'd be glad to take a look at any PRs you make to our library to include them. In that case, please try to follow our current commit pattern and code style, so we can more quickly include any features on the main branch.

If you have any other questions, please do not hesitate to ask!

Best,