scalar_alias_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (c) 2019 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 edwards25519
  5. import (
  6. "testing"
  7. "testing/quick"
  8. )
  9. func TestScalarAliasing(t *testing.T) {
  10. checkAliasingOneArg := func(f func(v, x *Scalar) *Scalar, v, x Scalar) bool {
  11. x1, v1 := x, x
  12. // Calculate a reference f(x) without aliasing.
  13. if out := f(&v, &x); out != &v || !isReduced(out) {
  14. return false
  15. }
  16. // Test aliasing the argument and the receiver.
  17. if out := f(&v1, &v1); out != &v1 || v1 != v || !isReduced(out) {
  18. return false
  19. }
  20. // Ensure the arguments was not modified.
  21. return x == x1
  22. }
  23. checkAliasingTwoArgs := func(f func(v, x, y *Scalar) *Scalar, v, x, y Scalar) bool {
  24. x1, y1, v1 := x, y, Scalar{}
  25. // Calculate a reference f(x, y) without aliasing.
  26. if out := f(&v, &x, &y); out != &v || !isReduced(out) {
  27. return false
  28. }
  29. // Test aliasing the first argument and the receiver.
  30. v1 = x
  31. if out := f(&v1, &v1, &y); out != &v1 || v1 != v || !isReduced(out) {
  32. return false
  33. }
  34. // Test aliasing the second argument and the receiver.
  35. v1 = y
  36. if out := f(&v1, &x, &v1); out != &v1 || v1 != v || !isReduced(out) {
  37. return false
  38. }
  39. // Calculate a reference f(x, x) without aliasing.
  40. if out := f(&v, &x, &x); out != &v || !isReduced(out) {
  41. return false
  42. }
  43. // Test aliasing the first argument and the receiver.
  44. v1 = x
  45. if out := f(&v1, &v1, &x); out != &v1 || v1 != v || !isReduced(out) {
  46. return false
  47. }
  48. // Test aliasing the second argument and the receiver.
  49. v1 = x
  50. if out := f(&v1, &x, &v1); out != &v1 || v1 != v || !isReduced(out) {
  51. return false
  52. }
  53. // Test aliasing both arguments and the receiver.
  54. v1 = x
  55. if out := f(&v1, &v1, &v1); out != &v1 || v1 != v || !isReduced(out) {
  56. return false
  57. }
  58. // Ensure the arguments were not modified.
  59. return x == x1 && y == y1
  60. }
  61. for name, f := range map[string]any{
  62. "Negate": func(v, x Scalar) bool {
  63. return checkAliasingOneArg((*Scalar).Negate, v, x)
  64. },
  65. "Multiply": func(v, x, y Scalar) bool {
  66. return checkAliasingTwoArgs((*Scalar).Multiply, v, x, y)
  67. },
  68. "Add": func(v, x, y Scalar) bool {
  69. return checkAliasingTwoArgs((*Scalar).Add, v, x, y)
  70. },
  71. "Subtract": func(v, x, y Scalar) bool {
  72. return checkAliasingTwoArgs((*Scalar).Subtract, v, x, y)
  73. },
  74. } {
  75. err := quick.Check(f, &quick.Config{MaxCountScale: 1 << 5})
  76. if err != nil {
  77. t.Errorf("%v: %v", name, err)
  78. }
  79. }
  80. }