Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Sep 29, 2024
1 parent a9e6500 commit ac659a5
Show file tree
Hide file tree
Showing 19 changed files with 740 additions and 612 deletions.
5 changes: 1 addition & 4 deletions elliptic/base_elliptic/elliptic.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package base_elliptic

// Package `base_elliptic` implements Elliptic curves over binary fields

// from github.com/RyuaNerin/elliptic2
package base_elliptic

// Create new elliptic curves over binary fields
// warning: params dose not validated.
Expand Down
55 changes: 0 additions & 55 deletions elliptic/bign/bign.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package bign

import (
"sync"
"math/big"
"encoding/asn1"
"crypto/elliptic"
)
Expand All @@ -13,50 +11,6 @@ var (
OIDNamedCurveP512v1 = asn1.ObjectIdentifier{1, 2, 112, 0, 2, 0, 34, 101, 45, 3, 3}
)

var (
once sync.Once
p256v1, p384v1, p512v1 *elliptic.CurveParams
)

func initAll() {
initP256v1()
initP384v1()
initP512v1()
}

func initP256v1() {
p256v1 = new(elliptic.CurveParams)
p256v1.P = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43")
p256v1.N = bigFromHex("ffffffffffffffffffffffffffffffffd95c8ed60dfb4dfc7e5abf99263d6607")
p256v1.B = bigFromHex("77ce6c1515f3a8edd2c13aabe4d8fbbe4cf55069978b9253b22e7d6bd69c03f1")
p256v1.Gx = bigFromHex("0000000000000000000000000000000000000000000000000000000000000000")
p256v1.Gy = bigFromHex("6bf7fc3cfb16d69f5ce4c9a351d6835d78913966c408f6521e29cf1804516a93")
p256v1.BitSize = 256
p256v1.Name = "BIGN256V1"
}

func initP384v1() {
p384v1 = new(elliptic.CurveParams)
p384v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec3")
p384v1.N = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffe6cccc40373af7bbb8046dae7a6a4ff0a3db7dc3ff30ca7b7")
p384v1.B = bigFromHex("3c75dfe1959cef2033075aab655d34d2712748bb0ffbb196a6216af9e9712e3a14bde2f0f3cebd7cbca7fc236873bf64")
p384v1.Gx = bigFromHex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
p384v1.Gy = bigFromHex("5d438224a82e9e9e6330117e432dbf893a729a11dc86ffa00549e79e66b1d35584403e276b2a42f9ea5ecb31f733c451")
p384v1.BitSize = 384
p384v1.Name = "BIGN384V1"
}

func initP512v1() {
p512v1 = new(elliptic.CurveParams)
p512v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7")
p512v1.N = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb2c0092c0198004ef26bebb02e2113f4361bcae59556df32dcffad490d068ef1")
p512v1.B = bigFromHex("6cb45944933b8c43d88c5d6a60fd58895bc6a9eedd5d255117ce13e3daadb0882711dcb5c4245e952933008c87aca243ea8622273a49a27a09346998d6139c90")
p512v1.Gx = bigFromHex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
p512v1.Gy = bigFromHex("a826ff7ae4037681b182e6f7a0d18fabb0ab41b3b361bce2d2edf81b00cccada6973dde20efa6fd2ff777395eee8226167aa83b9c94c0d04b792ae6fceefedbd")
p512v1.BitSize = 512
p512v1.Name = "BIGN512V1"
}

