cbkhoo1492006

test_keccak256_done.py

Jul 3rd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.65 KB | None | 0 0
  1. import sha3
  2. #import ed25519
  3. import binascii
  4. import base58 as _b58
  5.  
  6. b = 256
  7. q = 2**255 - 19
  8. l = 2 ** 252 + 27742317777372353535851937790883648493
  9.  
  10. def expmod(b,e,m):
  11.   if e == 0: return 1
  12.   t = expmod(b,e/2,m)**2 % m
  13.   if e & 1: t = (t*b) % m
  14.   return t
  15.  
  16. def inv(x):
  17.   return expmod(x,q-2,q)
  18.  
  19. d = -121665 * inv(121666)
  20.  
  21. def xrecover(y):
  22.   xx = (y*y-1) * inv(d*y*y+1)
  23.   x = expmod(xx,(q+3)/8,q)
  24.   if (x*x - xx) % q != 0: x = (x*I) % q
  25.   if x % 2 != 0: x = q-x
  26.   return x
  27.  
  28. By = 4 * inv(5)
  29. Bx = xrecover(By)
  30. B = [Bx % q,By % q]
  31.  
  32.  
  33. def reverse_byte_order(hex):
  34.     if(len(hex)%2==1): hex = '0' + hex
  35.     return "".join(reversed([hex[i:i+2] for i in range(0, len(hex), 2)]))
  36.  
  37. def sc_reduce32(key):
  38.     return reverse_byte_order("%x" % int((int(reverse_byte_order(key), 16) % l)))
  39.  
  40. def cn_fast_hash(hex):
  41.     k = sha3.keccak_256()
  42.     k.update(bytearray.fromhex(hex))
  43.     return k.hexdigest()
  44.  
  45. def bit(h,i):
  46.   return (ord(h[i/8]) >> (i%8)) & 1
  47.  
  48. def decodeint(s):
  49.   return sum(2**i * bit(s,i) for i in range(0,b))  
  50.  
  51. def edwards(P,Q):
  52.   x1 = P[0]
  53.   y1 = P[1]
  54.   x2 = Q[0]
  55.   y2 = Q[1]
  56.   x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
  57.   y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
  58.   return [x3 % q,y3 % q]
  59.  
  60. def scalarmult(P,e):
  61.   if e == 0: return [0,1]
  62.   Q = scalarmult(P,e/2)
  63.   Q = edwards(Q,Q)
  64.   if e & 1: Q = edwards(Q,P)
  65.   return Q
  66.  
  67. def encodepoint(P):
  68.   x = P[0]
  69.   y = P[1]
  70.   bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
  71.   return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b/8)])
  72.  
  73. def publickey(sk):
  74.   sk = binascii.unhexlify(sk)
  75.   a = decodeint(sk)
  76.   A = scalarmult(B,a)
  77.   return binascii.hexlify(encodepoint(A))
  78.  
  79. def encode_addr(version, publicSpendKey, publicViewKey):
  80.     '''Given address version and public spend and view keys, derive address.'''
  81.     data = version + publicSpendKey + publicViewKey
  82.     checksum = cn_fast_hash(data)
  83.     return _b58.encode(data + checksum[0:8])
  84.  
  85. hexadecimal_seed = "852249bca4446e65501cc7f8338027ec"
  86. hash_of_seed = cn_fast_hash(hexadecimal_seed)
  87. private_spend_key = sc_reduce32(hash_of_seed)
  88. public_spend_key = publickey(private_spend_key)
  89. private_view_key = sc_reduce32(cn_fast_hash(hash_of_seed))
  90. public_view_key = publickey(private_view_key)
  91. monero_public_address = encode_addr("12",public_spend_key,public_view_key)
  92.  
  93. print "Non-standard Seed = ", hexadecimal_seed
  94. print "Normalized Seed   = ", hash_of_seed
  95. print "Private Spend Key = ", private_spend_key
  96. print "Public Spend Key = ", public_spend_key
  97. print "Private View Key  = ", private_view_key
  98. print "Public View Key  = ", public_view_key
  99. print "Public Address = ", monero_public_address
Add Comment
Please, Sign In to add comment