init: v1.0.0
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package sm2
|
||||
|
||||
import (
|
||||
"hash"
|
||||
"math/big"
|
||||
|
||||
"xdx.jelly/xgcl/gmath"
|
||||
"xdx.jelly/xgcl/sm/sm3"
|
||||
)
|
||||
|
||||
var (
|
||||
// abg曲线相关参数,若曲线参数改变了,应该改变这里的数值。
|
||||
// 在init中从sm2Curve.Params()生成。
|
||||
abg []byte
|
||||
)
|
||||
|
||||
func initPrecompute() {
|
||||
abg = make([]byte, 0, 4*byteSize)
|
||||
abg = append(abg, gmath.BigIntToNByte(new(big.Int).Sub(sm2Curve.Params().P, gmath.BigInt3), byteSize)...)
|
||||
abg = append(abg, gmath.BigIntToNByte(sm2Curve.Params().B, byteSize)...)
|
||||
abg = append(abg, gmath.BigIntToNByte(sm2Curve.Params().Gx, byteSize)...)
|
||||
abg = append(abg, gmath.BigIntToNByte(sm2Curve.Params().Gy, byteSize)...)
|
||||
|
||||
// abg = []byte{
|
||||
// 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* a */
|
||||
// 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
|
||||
// 0xFF, 0xFC,
|
||||
// 0x28, 0xE9, 0xFA, 0x9E, 0x9D, 0x9F, 0x5E, 0x34, 0x4D, 0x5A, /* b */
|
||||
// 0x9E, 0x4B, 0xCF, 0x65, 0x09, 0xA7, 0xF3, 0x97, 0x89, 0xF5,
|
||||
// 0x15, 0xAB, 0x8F, 0x92, 0xDD, 0xBC, 0xBD, 0x41, 0x4D, 0x94,
|
||||
// 0x0E, 0x93,
|
||||
// 0x32, 0xC4, 0xAE, 0x2C, 0x1F, 0x19, 0x81, 0x19, 0x5F, 0x99, /* Gx */
|
||||
// 0x04, 0x46, 0x6A, 0x39, 0xC9, 0x94, 0x8F, 0xE3, 0x0B, 0xBF,
|
||||
// 0xF2, 0x66, 0x0B, 0xE1, 0x71, 0x5A, 0x45, 0x89, 0x33, 0x4C,
|
||||
// 0x74, 0xC7,
|
||||
// 0xBC, 0x37, 0x36, 0xA2, 0xF4, 0xF6, 0x77, 0x9C, 0x59, 0xBD, /* Gy */
|
||||
// 0xCE, 0xE3, 0x6B, 0x69, 0x21, 0x53, 0xD0, 0xA9, 0x87, 0x7C,
|
||||
// 0xC6, 0x2A, 0x47, 0x40, 0x02, 0xDF, 0x32, 0xE5, 0x21, 0x39,
|
||||
// 0xF0, 0xA0,
|
||||
// }
|
||||
}
|
||||
|
||||
// GetDefaultID rt
|
||||
func GetDefaultID() []byte {
|
||||
|
||||
return []byte{
|
||||
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
||||
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
|
||||
}
|
||||
}
|
||||
|
||||
// PreComputeWithIdAndPubkey 预计算Z
|
||||
// id = nil使用默认id, len(id)==0则使用空, 如[]byte("")
|
||||
// 注:基本上所有情况下,都是用默认id。所以id仅可传入默认id或nil.
|
||||
func PreComputeWithIdAndPubkey(id []byte, pubkey *PublicKey) []byte {
|
||||
var entl [2]byte
|
||||
var d hash.Hash
|
||||
|
||||
if id == nil {
|
||||
d = sm3.NewSm2Precomputed()
|
||||
} else {
|
||||
d = sm3.New()
|
||||
entl[0] = byte((len(id) >> 5) & 0xff)
|
||||
entl[1] = byte((len(id) << 3) & 0xff)
|
||||
d.Write(entl[:])
|
||||
d.Write(id)
|
||||
d.Write(abg)
|
||||
}
|
||||
|
||||
d.Write(gmath.BigIntToNByte(pubkey.X, byteSize))
|
||||
d.Write(gmath.BigIntToNByte(pubkey.Y, byteSize))
|
||||
return d.Sum(nil)
|
||||
}
|
||||
|
||||
// PreComputeWithIdAndPubkeyAndMessage 计算 e=hash(z||m)
|
||||
// z由PreComputeWithIdAndPubkey计算
|
||||
func PreComputeWithIdAndPubkeyAndMessage(id, msg []byte, pubkey *PublicKey) []byte {
|
||||
e := PreComputeWithIdAndPubkey(id, pubkey)
|
||||
d := sm3.New()
|
||||
d.Write(e)
|
||||
d.Write(msg)
|
||||
return d.Sum(e[:0])
|
||||
}
|
||||
Reference in New Issue
Block a user