curve25519_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 curve25519
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "fmt"
  9. "testing"
  10. )
  11. const expectedHex = "89161fde887b2b53de549af483940106ecc114d6982daa98256de23bdf77661a"
  12. func TestBaseScalarMult(t *testing.T) {
  13. var a, b [32]byte
  14. in := &a
  15. out := &b
  16. a[0] = 1
  17. for i := 0; i < 200; i++ {
  18. ScalarBaseMult(out, in)
  19. in, out = out, in
  20. }
  21. result := fmt.Sprintf("%x", in[:])
  22. if result != expectedHex {
  23. t.Errorf("incorrect result: got %s, want %s", result, expectedHex)
  24. }
  25. }
  26. // TestHighBitIgnored tests the following requirement in RFC 7748:
  27. //
  28. // When receiving such an array, implementations of X25519 (but not X448) MUST
  29. // mask the most significant bit in the final byte.
  30. //
  31. // Regression test for issue #30095.
  32. func TestHighBitIgnored(t *testing.T) {
  33. var s, u [32]byte
  34. rand.Read(s[:])
  35. rand.Read(u[:])
  36. var hi0, hi1 [32]byte
  37. u[31] &= 0x7f
  38. ScalarMult(&hi0, &s, &u)
  39. u[31] |= 0x80
  40. ScalarMult(&hi1, &s, &u)
  41. if !bytes.Equal(hi0[:], hi1[:]) {
  42. t.Errorf("high bit of group point should not affect result")
  43. }
  44. }
  45. func BenchmarkScalarBaseMult(b *testing.B) {
  46. var in, out [32]byte
  47. in[0] = 1
  48. b.SetBytes(32)
  49. for i := 0; i < b.N; i++ {
  50. ScalarBaseMult(&out, &in)
  51. }
  52. }