bitmask = (2^(30*8) - 1);
s_i = None;
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):
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)
result = result >> ((30*req - byte)*8)
return result;
p = 115792089210356248762697446949407573530086143415290314195533631308867097853951;
n = 115792089210356248762697446949407573529996955224135760342422259061068512044369;
b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b;
Px = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296;
Py = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5;
Qx = 0xc97445f45cdef9f0d3e05e1e585fc297235b82b5be8ff3efca67c59852018192;
Qy = 0xb28ef557ba31dfcbdd21ac46e2a91e3c304f44cb87058ada2cb815151e610046;
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;
%time r = Random_Generator(P, Q, 32);
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)
CPU time: 0.07 s, Wall time: 0.08 s
r = 4aec86488209161eeff9990cc8c8b2ff77d3ca0c0646330aa90d5145f6ed3260