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.

187 lines
7.0 KiB

  1. package keccak
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. qt "github.com/frankban/quicktest"
  8. )
  9. func TestKeccak(t *testing.T) {
  10. testKeccak(t, []byte("test"), "9c22ff5f21f0b81b113e63f7db6da94fedef11b2119b4088b89664fb9a3cb658")
  11. testKeccak(t, make([]byte, 32), "290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
  12. testKeccak(t, make([]byte, 100), "913fb9e1f6f1c6d910fd574a5cad8857aa43bfba24e401ada4f56090d4d997a7")
  13. }
  14. func testKeccak(t *testing.T, input []byte, expectedHex string) {
  15. expected := crypto.Keccak256(input)
  16. hBits := ComputeKeccak(bytesToBits(input))
  17. h := bitsToBytes(hBits)
  18. qt.Assert(t, h, qt.DeepEquals, expected)
  19. qt.Assert(t, hex.EncodeToString(h), qt.Equals, expectedHex)
  20. }
  21. func TestPad(t *testing.T) {
  22. b := make([]byte, 32)
  23. for i := 0; i < len(b); i++ {
  24. b[i] = byte(i)
  25. }
  26. bBits := bytesToBits(b)
  27. fBits := pad(bBits)
  28. qt.Assert(t, bitsToBytes(fBits[:]), qt.DeepEquals,
  29. []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  30. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  31. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  32. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128})
  36. }
  37. func TestKeccakfRound(t *testing.T) {
  38. s, _ := newS()
  39. s = keccakfRound(s, 0)
  40. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  41. []uint64{
  42. 26388279066651, 246290629787648, 26388279902208,
  43. 25165850, 246290605457408, 7784628352, 844424965783552,
  44. 2305843009213694083, 844432714760192,
  45. 2305843009249345539, 637534226, 14848, 641204224,
  46. 14354, 3670528, 6308236288, 2130304761856,
  47. 648518346341354496, 6309216256, 648520476645130240,
  48. 4611706359392501763, 792677514882318336,
  49. 20340965113972, 4611732197915754499,
  50. 792633534417207412})
  51. s = keccakfRound(s, 20)
  52. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  53. []uint64{17728382861289829725, 13654073086381141005,
  54. 9912591532945168756, 2030068283137172501, 5084683018496047808,
  55. 151244976540463006, 11718217461613725815, 11636071286320763433,
  56. 15039144509240642782, 11629028282864249197,
  57. 2594633730779457624, 14005558505838459171, 4612881094252610438,
  58. 2828009553220809993, 4838578484623267135, 1006588603063111352,
  59. 11109191860075454495, 1187545859779038208,
  60. 14661669042642437042, 5345317080454741069, 8196674451365552863,
  61. 635818354583088260, 13515759754032305626, 1708499319988748543,
  62. 7509292798507899312})
  63. }
  64. func TestKeccakf(t *testing.T) {
  65. s, _ := newS()
  66. s = keccakf(s)
  67. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  68. []uint64{9472389783892099349, 2159377575142921216,
  69. 17826682512249813373, 2325963263767348549,
  70. 15086930817298358378, 11661812091723830419,
  71. 3517755057770134847, 5223775837645169598, 933274647126506074,
  72. 3451250694486589320, 825065683101361807, 6192414258352188799,
  73. 14426505790672879210, 3326742392640380689,
  74. 16749975585634164134, 17847697619892908514,
  75. 11598434253200954839, 6049795840392747215, 8610635351954084385,
  76. 18234131770974529925, 15330347418010067760,
  77. 12047099911907354591, 4763389569697138851, 6779624089296570504,
  78. 15083668107635345971})
  79. // compute again keccakf on the current state
  80. s = keccakf(s)
  81. qt.Assert(t, bitsToU64Array(s[:]), qt.DeepEquals,
  82. []uint64{269318771259381490, 15892848561416382510,
  83. 12485559500958802382, 4360182510883008729,
  84. 14284025675983944434, 8800366419087562177, 7881853509112258378,
  85. 9503857914080778528, 17110477940977988953,
  86. 13825318756568052601, 11460650932194163315,
  87. 13272167288297399439, 13599957064256729412,
  88. 12730838251751851758, 13736647180617564382,
  89. 5651695613583298166, 15496251216716036782, 9748494184433838858,
  90. 3637745438296580159, 3821184813198767406, 15603239432236101315,
  91. 3726326332491237029, 7819962668913661099, 2285898735263816116,
  92. 13518516210247555620})
  93. }
  94. func printBytes(name string, b []byte) {
  95. fmt.Printf("%s\n", name)
  96. for _, v := range b {
  97. fmt.Printf("\"%v\", ", v)
  98. }
  99. fmt.Println("")
  100. }
  101. func printU64Array(name string, b []uint64) {
  102. fmt.Printf("%s\n", name)
  103. for _, v := range b {
  104. fmt.Printf("\"%v\", ", v)
  105. }
  106. fmt.Println("")
  107. }
  108. func TestAbsorb(t *testing.T) {
  109. s, _ := newS()
  110. block := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  111. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  112. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  113. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  114. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128}
  117. // printU64Array("s", bitsToU64Array(s[:]))
  118. // printBytes("block", block[:])
  119. absorbed := absorb(s, bytesToBits(block))
  120. // printU64Array("absorbed", bitsToU64Array(absorbed[:]))
  121. qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
  122. []uint64{8342348566319207042, 319359607942176202, 14410076088654599075,
  123. 15666111399434436772, 9558421567405313402, 3396178318116504023,
  124. 794353847439963108, 12717011319735989377, 3503398863218919239,
  125. 5517201702366862678, 15999361614129160496, 1325524015888689985,
  126. 11971708408118944333, 14874486179441062217, 12554876384974234666,
  127. 11129975558302206043, 11257826431949606534, 2740710607956478714,
  128. 15000019752453010167, 15593606854132419294, 2598425978562809333,
  129. 8872504799797239246, 1212062965004664308, 5443427421087086722,
  130. 10946808592826700411})
  131. absorbed = absorb(absorbed, bytesToBits(block))
  132. // printU64Array("absorbed", bitsToU64Array(absorbed[:]))
  133. qt.Assert(t, bitsToU64Array(absorbed[:]), qt.DeepEquals,
  134. []uint64{8909243822027471379, 1111840847970088140,
  135. 12093072708540612559, 11255033638786021658, 2082116894939842214,
  136. 12821085060245261575, 6901785969834988344, 3182430130277914993,
  137. 2164708585929408975, 14402143231999718904, 16231444410553803968,
  138. 1850945423480060493, 12856855675247400303, 1137248620532111171,
  139. 7389129221921446308, 12932467982741614601, 1350606937385760406,
  140. 10983682292859713641, 10305595434820307765, 13958651111365489854,
  141. 17206620388135196198, 4238113785249530092, 7230868147643218103,
  142. 603011106238724524, 16480095441097880488})
  143. }
  144. func TestFinal(t *testing.T) {
  145. b := make([]byte, 32)
  146. for i := 0; i < len(b); i++ {
  147. b[i] = byte(i)
  148. }
  149. bBits := bytesToBits(b)
  150. fBits := final(bBits)
  151. qt.Assert(t, bitsToU64Array(fBits[:]), qt.DeepEquals,
  152. []uint64{16953415415620100490, 7495738965189503699,
  153. 12723370805759944158, 3295955328722933810,
  154. 12121371508560456016, 174876831679863147, 15944933357501475584,
  155. 7502339663607726274, 12048918224562833898,
  156. 16715284461100269102, 15582559130083209842,
  157. 1743886467337678829, 2424196198791253761, 1116417308245482383,
  158. 10367365997906434042, 1849801549382613906,
  159. 13294939539683415102, 4478091053375708790, 2969967870313332958,
  160. 14618962068930014237, 2721742233407503451,
  161. 12003265593030191290, 8109318293656735684, 6346795302983965746,
  162. 12210038122000333046})
  163. }