MAGIC - first version of SM2 P256 curve

This commit is contained in:
Emman 2021-01-19 14:23:56 +08:00
parent 321bf1fe2d
commit afb0962761
3 changed files with 1393 additions and 9 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@ package sm2
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"math/big"
@ -55,17 +56,25 @@ func Test_encryptDecrypt(t *testing.T) {
}
}
func benchmarkEncrypt(b *testing.B, plaintext string) {
func benchmarkEncrypt(b *testing.B, curve elliptic.Curve, plaintext string) {
for i := 0; i < b.N; i++ {
priv, _ := ecdsa.GenerateKey(P256(), rand.Reader)
priv, _ := ecdsa.GenerateKey(curve, rand.Reader)
Encrypt(rand.Reader, &priv.PublicKey, []byte(plaintext))
}
}
func BenchmarkLessThan32(b *testing.B) {
benchmarkEncrypt(b, "encryption standard")
func BenchmarkLessThan32_P256(b *testing.B) {
benchmarkEncrypt(b, elliptic.P256(), "encryption standard")
}
func BenchmarkMoreThan32(b *testing.B) {
benchmarkEncrypt(b, "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard")
func BenchmarkLessThan32_P256SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard")
}
func BenchmarkMoreThan32_P256(b *testing.B) {
benchmarkEncrypt(b, elliptic.P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard")
}
func BenchmarkMoreThan32_P256SM2(b *testing.B) {
benchmarkEncrypt(b, P256(), "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard")
}

View File

@ -8,7 +8,7 @@ import (
"strings"
)
var zero = new(big.Int).SetInt64(0)
var zero = big.NewInt(0)
func toBytes(curve elliptic.Curve, value *big.Int) []byte {
bytes := value.Bytes()
@ -97,7 +97,7 @@ func bytes2Point(curve elliptic.Curve, bytes []byte) (*big.Int, *big.Int, int, e
return nil, nil, 0, fmt.Errorf("invalid compressed bytes length %d", len(bytes))
}
if strings.HasPrefix(curve.Params().Name, "P-") {
// y² = x³ - 3x + b
// y² = x³ - 3x + b, prime curves
x := toPointXY(bytes[1 : 1+byteLen])
y, err := calculatePrimeCurveY(curve, x)
if err != nil {