No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

arith_amd64_test.go 1.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // +build amd64,!noasm
  2. package p751
  3. import (
  4. . "github.com/cloudflare/p751sidh/internal/isogeny"
  5. cpu "github.com/cloudflare/p751sidh/internal/utils"
  6. "testing"
  7. "testing/quick"
  8. )
  9. func TestFp751MontgomeryReduce(t *testing.T) {
  10. // First make sure that at least one value with a known result reduces
  11. // correctly as defined in TestPrimeFieldElementToBigInt.
  12. fp751MontgomeryReduce = fp751MontgomeryReduceFallback
  13. t.Run("PrimeFieldElementToBigInt", TestPrimeFieldElementToBigInt)
  14. if !cpu.HasBMI2 {
  15. return
  16. }
  17. fp751MontgomeryReduce = fp751MontgomeryReduceBMI2
  18. t.Run("PrimeFieldElementToBigInt", TestPrimeFieldElementToBigInt)
  19. // Also check that the BMI2 implementation produces the same results
  20. // as the fallback implementation.
  21. compareMontgomeryReduce := func(x, y primeFieldElement) bool {
  22. var z, zbackup FpElementX2
  23. var zred1, zred2 FpElement
  24. fp751Mul(&z, &x.A, &y.A)
  25. zbackup = z
  26. fp751MontgomeryReduceFallback(&zred1, &z)
  27. // z may be destroyed.
  28. z = zbackup
  29. fp751MontgomeryReduceBMI2(&zred2, &z)
  30. return zred1 == zred2
  31. }
  32. if err := quick.Check(compareMontgomeryReduce, quickCheckConfig); err != nil {
  33. t.Error(err)
  34. }
  35. if !cpu.HasADX {
  36. return
  37. }
  38. fp751MontgomeryReduce = fp751MontgomeryReduceBMI2ADX
  39. t.Run("PrimeFieldElementToBigInt", TestPrimeFieldElementToBigInt)
  40. // Check that the BMI2ADX implementation produces the same results as
  41. // the BMI2 implementation. By transitivity, it should also produce the
  42. // same results as the fallback implementation.
  43. compareMontgomeryReduce = func(x, y primeFieldElement) bool {
  44. var z, zbackup FpElementX2
  45. var zred1, zred2 FpElement
  46. fp751Mul(&z, &x.A, &y.A)
  47. zbackup = z
  48. fp751MontgomeryReduceBMI2(&zred1, &z)
  49. // z may be destroyed.
  50. z = zbackup
  51. fp751MontgomeryReduceBMI2ADX(&zred2, &z)
  52. return zred1 == zred2
  53. }
  54. if err := quick.Check(compareMontgomeryReduce, quickCheckConfig); err != nil {
  55. t.Error(err)
  56. }
  57. }