crypto/secp256k1: change receiver variable name to lowercase (#29889)
This commit is contained in:
parent
e015c1116f
commit
2262bf3415
@ -79,52 +79,52 @@ type BitCurve struct {
|
|||||||
BitSize int // the size of the underlying field
|
BitSize int // the size of the underlying field
|
||||||
}
|
}
|
||||||
|
|
||||||
func (BitCurve *BitCurve) Params() *elliptic.CurveParams {
|
func (bitCurve *BitCurve) Params() *elliptic.CurveParams {
|
||||||
return &elliptic.CurveParams{
|
return &elliptic.CurveParams{
|
||||||
P: BitCurve.P,
|
P: bitCurve.P,
|
||||||
N: BitCurve.N,
|
N: bitCurve.N,
|
||||||
B: BitCurve.B,
|
B: bitCurve.B,
|
||||||
Gx: BitCurve.Gx,
|
Gx: bitCurve.Gx,
|
||||||
Gy: BitCurve.Gy,
|
Gy: bitCurve.Gy,
|
||||||
BitSize: BitCurve.BitSize,
|
BitSize: bitCurve.BitSize,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsOnCurve returns true if the given (x,y) lies on the BitCurve.
|
// IsOnCurve returns true if the given (x,y) lies on the BitCurve.
|
||||||
func (BitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
|
func (bitCurve *BitCurve) IsOnCurve(x, y *big.Int) bool {
|
||||||
// y² = x³ + b
|
// y² = x³ + b
|
||||||
y2 := new(big.Int).Mul(y, y) //y²
|
y2 := new(big.Int).Mul(y, y) //y²
|
||||||
y2.Mod(y2, BitCurve.P) //y²%P
|
y2.Mod(y2, bitCurve.P) //y²%P
|
||||||
|
|
||||||
x3 := new(big.Int).Mul(x, x) //x²
|
x3 := new(big.Int).Mul(x, x) //x²
|
||||||
x3.Mul(x3, x) //x³
|
x3.Mul(x3, x) //x³
|
||||||
|
|
||||||
x3.Add(x3, BitCurve.B) //x³+B
|
x3.Add(x3, bitCurve.B) //x³+B
|
||||||
x3.Mod(x3, BitCurve.P) //(x³+B)%P
|
x3.Mod(x3, bitCurve.P) //(x³+B)%P
|
||||||
|
|
||||||
return x3.Cmp(y2) == 0
|
return x3.Cmp(y2) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
// affineFromJacobian reverses the Jacobian transform. See the comment at the
|
||||||
// top of the file.
|
// top of the file.
|
||||||
func (BitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
func (bitCurve *BitCurve) affineFromJacobian(x, y, z *big.Int) (xOut, yOut *big.Int) {
|
||||||
if z.Sign() == 0 {
|
if z.Sign() == 0 {
|
||||||
return new(big.Int), new(big.Int)
|
return new(big.Int), new(big.Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
zinv := new(big.Int).ModInverse(z, BitCurve.P)
|
zinv := new(big.Int).ModInverse(z, bitCurve.P)
|
||||||
zinvsq := new(big.Int).Mul(zinv, zinv)
|
zinvsq := new(big.Int).Mul(zinv, zinv)
|
||||||
|
|
||||||
xOut = new(big.Int).Mul(x, zinvsq)
|
xOut = new(big.Int).Mul(x, zinvsq)
|
||||||
xOut.Mod(xOut, BitCurve.P)
|
xOut.Mod(xOut, bitCurve.P)
|
||||||
zinvsq.Mul(zinvsq, zinv)
|
zinvsq.Mul(zinvsq, zinv)
|
||||||
yOut = new(big.Int).Mul(y, zinvsq)
|
yOut = new(big.Int).Mul(y, zinvsq)
|
||||||
yOut.Mod(yOut, BitCurve.P)
|
yOut.Mod(yOut, bitCurve.P)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add returns the sum of (x1,y1) and (x2,y2)
|
// Add returns the sum of (x1,y1) and (x2,y2)
|
||||||
func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
func (bitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
||||||
// If one point is at infinity, return the other point.
|
// If one point is at infinity, return the other point.
|
||||||
// Adding the point at infinity to any point will preserve the other point.
|
// Adding the point at infinity to any point will preserve the other point.
|
||||||
if x1.Sign() == 0 && y1.Sign() == 0 {
|
if x1.Sign() == 0 && y1.Sign() == 0 {
|
||||||
@ -135,27 +135,27 @@ func (BitCurve *BitCurve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
|
|||||||
}
|
}
|
||||||
z := new(big.Int).SetInt64(1)
|
z := new(big.Int).SetInt64(1)
|
||||||
if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
|
if x1.Cmp(x2) == 0 && y1.Cmp(y2) == 0 {
|
||||||
return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z))
|
return bitCurve.affineFromJacobian(bitCurve.doubleJacobian(x1, y1, z))
|
||||||
}
|
}
|
||||||
return BitCurve.affineFromJacobian(BitCurve.addJacobian(x1, y1, z, x2, y2, z))
|
return bitCurve.affineFromJacobian(bitCurve.addJacobian(x1, y1, z, x2, y2, z))
|
||||||
}
|
}
|
||||||
|
|
||||||
// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
|
// addJacobian takes two points in Jacobian coordinates, (x1, y1, z1) and
|
||||||
// (x2, y2, z2) and returns their sum, also in Jacobian form.
|
// (x2, y2, z2) and returns their sum, also in Jacobian form.
|
||||||
func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
|
func (bitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int, *big.Int, *big.Int) {
|
||||||
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#addition-add-2007-bl
|
||||||
z1z1 := new(big.Int).Mul(z1, z1)
|
z1z1 := new(big.Int).Mul(z1, z1)
|
||||||
z1z1.Mod(z1z1, BitCurve.P)
|
z1z1.Mod(z1z1, bitCurve.P)
|
||||||
z2z2 := new(big.Int).Mul(z2, z2)
|
z2z2 := new(big.Int).Mul(z2, z2)
|
||||||
z2z2.Mod(z2z2, BitCurve.P)
|
z2z2.Mod(z2z2, bitCurve.P)
|
||||||
|
|
||||||
u1 := new(big.Int).Mul(x1, z2z2)
|
u1 := new(big.Int).Mul(x1, z2z2)
|
||||||
u1.Mod(u1, BitCurve.P)
|
u1.Mod(u1, bitCurve.P)
|
||||||
u2 := new(big.Int).Mul(x2, z1z1)
|
u2 := new(big.Int).Mul(x2, z1z1)
|
||||||
u2.Mod(u2, BitCurve.P)
|
u2.Mod(u2, bitCurve.P)
|
||||||
h := new(big.Int).Sub(u2, u1)
|
h := new(big.Int).Sub(u2, u1)
|
||||||
if h.Sign() == -1 {
|
if h.Sign() == -1 {
|
||||||
h.Add(h, BitCurve.P)
|
h.Add(h, bitCurve.P)
|
||||||
}
|
}
|
||||||
i := new(big.Int).Lsh(h, 1)
|
i := new(big.Int).Lsh(h, 1)
|
||||||
i.Mul(i, i)
|
i.Mul(i, i)
|
||||||
@ -163,13 +163,13 @@ func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int
|
|||||||
|
|
||||||
s1 := new(big.Int).Mul(y1, z2)
|
s1 := new(big.Int).Mul(y1, z2)
|
||||||
s1.Mul(s1, z2z2)
|
s1.Mul(s1, z2z2)
|
||||||
s1.Mod(s1, BitCurve.P)
|
s1.Mod(s1, bitCurve.P)
|
||||||
s2 := new(big.Int).Mul(y2, z1)
|
s2 := new(big.Int).Mul(y2, z1)
|
||||||
s2.Mul(s2, z1z1)
|
s2.Mul(s2, z1z1)
|
||||||
s2.Mod(s2, BitCurve.P)
|
s2.Mod(s2, bitCurve.P)
|
||||||
r := new(big.Int).Sub(s2, s1)
|
r := new(big.Int).Sub(s2, s1)
|
||||||
if r.Sign() == -1 {
|
if r.Sign() == -1 {
|
||||||
r.Add(r, BitCurve.P)
|
r.Add(r, bitCurve.P)
|
||||||
}
|
}
|
||||||
r.Lsh(r, 1)
|
r.Lsh(r, 1)
|
||||||
v := new(big.Int).Mul(u1, i)
|
v := new(big.Int).Mul(u1, i)
|
||||||
@ -179,7 +179,7 @@ func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int
|
|||||||
x3.Sub(x3, j)
|
x3.Sub(x3, j)
|
||||||
x3.Sub(x3, v)
|
x3.Sub(x3, v)
|
||||||
x3.Sub(x3, v)
|
x3.Sub(x3, v)
|
||||||
x3.Mod(x3, BitCurve.P)
|
x3.Mod(x3, bitCurve.P)
|
||||||
|
|
||||||
y3 := new(big.Int).Set(r)
|
y3 := new(big.Int).Set(r)
|
||||||
v.Sub(v, x3)
|
v.Sub(v, x3)
|
||||||
@ -187,33 +187,33 @@ func (BitCurve *BitCurve) addJacobian(x1, y1, z1, x2, y2, z2 *big.Int) (*big.Int
|
|||||||
s1.Mul(s1, j)
|
s1.Mul(s1, j)
|
||||||
s1.Lsh(s1, 1)
|
s1.Lsh(s1, 1)
|
||||||
y3.Sub(y3, s1)
|
y3.Sub(y3, s1)
|
||||||
y3.Mod(y3, BitCurve.P)
|
y3.Mod(y3, bitCurve.P)
|
||||||
|
|
||||||
z3 := new(big.Int).Add(z1, z2)
|
z3 := new(big.Int).Add(z1, z2)
|
||||||
z3.Mul(z3, z3)
|
z3.Mul(z3, z3)
|
||||||
z3.Sub(z3, z1z1)
|
z3.Sub(z3, z1z1)
|
||||||
if z3.Sign() == -1 {
|
if z3.Sign() == -1 {
|
||||||
z3.Add(z3, BitCurve.P)
|
z3.Add(z3, bitCurve.P)
|
||||||
}
|
}
|
||||||
z3.Sub(z3, z2z2)
|
z3.Sub(z3, z2z2)
|
||||||
if z3.Sign() == -1 {
|
if z3.Sign() == -1 {
|
||||||
z3.Add(z3, BitCurve.P)
|
z3.Add(z3, bitCurve.P)
|
||||||
}
|
}
|
||||||
z3.Mul(z3, h)
|
z3.Mul(z3, h)
|
||||||
z3.Mod(z3, BitCurve.P)
|
z3.Mod(z3, bitCurve.P)
|
||||||
|
|
||||||
return x3, y3, z3
|
return x3, y3, z3
|
||||||
}
|
}
|
||||||
|
|
||||||
// Double returns 2*(x,y)
|
// Double returns 2*(x,y)
|
||||||
func (BitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
func (bitCurve *BitCurve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
|
||||||
z1 := new(big.Int).SetInt64(1)
|
z1 := new(big.Int).SetInt64(1)
|
||||||
return BitCurve.affineFromJacobian(BitCurve.doubleJacobian(x1, y1, z1))
|
return bitCurve.affineFromJacobian(bitCurve.doubleJacobian(x1, y1, z1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
|
// doubleJacobian takes a point in Jacobian coordinates, (x, y, z), and
|
||||||
// returns its double, also in Jacobian form.
|
// returns its double, also in Jacobian form.
|
||||||
func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
|
func (bitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int, *big.Int) {
|
||||||
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
|
// See http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l
|
||||||
|
|
||||||
a := new(big.Int).Mul(x, x) //X1²
|
a := new(big.Int).Mul(x, x) //X1²
|
||||||
@ -231,30 +231,30 @@ func (BitCurve *BitCurve) doubleJacobian(x, y, z *big.Int) (*big.Int, *big.Int,
|
|||||||
|
|
||||||
x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D
|
x3 := new(big.Int).Mul(big.NewInt(2), d) //2*D
|
||||||
x3.Sub(f, x3) //F-2*D
|
x3.Sub(f, x3) //F-2*D
|
||||||
x3.Mod(x3, BitCurve.P)
|
x3.Mod(x3, bitCurve.P)
|
||||||
|
|
||||||
y3 := new(big.Int).Sub(d, x3) //D-X3
|
y3 := new(big.Int).Sub(d, x3) //D-X3
|
||||||
y3.Mul(e, y3) //E*(D-X3)
|
y3.Mul(e, y3) //E*(D-X3)
|
||||||
y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C
|
y3.Sub(y3, new(big.Int).Mul(big.NewInt(8), c)) //E*(D-X3)-8*C
|
||||||
y3.Mod(y3, BitCurve.P)
|
y3.Mod(y3, bitCurve.P)
|
||||||
|
|
||||||
z3 := new(big.Int).Mul(y, z) //Y1*Z1
|
z3 := new(big.Int).Mul(y, z) //Y1*Z1
|
||||||
z3.Mul(big.NewInt(2), z3) //3*Y1*Z1
|
z3.Mul(big.NewInt(2), z3) //3*Y1*Z1
|
||||||
z3.Mod(z3, BitCurve.P)
|
z3.Mod(z3, bitCurve.P)
|
||||||
|
|
||||||
return x3, y3, z3
|
return x3, y3, z3
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult returns k*G, where G is the base point of the group and k is
|
// ScalarBaseMult returns k*G, where G is the base point of the group and k is
|
||||||
// an integer in big-endian form.
|
// an integer in big-endian form.
|
||||||
func (BitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
func (bitCurve *BitCurve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
|
||||||
return BitCurve.ScalarMult(BitCurve.Gx, BitCurve.Gy, k)
|
return bitCurve.ScalarMult(bitCurve.Gx, bitCurve.Gy, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts a point into the form specified in section 4.3.6 of ANSI
|
// Marshal converts a point into the form specified in section 4.3.6 of ANSI
|
||||||
// X9.62.
|
// X9.62.
|
||||||
func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
|
func (bitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
|
||||||
byteLen := (BitCurve.BitSize + 7) >> 3
|
byteLen := (bitCurve.BitSize + 7) >> 3
|
||||||
ret := make([]byte, 1+2*byteLen)
|
ret := make([]byte, 1+2*byteLen)
|
||||||
ret[0] = 4 // uncompressed point flag
|
ret[0] = 4 // uncompressed point flag
|
||||||
readBits(x, ret[1:1+byteLen])
|
readBits(x, ret[1:1+byteLen])
|
||||||
@ -264,8 +264,8 @@ func (BitCurve *BitCurve) Marshal(x, y *big.Int) []byte {
|
|||||||
|
|
||||||
// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
|
// Unmarshal converts a point, serialised by Marshal, into an x, y pair. On
|
||||||
// error, x = nil.
|
// error, x = nil.
|
||||||
func (BitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) {
|
func (bitCurve *BitCurve) Unmarshal(data []byte) (x, y *big.Int) {
|
||||||
byteLen := (BitCurve.BitSize + 7) >> 3
|
byteLen := (bitCurve.BitSize + 7) >> 3
|
||||||
if len(data) != 1+2*byteLen {
|
if len(data) != 1+2*byteLen {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ extern int secp256k1_ext_scalar_mul(const secp256k1_context* ctx, const unsigned
|
|||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
|
func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
|
||||||
// Ensure scalar is exactly 32 bytes. We pad always, even if
|
// Ensure scalar is exactly 32 bytes. We pad always, even if
|
||||||
// scalar is 32 bytes long, to avoid a timing side channel.
|
// scalar is 32 bytes long, to avoid a timing side channel.
|
||||||
if len(scalar) > 32 {
|
if len(scalar) > 32 {
|
||||||
|
@ -9,6 +9,6 @@ package secp256k1
|
|||||||
|
|
||||||
import "math/big"
|
import "math/big"
|
||||||
|
|
||||||
func (BitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
|
func (bitCurve *BitCurve) ScalarMult(Bx, By *big.Int, scalar []byte) (*big.Int, *big.Int) {
|
||||||
panic("ScalarMult is not available when secp256k1 is built without cgo")
|
panic("ScalarMult is not available when secp256k1 is built without cgo")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user