cbkhoo1492006

test_keccak256.py

Jul 2nd, 2018
404
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import binascii
  3. import ed25519
  4.  
  5. import sys
  6. import hashlib
  7. if sys.version_info < (3, 6):
  8.     import sha3
  9.  
  10. import utils
  11.  
  12. def hexToInt(h):
  13.     s = binascii.unhexlify(h) #does hex to bytes
  14.     bb = len(h) * 4 #I guess 8 bits / b
  15.     return sum(2**i * ed25519.bit(s,i) for i in range(0,bb)) #does to int
  16.  
  17. def intToHex(i):
  18.     return binascii.hexlify(ed25519.encodeint(i)) #hexlify does bytes to hex
  19.  
  20.  
  21. b = 256
  22. q = 2**255 - 19
  23. l = 2**252 + 27742317777372353535851937790883648493
  24.  
  25.  
  26. def sc_reduce32(key):
  27.     keyint = int(key,16)
  28.     #print keyint
  29.     #print q
  30.     newdata = (keyint % q)
  31.     return newdata
  32.  
  33. """
  34. _hexStrToInt = utils.hexStrToInt
  35. _intToHexStr = utils.intToHexStr
  36. """
  37.  
  38. """
  39. def from_bytes (data, big_endian):
  40.    if isinstance(data, str):
  41.        data = bytearray(data)
  42.    if big_endian=='big':
  43.        data = reversed(data)
  44.    num = 0
  45.    for offset, byte in enumerate(data):
  46.        num += byte << (offset * 8)
  47.    return num
  48.  
  49.  
  50. def to_bytes(n, length, byteorder):
  51.    h = '%x' % n
  52.    s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
  53.     return s if byteorder == 'big' else s[::-1]
  54. """
  55. """
  56. def sc_reduce32(n):
  57.    n = from_bytes(n, 'little')
  58.    l = (2**252 + 27742317777372353535851937790883648493)
  59.    reduced = n % l
  60.    print reduced, "herw"
  61.    newbytes = to_bytes(reduced,32, 'big')
  62.    return binascii.hexlify(newbytes)
  63. """
  64.  
  65. """
  66. # This is an attempt to make a real version of sc_reduce32,
  67. # to match exactly the behavior in in:
  68. # https://github.com/monero-project/monero/blob/2846d0850d6e92d38c44eac9e6a4fca6b1545453/src/crypto/crypto-ops.c
  69.  
  70.  
  71. def loadN(b, n, index):
  72.    base = 0;
  73.    result = 0
  74.    for i in range(n):
  75.        byte = b[index+i]
  76.        result |= (byte << base)
  77.        base += 8
  78.    return result;
  79.  
  80. def sc_reduce32(b):
  81.    s0 = 2097151 & loadN(b, 3, 0)
  82.    s1 = 2097151 & (loadN(b, 4, 2) >> 5)
  83.    s2 = 2097151 & (loadN(b, 3, 5) >> 2)
  84.    s3 = 2097151 & (loadN(b, 4, 7) >> 7)
  85.    s4 = 2097151 & (loadN(b, 4, 10) >> 4)
  86.    s5 = 2097151 & (loadN(b, 3, 13) >> 1)
  87.    s6 = 2097151 & (loadN(b, 4, 15) >> 6)
  88.    s7 = 2097151 & (loadN(b, 3, 18) >> 3)
  89.    s8 = 2097151 & loadN(b, 3, 21)
  90.    s9 = 2097151 & (loadN(b, 4, 23) >> 5)
  91.    s10 = 2097151 & (loadN(b, 3, 26) >> 2)
  92.    s11 = (loadN(b, 4, 28) >> 7)
  93.    s12 = 0
  94.  
  95.    carry0 = (s0 + (1<<20)) >> 21; s1 += carry0; s0 -= carry0 << 21;
  96.    carry2 = (s2 + (1<<20)) >> 21; s3 += carry2; s2 -= carry2 << 21;
  97.    carry4 = (s4 + (1<<20)) >> 21; s5 += carry4; s4 -= carry4 << 21;
  98.    carry6 = (s6 + (1<<20)) >> 21; s7 += carry6; s6 -= carry6 << 21;
  99.    carry8 = (s8 + (1<<20)) >> 21; s9 += carry8; s8 -= carry8 << 21;
  100.    carry10 = (s10 + (1<<20)) >> 21; s11 += carry10; s10 -= carry10 << 21;
  101.  
  102.    carry1 = (s1 + (1<<20)) >> 21; s2 += carry1; s1 -= carry1 << 21;
  103.    carry3 = (s3 + (1<<20)) >> 21; s4 += carry3; s3 -= carry3 << 21;
  104.    carry5 = (s5 + (1<<20)) >> 21; s6 += carry5; s5 -= carry5 << 21;
  105.    carry7 = (s7 + (1<<20)) >> 21; s8 += carry7; s7 -= carry7 << 21;
  106.    carry9 = (s9 + (1<<20)) >> 21; s10 += carry9; s9 -= carry9 << 21;
  107.    carry11 = (s11 + (1<<20)) >> 21; s12 += carry11; s11 -= carry11 << 21;
  108.  
  109.    s0 += s12 * 666643;
  110.    s1 += s12 * 470296;
  111.    s2 += s12 * 654183;
  112.    s3 -= s12 * 997805;
  113.    s4 += s12 * 136657;
  114.    s5 -= s12 * 683901;
  115.    s12 = 0;
  116.  
  117.    carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
  118.    carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
  119.    carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
  120.    carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
  121.    carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
  122.    carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
  123.    carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
  124.    carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
  125.    carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
  126.    carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
  127.    carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
  128.    carry11 = s11 >> 21; s12 += carry11; s11 -= carry11 << 21;
  129.  
  130.    s0 += s12 * 666643;
  131.    s1 += s12 * 470296;
  132.    s2 += s12 * 654183;
  133.    s3 -= s12 * 997805;
  134.    s4 += s12 * 136657;
  135.    s5 -= s12 * 683901;
  136.  
  137.    carry0 = s0 >> 21; s1 += carry0; s0 -= carry0 << 21;
  138.    carry1 = s1 >> 21; s2 += carry1; s1 -= carry1 << 21;
  139.    carry2 = s2 >> 21; s3 += carry2; s2 -= carry2 << 21;
  140.    carry3 = s3 >> 21; s4 += carry3; s3 -= carry3 << 21;
  141.    carry4 = s4 >> 21; s5 += carry4; s4 -= carry4 << 21;
  142.    carry5 = s5 >> 21; s6 += carry5; s5 -= carry5 << 21;
  143.    carry6 = s6 >> 21; s7 += carry6; s6 -= carry6 << 21;
  144.    carry7 = s7 >> 21; s8 += carry7; s7 -= carry7 << 21;
  145.    carry8 = s8 >> 21; s9 += carry8; s8 -= carry8 << 21;
  146.    carry9 = s9 >> 21; s10 += carry9; s9 -= carry9 << 21;
  147.    carry10 = s10 >> 21; s11 += carry10; s10 -= carry10 << 21;
  148.    s = []
  149.  
  150.    myint = 0
  151.  
  152.    items = []
  153.    items.append(s0 >> 0)
  154.    items.append(s0 >> 8)
  155.    items.append((s0 >> 16) | (s1 << 5))
  156.    items.append(s1 >> 3)
  157.    items.append(s1 >> 11)
  158.    items.append((s1 >> 19) | (s2 << 2))
  159.    items.append(s2 >> 6)
  160.    items.append((s2 >> 14) | (s3 << 7))
  161.    items.append(s3 >> 1)
  162.    items.append(s3 >> 9)
  163.    items.append((s3 >> 17) | (s4 << 4))
  164.    items.append(s4 >> 4)
  165.    items.append(s4 >> 12)
  166.    items.append((s4 >> 20) | (s5 << 1))
  167.    items.append(s5 >> 7)
  168.    items.append((s5 >> 15) | (s6 << 6))
  169.    items.append(s6 >> 2)
  170.    items.append(s6 >> 10)
  171.    items.append((s6 >> 18) | (s7 << 3))
  172.    items.append(s7 >> 5)
  173.    items.append(s7 >> 13)
  174.    items.append(s8 >> 0)
  175.    items.append(s8 >> 8)
  176.    items.append((s8 >> 16) | (s9 << 5))
  177.    items.append(s9 >> 3)
  178.    items.append(s9 >> 11)
  179.    items.append((s9 >> 19) | (s10 << 2))
  180.    items.append(s10 >> 6)
  181.    items.append((s10 >> 14) | (s11 << 7))
  182.    items.append(s11 >> 1)
  183.    items.append(s11 >> 9)
  184.    items.append(s11 >> 17)
  185.  
  186.    newb = b''
  187.    for i in items:
  188.        tmp = to_bytes(i,(i.bit_length() + 7) // 8,byteorder='little')
  189.  
  190.        tmp = i.to_bytes((i.bit_length() + 7) // 8, byteorder='little')
  191.  
  192.        localnewbyte = bytes([tmp[0]])
  193.        newb += localnewbyte
  194.    return newb
  195. """
  196. """
  197. s = hashlib.sha3_512()
  198. print s.name,"1"
  199.  
  200. print s.digest_size,"2"
  201. s.update(b"data")
  202. print s.hexdigest(),"3"
  203.  
  204. s = hashlib.shake_256()
  205. s.update(b"data")
  206. print s.hexdigest(4),"4"
  207.  
  208. print s.hexdigest(8),"5"
  209.  
  210. print s.hexdigest(16),"6"
  211. """
  212.  
  213. import sha3
  214. k = sha3.keccak_256()
  215. print "keccak_256 = ", k
  216. hexadecimal_seed="852249bca4446e65501cc7f8338027ec"
  217. print type(hexadecimal_seed), ", Hexadecimal seed = ", hexadecimal_seed
  218.  
  219. """
  220. print int(ss, 16)
  221. sss="%x" %int(ss,16)
  222. print sss
  223. """
  224. hexadecimal_seed_byte=bytearray.fromhex(hexadecimal_seed)
  225. print type(hexadecimal_seed_byte), ",Hexadecimal Seed in byte = ", hexadecimal_seed_byte
  226. k.update(hexadecimal_seed_byte)
  227. """
  228. print k.hexdigest(),"8"
  229. """
  230.  
  231. hash_of_seed = k.hexdigest()
  232. print type(hash_of_seed), "Hash of Seed = ", hash_of_seed
  233. print "Length of Hash of Seed = ", len(hash_of_seed)
  234.  
  235. """
  236. print len(private_spend_key_bf)
  237. """
  238. print "Convert Hash of Seed in Integer = ", int(hash_of_seed,16)
  239. hexadecimal_hash_of_seed_byte=bytearray(hash_of_seed)
  240. print "Convert and stored Hash of Seed in bytearray = ", hexadecimal_hash_of_seed_byte
  241.  
  242. private_spend_key = sc_reduce32(hash_of_seed)
  243. #print binascii.hexlify(private_spend_key),"private_spend_key"
  244. print type(private_spend_key), "Private Spend Key in long = ", private_spend_key
  245. private_spend_key_hex = "%x" %int(private_spend_key)
  246. print type(private_spend_key_hex), "Private Spend Key in Hex = ", private_spend_key_hex
  247.  
  248. """
  249. private_spend_key = int(hash_of_seed,16) % l
  250. """
  251. """
  252. print private_spend_key,10
  253. print sc_reduce32(private_spend_key)
  254. print len(private_spend_key)
  255. """
  256. """
  257. print binascii.hexlify(kk),"10"
  258. """
RAW Paste Data