github.com/cloudflare/circl@v1.5.0/math/fp448/edgecases_test.go (about)

     1  package fp448
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/cloudflare/circl/internal/conv"
     9  	"github.com/cloudflare/circl/internal/test"
    10  )
    11  
    12  func TestEdgeCases(t *testing.T) {
    13  	t.Run("red64", func(t *testing.T) { ecRed64(t) })
    14  }
    15  
    16  func ecRed64(t *testing.T) {
    17  	cases := [][2 * Size]byte{
    18  		{
    19  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    20  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    21  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    22  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    23  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    24  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    25  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    26  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    27  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    28  			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    29  			0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
    30  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    31  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    32  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    33  		},
    34  		{
    35  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    36  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    37  			0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xff,
    38  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    39  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    40  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    41  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    42  			0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    43  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    44  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    45  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    46  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    47  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    48  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    49  		},
    50  		{
    51  			0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    52  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    53  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x45, 0xff,
    54  			0xff, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff,
    55  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    56  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    57  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    58  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    59  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    60  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    61  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    62  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    63  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    64  			0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    65  		},
    66  	}
    67  	two448 := big.NewInt(1)
    68  	two448.Lsh(two448, 448) // 2^448
    69  	mask448 := big.NewInt(1)
    70  	mask448.Sub(two448, mask448) // 2^448-1
    71  	two224plus1 := big.NewInt(1)
    72  	two224plus1.Lsh(two224plus1, 224)
    73  	two224plus1.Add(two224plus1, big.NewInt(1)) // 2^224+1
    74  
    75  	var got, want Elt
    76  	var lo, hi [7]uint64
    77  	for _, c := range cases {
    78  		cLo := c[0*Size:]
    79  		cHi := c[1*Size:]
    80  		for i := range lo {
    81  			lo[i] = binary.LittleEndian.Uint64(cLo[i*8 : (i+1)*8])
    82  			hi[i] = binary.LittleEndian.Uint64(cHi[i*8 : (i+1)*8])
    83  		}
    84  		red64(&got, &lo, &hi)
    85  
    86  		tt := conv.BytesLe2BigInt(c[:2*Size])
    87  
    88  		var loBig, hiBig big.Int
    89  		for tt.Cmp(two448) >= 0 {
    90  			loBig.And(tt, mask448)
    91  			hiBig.Rsh(tt, 448)
    92  			tt.Mul(&hiBig, two224plus1)
    93  			tt.Add(tt, &loBig)
    94  		}
    95  		conv.BigInt2BytesLe(want[:], tt)
    96  
    97  		if got != want {
    98  			test.ReportError(t, got, want, conv.BytesLe2BigInt(c[:2*Size]))
    99  		}
   100  	}
   101  }