func P256v1() elliptic.Curve {
once.Do(initAll)
return p256v1
Expand All @@ -71,12 +25,3 @@ func P512v1() elliptic.Curve {
once.Do(initAll)
return p512v1
}

func bigFromHex(s string) *big.Int {
b, ok := new(big.Int).SetString(s, 16)
if !ok {
panic("go-cryptobin/bign: invalid encoding")
}

return b
}
60 changes: 60 additions & 0 deletions elliptic/bign/bign_curves.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package bign

import (
"sync"
"math/big"
"crypto/elliptic"
)

var (
once sync.Once
p256v1, p384v1, p512v1 *elliptic.CurveParams
)

func initAll() {
initP256v1()
initP384v1()
initP512v1()
}

func initP256v1() {
p256v1 = new(elliptic.CurveParams)
p256v1.P = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff43")
p256v1.N = bigFromHex("ffffffffffffffffffffffffffffffffd95c8ed60dfb4dfc7e5abf99263d6607")
p256v1.B = bigFromHex("77ce6c1515f3a8edd2c13aabe4d8fbbe4cf55069978b9253b22e7d6bd69c03f1")
p256v1.Gx = bigFromHex("0000000000000000000000000000000000000000000000000000000000000000")
p256v1.Gy = bigFromHex("6bf7fc3cfb16d69f5ce4c9a351d6835d78913966c408f6521e29cf1804516a93")
p256v1.BitSize = 256
p256v1.Name = "BIGN256V1"
}

func initP384v1() {
p384v1 = new(elliptic.CurveParams)
p384v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec3")
p384v1.N = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffe6cccc40373af7bbb8046dae7a6a4ff0a3db7dc3ff30ca7b7")
p384v1.B = bigFromHex("3c75dfe1959cef2033075aab655d34d2712748bb0ffbb196a6216af9e9712e3a14bde2f0f3cebd7cbca7fc236873bf64")
p384v1.Gx = bigFromHex("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
p384v1.Gy = bigFromHex("5d438224a82e9e9e6330117e432dbf893a729a11dc86ffa00549e79e66b1d35584403e276b2a42f9ea5ecb31f733c451")
p384v1.BitSize = 384
p384v1.Name = "BIGN384V1"
}

func initP512v1() {
p512v1 = new(elliptic.CurveParams)
p512v1.P = bigFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7")
p512v1.N = bigFromHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffb2c0092c0198004ef26bebb02e2113f4361bcae59556df32dcffad490d068ef1")
p512v1.B = bigFromHex("6cb45944933b8c43d88c5d6a60fd58895bc6a9eedd5d255117ce13e3daadb0882711dcb5c4245e952933008c87aca243ea8622273a49a27a09346998d6139c90")
p512v1.Gx = bigFromHex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
p512v1.Gy = bigFromHex("a826ff7ae4037681b182e6f7a0d18fabb0ab41b3b361bce2d2edf81b00cccada6973dde20efa6fd2ff777395eee8226167aa83b9c94c0d04b792ae6fceefedbd")
p512v1.BitSize = 512
p512v1.Name = "BIGN512V1"
}

func bigFromHex(s string) *big.Int {
b, ok := new(big.Int).SetString(s, 16)
if !ok {
panic("go-cryptobin/bign: invalid encoding")
}

return b
}
89 changes: 10 additions & 79 deletions elliptic/bitcurves/bitcurve.go
Original file line number Diff line number Diff line change
@@ -1,88 +1,19 @@
// Package bitelliptic implements several Koblitz elliptic curves over prime fields.
package bitcurves

// Copyright 2010 The Go Authors. All rights reserved.
// Copyright 2011 ThePiachu. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package bitelliptic implements several Koblitz elliptic curves over prime
// fields.

// This package operates, internally, on Jacobian coordinates. For a given
// (x, y) position on the curve, the Jacobian coordinates are (x1, y1, z1)
// where x = x1/z1² and y = y1/z1³. The greatest speedups come when the whole
// calculation can be performed within the transform (as in ScalarMult and
// ScalarBaseMult). But even for Add and Double, it's faster to apply and
// reverse the transform than to operate in affine coordinates.

import (
"sync"
"math/big"
"encoding/asn1"
)

//curve parameters taken from:
//http://www.secg.org/collateral/sec2_final.pdf

var initonce sync.Once
var secp160k1 *BitCurve
var secp192k1 *BitCurve
var secp224k1 *BitCurve
var secp256k1 *BitCurve

func initAll() {
initS160()
initS192()
initS224()
initS256()
}

func initS160() {
// See SEC 2 section 2.4.1
secp160k1 = new(BitCurve)
secp160k1.Name = "secp160k1"
secp160k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", 16)
secp160k1.N, _ = new(big.Int).SetString("0100000000000000000001B8FA16DFAB9ACA16B6B3", 16)
secp160k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000007", 16)
secp160k1.Gx, _ = new(big.Int).SetString("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", 16)
secp160k1.Gy, _ = new(big.Int).SetString("938CF935318FDCED6BC28286531733C3F03C4FEE", 16)
secp160k1.BitSize = 160
}

func initS192() {
// See SEC 2 section 2.5.1
secp192k1 = new(BitCurve)
secp192k1.Name = "secp192k1"
secp192k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", 16)
secp192k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", 16)
secp192k1.B, _ = new(big.Int).SetString("000000000000000000000000000000000000000000000003", 16)
secp192k1.Gx, _ = new(big.Int).SetString("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", 16)
secp192k1.Gy, _ = new(big.Int).SetString("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", 16)
secp192k1.BitSize = 192
}
// curve parameters taken from:
// http://www.secg.org/collateral/sec2_final.pdf

func initS224() {
// See SEC 2 section 2.6.1
secp224k1 = new(BitCurve)
secp224k1.Name = "secp224k1"
secp224k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", 16)
secp224k1.N, _ = new(big.Int).SetString("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7", 16)
secp224k1.B, _ = new(big.Int).SetString("00000000000000000000000000000000000000000000000000000005", 16)
secp224k1.Gx, _ = new(big.Int).SetString("A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", 16)
secp224k1.Gy, _ = new(big.Int).SetString("7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", 16)
secp224k1.BitSize = 224
}

func initS256() {
// See SEC 2 section 2.7.1
secp256k1 = new(BitCurve)
secp256k1.Name = "secp256k1"
secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16)
secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16)
secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16)
secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
secp256k1.BitSize = 256
}
var (
OIDNamedCurveSecp160k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 9}
OIDNamedCurveSecp192k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 31}
OIDNamedCurveSecp224k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 32}
OIDNamedCurveSecp256k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 10}
)

