github.com/emmansun/gmsm@v0.29.1/internal/sm2ec/fiat/sm2p256scalar_test.go (about)

     1  package fiat_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"math/big"
     7  	"testing"
     8  
     9  	"github.com/emmansun/gmsm/internal/sm2ec/fiat"
    10  )
    11  
    12  var ordN *big.Int
    13  
    14  func init() {
    15  	// n=115792089210356248756420345214020892766061623724957744567843809356293439045923
    16  	// p-n=188730266966446886577384576996245946076
    17  	ordN, _ = new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16)
    18  }
    19  
    20  var testValues = [20]string{
    21  	"e576e1aefe41c42a634a6937982dd8ea60654c4d406ef141018072b8a8ee10ff",
    22  	"374bf8d3ed1a35a109ccc73276e4fa3697d942eafcd514a82a985d0820f02645",
    23  	"d62fd995bdc9ed6d405cad6a5cd48e0b92b465c2c8fbb7b14cc86e16e6dba6e8",
    24  	"a8c28fe4b2c4abad3759ac3cb97c23eb0440273277f8d8be794eea0a2561357d",
    25  	"f3bcfff783d0eb4de34bffd0c6290f75381bf715a1bc2b02ffbb58cc794ef1b7",
    26  	"a08b119bb9bf49b2cda951de57df6e95f413a609aefa51eefa554a4906963942",
    27  	"1b767aabebdf28a447de4c37b18d8c86e431c70acbb6d05eab459180e3731075",
    28  	"40616625f9dd4e7c396106e539ed7891636acfb3ba7f80e72dc305b8cb2955d8",
    29  	"3246e27330be55dc574e97a9e0c5ab6a476bb2b5422e8c47b2248a40504fc8a0",
    30  	"aa54dec0a14ee69417186ff2711e59282d5badc3faa1528c4171e14baa525865",
    31  	"408817dd964bd439aec08c3ebda707dc8ff969d25aef0ec0ba6085bc8da6996f",
    32  	"99ed1792abdda9f0e43fd50c59a57b7f9c3c60d69c8046c71b67a1a71d9f7d55",
    33  	"455705f9823bd5ba6f58c2a4dbdf6f10a0de1947a82c2653b00833ea39e26b5d",
    34  	"b43fdba6043be8524bcc4cd6ab7d71534fcaf42869ab838e98608d5e9d801cf9",
    35  	"c97498821b3b4db41239d1a3d47d49754e5e6b7bb7ae21d4eb0826bd5c0aeed6",
    36  	"c0213f02d06c935b798594c9c3b4feaebea881205733a21484a48df4643fbde7",
    37  	"313c9f7129eb1a09c385dc755aab9d88fcab79a7e4deaca68dd08d93fd68d252",
    38  	"eb7b96f239402bd494dc258672cd4a1643ae9fe092ddaaca54f9e909548eaa90",
    39  	"24567a167761a040aed80ea4655616b5aae5a0548b2a2a39a99bd4a6d7791610",
    40  	"c79886c5cd9de1f2a0deee1c76cd8c38da7dcd401f59ec4bebbaf815006f2f71",
    41  }
    42  
    43  func p256OrderMulTest(t *testing.T, x, y, n *big.Int) {
    44  	var scalar1 [32]byte
    45  	var scalar2 [32]byte
    46  	var scalar [32]byte
    47  	x1 := new(big.Int).Mod(x, n)
    48  	y1 := new(big.Int).Mod(y, n)
    49  	ax := new(fiat.SM2P256OrderElement)
    50  	ay := new(fiat.SM2P256OrderElement)
    51  	res := new(fiat.SM2P256OrderElement)
    52  	x1.FillBytes(scalar1[:])
    53  	y1.FillBytes(scalar2[:])
    54  	_, err := ax.SetBytes(scalar1[:])
    55  	if err != nil {
    56  		t.Error(err)
    57  	}
    58  	if !bytes.Equal(scalar1[:], ax.Bytes()) {
    59  		t.Errorf("x SetBytes/Bytes error, expected %v, got %v\n", hex.EncodeToString(scalar1[:]), hex.EncodeToString(ax.Bytes()))
    60  	}
    61  	_, err = ay.SetBytes(scalar2[:])
    62  	if err != nil {
    63  		t.Error(err)
    64  	}
    65  	if !bytes.Equal(scalar2[:], ay.Bytes()) {
    66  		t.Errorf("y SetBytes/Bytes error, expected %v, got %v\n", hex.EncodeToString(scalar2[:]), hex.EncodeToString(ay.Bytes()))
    67  	}
    68  	res = res.Mul(ax, ay)
    69  	expected := new(big.Int).Mul(x1, y1)
    70  	expected = expected.Mod(expected, n)
    71  	expected.FillBytes(scalar[:])
    72  	if !bytes.Equal(res.Bytes(), scalar[:]) {
    73  		t.Errorf("expected %v, got %v\n", hex.EncodeToString(scalar[:]), hex.EncodeToString(res.Bytes()))
    74  	}
    75  }
    76  
    77  func TestP256Mul(t *testing.T) {
    78  	for i := 0; i < 20; i += 2 {
    79  		x, _ := new(big.Int).SetString(testValues[i], 16)
    80  		y, _ := new(big.Int).SetString(testValues[i+1], 16)
    81  		p256OrderMulTest(t, x, y, ordN)
    82  	}
    83  }
    84  
    85  func TestP256Square(t *testing.T) {
    86  	var scalar [32]byte
    87  	for i := 0; i < 20; i++ {
    88  		x, _ := new(big.Int).SetString(testValues[i], 16)
    89  		ax := new(fiat.SM2P256OrderElement)
    90  		ax.SetBytes(x.Bytes())
    91  		res := new(fiat.SM2P256OrderElement)
    92  		res.Square(ax)
    93  		expected := new(big.Int).Mul(x, x)
    94  		expected = expected.Mod(expected, ordN)
    95  		expected.FillBytes(scalar[:])
    96  		if !bytes.Equal(res.Bytes(), scalar[:]) {
    97  			t.Errorf("expected %v, got %v\n", hex.EncodeToString(scalar[:]), hex.EncodeToString(res.Bytes()))
    98  		}
    99  	}
   100  }
   101  
   102  func TestP256Add(t *testing.T) {
   103  	var scalar [32]byte
   104  	for i := 0; i < 20; i += 2 {
   105  		x, _ := new(big.Int).SetString(testValues[i], 16)
   106  		y, _ := new(big.Int).SetString(testValues[i+1], 16)
   107  		expected := new(big.Int).Add(x, y)
   108  		expected = expected.Mod(expected, ordN)
   109  		expected.FillBytes(scalar[:])
   110  
   111  		ax := new(fiat.SM2P256OrderElement)
   112  		ax.SetBytes(x.Bytes())
   113  
   114  		ay := new(fiat.SM2P256OrderElement)
   115  		ay.SetBytes(y.Bytes())
   116  
   117  		res := new(fiat.SM2P256OrderElement)
   118  		res.Add(ax, ay)
   119  
   120  		if !bytes.Equal(res.Bytes(), scalar[:]) {
   121  			t.Errorf("expected %v, got %v\n", hex.EncodeToString(scalar[:]), hex.EncodeToString(res.Bytes()))
   122  		}
   123  	}
   124  }