You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
11 KiB

  1. package keccak
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/ethereum/go-ethereum/crypto"
  6. qt "github.com/frankban/quicktest"
  7. )
  8. func TestKeccak(t *testing.T) {
  9. // 32 bytes input
  10. // 1
  11. testKeccak(t, []byte{116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  13. []byte{37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143, 65,
  14. 228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249,
  15. 210, 214, 116, 170, 85, 45, 21})
  16. // 2
  17. testKeccak(t, []byte{37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143,
  18. 65, 228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249, 210,
  19. 214, 116, 170, 85, 45, 21},
  20. []byte{182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73, 142, 67,
  21. 205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239, 68,
  22. 111, 116, 164, 127, 103, 141})
  23. // 3
  24. testKeccak(t, []byte{182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73,
  25. 142, 67, 205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239, 68,
  26. 111, 116, 164, 127, 103, 141},
  27. []byte{191, 235, 249, 254, 70, 24, 106, 244, 212, 163, 52, 240,
  28. 1, 128, 235, 61, 158, 52, 138, 60, 197, 80, 113, 36, 44, 217,
  29. 55, 211, 97, 231, 26, 7})
  30. // 4
  31. testKeccak(t, make([]byte, 32), []byte{41, 13, 236, 217, 84, 139, 98,
  32. 168, 214, 3, 69, 169, 136, 56, 111, 200, 75, 166, 188, 149, 72, 64, 8,
  33. 246, 54, 47, 147, 22, 14, 243, 229, 99})
  34. // variable input length
  35. testKeccak(t, []byte("test"), []byte{156, 34, 255, 95, 33, 240, 184,
  36. 27, 17, 62, 99, 247, 219, 109, 169, 79, 237, 239, 17, 178, 17, 155, 64,
  37. 136, 184, 150, 100, 251, 154, 60, 182, 88})
  38. // other
  39. testKeccak(t, make([]byte, 100), []byte{145, 63, 185, 225, 246, 241,
  40. 198, 217, 16, 253, 87, 74, 92, 173, 136, 87, 170, 67, 191, 186, 36,
  41. 228, 1, 173, 164, 245, 96, 144, 212, 217, 151, 167})
  42. }
  43. func testKeccak(t *testing.T, input, expected []byte) {
  44. // printBytes("in", input, false)
  45. goH := crypto.Keccak256(input)
  46. hBits := ComputeKeccak(bytesToBits(input))
  47. h := bitsToBytes(hBits)
  48. // printBytes("out", goH, false)
  49. qt.Assert(t, h, qt.DeepEquals, goH)
  50. qt.Assert(t, h, qt.DeepEquals, expected)
  51. }
  52. func TestPad(t *testing.T) {
  53. b := make([]byte, 32)
  54. for i := 0; i < len(b); i++ {
  55. b[i] = byte(i)
  56. }
  57. bBits := bytesToBits(b)
  58. fBits := pad(bBits)
  59. qt.Assert(t, bitsToBytes(fBits[:]), qt.DeepEquals,
  60. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  61. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  62. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  63. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  64. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  65. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  66. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128})
  67. }
  68. func TestKeccakfRound(t *testing.T) {
  69. s, _ := newS()
  70. s = keccakfRound(s, 0)
  71. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  72. []uint64{
  73. 26388279066651, 246290629787648, 26388279902208,
  74. 25165850, 246290605457408, 7784628352, 844424965783552,
  75. 2305843009213694083, 844432714760192,
  76. 2305843009249345539, 637534226, 14848, 641204224,
  77. 14354, 3670528, 6308236288, 2130304761856,
  78. 648518346341354496, 6309216256, 648520476645130240,
  79. 4611706359392501763, 792677514882318336,
  80. 20340965113972, 4611732197915754499,
  81. 792633534417207412})
  82. s = keccakfRound(s, 20)
  83. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  84. []uint64{17728382861289829725, 13654073086381141005,
  85. 9912591532945168756, 2030068283137172501, 5084683018496047808,
  86. 151244976540463006, 11718217461613725815, 11636071286320763433,
  87. 15039144509240642782, 11629028282864249197,
  88. 2594633730779457624, 14005558505838459171, 4612881094252610438,
  89. 2828009553220809993, 4838578484623267135, 1006588603063111352,
  90. 11109191860075454495, 1187545859779038208,
  91. 14661669042642437042, 5345317080454741069, 8196674451365552863,
  92. 635818354583088260, 13515759754032305626, 1708499319988748543,
  93. 7509292798507899312})
  94. }
  95. func TestKeccakf(t *testing.T) {
  96. s, _ := newS()
  97. s = keccakf(s)
  98. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  99. []uint64{9472389783892099349, 2159377575142921216,
  100. 17826682512249813373, 2325963263767348549,
  101. 15086930817298358378, 11661812091723830419,
  102. 3517755057770134847, 5223775837645169598, 933274647126506074,
  103. 3451250694486589320, 825065683101361807, 6192414258352188799,
  104. 14426505790672879210, 3326742392640380689,
  105. 16749975585634164134, 17847697619892908514,
  106. 11598434253200954839, 6049795840392747215, 8610635351954084385,
  107. 18234131770974529925, 15330347418010067760,
  108. 12047099911907354591, 4763389569697138851, 6779624089296570504,
  109. 15083668107635345971})
  110. // compute again keccakf on the current state
  111. s = keccakf(s)
  112. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  113. []uint64{269318771259381490, 15892848561416382510,
  114. 12485559500958802382, 4360182510883008729,
  115. 14284025675983944434, 8800366419087562177, 7881853509112258378,
  116. 9503857914080778528, 17110477940977988953,
  117. 13825318756568052601, 11460650932194163315,
  118. 13272167288297399439, 13599957064256729412,
  119. 12730838251751851758, 13736647180617564382,
  120. 5651695613583298166, 15496251216716036782, 9748494184433838858,
  121. 3637745438296580159, 3821184813198767406, 15603239432236101315,
  122. 3726326332491237029, 7819962668913661099, 2285898735263816116,
  123. 13518516210247555620})
  124. }
  125. func printBytes(name string, b []byte, str bool) {
  126. fmt.Printf("%s\n", name)
  127. for _, v := range b {
  128. if str {
  129. fmt.Printf("\"%v\", ", v)
  130. } else {
  131. fmt.Printf("%v, ", v)
  132. }
  133. }
  134. fmt.Println("")
  135. }
  136. func printU64Array(name string, b []uint64, str bool) {
  137. fmt.Printf("%s\n", name)
  138. for _, v := range b {
  139. if str {
  140. fmt.Printf("\"%v\", ", v)
  141. } else {
  142. fmt.Printf("%v, ", v)
  143. }
  144. }
  145. fmt.Println("")
  146. }
  147. func TestAbsorb(t *testing.T) {
  148. s, _ := newS()
  149. block := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  150. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  151. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}
  156. // printU64Array("s", bitsToU64Array(s[:]), true)
  157. // printBytes("block", block[:], true)
  158. absorbed := absorb(s, bytesToBits(block))
  159. // printU64Array("absorbed", bitsToU64Array(absorbed[:]), true)
  160. qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
  161. []uint64{8342348566319207042, 319359607942176202, 14410076088654599075,
  162. 15666111399434436772, 9558421567405313402, 3396178318116504023,
  163. 794353847439963108, 12717011319735989377, 3503398863218919239,
  164. 5517201702366862678, 15999361614129160496, 1325524015888689985,
  165. 11971708408118944333, 14874486179441062217, 12554876384974234666,
  166. 11129975558302206043, 11257826431949606534, 2740710607956478714,
  167. 15000019752453010167, 15593606854132419294, 2598425978562809333,
  168. 8872504799797239246, 1212062965004664308, 5443427421087086722,
  169. 10946808592826700411})
  170. absorbed = absorb(absorbed, bytesToBits(block))
  171. // printU64Array("absorbed", bitsToU64Array(absorbed[:]), true)
  172. qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
  173. []uint64{8909243822027471379, 1111840847970088140,
  174. 12093072708540612559, 11255033638786021658, 2082116894939842214,
  175. 12821085060245261575, 6901785969834988344, 3182430130277914993,
  176. 2164708585929408975, 14402143231999718904, 16231444410553803968,
  177. 1850945423480060493, 12856855675247400303, 1137248620532111171,
  178. 7389129221921446308, 12932467982741614601, 1350606937385760406,
  179. 10983682292859713641, 10305595434820307765, 13958651111365489854,
  180. 17206620388135196198, 4238113785249530092, 7230868147643218103,
  181. 603011106238724524, 16480095441097880488})
  182. }
  183. func TestFinal(t *testing.T) {
  184. b := make([]byte, 32)
  185. for i := 0; i < len(b); i++ {
  186. b[i] = byte(i)
  187. }
  188. bBits := bytesToBits(b)
  189. fBits := final(bBits)
  190. // printBytes("in", b[:], true)
  191. // printU64Array("out", bitsToU64Array(fBits[:]), true)
  192. qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
  193. []uint64{16953415415620100490, 7495738965189503699,
  194. 12723370805759944158, 3295955328722933810,
  195. 12121371508560456016, 174876831679863147, 15944933357501475584,
  196. 7502339663607726274, 12048918224562833898,
  197. 16715284461100269102, 15582559130083209842,
  198. 1743886467337678829, 2424196198791253761, 1116417308245482383,
  199. 10367365997906434042, 1849801549382613906,
  200. 13294939539683415102, 4478091053375708790, 2969967870313332958,
  201. 14618962068930014237, 2721742233407503451,
  202. 12003265593030191290, 8109318293656735684, 6346795302983965746,
  203. 12210038122000333046})
  204. // 2nd test
  205. for i := 0; i < len(b); i++ {
  206. b[i] = byte(254)
  207. }
  208. bBits = bytesToBits(b)
  209. fBits = final(bBits)
  210. // printBytes("in", b[:], true)
  211. // printU64Array("out", bitsToU64Array(fBits[:]), true)
  212. qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
  213. []uint64{16852464862333879129, 9588646233186836430, 693207875935078627,
  214. 6545910230963382296, 3599194178366828471, 13130606490077331384,
  215. 10374798023615518933, 7285576075118720444, 4097382401500492461,
  216. 3968685317688314807, 3350659309646210303, 640023485234837464,
  217. 2550030127986774041, 8948768022010378840, 10678227883444996205,
  218. 1395278318096830339, 2744077813166753978, 13362598477502046010,
  219. 14601579319881128511, 4070707967569603186, 16833768365875755098,
  220. 1486295134719870048, 9161068934282437999, 8245604251371175619,
  221. 8421994351908003183})
  222. }
  223. func TestSqueeze(t *testing.T) {
  224. in := []uint64{16852464862333879129, 9588646233186836430, 693207875935078627,
  225. 6545910230963382296, 3599194178366828471, 13130606490077331384,
  226. 10374798023615518933, 7285576075118720444, 4097382401500492461,
  227. 3968685317688314807, 3350659309646210303, 640023485234837464,
  228. 2550030127986774041, 8948768022010378840, 10678227883444996205,
  229. 1395278318096830339, 2744077813166753978, 13362598477502046010,
  230. 14601579319881128511, 4070707967569603186, 16833768365875755098,
  231. 1486295134719870048, 9161068934282437999, 8245604251371175619,
  232. 8421994351908003183}
  233. inBits := u64ArrayToBits(in)
  234. var inBits1600 [25 * 64]bool
  235. copy(inBits1600[:], inBits[:])
  236. outBits := squeeze(inBits1600)
  237. // printU64Array("in", in, true)
  238. // printBytes("out", bitsToBytes(outBits[:]), true)
  239. qt.Assert(t, bitsToBytes(outBits[:]), qt.DeepEquals,
  240. []byte{89, 195, 41, 13, 129, 251, 223, 233, 206, 31, 253, 61,
  241. 242, 182, 17, 133, 227, 8, 157, 240, 227, 196, 158, 9, 24, 232,
  242. 42, 96, 172, 190, 215, 90})
  243. // 2nd test
  244. in = []uint64{16953415415620100490, 7495738965189503699,
  245. 12723370805759944158, 3295955328722933810, 12121371508560456016,
  246. 174876831679863147, 15944933357501475584, 7502339663607726274,
  247. 12048918224562833898, 16715284461100269102, 15582559130083209842,
  248. 1743886467337678829, 2424196198791253761, 1116417308245482383,
  249. 10367365997906434042, 1849801549382613906, 13294939539683415102,
  250. 4478091053375708790, 2969967870313332958, 14618962068930014237,
  251. 2721742233407503451, 12003265593030191290, 8109318293656735684,
  252. 6346795302983965746, 12210038122000333046}
  253. inBits = u64ArrayToBits(in)
  254. copy(inBits1600[:], inBits[:])
  255. outBits = squeeze(inBits1600)
  256. // printU64Array("in", in, true)
  257. // printBytes("out", bitsToBytes(outBits[:]), true)
  258. qt.Assert(t, bitsToBytes(outBits[:]), qt.DeepEquals,
  259. []byte{138, 225, 170, 89, 127, 161, 70, 235, 211, 170, 44, 237,
  260. 223, 54, 6, 104, 222, 165, 229, 38, 86, 126, 146, 176, 50, 24,
  261. 22, 164, 232, 149, 189, 45})
  262. }