Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

132 wiersze
3.9 KiB

  1. // +build amd64,!noasm
  2. package p503
  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 Mul implementations. Tests caller provided
  20. // mulFunc against mul()
  21. func testMul(t *testing.T, f1, f2 OptimFlag) {
  22. doMulTest := func(multiplier, multiplicant FpElement) bool {
  23. defer cpu.RecognizeCpu()
  24. var resMulRef, resMulOptim FpElementX2
  25. // Compute multiplier*multiplicant with first implementation
  26. cpu.HasBMI2 = (kUse_MULX & f1) == kUse_MULX
  27. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
  28. fp503Mul(&resMulOptim, &multiplier, &multiplicant)
  29. // Compute multiplier*multiplicant with second implementation
  30. cpu.HasBMI2 = (kUse_MULX & f2) == kUse_MULX
  31. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
  32. fp503Mul(&resMulRef, &multiplier, &multiplicant)
  33. // Compare results
  34. return reflect.DeepEqual(resMulRef, resMulOptim)
  35. }
  36. if err := quick.Check(doMulTest, quickCheckConfig); err != nil {
  37. t.Error(err)
  38. }
  39. }
  40. // Utility function used for testing REDC implementations. Tests caller provided
  41. // redcFunc against redc()
  42. func testRedc(t *testing.T, f1, f2 OptimFlag) {
  43. doRedcTest := func(aRR FpElementX2) bool {
  44. defer cpu.RecognizeCpu()
  45. var resRedcF1, resRedcF2 FpElement
  46. var aRRcpy = aRR
  47. // Compute redc with first implementation
  48. cpu.HasBMI2 = (kUse_MULX & f1) == kUse_MULX
  49. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
  50. fp503MontgomeryReduce(&resRedcF1, &aRR)
  51. // Compute redc with second implementation
  52. cpu.HasBMI2 = (kUse_MULX & f2) == kUse_MULX
  53. cpu.HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
  54. fp503MontgomeryReduce(&resRedcF2, &aRRcpy)
  55. // Compare results
  56. return reflect.DeepEqual(resRedcF2, resRedcF1)
  57. }
  58. if err := quick.Check(doRedcTest, quickCheckConfig); err != nil {
  59. t.Error(err)
  60. }
  61. }
  62. // Ensures correctness of implementation of mul operation which uses MULX
  63. func TestMulWithMULX(t *testing.T) {
  64. defer cpu.RecognizeCpu()
  65. if !cpu.HasBMI2 {
  66. t.Skip("MULX not supported by the platform")
  67. }
  68. testMul(t, kUse_MULX, kUse_MUL)
  69. }
  70. // Ensures correctness of implementation of mul operation which uses MULX and ADOX/ADCX
  71. func TestMulWithMULXADxX(t *testing.T) {
  72. defer cpu.RecognizeCpu()
  73. if !cpu.HasADXandBMI2 {
  74. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  75. }
  76. testMul(t, kUse_MULXandADxX, kUse_MUL)
  77. }
  78. // Ensures correctness of implementation of mul operation which uses MULX and ADOX/ADCX
  79. func TestMulWithMULXADxXAgainstMULX(t *testing.T) {
  80. defer cpu.RecognizeCpu()
  81. if !cpu.HasADXandBMI2 {
  82. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  83. }
  84. testMul(t, kUse_MULX, kUse_MULXandADxX)
  85. }
  86. // Ensures correctness of Montgomery reduction implementation which uses MULX
  87. func TestRedcWithMULX(t *testing.T) {
  88. defer cpu.RecognizeCpu()
  89. if !cpu.HasBMI2 {
  90. t.Skip("MULX not supported by the platform")
  91. }
  92. testRedc(t, kUse_MULX, kUse_MUL)
  93. }
  94. // Ensures correctness of Montgomery reduction implementation which uses MULX
  95. // and ADCX/ADOX.
  96. func TestRedcWithMULXADxX(t *testing.T) {
  97. defer cpu.RecognizeCpu()
  98. if !cpu.HasADXandBMI2 {
  99. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  100. }
  101. testRedc(t, kUse_MULXandADxX, kUse_MUL)
  102. }
  103. // Ensures correctness of Montgomery reduction implementation which uses MULX
  104. // and ADCX/ADOX.
  105. func TestRedcWithMULXADxXAgainstMULX(t *testing.T) {
  106. defer cpu.RecognizeCpu()
  107. if !cpu.HasADXandBMI2 {
  108. t.Skip("MULX, ADCX and ADOX not supported by the platform")
  109. }
  110. testRedc(t, kUse_MULXandADxX, kUse_MULX)
  111. }