github.com/emmansun/gmsm@v0.29.1/internal/sm2ec/p256_asm_ord_test.go (about) 1 //go:build (amd64 || arm64 || s390x || ppc64le) && !purego 2 3 package sm2ec 4 5 import ( 6 "crypto/rand" 7 "io" 8 "math/big" 9 "testing" 10 "time" 11 ) 12 13 func ordFromBig(out *p256OrdElement, big *big.Int) { 14 for i := range out { 15 out[i] = 0 16 } 17 18 for i, v := range big.Bits() { 19 out[i] = uint64(v) 20 } 21 } 22 23 func p256OrderSqrTest(t *testing.T, x, p, r *big.Int) { 24 x1 := new(big.Int).Mul(x, r) 25 x1 = x1.Mod(x1, p) 26 ax := new(p256OrdElement) 27 res2 := new(p256OrdElement) 28 ordFromBig(ax, x1) 29 p256OrdSqr(res2, ax, 1) 30 resInt := new(big.Int).SetBytes(p256OrderFromMont(res2)) 31 32 expected := new(big.Int).Mul(x, x) 33 expected = expected.Mod(expected, p) 34 if resInt.Cmp(expected) != 0 { 35 t.Fatalf("expected %x, got %x", expected.Bytes(), resInt.Bytes()) 36 } 37 } 38 39 func TestP256OrdSqrOrdMinus1(t *testing.T) { 40 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 41 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 42 pMinus1 := new(big.Int).Sub(p, big.NewInt(1)) 43 p256OrderSqrTest(t, pMinus1, p, r) 44 } 45 46 func TestFuzzyP256OrdSqr(t *testing.T) { 47 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 48 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 49 var scalar1 [32]byte 50 var timeout *time.Timer 51 52 if testing.Short() { 53 timeout = time.NewTimer(10 * time.Millisecond) 54 } else { 55 timeout = time.NewTimer(2 * time.Second) 56 } 57 for { 58 select { 59 case <-timeout.C: 60 return 61 default: 62 } 63 io.ReadFull(rand.Reader, scalar1[:]) 64 x := new(big.Int).SetBytes(scalar1[:]) 65 p256OrderSqrTest(t, x, p, r) 66 } 67 } 68 69 func BenchmarkP25OrdSqr(b *testing.B) { 70 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 71 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 72 var scalar1 [32]byte 73 io.ReadFull(rand.Reader, scalar1[:]) 74 x := new(big.Int).SetBytes(scalar1[:]) 75 x1 := new(big.Int).Mul(x, r) 76 x1 = x1.Mod(x1, p) 77 ax := new(p256OrdElement) 78 res := new(p256OrdElement) 79 ordFromBig(ax, x1) 80 b.ResetTimer() 81 for i := 0; i < b.N; i++ { 82 p256OrdSqr(res, ax, 20) 83 } 84 } 85 86 func p256OrdMulTest(t *testing.T, x, y, p, r *big.Int) { 87 x1 := new(big.Int).Mul(x, r) 88 x1 = x1.Mod(x1, p) 89 y1 := new(big.Int).Mul(y, r) 90 y1 = y1.Mod(y1, p) 91 ax := new(p256OrdElement) 92 ay := new(p256OrdElement) 93 res2 := new(p256OrdElement) 94 ordFromBig(ax, x1) 95 ordFromBig(ay, y1) 96 p256OrdMul(res2, ax, ay) 97 resInt := new(big.Int).SetBytes(p256OrderFromMont(res2)) 98 99 expected := new(big.Int).Mul(x, y) 100 expected = expected.Mod(expected, p) 101 if resInt.Cmp(expected) != 0 { 102 t.Fatalf("expected %x, got %x", expected.Bytes(), resInt.Bytes()) 103 } 104 } 105 106 func TestP256OrdMulOrdMinus1(t *testing.T) { 107 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 108 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 109 pMinus1 := new(big.Int).Sub(p, big.NewInt(1)) 110 p256OrdMulTest(t, pMinus1, pMinus1, p, r) 111 } 112 113 func TestFuzzyP256OrdMul(t *testing.T) { 114 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 115 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 116 var scalar1 [32]byte 117 var scalar2 [32]byte 118 var timeout *time.Timer 119 120 if testing.Short() { 121 timeout = time.NewTimer(10 * time.Millisecond) 122 } else { 123 timeout = time.NewTimer(2 * time.Second) 124 } 125 for { 126 select { 127 case <-timeout.C: 128 return 129 default: 130 } 131 io.ReadFull(rand.Reader, scalar1[:]) 132 io.ReadFull(rand.Reader, scalar2[:]) 133 x := new(big.Int).SetBytes(scalar1[:]) 134 y := new(big.Int).SetBytes(scalar2[:]) 135 p256OrdMulTest(t, x, y, p, r) 136 } 137 } 138 139 func BenchmarkP25OrdMul(b *testing.B) { 140 p, _ := new(big.Int).SetString("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", 16) 141 r, _ := new(big.Int).SetString("10000000000000000000000000000000000000000000000000000000000000000", 16) 142 var scalar1 [32]byte 143 io.ReadFull(rand.Reader, scalar1[:]) 144 x := new(big.Int).SetBytes(scalar1[:]) 145 x1 := new(big.Int).Mul(x, r) 146 x1 = x1.Mod(x1, p) 147 ax := new(p256OrdElement) 148 res := new(p256OrdElement) 149 ordFromBig(ax, x1) 150 b.ResetTimer() 151 for i := 0; i < b.N; i++ { 152 p256OrdMul(res, ax, ax) 153 } 154 }