github.com/cloudflare/circl@v1.5.0/ecc/p384/opt_test.go (about)

     1  //go:build (!purego && arm64) || (!purego && amd64)
     2  // +build !purego,arm64 !purego,amd64
     3  
     4  package p384
     5  
     6  import (
     7  	"bytes"
     8  	"crypto/elliptic"
     9  	"crypto/rand"
    10  	"math/big"
    11  	"testing"
    12  
    13  	"github.com/cloudflare/circl/internal/test"
    14  )
    15  
    16  func TestInternals(t *testing.T) {
    17  	t.Run("reduceScalar", func(t *testing.T) {
    18  		var c curve
    19  		order := c.Params().N
    20  		var buffer [3 * sizeFp]byte
    21  
    22  		for i := 0; i < sizeFp; i++ {
    23  			buffer[i] = 0xFF
    24  		}
    25  		_, err := rand.Read(buffer[sizeFp:])
    26  		if err != nil {
    27  			t.Fatal(err)
    28  		}
    29  
    30  		for i := 0; i < 3*sizeFp; i++ {
    31  			k := buffer[:i]
    32  			got := c.reduceScalar(k)
    33  
    34  			bigK := new(big.Int).SetBytes(k)
    35  			bigK.Mod(bigK, order)
    36  			want := bigK.FillBytes(make([]byte, sizeFp))
    37  
    38  			if !bytes.Equal(got, want) {
    39  				test.ReportError(t, got, want, k)
    40  			}
    41  		}
    42  	})
    43  
    44  	t.Run("absolute", func(t *testing.T) {
    45  		cases := []int32{-2, -1, 0, 1, 2}
    46  		expected := []int32{2, 1, 0, 1, 2}
    47  		for i := range cases {
    48  			got := absolute(cases[i])
    49  			want := expected[i]
    50  			if got != want {
    51  				test.ReportError(t, got, want, cases[i])
    52  			}
    53  		}
    54  	})
    55  
    56  	t.Run("toOdd", func(t *testing.T) {
    57  		var c curve
    58  		k := []byte{0xF0}
    59  		oddK, _ := c.toOdd(k)
    60  		got := len(oddK)
    61  		want := 48
    62  		if got != want {
    63  			test.ReportError(t, got, want)
    64  		}
    65  
    66  		oddK[sizeFp-1] = 0x0
    67  		smallOddK, _ := c.toOdd(oddK)
    68  		got = len(smallOddK)
    69  		want = 48
    70  		if got != want {
    71  			test.ReportError(t, got, want)
    72  		}
    73  	})
    74  
    75  	t.Run("special k", func(t *testing.T) {
    76  		cases := []struct { // known cases that require complete addition
    77  			w uint
    78  			k int
    79  		}{
    80  			{w: 2, k: 2},
    81  			{w: 5, k: 6},
    82  			{w: 6, k: 38},
    83  			{w: 7, k: 102},
    84  			{w: 9, k: 230},
    85  			{w: 12, k: 742},
    86  			{w: 14, k: 4838},
    87  			{w: 17, k: 21222},
    88  			{w: 19, k: 152294},
    89  		}
    90  
    91  		var c curve
    92  
    93  		StdCurve := elliptic.P384()
    94  		params := StdCurve.Params()
    95  		for _, caseI := range cases {
    96  			k := big.NewInt(int64(caseI.k)).Bytes()
    97  			gotX, gotY := c.scalarMultOmega(params.Gx, params.Gy, k, caseI.w)
    98  			wantX, wantY := StdCurve.ScalarMult(params.Gx, params.Gy, k)
    99  
   100  			if gotX.Cmp(wantX) != 0 {
   101  				test.ReportError(t, gotX, wantX, caseI)
   102  			}
   103  			if gotY.Cmp(wantY) != 0 {
   104  				test.ReportError(t, gotY, wantY, caseI)
   105  			}
   106  		}
   107  	})
   108  }