-snip-
I have some functions in Python and it runs very slow compared to C.
The sage I want to do with the GPU is as follows
Pr = 115792089237316195423570985008687907853269984665640564039457584007908834671663
E = EllipticCurve (GF (P), [0,7])
N = E.order ()
G = E(55066263022277343669578718895168534326250603453777594175500187360389116729240,32670510020758816978083085130507043184471273380659243275938904335757337482424) # on E
T = E(26864879445837655118481716049217967286489564259939711339119540571911158650839,29571359081268663540055655726653840143920402820693420787986280659961264797165) # on E
numInt = 5646546546563131314723897429834729834798237429837498237498237489273948728934798237489723489723984729837489237498237498237498237498273493729847
numMod = numInt %N
numInv = pow(numMod ,N-2,N) # detail -> https://stackoverflow.com/questions/59234775/how-to-calculate-2-to-the-power-of-a-large-number-modulo-another-large-number
numMod * G
numMod * T
(T-G) * numInv
print (5*T)
print (2*G)
print (numMod * G)
print (numMod * (-G))
print (numMod * T)
print ((numMod-3) * (T-G))
Do you have any suggestions? What should I do ?
I wrote my question here because it is indirectly related to this project. Please forgive.
Hi! The slowest part in your python is inverse function. Try to implement gmpy2 inverse function (included in gmpy2) - it is C-based and very fast:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#gmpyYou can find the details here:
https://bitcointalksearch.org/topic/m.55214449Hello MrFreeDragon
Is it possible to write the python code you show with Cython?
Thanks again, my work was faster than before.
import gmpy2
modulo = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
order = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0X483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
PG = Point(Gx,Gy)
Z = Point(0,0) # zero-point, infinite in real x,y - plane
# return (g, x, y) a*x + b*y = gcd(x, y)
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, x, y = egcd(b % a, a)
return (g, y - (b // a) * x, x)
def modinv(m, n = modulo):
while m < 0:
m += n
g, x, _ = egcd(m, n)
if g == 1:
return x % n
else: print (' no inverse exist')
def mul2(Pmul2, p = modulo):
R = Point(0,0)
#c = 3*Pmul2.x*Pmul2.x*modinv(2*Pmul2.y, p) % p
c = 3*Pmul2.x*Pmul2.x*gmpy2.invert(2*Pmul2.y, p) % p
R.x = (c*c-2*Pmul2.x) % p
R.y = (c*(Pmul2.x - R.x)-Pmul2.y) % p
return R
def add(Padd, Q, p = modulo):
if Padd.x == Padd.y == 0: return Q
if Q.x == Q.y == 0: return Padd
if Padd == Q: return mul2(Q)
R = Point()
dx = (Q.x - Padd.x) % p
dy = (Q.y - Padd.y) % p
c = dy * gmpy2.invert(dx, p) % p
#c = dy * modinv(dx, p) % p
R.x = (c*c - Padd.x - Q.x) % p
R.y = (c*(Padd.x - R.x) - Padd.y) % p
return R # 6 sub, 3 mul, 1 inv
def mulk(k, Pmulk, p = modulo):
if k == 0: return Z
if k == 1: return Pmulk
if (k % 2 == 0): return mulk(k//2, mul2(Pmulk, p), p)
return add(Pmulk, mulk((k-1)//2, mul2(Pmulk, p), p), p)