From c201549eb8bcf0790a787dee66539510c4389811 Mon Sep 17 00:00:00 2001 From: Antonis Geralis <43617260+planetis-m@users.noreply.github.com> Date: Sun, 3 Apr 2022 10:49:06 +0300 Subject: [PATCH 1/3] Update elliptic.nim --- examples/elliptic.nim | 51 ++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/examples/elliptic.nim b/examples/elliptic.nim index 90e25a7..83cfc10 100644 --- a/examples/elliptic.nim +++ b/examples/elliptic.nim @@ -1,24 +1,24 @@ # By Cyther606: https://forum.nim-lang.org/t/522 # Adapted from: https://github.com/wobine/blackboard101/blob/master/EllipticCurvesPart4-PrivateKeyToPublicKey.py import bigints -import std/[math, strutils] +import std/[math, strformat] const - one = 1.initBigInt - two = 2.initBigInt - zero = 0.initBigInt + one = initBigInt(1) + two = initBigInt(2) + zero = initBigInt(0) proc `^`(base: int; exp: int): BigInt = pow(base.initBigInt, exp) # Specs of the Bitcoin's curve - secp256k1 -let +const primeCurve: BigInt = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - one - numberPoints = initBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) + numberPoints = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'bi Acurve = zero # with Bcurve = 7, coefficients in the elliptic curve equation y^2 = x^3 + Acurve * x + Bcurve - Gx = initBigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240") - Gy = initBigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424") + Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240'bi + Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424'bi Gpoint = (Gx, Gy) - privKey = initBigInt("A0DC65FFCA799873CBEA0AC274015B9526505DAAAED385155425F7337704883E", 16) + privKey = 0xA0DC65FFCA799873CBEA0AC274015B9526505DAAAED385155425F7337704883E'bi proc ecAdd(a: tuple, b: tuple): (BigInt, BigInt) = let @@ -29,7 +29,7 @@ proc ecAdd(a: tuple, b: tuple): (BigInt, BigInt) = proc ecDouble(a: tuple): (BigInt, BigInt) = var - lam = ((3.initBigInt * a[0] * a[0] + Acurve) * invmod(2.initBigInt * a[1], primeCurve)) + lam = (3.initBigInt * a[0] * a[0] + Acurve) * invmod(2.initBigInt * a[1], primeCurve) x = ((lam * lam) - (2.initBigInt * a[0])) mod primeCurve y = (lam * (a[0] - x) - a[1]) mod primeCurve lam = lam mod primeCurve @@ -50,20 +50,21 @@ proc ecMultiply(genPoint: tuple, scalarHex: BigInt): (BigInt, BigInt) = proc main() = let publicKey = ecMultiply(Gpoint, privKey) - echo "" - echo "******* Public Key Generation *********" - echo "" - echo "the private key: " - echo privKey - echo "" - echo "the uncompressed public key (not address):" - echo publicKey - echo "" - echo "the uncompressed public key (HEX):" - echo "04", publicKey[0].toString(base = 16).align(64, '0'), publicKey[1].toString(base = 16).align(64, '0') - echo "" - echo "the official Public Key - compressed:" - echo if publicKey[1] mod two == one: "03" & publicKey[0].toString(base = 16).align(64, '0') - else: "02" & publicKey[0].toString(base = 16).align(64, '0') + echo &""" +******* Public Key Generation ********* + +the private key: +{privKey} + +the uncompressed public key (not address): +{publicKey} + +the uncompressed public key (HEX): +04{publicKey[0].toString(base = 16):0>64}{publicKey[1].toString(base = 16):0>64} + +the official Public Key - compressed: +{(if publicKey[1] mod two == one: "03" & publicKey[0].toString(base = 16) + else: "02" & publicKey[0].toString(base = 16)):0>64} +""" main() From cb951e2633f0507946fd0958671def9c53696a68 Mon Sep 17 00:00:00 2001 From: Antonis Geralis <43617260+planetis-m@users.noreply.github.com> Date: Sun, 3 Apr 2022 19:47:31 +0300 Subject: [PATCH 2/3] drop 'bi, use more consts --- examples/elliptic.nim | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/elliptic.nim b/examples/elliptic.nim index 83cfc10..75a02ef 100644 --- a/examples/elliptic.nim +++ b/examples/elliptic.nim @@ -4,21 +4,22 @@ import bigints import std/[math, strformat] const - one = initBigInt(1) - two = initBigInt(2) - zero = initBigInt(0) + one = 1.initBigInt + two = 2.initBigInt + three = 3.initBigInt + zero = 0.initBigInt proc `^`(base: int; exp: int): BigInt = pow(base.initBigInt, exp) # Specs of the Bitcoin's curve - secp256k1 const primeCurve: BigInt = 2^256 - 2^32 - 2^9 - 2^8 - 2^7 - 2^6 - 2^4 - one - numberPoints = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'bi + numberPoints = initBigInt("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16) Acurve = zero # with Bcurve = 7, coefficients in the elliptic curve equation y^2 = x^3 + Acurve * x + Bcurve - Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240'bi - Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424'bi + Gx = initBigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240") + Gy = initBigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424") Gpoint = (Gx, Gy) - privKey = 0xA0DC65FFCA799873CBEA0AC274015B9526505DAAAED385155425F7337704883E'bi + privKey = initBigInt("A0DC65FFCA799873CBEA0AC274015B9526505DAAAED385155425F7337704883E", 16) proc ecAdd(a: tuple, b: tuple): (BigInt, BigInt) = let @@ -29,8 +30,8 @@ proc ecAdd(a: tuple, b: tuple): (BigInt, BigInt) = proc ecDouble(a: tuple): (BigInt, BigInt) = var - lam = (3.initBigInt * a[0] * a[0] + Acurve) * invmod(2.initBigInt * a[1], primeCurve) - x = ((lam * lam) - (2.initBigInt * a[0])) mod primeCurve + lam = (three * a[0] * a[0] + Acurve) * invmod(two * a[1], primeCurve) + x = ((lam * lam) - (two * a[0])) mod primeCurve y = (lam * (a[0] - x) - a[1]) mod primeCurve lam = lam mod primeCurve result = (x, y) From 29c83be7697ab9593a51a51a0aa35a550a1a950d Mon Sep 17 00:00:00 2001 From: Antonis Geralis <43617260+planetis-m@users.noreply.github.com> Date: Sat, 9 Apr 2022 11:16:46 +0300 Subject: [PATCH 3/3] fix compilation --- examples/elliptic.nim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/elliptic.nim b/examples/elliptic.nim index 75a02ef..e90d1ff 100644 --- a/examples/elliptic.nim +++ b/examples/elliptic.nim @@ -50,6 +50,9 @@ proc ecMultiply(genPoint: tuple, scalarHex: BigInt): (BigInt, BigInt) = proc main() = let publicKey = ecMultiply(Gpoint, privKey) + let officialKey = + if publicKey[1] mod two == one: "03" & publicKey[0].toString(base = 16) + else: "02" & publicKey[0].toString(base = 16) echo &""" ******* Public Key Generation ********* @@ -64,8 +67,7 @@ the uncompressed public key (HEX): 04{publicKey[0].toString(base = 16):0>64}{publicKey[1].toString(base = 16):0>64} the official Public Key - compressed: -{(if publicKey[1] mod two == one: "03" & publicKey[0].toString(base = 16) - else: "02" & publicKey[0].toString(base = 16)):0>64} +{officialKey:0>64} """ main()