f_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package blake2b
  2. import (
  3. "encoding/binary"
  4. "encoding/hex"
  5. "fmt"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestF(t *testing.T) {
  10. for i, test := range testVectorsF {
  11. t.Run(fmt.Sprintf("test vector %v", i), func(t *testing.T) {
  12. //toEthereumTestCase(test)
  13. h := test.hIn
  14. F(&h, test.m, test.c, test.f, test.rounds)
  15. if !reflect.DeepEqual(test.hOut, h) {
  16. t.Errorf("Unexpected result\nExpected: [%v]\nActual: [%v]\n", test.hOut, h)
  17. }
  18. })
  19. }
  20. }
  21. type testVector struct {
  22. hIn [8]uint64
  23. m [16]uint64
  24. c [2]uint64
  25. f bool
  26. rounds uint32
  27. hOut [8]uint64
  28. }
  29. // https://tools.ietf.org/html/rfc7693#appendix-A
  30. var testVectorsF = []testVector{
  31. {
  32. hIn: [8]uint64{
  33. 0x6a09e667f2bdc948, 0xbb67ae8584caa73b,
  34. 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
  35. 0x510e527fade682d1, 0x9b05688c2b3e6c1f,
  36. 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179,
  37. },
  38. m: [16]uint64{
  39. 0x0000000000636261, 0x0000000000000000, 0x0000000000000000,
  40. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  41. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  42. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  43. 0x0000000000000000, 0x0000000000000000, 0x0000000000000000,
  44. 0x0000000000000000,
  45. },
  46. c: [2]uint64{3, 0},
  47. f: true,
  48. rounds: 12,
  49. hOut: [8]uint64{
  50. 0x0D4D1C983FA580BA, 0xE9F6129FB697276A, 0xB7C45A68142F214C,
  51. 0xD1A2FFDB6FBB124B, 0x2D79AB2A39C5877D, 0x95CC3345DED552C2,
  52. 0x5A92F1DBA88AD318, 0x239900D4ED8623B9,
  53. },
  54. },
  55. }
  56. // toEthereumTestCase transforms F test vector into test vector format used by
  57. // go-ethereum precompiles
  58. func toEthereumTestCase(vector testVector) {
  59. var memory [213]byte
  60. // 4 bytes for rounds
  61. binary.BigEndian.PutUint32(memory[0:4], uint32(vector.rounds))
  62. // for h (512 bits = 64 bytes)
  63. for i := 0; i < 8; i++ {
  64. offset := 4 + i*8
  65. binary.LittleEndian.PutUint64(memory[offset:offset+8], vector.hIn[i])
  66. }
  67. // for m (1024 bits = 128 bytes)
  68. for i := 0; i < 16; i++ {
  69. offset := 68 + i*8
  70. binary.LittleEndian.PutUint64(memory[offset:offset+8], vector.m[i])
  71. }
  72. // 8 bytes for t[0], 8 bytes for t[1]
  73. binary.LittleEndian.PutUint64(memory[196:204], vector.c[0])
  74. binary.LittleEndian.PutUint64(memory[204:212], vector.c[1])
  75. // 1 byte for f
  76. if vector.f {
  77. memory[212] = 1
  78. }
  79. fmt.Printf("input: \"%v\"\n", hex.EncodeToString(memory[:]))
  80. var result [64]byte
  81. binary.LittleEndian.PutUint64(result[0:8], vector.hOut[0])
  82. binary.LittleEndian.PutUint64(result[8:16], vector.hOut[1])
  83. binary.LittleEndian.PutUint64(result[16:24], vector.hOut[2])
  84. binary.LittleEndian.PutUint64(result[24:32], vector.hOut[3])
  85. binary.LittleEndian.PutUint64(result[32:40], vector.hOut[4])
  86. binary.LittleEndian.PutUint64(result[40:48], vector.hOut[5])
  87. binary.LittleEndian.PutUint64(result[48:56], vector.hOut[6])
  88. binary.LittleEndian.PutUint64(result[56:64], vector.hOut[7])
  89. fmt.Printf("expected: \"%v\"\n", hex.EncodeToString(result[:]))
  90. }