Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

80 řádky
2.2 KiB

  1. // +build amd64,!noasm
  2. package p751
  3. import (
  4. . "github.com/cloudflare/sidh/internal/isogeny"
  5. cpu "github.com/cloudflare/sidh/internal/utils"
  6. "reflect"
  7. "testing"
  8. "testing/quick"
  9. )
  10. type OptimFlag uint
  11. const (
  12. // Indicates that optimisation which uses MUL instruction should be used
  13. kUse_MUL OptimFlag = 1 << 0
  14. // Indicates that optimisation which uses MULX instruction should be used
  15. kUse_MULX = 1 << 1
  16. // Indicates that optimisation which uses MULX, ADOX and ADCX instructions should be used
  17. kUse_MULXandADxX = 1 << 2
  18. )
  19. // Utility function used for testing REDC implementations. Tests caller provided
  20. // redcFunc against redc()
  21. func testRedc(t *testing.T, f1, f2 OptimFlag) {
  22. doRedcTest := func(aRR FpElementX2) bool {
  23. defer cpu.RecognizeCpu()
  24. var resRedcF1, resRedcF2 FpElement
  25. var aRRcpy = aRR
  26. // Compute redc with first implementation
  27. cpu.HasBMI2 = (kUse_MULX & f1) == kUse_MULX
  28. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
  29. fp751MontgomeryReduce(&resRedcF1, &aRR)
  30. // Compute redc with second implementation
  31. cpu.HasBMI2 = (kUse_MULX & f2) == kUse_MULX
  32. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
  33. fp751MontgomeryReduce(&resRedcF2, &aRRcpy)
  34. // Compare results
  35. return reflect.DeepEqual(resRedcF2, resRedcF1)
  36. }
  37. if err := quick.Check(doRedcTest, quickCheckConfig); err != nil {
  38. t.Error(err)
  39. }
  40. }
  41. // Ensures correctness of Montgomery reduction implementation which uses MULX
  42. func TestRedcWithMULX(t *testing.T) {
  43. defer cpu.RecognizeCpu()
  44. if !cpu.HasBMI2 {
  45. t.Skip("MULX not supported by the platform")
  46. }
  47. testRedc(t, kUse_MULX, kUse_MUL)
  48. }
  49. // Ensures correctness of Montgomery reduction implementation which uses MULX
  50. // and ADCX/ADOX.
  51. func TestRedcWithMULXADxX(t *testing.T) {
  52. defer cpu.RecognizeCpu()
  53. if !cpu.HasADXandBMI2 {
  54. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  55. }
  56. testRedc(t, kUse_MULXandADxX, kUse_MUL)
  57. }
  58. // Ensures correctness of Montgomery reduction implementation which uses MULX
  59. // and ADCX/ADOX.
  60. func TestRedcWithMULXADxXAgainstMULX(t *testing.T) {
  61. defer cpu.RecognizeCpu()
  62. if !cpu.HasADXandBMI2 {
  63. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  64. }
  65. testRedc(t, kUse_MULXandADxX, kUse_MULX)
  66. }