Permalink
Browse files

K12 ref code now works with both Python 2 and 3 (thanks to David Wong)

  • Loading branch information...
gvanas committed May 18, 2017
1 parent 4799702 commit dc2c9d0792376cfc276067f59f6f5acb2577ac24
Showing with 14 additions and 12 deletions.
  1. +4 −3 Standalone/KangarooTwelve-reference/K12-test.py
  2. +10 −9 Standalone/KangarooTwelve-reference/K12.py
@@ -12,6 +12,7 @@
# and related or neighboring rights to the source code in this file.
# http://creativecommons.org/publicdomain/zero/1.0/
from __future__ import print_function
import K12
def generateSimpleRawMaterial(length, seed1, seed2):
@@ -82,12 +83,12 @@ def printTestVectors():
outputHex(K12.KangarooTwelve(b'', b'', 10032)[10000:])
for i in range(7):
C = b''
M = bytes([(j % 251) for j in range(17**i)])
M = bytearray([(j % 251) for j in range(17**i)])
print("KangarooTwelve(M=pattern 0x00 to 0xFA for 17^{0:d} bytes, C=empty, 32 output bytes):".format(i))
outputHex(K12.KangarooTwelve(M, C, 32))
for i in range(4):
M = bytes([0xFF for j in range(2**i-1)])
C = bytes([(j % 251) for j in range(41**i)])
M = bytearray([0xFF for j in range(2**i-1)])
C = bytearray([(j % 251) for j in range(41**i)])
print("KangarooTwelve(M={0:d} times byte 0xFF, C=pattern 0x00 to 0xFA for 41^{1:d} bytes, 32 output bytes):".format(2**i-1, i))
outputHex(K12.KangarooTwelve(M, C, 32))
@@ -48,16 +48,16 @@ def load64(b):
return sum((b[i] << (8*i)) for i in range(8))
def store64(a):
return bytes((a >> (8*i)) % 256 for i in range(8))
return bytearray((a >> (8*i)) % 256 for i in range(8))
def KeccakP1600(state, nrRounds):
lanes = [[load64(state[8*(x+5*y):8*(x+5*y)+8]) for y in range(5)] for x in range(5)]
lanes = KeccakP1600onLanes(lanes, nrRounds)
state = b''.join([store64(lanes[x][y]) for y in range(5) for x in range(5)])
state = bytearray().join([store64(lanes[x][y]) for y in range(5) for x in range(5)])
return bytearray(state)
def F(inputBytes, delimitedSuffix, outputByteLen):
outputBytes = b''
outputBytes = bytearray()
state = bytearray([0 for i in range(200)])
rateInBytes = 1344//8
blockSize = 0
@@ -87,26 +87,27 @@ def F(inputBytes, delimitedSuffix, outputByteLen):
return outputBytes
def right_encode(x):
S = b''
S = bytearray()
while(x > 0):
S = bytes([x % 256]) + S
S = bytearray([x % 256]) + S
x = x//256
S = S + bytes([len(S)])
S = S + bytearray([len(S)])
return S
# inputMessage and customizationString must be of type byte string or byte array
def KangarooTwelve(inputMessage, customizationString, outputByteLen):
B = 8192
c = 256
S = inputMessage + customizationString + right_encode(len(customizationString))
S = bytearray(inputMessage) + bytearray(customizationString) + right_encode(len(customizationString))
# === Cut the input string into chunks of B bytes ===
n = (len(S)+B-1)//B
Si = [bytes(S[i*B:(i+1)*B]) for i in range(n)]
Si = [bytearray(S[i*B:(i+1)*B]) for i in range(n)]
if (n == 1):
# === Process the tree with only a final node ===
return F(Si[0], 0x07, outputByteLen)
else:
# === Process the tree with kangaroo hopping ===
CVi = [F(Si[i+1], 0x0B, c//8) for i in range(n-1)]
NodeStar = Si[0] + b'\x03\x00\x00\x00\x00\x00\x00\x00' + b''.join(CVi) \
NodeStar = Si[0] + bytearray([3,0,0,0,0,0,0,0]) + bytearray().join(CVi) \
+ right_encode(n-1) + b'\xFF\xFF'
return F(NodeStar, 0x06, outputByteLen)

0 comments on commit dc2c9d0

Please sign in to comment.