poly1305_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright 2012 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package poly1305
  5. import (
  6. "bytes"
  7. "testing"
  8. )
  9. var testData = []struct {
  10. in, k, correct []byte
  11. }{
  12. {
  13. []byte("Hello world!"),
  14. []byte("this is 32-byte key for Poly1305"),
  15. []byte{0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0},
  16. },
  17. {
  18. make([]byte, 32),
  19. []byte("this is 32-byte key for Poly1305"),
  20. []byte{0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07},
  21. },
  22. {
  23. make([]byte, 2007),
  24. []byte("this is 32-byte key for Poly1305"),
  25. []byte{0xda, 0x84, 0xbc, 0xab, 0x02, 0x67, 0x6c, 0x38, 0xcd, 0xb0, 0x15, 0x60, 0x42, 0x74, 0xc2, 0xaa},
  26. },
  27. {
  28. make([]byte, 2007),
  29. make([]byte, 32),
  30. make([]byte, 16),
  31. },
  32. }
  33. func TestSum(t *testing.T) {
  34. var out [16]byte
  35. var key [32]byte
  36. for i, v := range testData {
  37. copy(key[:], v.k)
  38. Sum(&out, v.in, &key)
  39. if !bytes.Equal(out[:], v.correct) {
  40. t.Errorf("%d: expected %x, got %x", i, v.correct, out[:])
  41. }
  42. }
  43. }
  44. func Benchmark1K(b *testing.B) {
  45. b.StopTimer()
  46. var out [16]byte
  47. var key [32]byte
  48. in := make([]byte, 1024)
  49. b.SetBytes(int64(len(in)))
  50. b.StartTimer()
  51. for i := 0; i < b.N; i++ {
  52. Sum(&out, in, &key)
  53. }
  54. }
  55. func Benchmark64(b *testing.B) {
  56. b.StopTimer()
  57. var out [16]byte
  58. var key [32]byte
  59. in := make([]byte, 64)
  60. b.SetBytes(int64(len(in)))
  61. b.StartTimer()
  62. for i := 0; i < b.N; i++ {
  63. Sum(&out, in, &key)
  64. }
  65. }