Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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