github.com/cloudflare/circl@v1.5.0/dh/sidh/internal/p434/arith_amd64_test.go (about)

     1  // Code generated by go generate; DO NOT EDIT.
     2  // This file was generated by robots.
     3  
     4  //go:build amd64 && !purego
     5  // +build amd64,!purego
     6  
     7  package p434
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  	"testing/quick"
    13  
    14  	"github.com/cloudflare/circl/dh/sidh/internal/common"
    15  	"golang.org/x/sys/cpu"
    16  )
    17  
    18  type OptimFlag uint
    19  
    20  const (
    21  	// Indicates that optimisation which uses MUL instruction should be used
    22  	kUse_MUL OptimFlag = 1 << 0
    23  	// Indicates that optimisation which uses MULX, ADOX and ADCX instructions should be used
    24  	kUse_MULXandADxX = 1 << 1
    25  )
    26  
    27  func resetCpuFeatures() {
    28  	HasADXandBMI2 = cpu.X86.HasBMI2 && cpu.X86.HasADX
    29  }
    30  
    31  // Utility function used for testing Mul implementations. Tests caller provided
    32  // mulFunc against mul()
    33  func testMul(t *testing.T, f1, f2 OptimFlag) {
    34  	doMulTest := func(multiplier, multiplicant common.Fp) bool {
    35  		defer resetCpuFeatures()
    36  		var resMulRef, resMulOptim common.FpX2
    37  
    38  		// Compute multiplier*multiplicant with first implementation
    39  		HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
    40  		mulP434(&resMulOptim, &multiplier, &multiplicant)
    41  
    42  		// Compute multiplier*multiplicant with second implementation
    43  		HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
    44  		mulP434(&resMulRef, &multiplier, &multiplicant)
    45  
    46  		// Compare results
    47  		return reflect.DeepEqual(resMulRef, resMulOptim)
    48  	}
    49  
    50  	if err := quick.Check(doMulTest, quickCheckConfig); err != nil {
    51  		t.Error(err)
    52  	}
    53  }
    54  
    55  // Utility function used for testing REDC implementations. Tests caller provided
    56  // redcFunc against redc()
    57  func testRedc(t *testing.T, f1, f2 OptimFlag) {
    58  	doRedcTest := func(aRR common.FpX2) bool {
    59  		defer resetCpuFeatures()
    60  		var resRedcF1, resRedcF2 common.Fp
    61  		aRRcpy := aRR
    62  
    63  		// Compute redc with first implementation
    64  		HasADXandBMI2 = (kUse_MULXandADxX & f1) == kUse_MULXandADxX
    65  		rdcP434(&resRedcF1, &aRR)
    66  
    67  		// Compute redc with second implementation
    68  		HasADXandBMI2 = (kUse_MULXandADxX & f2) == kUse_MULXandADxX
    69  		rdcP434(&resRedcF2, &aRRcpy)
    70  
    71  		// Compare results
    72  		return reflect.DeepEqual(resRedcF2, resRedcF1)
    73  	}
    74  
    75  	if err := quick.Check(doRedcTest, quickCheckConfig); err != nil {
    76  		t.Error(err)
    77  	}
    78  }
    79  
    80  // Ensures correctness of implementation of mul operation which uses MULX and ADOX/ADCX
    81  func TestMulWithMULXADxX(t *testing.T) {
    82  	defer resetCpuFeatures()
    83  	if !HasADXandBMI2 {
    84  		t.Skip("MULX, ADCX and ADOX not supported by the platform")
    85  	}
    86  	testMul(t, kUse_MULXandADxX, kUse_MUL)
    87  }
    88  
    89  // Ensures correctness of Montgomery reduction implementation which uses MULX
    90  // and ADCX/ADOX.
    91  func TestRedcWithMULXADxX(t *testing.T) {
    92  	defer resetCpuFeatures()
    93  	if !HasADXandBMI2 {
    94  		t.Skip("MULX, ADCX and ADOX not supported by the platform")
    95  	}
    96  	testRedc(t, kUse_MULXandADxX, kUse_MUL)
    97  }