github.com/LagrangeDev/LagrangeGo@v0.0.0-20240512064304-ad4a85e10cb4/utils/crypto/ecdh/curve.go (about)

     1  package ecdh
     2  
     3  /*
     4  
     5  import (
     6  	"math/big"
     7  )
     8  
     9  type ec struct {
    10  	p, a, b  *big.Int
    11  	g        *ep
    12  	n, h     *big.Int
    13  	size     *big.Int
    14  	packSize *big.Int
    15  }
    16  
    17  func newEllipticCurve(P, A, B *big.Int, G *ep, N, H, Size, PackSize *big.Int) *ec {
    18  	return &ec{p: P, a: A, b: B, g: G, n: N, h: H, size: Size, packSize: PackSize}
    19  }
    20  
    21  // 此函数验证没有问题
    22  func (c *ec) checkOn(point *ep) bool {
    23  	// (pow(point.y, 2) - pow(point.x, 3) - self._A * point.x - self._B) % self._P == 0
    24  	// return (point.y*point.y-point.x*point.x*point.x-c.a*point.x-c.b)%c.p == 0
    25  	// 计算 point.y*point.y-point.x*point.x*point.x-c.a*point.x-c.b
    26  	left := new(big.Int).Mod(new(big.Int).Sub(new(big.Int).Exp(point.y, big.NewInt(2), nil),
    27  		new(big.Int).Add(new(big.Int).Exp(point.x, big.NewInt(3), nil),
    28  			new(big.Int).Add(new(big.Int).Mul(c.a, point.x),
    29  				c.b,
    30  			),
    31  		),
    32  	),
    33  		c.p,
    34  	)
    35  	// left == 0
    36  	return left.Cmp(big.NewInt(0)) == 0
    37  }
    38  
    39  func newP256Curve() *ec {
    40  	// SetString方法接收纯16进制字符串,需要去掉0x前缀
    41  	P256P, _ := new(big.Int).SetString("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", 16)
    42  	P256A, _ := new(big.Int).SetString("ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", 16)
    43  	P256B, _ := new(big.Int).SetString("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", 16)
    44  	P256Gx, _ := new(big.Int).SetString("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", 16)
    45  	P256Gy, _ := new(big.Int).SetString("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", 16)
    46  	P256N, _ := new(big.Int).SetString("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", 16)
    47  
    48  	return newEllipticCurve(
    49  		P256P,
    50  		P256A,
    51  		P256B,
    52  		&ep{
    53  			P256Gx,
    54  			P256Gy,
    55  		},
    56  		P256N,
    57  		big.NewInt(1),
    58  		big.NewInt(32),
    59  		big.NewInt(16),
    60  	)
    61  }
    62  
    63  func newS192Curve() *ec {
    64  	// SetString方法接收纯16进制字符串,需要去掉0x前缀
    65  	S192P, _ := new(big.Int).SetString("fffffffffffffffffffffffffffffffffffffffeffffee37", 16)
    66  	S192Gx, _ := new(big.Int).SetString("db4ff10ec057e9ae26b07d0280b7f4341da5d1b1eae06c7d", 16)
    67  	S192Gy, _ := new(big.Int).SetString("9b2f2f6d9c5628a7844163d015be86344082aa88d95e2f9d", 16)
    68  	S192N, _ := new(big.Int).SetString("fffffffffffffffffffffffe26f2fc170f69466a74defd8d", 16)
    69  	return newEllipticCurve(
    70  		S192P,
    71  		big.NewInt(0),
    72  		big.NewInt(3),
    73  		&ep{
    74  			S192Gx,
    75  			S192Gy,
    76  		},
    77  		S192N,
    78  		big.NewInt(1),
    79  		big.NewInt(24),
    80  		big.NewInt(24),
    81  	)
    82  }
    83  
    84  */