mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 20:26:19 +08:00
MAGIC - first version of SM2 P256 curve
This commit is contained in:
parent
321bf1fe2d
commit
afb0962761
1375
sm2/p256.go
1375
sm2/p256.go
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,7 @@ package sm2
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"math/big"
|
"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++ {
|
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))
|
Encrypt(rand.Reader, &priv.PublicKey, []byte(plaintext))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLessThan32(b *testing.B) {
|
func BenchmarkLessThan32_P256(b *testing.B) {
|
||||||
benchmarkEncrypt(b, "encryption standard")
|
benchmarkEncrypt(b, elliptic.P256(), "encryption standard")
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkMoreThan32(b *testing.B) {
|
func BenchmarkLessThan32_P256SM2(b *testing.B) {
|
||||||
benchmarkEncrypt(b, "encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard encryption standard")
|
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")
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var zero = new(big.Int).SetInt64(0)
|
var zero = big.NewInt(0)
|
||||||
|
|
||||||
func toBytes(curve elliptic.Curve, value *big.Int) []byte {
|
func toBytes(curve elliptic.Curve, value *big.Int) []byte {
|
||||||
bytes := value.Bytes()
|
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))
|
return nil, nil, 0, fmt.Errorf("invalid compressed bytes length %d", len(bytes))
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(curve.Params().Name, "P-") {
|
if strings.HasPrefix(curve.Params().Name, "P-") {
|
||||||
// y² = x³ - 3x + b
|
// y² = x³ - 3x + b, prime curves
|
||||||
x := toPointXY(bytes[1 : 1+byteLen])
|
x := toPointXY(bytes[1 : 1+byteLen])
|
||||||
y, err := calculatePrimeCurveY(curve, x)
|
y, err := calculatePrimeCurveY(curve, x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user