Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble with certain curves #14

Closed
jand271 opened this issue Apr 17, 2020 · 3 comments
Closed

Trouble with certain curves #14

jand271 opened this issue Apr 17, 2020 · 3 comments

Comments

@jand271
Copy link

jand271 commented Apr 17, 2020

Hoping that I am missing something obvious. I actually want to use NIST-P192.

After running the following a couple of times (sometimes the key int is too large), I get the following results for the curves.

from ecpy.curves import Curve, Point
from ecpy.keys import ECPublicKey, ECPrivateKey
from ecpy.ecschnorr import ECSchnorr
import hashlib

import random

import sys
import ecpy


def testVerify(signer, msg, curve_string, bit_count):
    try:

        cv = Curve.get_curve(curve_string)
        pv_key = ECPrivateKey(random.getrandbits(bit_count), cv)
        pu_key = pv_key.get_public_key()
        sig = signer.sign(msg, pv_key)


        if signer.verify(msg, sig, pu_key):
            print('   Passed: ' + curve_string)
        else:
            print('FAILED: ' + curve_string)

    except:
        print(curve_string + ' threw')
        print(sys.exc_info()[1].value)



if __name__ == '__main__':


    print(sys.version)
    print('ECpy version: 1.2.1')

    curve_set = {'frp256v1': 256,
                 "secp521r1": 384,
                 "secp384r1": 384,
                 "secp256k1": 256,
                 "secp256r1": 256,
                 "secp224k1": 224,
                 "secp224r1": 224,
                 "secp192k1": 192,
                 "secp192r1": 192,
                 "secp160k1": 160,
                 "secp160r1": 160,
                 "secp160r2": 160,
                 "Brainpool-p512t1": 512,
                 "Brainpool-p512r1": 512,
                 "Brainpool-p384t1": 384,
                 "Brainpool-p384r1": 384,
                 "Brainpool-p320t1": 320,
                 "Brainpool-p320r1": 320,
                 "Brainpool-p256r1": 256,
                 "Brainpool-p256t1": 256,
                 "Brainpool-p224r1": 224,
                 "Brainpool-p224t1": 224,
                 "Brainpool-p192r1": 192,
                 "Brainpool-p192t1": 192,
                 "Brainpool-p160r1": 160,
                 "Brainpool-p160t1": 160,
                 "NIST-P256": 256,
                 "NIST-P224": 224,
                 "NIST-P192": 192,
                 "Ed448": 448,
                 "Ed25519": 25519,
                 "Curve448": 448,
                 "Curve25519": 25519}

    msg = int(0x616263)
    msg = msg.to_bytes(3, 'big')

    signer = ECSchnorr(hashlib.sha256)

    for curve, bit_length in curve_set.items():
        testVerify(signer, msg, curve, bit_length)

    print('Done')

Yields

3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
ECpy version: 1.2.1
   Passed: frp256v1
   Passed: secp521r1
   Passed: secp384r1
   Passed: secp256k1
   Passed: secp256r1
FAILED: secp224k1
FAILED: secp224r1
FAILED: secp192k1
   Passed: secp192r1
FAILED: secp160k1
FAILED: secp160r1
FAILED: secp160r2
   Passed: Brainpool-p512t1
   Passed: Brainpool-p512r1
   Passed: Brainpool-p384t1
   Passed: Brainpool-p384r1
   Passed: Brainpool-p320t1
   Passed: Brainpool-p320r1
   Passed: Brainpool-p256r1
   Passed: Brainpool-p256t1
FAILED: Brainpool-p224r1
Brainpool-p224t1 threw
Point not on curve
FAILED: Brainpool-p192r1
FAILED: Brainpool-p192t1
FAILED: Brainpool-p160r1
FAILED: Brainpool-p160t1
   Passed: NIST-P256
FAILED: NIST-P224
FAILED: NIST-P192
   Passed: Ed448
   Passed: Ed25519
   Passed: Curve448
   Passed: Curve25519
Done
@cslashm
Copy link
Owner

cslashm commented Apr 17, 2020

Hi, thanks for reporting.
I found the bug for the Schnorr verification failure. I will post a 1.2.2 in the coming days.

I do not understand the "Point not on curve" for Brainpool-p224t1. The point generator is rejected.
According python and the short Weierstrass equation y^2=x^3+a*x+b:

>>> p = 0x2DF271E14427A346910CF7A2E6CFA7B3F484E5C2CCE1C8B730E28B3F
>>> a = 0xD7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FC
>>> b = 0x4B337D934104CD7BEF271BF60CED1ED20DA14C08B3BB64F18A60888D
>>> x = 0x6AB1E344CE25FF3896424E7FFE14762ECB49F8928AC0C76029B4D580
>>> y = 0x374E9F5143E568CD23F3F4D7C0D4B1E41C8CC0D1C6ABD5F1A46DB4C
>>> left = (y*y)%p
>>> right = (x*x*x+a*x+b)%p
>>> hex(left)
'0x274e6e2e21da6f0102b6c9a84864c7a3160950f67747493e64961804'
>>> hex(right)
'0x1645c2378791b55b41505f5465bcd55faa8dddcdb091631f3d7c1f80'
>>>

So indeed the generator is not on curve which is so weird! I took the value here : https://tools.ietf.org/html/rfc5639#page-10:

 Curve-ID: brainpoolP224t1
Z = 2DF271E14427A346910CF7A2E6CFA7B3F484E5C2CCE1C8B730E28B3F
A = D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FC
B = 4B337D934104CD7BEF271BF60CED1ED20DA14C08B3BB64F18A60888D
x = 6AB1E344CE25FF3896424E7FFE14762ECB49F8928AC0C76029B4D580
y = 0374E9F5143E568CD23F3F4D7C0D4B1E41C8CC0D1C6ABD5F1A46DB4C
q = D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F
h = 1

Also note that in your snippet the line "secp521r1": 384, is wrong. bitsize is 521, not 384.
You should also reduce your private key modulo the curve order. (Maybe I should do it automagically or reject it if greater than order 🤔 )

@jand271
Copy link
Author

jand271 commented Apr 17, 2020 via email

@cslashm
Copy link
Owner

cslashm commented Apr 21, 2020

version 1.2.3 is out.
If it's ok for you, please close the bug.
Else, continue to report here ;)

@jand271 jand271 closed this as completed Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants