forked from ctz/keccak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
71 lines (61 loc) · 4.95 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from keccak import *
def test_permutation():
s = KeccakState(576, 1600)
s.set_bytes(s.get_bytes()) # must round trip
keccak_f(s)
state_after_rounds_on_zero = """
E7 DD E1 40 79 8F 25 F1 8A 47 C0 33 F9 CC D5 84 EE A9 5A A6 1E 26 98 D5 4D 49 80 6F 30 47 15 BD 57 D0 53 62 05 4E 28 8B D4 6F 8E 7F 2D A4 97 FF C4 47 46 A4 A0 E5 FE 90 76 2E 19 D6 0C DA 5B 8C 9C 05 19 1B F7 A6 30 AD 64 FC 8F D0 B7 5A 93 30 35 D6 17 23 3F A9 5A EB 03 21 71 0D 26 E6 A6 A9 5F 55 CF DB 16 7C A5 81 26 C8 47 03 CD 31 B8 43 9F 56 A5 11 1A 2F F2 01 61 AE D9 21 5A 63 E5 05 F2 70 C9 8C F2 FE BE 64 11 66 C4 7B 95 70 36 61 CB 0E D0 4F 55 5A 7C B8 C8 32 CF 1C 8A E8 3E 8C 14 26 3A AE 22 79 0C 94 E4 09 C5 A2 24 F9 41 18 C2 65 04 E7 26 35 F5 16 3B A1 30 7F E9 44 F6 75 49 A2 EC 5C 7B FF F1 EA
""".replace(' ', '').replace('\n', '').lower()
assert KeccakState.bytes2str(s.get_bytes()).encode('hex') == state_after_rounds_on_zero
s.set_bytes(s.get_bytes()) # must round trip
keccak_f(s)
state_after_rounds_on_previous = """
3C CB 6E F9 4D 95 5C 2D 6D B5 57 70 D0 2C 33 6A 6C 6B D7 70 12 8D 3D 09 94 D0 69 55 B2 D9 20 8A 56 F1 E7 E5 99 4F 9C 4F 38 FB 65 DA A2 B9 57 F9 0D AF 75 12 AE 3D 77 85 F7 10 D8 C3 47 F2 F4 FA 59 87 9A F7 E6 9E 1B 1F 25 B4 98 EE 0F CC FE E4 A1 68 CE B9 B6 61 CE 68 4F 97 8F BA C4 66 EA DE F5 B1 AF 6E 83 3D C4 33 D9 DB 19 27 04 54 06 E0 65 12 83 09 F0 A9 F8 7C 43 47 17 BF A6 49 54 FD 40 4B 99 D8 33 AD DD 97 74 E7 0B 5D FC D5 EA 48 3C B0 B7 55 EE C8 B8 E3 E9 42 9E 64 6E 22 A0 91 7B DD BA E7 29 31 0E 90 E8 CC A3 FA C5 9E 2A 20 B6 3D 1C 4E 46 02 34 5B 59 10 4C A4 62 4E 9F 60 5C BF 8F 6A D2 6C D0 20
""".replace(' ', '').replace('\n', '').lower()
assert KeccakState.bytes2str(s.get_bytes()).encode('hex') == state_after_rounds_on_previous
def test_ror_rol():
assert rol(0b11010010, 5, 8) == 0b01011010
assert rol(0b10000000, 0, 8) == 0b10000000
assert rol(0b10000000, 7, 8) == 0b01000000
assert rol(0b10000000, 8, 8) == 0b10000000
assert rol(0b00010111, 3, 8) == 0b10111000
assert ror(0b00010111, 3, 8) == 0b11100010
def test_sponge():
s = KeccakSponge(1024, 1600, multirate_padding, keccak_f)
s.absorb('cc'.decode('hex'))
s.absorb_final()
squeezed = """
56B97029B479FF5DD15F17D12983E3B835BB0531D9B8D49B103B025CA53F991741298E961D1FAD00FC365C7761BFB278AE473980D612C1629E075A3FDBAE7F82B0F0AF54DF187F358852E19EA4347CF5CEEA676A1DCE3A47447E237FD74204F9A4B7F7C9CC7CC8B865B1D554E2F5F4A8EE17DBDDE7267894558A20972C9EB6CF5F62CE9151437718ED4AFF08FA76D803806E6CE47D229AAE839369E31888B26429E27BC3756021CB51498BCF2527D4BB04838BC1CEED9985A2A66FF8CB8C2D58B7099304E7F9622C583B093024A5FCDE2BE781474C159DF24D77D328C298F5766A8A0DBF7AE790A509CCF59E0CACD0ABF21492E0095A87ECDB55990093917AAA96D7F68B7B859B8094AEC0DDB6FB352A6CC1F007FA988ED764F5D6F21F9D8ADE9CE7ACA4DE6570DA39D9ACCEB46D2582FA4C4231DE0B736FB341041D24CFAE6C0761F43A2CF7383F38742579218AFCAB53D2E6816640DE05644D877558E965B1A28406999F31CCC43AC0B02BC5448B66AD3B6F8DE04C0E25845C8671B6F0594909A057F17FD06031707C8B4599889C994A35C193DBF84A7A0919CD054F67CEB7965F420D02DA3477EFC8B55413C241ADCF71CB10FE7E3E720B8C1736837B06E4B27461B71C6CAC892437530BBFE05CF426272F80F11709B9DB964F5DEDAB9E757C2F7A972B6A4C2443B03AD787AB1E243660BCED739157A434800696841ACEA4
""".lower().strip()
assert KeccakState.bytes2str(s.squeeze(512)).encode('hex') == squeezed
def test_hash():
pt = 'cc'.decode('hex')
h = Keccak512(pt).hexdigest()
k = Keccak512()
k.update(pt)
h2 = k.hexdigest()
assert h == '8630c13cbd066ea74bbe7fe468fec1dee10edc1254fb4c1b7c5fd69b646e44160b8ce01d05a0908ca790dfb080f4b513bc3b6225ece7a810371441a5ac666eb9'
assert h == h2
def test_multipart():
pt = '41fb'.decode('hex')
expect = '615ba367afdc35aac397bc7eb5d58d106a734b24986d5d978fefd62c'
h = Keccak224()
h.update(pt[0])
assert h.hexdigest() != expect
h.update(pt[1])
assert h.hexdigest() == expect
def test_long():
pt = '023D91AC532601C7CA3942D62827566D9268BB4276FCAA1AE927693A6961652676DBA09219A01B3D5ADFA12547A946E78F3C5C62DD880B02D2EEEB4B96636529C6B01120B23EFC49CCFB36B8497CD19767B53710A636683BC5E0E5C9534CFC004691E87D1BEE39B86B953572927BD668620EAB87836D9F3F8F28ACE41150776C0BC6657178EBF297FE1F7214EDD9F215FFB491B681B06AC2032D35E6FDF832A8B06056DA70D77F1E9B4D26AE712D8523C86F79250718405F91B0A87C725F2D3F52088965F887D8CF87206DFDE422386E58EDDA34DDE2783B3049B86917B4628027A05D4D1F429D2B49C4B1C898DDDCB82F343E145596DE11A54182F39F4718ECAE8F506BD9739F5CD5D5686D7FEFC834514CD1B2C91C33B381B45E2E5335D7A8720A8F17AFC8C2CB2BD88B14AA2DCA099B00AA575D0A0CCF099CDEC4870FB710D2680E60C48BFC291FF0CEF2EEBF9B36902E9FBA8C889BF6B4B9F5CE53A19B0D9399CD19D61BD08C0C2EC25E099959848E6A550CA7137B63F43138D7B651'.decode('hex')
expect = '230620d710cf3ab835059e1aa170735db17cae74b345765ff02e8d89'
h = Keccak224(pt).hexdigest()
assert h == expect
def run():
test_permutation()
test_ror_rol()
test_sponge()
test_hash()
test_multipart()
test_long()
print 'tests passed'
if __name__ == '__main__':
run()