2020-05-14 01:02:32 +01:00
|
|
|
// Code generated by go generate; DO NOT EDIT.
|
|
|
|
// This file was generated by robots.
|
|
|
|
|
2018-12-03 23:07:01 +00:00
|
|
|
// +build amd64,!noasm
|
|
|
|
|
2020-05-14 01:02:32 +01:00
|
|
|
package p434
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
2020-05-14 01:02:32 +01:00
|
|
|
|
|
|
|
"github.com/henrydcase/nobs/sidh/internal/common"
|
|
|
|
"golang.org/x/sys/cpu"
|
2018-12-03 23:07:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OptimFlag uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Indicates that optimisation which uses MUL instruction should be used
|
|
|
|
kUse_MUL OptimFlag = 1 << 0
|
|
|
|
// Indicates that optimisation which uses MULX, ADOX and ADCX instructions should be used
|
2020-05-14 01:02:32 +01:00
|
|
|
kUse_MULXandADxX = 1 << 1
|
2018-12-03 23:07:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func resetCpuFeatures() {
|
|
|
|
HasADXandBMI2 = cpu.X86.HasBMI2 && cpu.X86.HasADX
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility function used for testing Mul implementations. Tests caller provided
|
|
|
|
// mulFunc against mul()
|
|
|
|
func testMul(t *testing.T, f1, f2 OptimFlag) {
|
2020-05-14 01:02:32 +01:00
|
|
|
doMulTest := func(multiplier, multiplicant common.Fp) bool {
|
2018-12-03 23:07:01 +00:00
|
|
|
defer resetCpuFeatures()
|
2020-05-14 01:02:32 +01:00
|
|
|
var resMulRef, resMulOptim common.FpX2
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
// Compute multiplier*multiplicant with first implementation
|
|
|
|
HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
|
2020-05-14 01:02:32 +01:00
|
|
|
mulP434(&resMulOptim, &multiplier, &multiplicant)
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
// Compute multiplier*multiplicant with second implementation
|
|
|
|
HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
|
2020-05-14 01:02:32 +01:00
|
|
|
mulP434(&resMulRef, &multiplier, &multiplicant)
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
// Compare results
|
|
|
|
return reflect.DeepEqual(resMulRef, resMulOptim)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := quick.Check(doMulTest, quickCheckConfig); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility function used for testing REDC implementations. Tests caller provided
|
|
|
|
// redcFunc against redc()
|
|
|
|
func testRedc(t *testing.T, f1, f2 OptimFlag) {
|
2020-05-14 01:02:32 +01:00
|
|
|
doRedcTest := func(aRR common.FpX2) bool {
|
2018-12-03 23:07:01 +00:00
|
|
|
defer resetCpuFeatures()
|
2020-05-14 01:02:32 +01:00
|
|
|
var resRedcF1, resRedcF2 common.Fp
|
2018-12-03 23:07:01 +00:00
|
|
|
var aRRcpy = aRR
|
|
|
|
|
|
|
|
// Compute redc with first implementation
|
|
|
|
HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
|
2020-05-14 01:02:32 +01:00
|
|
|
rdcP434(&resRedcF1, &aRR)
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
// Compute redc with second implementation
|
|
|
|
HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
|
2020-05-14 01:02:32 +01:00
|
|
|
rdcP434(&resRedcF2, &aRRcpy)
|
2018-12-03 23:07:01 +00:00
|
|
|
|
|
|
|
// Compare results
|
|
|
|
return reflect.DeepEqual(resRedcF2, resRedcF1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := quick.Check(doRedcTest, quickCheckConfig); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures correctness of implementation of mul operation which uses MULX and ADOX/ADCX
|
|
|
|
func TestMulWithMULXADxX(t *testing.T) {
|
|
|
|
defer resetCpuFeatures()
|
|
|
|
if !HasADXandBMI2 {
|
|
|
|
t.Skip("MULX, ADCX and ADOX not supported by the platform")
|
|
|
|
}
|
|
|
|
testMul(t, kUse_MULXandADxX, kUse_MUL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures correctness of Montgomery reduction implementation which uses MULX
|
|
|
|
// and ADCX/ADOX.
|
|
|
|
func TestRedcWithMULXADxX(t *testing.T) {
|
|
|
|
defer resetCpuFeatures()
|
|
|
|
if !HasADXandBMI2 {
|
|
|
|
t.Skip("MULX, ADCX and ADOX not supported by the platform")
|
|
|
|
}
|
|
|
|
testRedc(t, kUse_MULXandADxX, kUse_MUL)
|
|
|
|
}
|