Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Code

442 views
import hashlib from datetime import datetime bitmask = (2^(30*8) - 1); #internal state s_i = None; #additional input second = datetime.now().second; microsecond = datetime.now().microsecond; counter = 0; pid = os.getpid(); def Dual_EC_DRBG(P, Q, h_adin=0, s_0=None): global s_i; if(s_0 == None): s_0 = int(floor((2^16-1)*random())); if(s_i == None): s_i = s_0; t_i = s_i ^^ h_adin; s_i = (t_i*P)[0].lift(); r_i = (s_i*Q)[0].lift(); r_i = r_i & bitmask; return r_i; def Random_Generator(P, Q, byte, h_adin=0): global s_i; result = 0; req = (byte/30).ceil(); for i in range(req): if(i == 0): result = (result << (30*8)) | Dual_EC_DRBG(P, Q, h_adin) else: result = (result << (30*8)) | Dual_EC_DRBG(P, Q) s_i = (s_i*P)[0].lift(); result = result >> ((30*req - byte)*8) return result; def Get_H_Adin(): global second; global microsecond; global counter; global pid; second = datetime.now().second; microsecond = datetime.now().microsecond; counter = counter + 1; pid = os.getpid(); adin = (second << (12*8)) | (microsecond << (8*8)) | (counter << (4*8)) | pid; h = hashlib.sha256(); h.update(str(adin)); return int(h.hexdigest(), 16); #Curve P-256 p = 115792089210356248762697446949407573530086143415290314195533631308867097853951; n = 115792089210356248762697446949407573529996955224135760342422259061068512044369; b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b; Px = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296; Py = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5; Qx = 0xc97445f45cdef9f0d3e05e1e585fc297235b82b5be8ff3efca67c59852018192; Qy = 0xb28ef557ba31dfcbdd21ac46e2a91e3c304f44cb87058ada2cb815151e610046; # y^2 = x^3 - 3*x + b (mod p) curve = EllipticCurve(GF(p), [0, 0, 0, -3, b]); print curve; P = curve(Px, Py); Q = curve(Qx, Qy); print "P = ", P; print "Q = ", Q; h_adin = Get_H_Adin(); print "h_adin = ", hex(h_adin); %time r = Random_Generator(P, Q, 32, h_adin); print "r = ", hex(r);
Elliptic Curve defined by y^2 = x^3 + 115792089210356248762697446949407573530086143415290314195533631308867097853948*x + 41058363725152142129326129780047268409114441015993725554835256314039467401291 over Finite Field of size 115792089210356248762697446949407573530086143415290314195533631308867097853951 P = (48439561293906451759052585252797914202762949526041747995844080717082404635286 : 36134250956749795798585127919587881956611106672985015071877198253568414405109 : 1) Q = (91120319633256209954638481795610364441930342474826146651283703640232629993874 : 80764272623998874743522585409326200078679332703816718187804498579075161456710 : 1) h_adin = 0x9ac4e66ef19a764e7e709739740a742f33ad715919f230b0068ac7eb945cf634L CPU time: 0.08 s, Wall time: 0.09 s r = 5627876d4b7ae8e27316e8b30389a234d6a8c41104b8dcbae93b4b4ca209e2a0