Source File
ecdsa.go
Belonging Package
crypto/ecdsa
package ecdsa
import (
)
type PrivateKey struct {
PublicKey
D *big.Int
}
func ( *PrivateKey) () crypto.PublicKey {
return &.PublicKey
}
func ( *PrivateKey) ( crypto.PrivateKey) bool {
, := .(*PrivateKey)
if ! {
return false
}
return .PublicKey.Equal(&.PublicKey) && .D.Cmp(.D) == 0
}
func ( *PrivateKey) ( io.Reader, []byte, crypto.SignerOpts) ([]byte, error) {
, , := Sign(, , )
if != nil {
return nil,
}
var cryptobyte.Builder
.AddASN1(asn1.SEQUENCE, func( *cryptobyte.Builder) {
.AddASN1BigInt()
.AddASN1BigInt()
})
return .Bytes()
}
var one = new(big.Int).SetInt64(1)
func ( io.Reader, *PrivateKey, []byte) (, *big.Int, error) {
randutil.MaybeReadByte()
:= cipher.StreamReader{
R: zeroReader,
S: cipher.NewCTR(, []byte(aesIV)),
}
:= .PublicKey.Curve
return sign(, &, , )
}
func ( *PrivateKey, *cipher.StreamReader, elliptic.Curve, []byte) (, *big.Int, error) {
:= .Params().N
if .Sign() == 0 {
return nil, nil, errZeroParam
}
var , *big.Int
for {
for {
, = randFieldElement(, *)
if != nil {
= nil
return
}
if , := .Curve.(invertible); {
= .Inverse()
} else {
= fermatInverse(, ) // N != 0
}
, _ = .Curve.ScalarBaseMult(.Bytes())
.Mod(, )
if .Sign() != 0 {
break
}
}
:= hashToInt(, )
= new(big.Int).Mul(.D, )
.Add(, )
.Mul(, )
.Mod(, ) // N != 0
if .Sign() != 0 {
break
}
}
return
}
:= .Curve
:= .Params().N
if .Sign() <= 0 || .Sign() <= 0 {
return false
}
if .Cmp() >= 0 || .Cmp() >= 0 {
return false
}
return verify(, , , , )
}
func ( *PublicKey, elliptic.Curve, []byte, , *big.Int) bool {
:= hashToInt(, )
var *big.Int
:= .Params().N
if , := .(invertible); {
= .Inverse()
} else {
= new(big.Int).ModInverse(, )
}
:= .Mul(, )
.Mod(, )
:= .Mul(, )
.Mod(, )
var , *big.Int
if , := .(combinedMult); {
, = .CombinedMult(.X, .Y, .Bytes(), .Bytes())
} else {
, := .ScalarBaseMult(.Bytes())
, := .ScalarMult(.X, .Y, .Bytes())
, = .Add(, , , )
}
if .Sign() == 0 && .Sign() == 0 {
return false
}
.Mod(, )
return .Cmp() == 0
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |