Title: Adding points on an Elliptic curve Post by: redPanda on March 24, 2015, 07:24:42 PM I want to calculate the public key from the private key.
I can add points on an Elliptic curve with small p, but I have a problem when I want to use large numbers. Here is the description of the problem: https://i.imgur.com/IIxZcEI.png Title: Re: Adding points on an Elliptic curve Post by: TierNolan on March 24, 2015, 08:53:41 PM You need to multiply both sides by the inverse of x3.
What language are you using? There is probably a function for computing the modular inverse. The normal algorithm used is the Extended Euclidean algorithm. If you just have a way to handle large numbers, you could use the following formula: x_inv mod p = xp-2 mod p Lookup "modular exponentiation". Title: Re: Adding points on an Elliptic curve Post by: redPanda on March 25, 2015, 01:38:30 PM I'm using c++ with gmp for large numbers.
I have all the tools now. Thanks :-) Title: Re: Adding points on an Elliptic curve Post by: redPanda on March 27, 2015, 03:34:48 AM I implemented a solution using the Extended Euclidien algorithm,
I have the right results with small numbers but not with large numbers Here is my solution: https://i.imgur.com/Oo9a9LN.png I can add any points whitout problems, but with large numbers, using http://www.royalforkblog.com/2014/09/04/ecc/, I get Code: WRONG Code: OK (?) http://chimera.labs.oreilly.com/books/1234000001802/ch04.html#public_key_derivation Can you see the problem in my code (below) or at least can somebody give me the result of the addition of 2 points: 1 * G + 1 * G with Bitcoin parameters, then I will be able to track my code because now I have the result of a very large number of additions. Code: // g++ ellipticCurve.cpp -lgmpxx -lgmp -std=c++11 Title: Re: Adding points on an Elliptic curve Post by: gmaxwell on March 27, 2015, 10:41:59 AM I see at least one error in your addition implementation (I can point it out if you really want, but I think if your goal is to learn, you will learn more if I don't; if your goal is to get something working I suggest rethinking your goal)..., What are you trying to accomplish? If you want to make an implementation to actually use your approach (kludging together a result from semi-tech popular press books, rather than stepping back and understanding first principles) will be horrifyingly slow and likely end up broken or insecure.
If you're just trying to learn, you should probably step back and work on each component one at a time so you know exactly where your error is, and so you can gain some understanding. E.g. write tests for your field multiplies, field adds, write a test for your modular inverse (try lots of numbers, invert and multiply by itself to check the identity). You don't necessarily have to have the answers to check against... check the algebraic identities. e.g., for points: A = G + G B = A + G C = B + G D = C + G B == D + -A D == B + A A == C + -A A + C == B + B Infinity = C + -C. A + D + -A == -D + D + D ... and so on. Though it's perfectly possible to build a correct implementation with no known value vectors; if you really want them I would suggest I suggest installing (or getting web access to) sage (sagemath), as it has a fine implementation of elliptic curves on finite fields and can easily be used as a 'pocket calculator' to check your results: E.g. sage: F = FiniteField (0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F) sage: C = EllipticCurve ([F (0), F (7)]) sage: G = C.lift_x(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798) sage: G ( 55066263022277343669578718895168534326250603453777594175500187360389116729240 : 32670510020758816978083085130507043184471273380659243275938904335757337482424 : 1) sage: G+G ( 89565891926547004231252920425935692360644145829622209833684329913297188986597 : 12158399299693830322967808612713398636155367887041628176798871954788371653930 : 1) sage: G+G+G (112711660439710606056748659173929673102114977341539408544630613555209775888121 : 25583027980570883691656905877401976406448868254816295069919888960541586679410 : 1) sage: 8675309*G ( 66641067246008511739397675128206923493293851901978595085468284019495272794983 : 22882405661336615738255795181502754819791112635438119114432507482219105379189 : 1) sage: 8675309*G*0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72 (102906664656524967437285391368203362606397786797828404077808951036579554576623 : 22882405661336615738255795181502754819791112635438119114432507482219105379189 : 1) sage: 8675309*G*0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72^2 ( 62036446572098911670458903520965529606848330631474128915637932959742841971720 : 22882405661336615738255795181502754819791112635438119114432507482219105379189 : 1) sage: 8675309*G*0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72^3 ( 66641067246008511739397675128206923493293851901978595085468284019495272794983 : 22882405661336615738255795181502754819791112635438119114432507482219105379189 : 1) Though there are other high quality implementations of secp256k1 in C, ... since you seem comfortable using a C derived language, why not just use another implementation to get your test cases? Title: Re: Adding points on an Elliptic curve Post by: redPanda on March 29, 2015, 01:55:11 AM My goal is to understand the first principles.
Once I will be able to reproduce the results, I will no longer need to use my code but instead I will use the code implemented in bitcoin. As a physicist, I use a lot of math (continuous functions) but I’m not a mathematician. So I don’t want to become a cryptographer but the question is how far should I step back? For the next days, I will write test cases for extended euclidean algorithm, adding points on EC and my add and multiply algorithm to figure out where is my error. Sage works perfectly :-) Title: Re: Adding points on an Elliptic curve Post by: redPanda on April 01, 2015, 02:41:27 AM OK I finally found 2 errors:
1) In my code I cannot have a negative value for the denominator of the slope. I just have to multiply both the numerator and the denominator by -1 2) I use as p the value given by royalforkblog.com and this is not the value of p but instead the order of the curve (the number of points on the curve). The correct value is given by gmaxwell in a previous post. Now it works ! ;D Last (optional) question: for an elliptique curve, the order of the curve and the prime number p should be the same only if p mod 4 = 3 I try 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F mod 4 = 115792089237316195423570985008687907853269984665640564039457584007908834671663 mod 4 and it is equal to 3 ! So they should be the same ? |