package elliptic
import (
)
var p224 = &nistCurve[*nistec.P224Point]{
newPoint: nistec.NewP224Point,
}
func () {
p224.params = &CurveParams{
Name: "P-224",
BitSize: 224,
P: bigFromDecimal("26959946667150639794667015087019630673557916260026308143510066298881"),
N: bigFromDecimal("26959946667150639794667015087019625940457807714424391721682722368061"),
B: bigFromHex("b4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"),
Gx: bigFromHex("b70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
Gy: bigFromHex("bd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
}
}
type p256Curve struct {
nistCurve[*nistec.P256Point]
}
var p256 = &p256Curve{nistCurve[*nistec.P256Point]{
newPoint: nistec.NewP256Point,
}}
func () {
p256.params = &CurveParams{
Name: "P-256",
BitSize: 256,
P: bigFromDecimal("115792089210356248762697446949407573530086143415290314195533631308867097853951"),
N: bigFromDecimal("115792089210356248762697446949407573529996955224135760342422259061068512044369"),
B: bigFromHex("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"),
Gx: bigFromHex("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"),
Gy: bigFromHex("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"),
}
}
var p384 = &nistCurve[*nistec.P384Point]{
newPoint: nistec.NewP384Point,
}
func () {
p384.params = &CurveParams{
Name: "P-384",
BitSize: 384,
P: bigFromDecimal("394020061963944792122790401001436138050797392704654" +
"46667948293404245721771496870329047266088258938001861606973112319"),
N: bigFromDecimal("394020061963944792122790401001436138050797392704654" +
"46667946905279627659399113263569398956308152294913554433653942643"),
B: bigFromHex("b3312fa7e23ee7e4988e056be3f82d19181d9c6efe8141120314088" +
"f5013875ac656398d8a2ed19d2a85c8edd3ec2aef"),
Gx: bigFromHex("aa87ca22be8b05378eb1c71ef320ad746e1d3b628ba79b9859f741" +
"e082542a385502f25dbf55296c3a545e3872760ab7"),
Gy: bigFromHex("3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da31" +
"13b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f"),
}
}
var p521 = &nistCurve[*nistec.P521Point]{
newPoint: nistec.NewP521Point,
}
func () {
p521.params = &CurveParams{
Name: "P-521",
BitSize: 521,
P: bigFromDecimal("68647976601306097149819007990813932172694353001433" +
"0540939446345918554318339765605212255964066145455497729631139148" +
"0858037121987999716643812574028291115057151"),
N: bigFromDecimal("68647976601306097149819007990813932172694353001433" +
"0540939446345918554318339765539424505774633321719753296399637136" +
"3321113864768612440380340372808892707005449"),
B: bigFromHex("0051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8" +
"b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef" +
"451fd46b503f00"),
Gx: bigFromHex("00c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f8" +
"28af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf9" +
"7e7e31c2e5bd66"),
Gy: bigFromHex("011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817" +
"afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088" +
"be94769fd16650"),
}
}
type nistCurve[ nistPoint[]] struct {
newPoint func()
params *CurveParams
}
type nistPoint[ any] interface {
Bytes() []byte
SetBytes([]byte) (, error)
Add(, )
Double()
ScalarMult(, []byte) (, error)
ScalarBaseMult([]byte) (, error)
}
func ( *nistCurve[]) () *CurveParams {
return .params
}
func ( *nistCurve[]) (, *big.Int) bool {
if .Sign() == 0 && .Sign() == 0 {
return false
}
, := .pointFromAffine(, )
return == nil
}
func ( *nistCurve[]) (, *big.Int) ( , error) {
if .Sign() == 0 && .Sign() == 0 {
return .newPoint(), nil
}
if .Sign() < 0 || .Sign() < 0 {
return , errors.New("negative coordinate")
}
if .BitLen() > .params.BitSize || .BitLen() > .params.BitSize {
return , errors.New("overflowing coordinate")
}
:= (.params.BitSize + 7) / 8
:= make([]byte, 1+2*)
[0] = 4
.FillBytes([1 : 1+])
.FillBytes([1+ : 1+2*])
return .newPoint().SetBytes()
}
func ( *nistCurve[]) ( ) (, *big.Int) {
:= .Bytes()
if len() == 1 && [0] == 0 {
return new(big.Int), new(big.Int)
}
:= (.params.BitSize + 7) / 8
= new(big.Int).SetBytes([1 : 1+])
= new(big.Int).SetBytes([1+:])
return ,
}
func ( *nistCurve[]) (, , , *big.Int) (*big.Int, *big.Int) {
, := .pointFromAffine(, )
if != nil {
panic("crypto/elliptic: Add was called on an invalid point")
}
, := .pointFromAffine(, )
if != nil {
panic("crypto/elliptic: Add was called on an invalid point")
}
return .pointToAffine(.Add(, ))
}
func ( *nistCurve[]) (, *big.Int) (*big.Int, *big.Int) {
, := .pointFromAffine(, )
if != nil {
panic("crypto/elliptic: Double was called on an invalid point")
}
return .pointToAffine(.Double())
}
func ( *nistCurve[]) ( []byte) []byte {
:= (.params.N.BitLen() + 7) / 8
if len() == {
return
}
:= new(big.Int).SetBytes()
if len() > {
.Mod(, .params.N)
}
:= make([]byte, )
return .FillBytes()
}
func ( *nistCurve[]) (, *big.Int, []byte) (*big.Int, *big.Int) {
, := .pointFromAffine(, )
if != nil {
panic("crypto/elliptic: ScalarMult was called on an invalid point")
}
= .normalizeScalar()
, = .ScalarMult(, )
if != nil {
panic("crypto/elliptic: nistec rejected normalized scalar")
}
return .pointToAffine()
}
func ( *nistCurve[]) ( []byte) (*big.Int, *big.Int) {
= .normalizeScalar()
, := .newPoint().ScalarBaseMult()
if != nil {
panic("crypto/elliptic: nistec rejected normalized scalar")
}
return .pointToAffine()
}
func ( *nistCurve[]) (, *big.Int, , []byte) (, *big.Int) {
= .normalizeScalar()
, := .newPoint().ScalarBaseMult()
if != nil {
panic("crypto/elliptic: nistec rejected normalized scalar")
}
, := .pointFromAffine(, )
if != nil {
panic("crypto/elliptic: CombinedMult was called on an invalid point")
}
= .normalizeScalar()
, = .ScalarMult(, )
if != nil {
panic("crypto/elliptic: nistec rejected normalized scalar")
}
return .pointToAffine(.Add(, ))
}
func ( *nistCurve[]) ( []byte) (, *big.Int) {
if len() == 0 || [0] != 4 {
return nil, nil
}
, := .newPoint().SetBytes()
if != nil {
return nil, nil
}
:= (.params.BitSize + 7) / 8
= new(big.Int).SetBytes([1 : 1+])
= new(big.Int).SetBytes([1+:])
return ,
}
func ( *nistCurve[]) ( []byte) (, *big.Int) {
if len() == 0 || ([0] != 2 && [0] != 3) {
return nil, nil
}
, := .newPoint().SetBytes()
if != nil {
return nil, nil
}
return .pointToAffine()
}
func ( string) *big.Int {
, := new(big.Int).SetString(, 10)
if ! {
panic("crypto/elliptic: internal error: invalid encoding")
}
return
}
func ( string) *big.Int {
, := new(big.Int).SetString(, 16)
if ! {
panic("crypto/elliptic: internal error: invalid encoding")
}
return
}