// S160 returns a BitCurve which implements secp160k1 (see SEC 2 section 2.4.1)
func S160() *BitCurve {
Expand Down
70 changes: 70 additions & 0 deletions elliptic/bitcurves/bitcurve_curves.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Package bitelliptic implements several Koblitz elliptic curves over prime fields.
package bitcurves

import (
"sync"
"math/big"
)

// curve parameters taken from:
// http://www.secg.org/collateral/sec2_final.pdf

var (
initonce sync.Once
secp160k1, secp192k1, secp224k1, secp256k1 *BitCurve
)

func initAll() {
initS160()
initS192()
initS224()
initS256()
}

func initS160() {
// See SEC 2 section 2.4.1
secp160k1 = new(BitCurve)
secp160k1.Name = "secp160k1"
secp160k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", 16)
secp160k1.N, _ = new(big.Int).SetString("0100000000000000000001B8FA16DFAB9ACA16B6B3", 16)
secp160k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000007", 16)
secp160k1.Gx, _ = new(big.Int).SetString("3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", 16)
secp160k1.Gy, _ = new(big.Int).SetString("938CF935318FDCED6BC28286531733C3F03C4FEE", 16)
secp160k1.BitSize = 160
}

func initS192() {
// See SEC 2 section 2.5.1
secp192k1 = new(BitCurve)
secp192k1.Name = "secp192k1"
secp192k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", 16)
secp192k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", 16)
secp192k1.B, _ = new(big.Int).SetString("000000000000000000000000000000000000000000000003", 16)
secp192k1.Gx, _ = new(big.Int).SetString("DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", 16)
secp192k1.Gy, _ = new(big.Int).SetString("9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", 16)
secp192k1.BitSize = 192
}

func initS224() {
// See SEC 2 section 2.6.1
secp224k1 = new(BitCurve)
secp224k1.Name = "secp224k1"
secp224k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D", 16)
secp224k1.N, _ = new(big.Int).SetString("010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7", 16)
secp224k1.B, _ = new(big.Int).SetString("00000000000000000000000000000000000000000000000000000005", 16)
secp224k1.Gx, _ = new(big.Int).SetString("A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C", 16)
secp224k1.Gy, _ = new(big.Int).SetString("7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5", 16)
secp224k1.BitSize = 224
}

func initS256() {
// See SEC 2 section 2.7.1
secp256k1 = new(BitCurve)
secp256k1.Name = "secp256k1"
secp256k1.P, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16)
secp256k1.N, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
secp256k1.B, _ = new(big.Int).SetString("0000000000000000000000000000000000000000000000000000000000000007", 16)
secp256k1.Gx, _ = new(big.Int).SetString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16)
secp256k1.Gy, _ = new(big.Int).SetString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16)
secp256k1.BitSize = 256
}
16 changes: 12 additions & 4 deletions elliptic/koblitz/koblitz.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package koblitz

// Support for Koblitz elliptic curves
// http://www.secg.org/SEC2-Ver-1.0.pdf

import (
"encoding/asn1"
"crypto/elliptic"

"github.com/deatil/go-cryptobin/elliptic/bitcurves"
)

// Support for Koblitz elliptic curves
// http://www.secg.org/SEC2-Ver-1.0.pdf

var (
OIDNamedCurveSecp160k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 9}
OIDNamedCurveSecp192k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 31}
OIDNamedCurveSecp224k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 32}
OIDNamedCurveSecp256k1 = asn1.ObjectIdentifier{1, 3, 132, 0, 10}
)

func P160k1() elliptic.Curve {
return bitcurves.S160()
}
Expand Down
15 changes: 5 additions & 10 deletions elliptic/nist/nist_curves.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,11 @@ import (
var initonce sync.Once

var (
k163 base_elliptic.Curve
b163 base_elliptic.Curve
k233 base_elliptic.Curve
b233 base_elliptic.Curve
k283 base_elliptic.Curve
b283 base_elliptic.Curve
k409 base_elliptic.Curve
b409 base_elliptic.Curve
k571 base_elliptic.Curve
b571 base_elliptic.Curve
k163, b163 base_elliptic.Curve
k233, b233 base_elliptic.Curve
k283, b283 base_elliptic.Curve
k409, b409 base_elliptic.Curve
k571, b571 base_elliptic.Curve
)

func initAll() {
Expand Down
Loading

0 comments on commit ac659a5

Please sign in to comment.