mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
Merge c9bb67966b into d6ed2f67a8
This commit is contained in:
commit
42937c2404
19 changed files with 403 additions and 403 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
//
|
//
|
||||||
// Bilinear groups are the basis of many of the new cryptographic protocols that
|
// Bilinear groups are the basis of many of the new cryptographic protocols that
|
||||||
// have been proposed over the past decade. They consist of a triplet of groups
|
// have been proposed over the past decade. They consist of a triplet of groups
|
||||||
// (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ (where gₓ
|
// (G₁, G₂ and GT) such that there exists a function g(g₁ˣ,g₂ʸ)=gTˣʸ (where gₓ
|
||||||
// is a generator of the respective group). That function is called a pairing
|
// is a generator of the respective group). That function is called a pairing
|
||||||
// function.
|
// function.
|
||||||
//
|
//
|
||||||
|
|
@ -49,108 +49,108 @@ func (g *G1) String() string {
|
||||||
return "bn256.G1" + g.p.String()
|
return "bn256.G1" + g.p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult sets e to g*k where g is the generator of the group and then
|
// ScalarBaseMult sets g to g*k where g is the generator of the group and then
|
||||||
// returns e.
|
// returns g.
|
||||||
func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
|
func (g *G1) ScalarBaseMult(k *big.Int) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
}
|
}
|
||||||
e.p.Mul(curveGen, k)
|
g.p.Mul(curveGen, k)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
|
func (g *G1) ScalarMult(a *G1, k *big.Int) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, k)
|
g.p.Mul(a.p, k)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
func (e *G1) Add(a, b *G1) *G1 {
|
func (g *G1) Add(a, b *G1) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
}
|
}
|
||||||
e.p.Add(a.p, b.p)
|
g.p.Add(a.p, b.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neg sets e to -a and then returns e.
|
// Neg sets g to -a and then returns g.
|
||||||
func (e *G1) Neg(a *G1) *G1 {
|
func (g *G1) Neg(a *G1) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
}
|
}
|
||||||
e.p.Neg(a.p)
|
g.p.Neg(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets e to a and then returns e.
|
// Set sets g to a and then returns g.
|
||||||
func (e *G1) Set(a *G1) *G1 {
|
func (g *G1) Set(a *G1) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
}
|
}
|
||||||
e.p.Set(a.p)
|
g.p.Set(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts e to a byte slice.
|
// Marshal converts g to a byte slice.
|
||||||
func (e *G1) Marshal() []byte {
|
func (g *G1) Marshal() []byte {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
||||||
e.p.MakeAffine()
|
g.p.MakeAffine()
|
||||||
ret := make([]byte, numBytes*2)
|
ret := make([]byte, numBytes*2)
|
||||||
if e.p.IsInfinity() {
|
if g.p.IsInfinity() {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
temp := &gfP{}
|
temp := &gfP{}
|
||||||
|
|
||||||
montDecode(temp, &e.p.x)
|
montDecode(temp, &g.p.x)
|
||||||
temp.Marshal(ret)
|
temp.Marshal(ret)
|
||||||
montDecode(temp, &e.p.y)
|
montDecode(temp, &g.p.y)
|
||||||
temp.Marshal(ret[numBytes:])
|
temp.Marshal(ret[numBytes:])
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *G1) Unmarshal(m []byte) ([]byte, error) {
|
func (g *G1) Unmarshal(m []byte) ([]byte, error) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
if len(m) < 2*numBytes {
|
if len(m) < 2*numBytes {
|
||||||
return nil, errors.New("bn256: not enough data")
|
return nil, errors.New("bn256: not enough data")
|
||||||
}
|
}
|
||||||
// Unmarshal the points and check their caps
|
// Unmarshal the points and check their caps
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &curvePoint{}
|
g.p = &curvePoint{}
|
||||||
} else {
|
} else {
|
||||||
e.p.x, e.p.y = gfP{0}, gfP{0}
|
g.p.x, g.p.y = gfP{0}, gfP{0}
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if err = e.p.x.Unmarshal(m); err != nil {
|
if err = g.p.x.Unmarshal(m); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.Unmarshal(m[numBytes:]); err != nil {
|
if err = g.p.y.Unmarshal(m[numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Encode into Montgomery form and ensure it's on the curve
|
// Encode into Montgomery form and ensure it's on the curve
|
||||||
montEncode(&e.p.x, &e.p.x)
|
montEncode(&g.p.x, &g.p.x)
|
||||||
montEncode(&e.p.y, &e.p.y)
|
montEncode(&g.p.y, &g.p.y)
|
||||||
|
|
||||||
zero := gfP{0}
|
zero := gfP{0}
|
||||||
if e.p.x == zero && e.p.y == zero {
|
if g.p.x == zero && g.p.y == zero {
|
||||||
// This is the point at infinity.
|
// This is the point at infinity.
|
||||||
e.p.y = *newGFp(1)
|
g.p.y = *newGFp(1)
|
||||||
e.p.z = gfP{0}
|
g.p.z = gfP{0}
|
||||||
e.p.t = gfP{0}
|
g.p.t = gfP{0}
|
||||||
} else {
|
} else {
|
||||||
e.p.z = *newGFp(1)
|
g.p.z = *newGFp(1)
|
||||||
e.p.t = *newGFp(1)
|
g.p.t = *newGFp(1)
|
||||||
|
|
||||||
if !e.p.IsOnCurve() {
|
if !g.p.IsOnCurve() {
|
||||||
return nil, errors.New("bn256: malformed point")
|
return nil, errors.New("bn256: malformed point")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -173,125 +173,125 @@ func RandomG2(r io.Reader) (*big.Int, *G2, error) {
|
||||||
return k, new(G2).ScalarBaseMult(k), nil
|
return k, new(G2).ScalarBaseMult(k), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *G2) String() string {
|
func (g *G2) String() string {
|
||||||
return "bn256.G2" + e.p.String()
|
return "bn256.G2" + g.p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult sets e to g*k where g is the generator of the group and then
|
// ScalarBaseMult sets g to g*k where g is the generator of the group and then
|
||||||
// returns out.
|
// returns out.
|
||||||
func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
|
func (g *G2) ScalarBaseMult(k *big.Int) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
e.p.Mul(twistGen, k)
|
g.p.Mul(twistGen, k)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
|
func (g *G2) ScalarMult(a *G2, k *big.Int) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, k)
|
g.p.Mul(a.p, k)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
func (e *G2) Add(a, b *G2) *G2 {
|
func (g *G2) Add(a, b *G2) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
e.p.Add(a.p, b.p)
|
g.p.Add(a.p, b.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neg sets e to -a and then returns e.
|
// Neg sets g to -a and then returns g.
|
||||||
func (e *G2) Neg(a *G2) *G2 {
|
func (g *G2) Neg(a *G2) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
e.p.Neg(a.p)
|
g.p.Neg(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets e to a and then returns e.
|
// Set sets g to a and then returns g.
|
||||||
func (e *G2) Set(a *G2) *G2 {
|
func (g *G2) Set(a *G2) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
e.p.Set(a.p)
|
g.p.Set(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts e into a byte slice.
|
// Marshal converts g into a byte slice.
|
||||||
func (e *G2) Marshal() []byte {
|
func (g *G2) Marshal() []byte {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
|
|
||||||
e.p.MakeAffine()
|
g.p.MakeAffine()
|
||||||
ret := make([]byte, numBytes*4)
|
ret := make([]byte, numBytes*4)
|
||||||
if e.p.IsInfinity() {
|
if g.p.IsInfinity() {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
temp := &gfP{}
|
temp := &gfP{}
|
||||||
|
|
||||||
montDecode(temp, &e.p.x.x)
|
montDecode(temp, &g.p.x.x)
|
||||||
temp.Marshal(ret)
|
temp.Marshal(ret)
|
||||||
montDecode(temp, &e.p.x.y)
|
montDecode(temp, &g.p.x.y)
|
||||||
temp.Marshal(ret[numBytes:])
|
temp.Marshal(ret[numBytes:])
|
||||||
montDecode(temp, &e.p.y.x)
|
montDecode(temp, &g.p.y.x)
|
||||||
temp.Marshal(ret[2*numBytes:])
|
temp.Marshal(ret[2*numBytes:])
|
||||||
montDecode(temp, &e.p.y.y)
|
montDecode(temp, &g.p.y.y)
|
||||||
temp.Marshal(ret[3*numBytes:])
|
temp.Marshal(ret[3*numBytes:])
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *G2) Unmarshal(m []byte) ([]byte, error) {
|
func (g *G2) Unmarshal(m []byte) ([]byte, error) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
if len(m) < 4*numBytes {
|
if len(m) < 4*numBytes {
|
||||||
return nil, errors.New("bn256: not enough data")
|
return nil, errors.New("bn256: not enough data")
|
||||||
}
|
}
|
||||||
// Unmarshal the points and check their caps
|
// Unmarshal the points and check their caps
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &twistPoint{}
|
g.p = &twistPoint{}
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
if err = e.p.x.x.Unmarshal(m); err != nil {
|
if err = g.p.x.x.Unmarshal(m); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.y.Unmarshal(m[numBytes:]); err != nil {
|
if err = g.p.x.y.Unmarshal(m[numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
|
if err = g.p.y.x.Unmarshal(m[2*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
|
if err = g.p.y.y.Unmarshal(m[3*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Encode into Montgomery form and ensure it's on the curve
|
// Encode into Montgomery form and ensure it's on the curve
|
||||||
montEncode(&e.p.x.x, &e.p.x.x)
|
montEncode(&g.p.x.x, &g.p.x.x)
|
||||||
montEncode(&e.p.x.y, &e.p.x.y)
|
montEncode(&g.p.x.y, &g.p.x.y)
|
||||||
montEncode(&e.p.y.x, &e.p.y.x)
|
montEncode(&g.p.y.x, &g.p.y.x)
|
||||||
montEncode(&e.p.y.y, &e.p.y.y)
|
montEncode(&g.p.y.y, &g.p.y.y)
|
||||||
|
|
||||||
if e.p.x.IsZero() && e.p.y.IsZero() {
|
if g.p.x.IsZero() && g.p.y.IsZero() {
|
||||||
// This is the point at infinity.
|
// This is the point at infinity.
|
||||||
e.p.y.SetOne()
|
g.p.y.SetOne()
|
||||||
e.p.z.SetZero()
|
g.p.z.SetZero()
|
||||||
e.p.t.SetZero()
|
g.p.t.SetZero()
|
||||||
} else {
|
} else {
|
||||||
e.p.z.SetOne()
|
g.p.z.SetOne()
|
||||||
e.p.t.SetOne()
|
g.p.t.SetOne()
|
||||||
|
|
||||||
if !e.p.IsOnCurve() {
|
if !g.p.IsOnCurve() {
|
||||||
return nil, errors.New("bn256: malformed point")
|
return nil, errors.New("bn256: malformed point")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -334,88 +334,88 @@ func (g *GT) String() string {
|
||||||
return "bn256.GT" + g.p.String()
|
return "bn256.GT" + g.p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
|
func (g *GT) ScalarMult(a *GT, k *big.Int) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &gfP12{}
|
g.p = &gfP12{}
|
||||||
}
|
}
|
||||||
e.p.Exp(a.p, k)
|
g.p.Exp(a.p, k)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
func (e *GT) Add(a, b *GT) *GT {
|
func (g *GT) Add(a, b *GT) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &gfP12{}
|
g.p = &gfP12{}
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, b.p)
|
g.p.Mul(a.p, b.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neg sets e to -a and then returns e.
|
// Neg sets g to -a and then returns g.
|
||||||
func (e *GT) Neg(a *GT) *GT {
|
func (g *GT) Neg(a *GT) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &gfP12{}
|
g.p = &gfP12{}
|
||||||
}
|
}
|
||||||
e.p.Conjugate(a.p)
|
g.p.Conjugate(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set sets e to a and then returns e.
|
// Set sets g to a and then returns g.
|
||||||
func (e *GT) Set(a *GT) *GT {
|
func (g *GT) Set(a *GT) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &gfP12{}
|
g.p = &gfP12{}
|
||||||
}
|
}
|
||||||
e.p.Set(a.p)
|
g.p.Set(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finalize is a linear function from F_p^12 to GT.
|
// Finalize is a linear function from F_p^12 to GT.
|
||||||
func (e *GT) Finalize() *GT {
|
func (g *GT) Finalize() *GT {
|
||||||
ret := finalExponentiation(e.p)
|
ret := finalExponentiation(g.p)
|
||||||
e.p.Set(ret)
|
g.p.Set(ret)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts e into a byte slice.
|
// Marshal converts g into a byte slice.
|
||||||
func (e *GT) Marshal() []byte {
|
func (g *GT) Marshal() []byte {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
||||||
ret := make([]byte, numBytes*12)
|
ret := make([]byte, numBytes*12)
|
||||||
temp := &gfP{}
|
temp := &gfP{}
|
||||||
|
|
||||||
montDecode(temp, &e.p.x.x.x)
|
montDecode(temp, &g.p.x.x.x)
|
||||||
temp.Marshal(ret)
|
temp.Marshal(ret)
|
||||||
montDecode(temp, &e.p.x.x.y)
|
montDecode(temp, &g.p.x.x.y)
|
||||||
temp.Marshal(ret[numBytes:])
|
temp.Marshal(ret[numBytes:])
|
||||||
montDecode(temp, &e.p.x.y.x)
|
montDecode(temp, &g.p.x.y.x)
|
||||||
temp.Marshal(ret[2*numBytes:])
|
temp.Marshal(ret[2*numBytes:])
|
||||||
montDecode(temp, &e.p.x.y.y)
|
montDecode(temp, &g.p.x.y.y)
|
||||||
temp.Marshal(ret[3*numBytes:])
|
temp.Marshal(ret[3*numBytes:])
|
||||||
montDecode(temp, &e.p.x.z.x)
|
montDecode(temp, &g.p.x.z.x)
|
||||||
temp.Marshal(ret[4*numBytes:])
|
temp.Marshal(ret[4*numBytes:])
|
||||||
montDecode(temp, &e.p.x.z.y)
|
montDecode(temp, &g.p.x.z.y)
|
||||||
temp.Marshal(ret[5*numBytes:])
|
temp.Marshal(ret[5*numBytes:])
|
||||||
montDecode(temp, &e.p.y.x.x)
|
montDecode(temp, &g.p.y.x.x)
|
||||||
temp.Marshal(ret[6*numBytes:])
|
temp.Marshal(ret[6*numBytes:])
|
||||||
montDecode(temp, &e.p.y.x.y)
|
montDecode(temp, &g.p.y.x.y)
|
||||||
temp.Marshal(ret[7*numBytes:])
|
temp.Marshal(ret[7*numBytes:])
|
||||||
montDecode(temp, &e.p.y.y.x)
|
montDecode(temp, &g.p.y.y.x)
|
||||||
temp.Marshal(ret[8*numBytes:])
|
temp.Marshal(ret[8*numBytes:])
|
||||||
montDecode(temp, &e.p.y.y.y)
|
montDecode(temp, &g.p.y.y.y)
|
||||||
temp.Marshal(ret[9*numBytes:])
|
temp.Marshal(ret[9*numBytes:])
|
||||||
montDecode(temp, &e.p.y.z.x)
|
montDecode(temp, &g.p.y.z.x)
|
||||||
temp.Marshal(ret[10*numBytes:])
|
temp.Marshal(ret[10*numBytes:])
|
||||||
montDecode(temp, &e.p.y.z.y)
|
montDecode(temp, &g.p.y.z.y)
|
||||||
temp.Marshal(ret[11*numBytes:])
|
temp.Marshal(ret[11*numBytes:])
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *GT) Unmarshal(m []byte) ([]byte, error) {
|
func (g *GT) Unmarshal(m []byte) ([]byte, error) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
||||||
|
|
@ -423,59 +423,59 @@ func (e *GT) Unmarshal(m []byte) ([]byte, error) {
|
||||||
return nil, errors.New("bn256: not enough data")
|
return nil, errors.New("bn256: not enough data")
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = &gfP12{}
|
g.p = &gfP12{}
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if err = e.p.x.x.x.Unmarshal(m); err != nil {
|
if err = g.p.x.x.x.Unmarshal(m); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.x.y.Unmarshal(m[numBytes:]); err != nil {
|
if err = g.p.x.x.y.Unmarshal(m[numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.y.x.Unmarshal(m[2*numBytes:]); err != nil {
|
if err = g.p.x.y.x.Unmarshal(m[2*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.y.y.Unmarshal(m[3*numBytes:]); err != nil {
|
if err = g.p.x.y.y.Unmarshal(m[3*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.z.x.Unmarshal(m[4*numBytes:]); err != nil {
|
if err = g.p.x.z.x.Unmarshal(m[4*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.x.z.y.Unmarshal(m[5*numBytes:]); err != nil {
|
if err = g.p.x.z.y.Unmarshal(m[5*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.x.x.Unmarshal(m[6*numBytes:]); err != nil {
|
if err = g.p.y.x.x.Unmarshal(m[6*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.x.y.Unmarshal(m[7*numBytes:]); err != nil {
|
if err = g.p.y.x.y.Unmarshal(m[7*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.y.x.Unmarshal(m[8*numBytes:]); err != nil {
|
if err = g.p.y.y.x.Unmarshal(m[8*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.y.y.Unmarshal(m[9*numBytes:]); err != nil {
|
if err = g.p.y.y.y.Unmarshal(m[9*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.z.x.Unmarshal(m[10*numBytes:]); err != nil {
|
if err = g.p.y.z.x.Unmarshal(m[10*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = e.p.y.z.y.Unmarshal(m[11*numBytes:]); err != nil {
|
if err = g.p.y.z.y.Unmarshal(m[11*numBytes:]); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
montEncode(&e.p.x.x.x, &e.p.x.x.x)
|
montEncode(&g.p.x.x.x, &g.p.x.x.x)
|
||||||
montEncode(&e.p.x.x.y, &e.p.x.x.y)
|
montEncode(&g.p.x.x.y, &g.p.x.x.y)
|
||||||
montEncode(&e.p.x.y.x, &e.p.x.y.x)
|
montEncode(&g.p.x.y.x, &g.p.x.y.x)
|
||||||
montEncode(&e.p.x.y.y, &e.p.x.y.y)
|
montEncode(&g.p.x.y.y, &g.p.x.y.y)
|
||||||
montEncode(&e.p.x.z.x, &e.p.x.z.x)
|
montEncode(&g.p.x.z.x, &g.p.x.z.x)
|
||||||
montEncode(&e.p.x.z.y, &e.p.x.z.y)
|
montEncode(&g.p.x.z.y, &g.p.x.z.y)
|
||||||
montEncode(&e.p.y.x.x, &e.p.y.x.x)
|
montEncode(&g.p.y.x.x, &g.p.y.x.x)
|
||||||
montEncode(&e.p.y.x.y, &e.p.y.x.y)
|
montEncode(&g.p.y.x.y, &g.p.y.x.y)
|
||||||
montEncode(&e.p.y.y.x, &e.p.y.y.x)
|
montEncode(&g.p.y.y.x, &g.p.y.y.x)
|
||||||
montEncode(&e.p.y.y.y, &e.p.y.y.y)
|
montEncode(&g.p.y.y.y, &g.p.y.y.y)
|
||||||
montEncode(&e.p.y.z.x, &e.p.y.z.x)
|
montEncode(&g.p.y.z.x, &g.p.y.z.x)
|
||||||
montEncode(&e.p.y.z.y, &e.p.y.z.y)
|
montEncode(&g.p.y.z.y, &g.p.y.z.y)
|
||||||
|
|
||||||
return m[12*numBytes:], nil
|
return m[12*numBytes:], nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,7 @@ func (e *gfP12) MulScalar(a *gfP12, b *gfP6) *gfP12 {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gfP12) Exp(a *gfP12, power *big.Int) *gfP12 {
|
func (e *gfP12) Exp(a *gfP12, power *big.Int) *gfP12 {
|
||||||
sum := (&gfP12{}).SetOne()
|
sum := (&gfP12{}).SetOne()
|
||||||
t := &gfP12{}
|
t := &gfP12{}
|
||||||
|
|
||||||
|
|
@ -123,8 +123,8 @@ func (c *gfP12) Exp(a *gfP12, power *big.Int) *gfP12 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set(sum)
|
e.Set(sum)
|
||||||
return c
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *gfP12) Square(a *gfP12) *gfP12 {
|
func (e *gfP12) Square(a *gfP12) *gfP12 {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
//
|
//
|
||||||
// Bilinear groups are the basis of many of the new cryptographic protocols
|
// Bilinear groups are the basis of many of the new cryptographic protocols
|
||||||
// that have been proposed over the past decade. They consist of a triplet of
|
// that have been proposed over the past decade. They consist of a triplet of
|
||||||
// groups (G₁, G₂ and GT) such that there exists a function e(g₁ˣ,g₂ʸ)=gTˣʸ
|
// groups (G₁, G₂ and GT) such that there exists a function g(g₁ˣ,g₂ʸ)=gTˣʸ
|
||||||
// (where gₓ is a generator of the respective group). That function is called
|
// (where gₓ is a generator of the respective group). That function is called
|
||||||
// a pairing function.
|
// a pairing function.
|
||||||
//
|
//
|
||||||
|
|
@ -55,54 +55,54 @@ func (g *G1) String() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurvePoints returns p's curve points in big integer
|
// CurvePoints returns p's curve points in big integer
|
||||||
func (e *G1) CurvePoints() (*big.Int, *big.Int, *big.Int, *big.Int) {
|
func (g *G1) CurvePoints() (*big.Int, *big.Int, *big.Int, *big.Int) {
|
||||||
return e.p.x, e.p.y, e.p.z, e.p.t
|
return g.p.x, g.p.y, g.p.z, g.p.t
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult sets e to g*k where g is the generator of the group and
|
// ScalarBaseMult sets g to g*k where g is the generator of the group and
|
||||||
// then returns e.
|
// then returns g.
|
||||||
func (e *G1) ScalarBaseMult(k *big.Int) *G1 {
|
func (g *G1) ScalarBaseMult(k *big.Int) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newCurvePoint(nil)
|
g.p = newCurvePoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Mul(curveGen, k, new(bnPool))
|
g.p.Mul(curveGen, k, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *G1) ScalarMult(a *G1, k *big.Int) *G1 {
|
func (g *G1) ScalarMult(a *G1, k *big.Int) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newCurvePoint(nil)
|
g.p = newCurvePoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, k, new(bnPool))
|
g.p.Mul(a.p, k, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
// BUG(agl): this function is not complete: a==b fails.
|
// BUG(agl): this function is not complete: a==b fails.
|
||||||
func (e *G1) Add(a, b *G1) *G1 {
|
func (g *G1) Add(a, b *G1) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newCurvePoint(nil)
|
g.p = newCurvePoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Add(a.p, b.p, new(bnPool))
|
g.p.Add(a.p, b.p, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neg sets e to -a and then returns e.
|
// Neg sets g to -a and then returns g.
|
||||||
func (e *G1) Neg(a *G1) *G1 {
|
func (g *G1) Neg(a *G1) *G1 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newCurvePoint(nil)
|
g.p = newCurvePoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Negative(a.p)
|
g.p.Negative(a.p)
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts n to a byte slice.
|
// Marshal converts g to a byte slice.
|
||||||
func (n *G1) Marshal() []byte {
|
func (g *G1) Marshal() []byte {
|
||||||
n.p.MakeAffine(nil)
|
g.p.MakeAffine(nil)
|
||||||
|
|
||||||
xBytes := new(big.Int).Mod(n.p.x, P).Bytes()
|
xBytes := new(big.Int).Mod(g.p.x, P).Bytes()
|
||||||
yBytes := new(big.Int).Mod(n.p.y, P).Bytes()
|
yBytes := new(big.Int).Mod(g.p.y, P).Bytes()
|
||||||
|
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
@ -114,37 +114,37 @@ func (n *G1) Marshal() []byte {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *G1) Unmarshal(m []byte) ([]byte, error) {
|
func (g *G1) Unmarshal(m []byte) ([]byte, error) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
if len(m) != 2*numBytes {
|
if len(m) != 2*numBytes {
|
||||||
return nil, errors.New("bn256: not enough data")
|
return nil, errors.New("bn256: not enough data")
|
||||||
}
|
}
|
||||||
// Unmarshal the points and check their caps
|
// Unmarshal the points and check their caps
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newCurvePoint(nil)
|
g.p = newCurvePoint(nil)
|
||||||
}
|
}
|
||||||
e.p.x.SetBytes(m[0*numBytes : 1*numBytes])
|
g.p.x.SetBytes(m[0*numBytes : 1*numBytes])
|
||||||
if e.p.x.Cmp(P) >= 0 {
|
if g.p.x.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
e.p.y.SetBytes(m[1*numBytes : 2*numBytes])
|
g.p.y.SetBytes(m[1*numBytes : 2*numBytes])
|
||||||
if e.p.y.Cmp(P) >= 0 {
|
if g.p.y.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
// Ensure the point is on the curve
|
// Ensure the point is on the curve
|
||||||
if e.p.x.Sign() == 0 && e.p.y.Sign() == 0 {
|
if g.p.x.Sign() == 0 && g.p.y.Sign() == 0 {
|
||||||
// This is the point at infinity.
|
// This is the point at infinity.
|
||||||
e.p.y.SetInt64(1)
|
g.p.y.SetInt64(1)
|
||||||
e.p.z.SetInt64(0)
|
g.p.z.SetInt64(0)
|
||||||
e.p.t.SetInt64(0)
|
g.p.t.SetInt64(0)
|
||||||
} else {
|
} else {
|
||||||
e.p.z.SetInt64(1)
|
g.p.z.SetInt64(1)
|
||||||
e.p.t.SetInt64(1)
|
g.p.t.SetInt64(1)
|
||||||
|
|
||||||
if !e.p.IsOnCurve() {
|
if !g.p.IsOnCurve() {
|
||||||
return nil, errors.New("bn256: malformed point")
|
return nil, errors.New("bn256: malformed point")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -157,7 +157,7 @@ type G2 struct {
|
||||||
p *twistPoint
|
p *twistPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomG1 returns x and g₂ˣ where x is a random, non-zero number read from r.
|
// RandomG2 returns x and g₂ˣ where x is a random, non-zero number read from r.
|
||||||
func RandomG2(r io.Reader) (*big.Int, *G2, error) {
|
func RandomG2(r io.Reader) (*big.Int, *G2, error) {
|
||||||
var k *big.Int
|
var k *big.Int
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -181,47 +181,47 @@ func (g *G2) String() string {
|
||||||
|
|
||||||
// CurvePoints returns the curve points of p which includes the real
|
// CurvePoints returns the curve points of p which includes the real
|
||||||
// and imaginary parts of the curve point.
|
// and imaginary parts of the curve point.
|
||||||
func (e *G2) CurvePoints() (*gfP2, *gfP2, *gfP2, *gfP2) {
|
func (g *G2) CurvePoints() (*gfP2, *gfP2, *gfP2, *gfP2) {
|
||||||
return e.p.x, e.p.y, e.p.z, e.p.t
|
return g.p.x, g.p.y, g.p.z, g.p.t
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarBaseMult sets e to g*k where g is the generator of the group and
|
// ScalarBaseMult sets g to g*k where g is the generator of the group and
|
||||||
// then returns out.
|
// then returns out.
|
||||||
func (e *G2) ScalarBaseMult(k *big.Int) *G2 {
|
func (g *G2) ScalarBaseMult(k *big.Int) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newTwistPoint(nil)
|
g.p = newTwistPoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Mul(twistGen, k, new(bnPool))
|
g.p.Mul(twistGen, k, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *G2) ScalarMult(a *G2, k *big.Int) *G2 {
|
func (g *G2) ScalarMult(a *G2, k *big.Int) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newTwistPoint(nil)
|
g.p = newTwistPoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, k, new(bnPool))
|
g.p.Mul(a.p, k, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
// BUG(agl): this function is not complete: a==b fails.
|
// BUG(agl): this function is not complete: a==b fails.
|
||||||
func (e *G2) Add(a, b *G2) *G2 {
|
func (g *G2) Add(a, b *G2) *G2 {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newTwistPoint(nil)
|
g.p = newTwistPoint(nil)
|
||||||
}
|
}
|
||||||
e.p.Add(a.p, b.p, new(bnPool))
|
g.p.Add(a.p, b.p, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts n into a byte slice.
|
// Marshal converts g into a byte slice.
|
||||||
func (n *G2) Marshal() []byte {
|
func (g *G2) Marshal() []byte {
|
||||||
n.p.MakeAffine(nil)
|
g.p.MakeAffine(nil)
|
||||||
|
|
||||||
xxBytes := new(big.Int).Mod(n.p.x.x, P).Bytes()
|
xxBytes := new(big.Int).Mod(g.p.x.x, P).Bytes()
|
||||||
xyBytes := new(big.Int).Mod(n.p.x.y, P).Bytes()
|
xyBytes := new(big.Int).Mod(g.p.x.y, P).Bytes()
|
||||||
yxBytes := new(big.Int).Mod(n.p.y.x, P).Bytes()
|
yxBytes := new(big.Int).Mod(g.p.y.x, P).Bytes()
|
||||||
yyBytes := new(big.Int).Mod(n.p.y.y, P).Bytes()
|
yyBytes := new(big.Int).Mod(g.p.y.y, P).Bytes()
|
||||||
|
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
@ -235,48 +235,48 @@ func (n *G2) Marshal() []byte {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *G2) Unmarshal(m []byte) ([]byte, error) {
|
func (g *G2) Unmarshal(m []byte) ([]byte, error) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
if len(m) != 4*numBytes {
|
if len(m) != 4*numBytes {
|
||||||
return nil, errors.New("bn256: not enough data")
|
return nil, errors.New("bn256: not enough data")
|
||||||
}
|
}
|
||||||
// Unmarshal the points and check their caps
|
// Unmarshal the points and check their caps
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newTwistPoint(nil)
|
g.p = newTwistPoint(nil)
|
||||||
}
|
}
|
||||||
e.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
|
g.p.x.x.SetBytes(m[0*numBytes : 1*numBytes])
|
||||||
if e.p.x.x.Cmp(P) >= 0 {
|
if g.p.x.x.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
e.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
|
g.p.x.y.SetBytes(m[1*numBytes : 2*numBytes])
|
||||||
if e.p.x.y.Cmp(P) >= 0 {
|
if g.p.x.y.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
e.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
|
g.p.y.x.SetBytes(m[2*numBytes : 3*numBytes])
|
||||||
if e.p.y.x.Cmp(P) >= 0 {
|
if g.p.y.x.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
e.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
|
g.p.y.y.SetBytes(m[3*numBytes : 4*numBytes])
|
||||||
if e.p.y.y.Cmp(P) >= 0 {
|
if g.p.y.y.Cmp(P) >= 0 {
|
||||||
return nil, errors.New("bn256: coordinate exceeds modulus")
|
return nil, errors.New("bn256: coordinate exceeds modulus")
|
||||||
}
|
}
|
||||||
// Ensure the point is on the curve
|
// Ensure the point is on the curve
|
||||||
if e.p.x.x.Sign() == 0 &&
|
if g.p.x.x.Sign() == 0 &&
|
||||||
e.p.x.y.Sign() == 0 &&
|
g.p.x.y.Sign() == 0 &&
|
||||||
e.p.y.x.Sign() == 0 &&
|
g.p.y.x.Sign() == 0 &&
|
||||||
e.p.y.y.Sign() == 0 {
|
g.p.y.y.Sign() == 0 {
|
||||||
// This is the point at infinity.
|
// This is the point at infinity.
|
||||||
e.p.y.SetOne()
|
g.p.y.SetOne()
|
||||||
e.p.z.SetZero()
|
g.p.z.SetZero()
|
||||||
e.p.t.SetZero()
|
g.p.t.SetZero()
|
||||||
} else {
|
} else {
|
||||||
e.p.z.SetOne()
|
g.p.z.SetOne()
|
||||||
e.p.t.SetOne()
|
g.p.t.SetOne()
|
||||||
|
|
||||||
if !e.p.IsOnCurve() {
|
if !g.p.IsOnCurve() {
|
||||||
return nil, errors.New("bn256: malformed point")
|
return nil, errors.New("bn256: malformed point")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -293,49 +293,49 @@ func (g *GT) String() string {
|
||||||
return "bn256.GT" + g.p.String()
|
return "bn256.GT" + g.p.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScalarMult sets e to a*k and then returns e.
|
// ScalarMult sets g to a*k and then returns g.
|
||||||
func (e *GT) ScalarMult(a *GT, k *big.Int) *GT {
|
func (g *GT) ScalarMult(a *GT, k *big.Int) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newGFp12(nil)
|
g.p = newGFp12(nil)
|
||||||
}
|
}
|
||||||
e.p.Exp(a.p, k, new(bnPool))
|
g.p.Exp(a.p, k, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add sets e to a+b and then returns e.
|
// Add sets g to a+b and then returns g.
|
||||||
func (e *GT) Add(a, b *GT) *GT {
|
func (g *GT) Add(a, b *GT) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newGFp12(nil)
|
g.p = newGFp12(nil)
|
||||||
}
|
}
|
||||||
e.p.Mul(a.p, b.p, new(bnPool))
|
g.p.Mul(a.p, b.p, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Neg sets e to -a and then returns e.
|
// Neg sets g to -a and then returns g.
|
||||||
func (e *GT) Neg(a *GT) *GT {
|
func (g *GT) Neg(a *GT) *GT {
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newGFp12(nil)
|
g.p = newGFp12(nil)
|
||||||
}
|
}
|
||||||
e.p.Invert(a.p, new(bnPool))
|
g.p.Invert(a.p, new(bnPool))
|
||||||
return e
|
return g
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal converts n into a byte slice.
|
// Marshal converts g into a byte slice.
|
||||||
func (n *GT) Marshal() []byte {
|
func (g *GT) Marshal() []byte {
|
||||||
n.p.Minimal()
|
g.p.Minimal()
|
||||||
|
|
||||||
xxxBytes := n.p.x.x.x.Bytes()
|
xxxBytes := g.p.x.x.x.Bytes()
|
||||||
xxyBytes := n.p.x.x.y.Bytes()
|
xxyBytes := g.p.x.x.y.Bytes()
|
||||||
xyxBytes := n.p.x.y.x.Bytes()
|
xyxBytes := g.p.x.y.x.Bytes()
|
||||||
xyyBytes := n.p.x.y.y.Bytes()
|
xyyBytes := g.p.x.y.y.Bytes()
|
||||||
xzxBytes := n.p.x.z.x.Bytes()
|
xzxBytes := g.p.x.z.x.Bytes()
|
||||||
xzyBytes := n.p.x.z.y.Bytes()
|
xzyBytes := g.p.x.z.y.Bytes()
|
||||||
yxxBytes := n.p.y.x.x.Bytes()
|
yxxBytes := g.p.y.x.x.Bytes()
|
||||||
yxyBytes := n.p.y.x.y.Bytes()
|
yxyBytes := g.p.y.x.y.Bytes()
|
||||||
yyxBytes := n.p.y.y.x.Bytes()
|
yyxBytes := g.p.y.y.x.Bytes()
|
||||||
yyyBytes := n.p.y.y.y.Bytes()
|
yyyBytes := g.p.y.y.y.Bytes()
|
||||||
yzxBytes := n.p.y.z.x.Bytes()
|
yzxBytes := g.p.y.z.x.Bytes()
|
||||||
yzyBytes := n.p.y.z.y.Bytes()
|
yzyBytes := g.p.y.z.y.Bytes()
|
||||||
|
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
@ -357,9 +357,9 @@ func (n *GT) Marshal() []byte {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal sets e to the result of converting the output of Marshal back into
|
// Unmarshal sets g to the result of converting the output of Marshal back into
|
||||||
// a group element and then returns e.
|
// a group element and then returns g.
|
||||||
func (e *GT) Unmarshal(m []byte) (*GT, bool) {
|
func (g *GT) Unmarshal(m []byte) (*GT, bool) {
|
||||||
// Each value is a 256-bit number.
|
// Each value is a 256-bit number.
|
||||||
const numBytes = 256 / 8
|
const numBytes = 256 / 8
|
||||||
|
|
||||||
|
|
@ -367,24 +367,24 @@ func (e *GT) Unmarshal(m []byte) (*GT, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.p == nil {
|
if g.p == nil {
|
||||||
e.p = newGFp12(nil)
|
g.p = newGFp12(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
e.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
|
g.p.x.x.x.SetBytes(m[0*numBytes : 1*numBytes])
|
||||||
e.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
|
g.p.x.x.y.SetBytes(m[1*numBytes : 2*numBytes])
|
||||||
e.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
|
g.p.x.y.x.SetBytes(m[2*numBytes : 3*numBytes])
|
||||||
e.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
|
g.p.x.y.y.SetBytes(m[3*numBytes : 4*numBytes])
|
||||||
e.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
|
g.p.x.z.x.SetBytes(m[4*numBytes : 5*numBytes])
|
||||||
e.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
|
g.p.x.z.y.SetBytes(m[5*numBytes : 6*numBytes])
|
||||||
e.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
|
g.p.y.x.x.SetBytes(m[6*numBytes : 7*numBytes])
|
||||||
e.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
|
g.p.y.x.y.SetBytes(m[7*numBytes : 8*numBytes])
|
||||||
e.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
|
g.p.y.y.x.SetBytes(m[8*numBytes : 9*numBytes])
|
||||||
e.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
|
g.p.y.y.y.SetBytes(m[9*numBytes : 10*numBytes])
|
||||||
e.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
|
g.p.y.z.x.SetBytes(m[10*numBytes : 11*numBytes])
|
||||||
e.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
|
g.p.y.z.y.SetBytes(m[11*numBytes : 12*numBytes])
|
||||||
|
|
||||||
return e, true
|
return g, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pair calculates an Optimal Ate pairing.
|
// Pair calculates an Optimal Ate pairing.
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func bigFromBase10(s string) *big.Int {
|
||||||
// u is the BN parameter that determines the prime: 1868033³.
|
// u is the BN parameter that determines the prime: 1868033³.
|
||||||
var u = bigFromBase10("4965661367192848881")
|
var u = bigFromBase10("4965661367192848881")
|
||||||
|
|
||||||
// p is a prime over which we form a basic field: 36u⁴+36u³+24u²+6u+1.
|
// P is a prime over which we form a basic field: 36u⁴+36u³+24u²+6u+1.
|
||||||
var P = bigFromBase10("21888242871839275222246405745257275088696311157297823662689037894645226208583")
|
var P = bigFromBase10("21888242871839275222246405745257275088696311157297823662689037894645226208583")
|
||||||
|
|
||||||
// Order is the number of elements in both G₁ and G₂: 36u⁴+36u³+18u²+6u+1.
|
// Order is the number of elements in both G₁ and G₂: 36u⁴+36u³+18u²+6u+1.
|
||||||
|
|
|
||||||
|
|
@ -186,14 +186,14 @@ func (c *curvePoint) Double(a *curvePoint, pool *bnPool) {
|
||||||
A.Mod(A, P)
|
A.Mod(A, P)
|
||||||
B := pool.Get().Mul(a.y, a.y)
|
B := pool.Get().Mul(a.y, a.y)
|
||||||
B.Mod(B, P)
|
B.Mod(B, P)
|
||||||
C_ := pool.Get().Mul(B, B)
|
_C := pool.Get().Mul(B, B)
|
||||||
C_.Mod(C_, P)
|
_C.Mod(_C, P)
|
||||||
|
|
||||||
t := pool.Get().Add(a.x, B)
|
t := pool.Get().Add(a.x, B)
|
||||||
t2 := pool.Get().Mul(t, t)
|
t2 := pool.Get().Mul(t, t)
|
||||||
t2.Mod(t2, P)
|
t2.Mod(t2, P)
|
||||||
t.Sub(t2, A)
|
t.Sub(t2, A)
|
||||||
t2.Sub(t, C_)
|
t2.Sub(t, _C)
|
||||||
d := pool.Get().Add(t2, t2)
|
d := pool.Get().Add(t2, t2)
|
||||||
t.Add(A, A)
|
t.Add(A, A)
|
||||||
e := pool.Get().Add(t, A)
|
e := pool.Get().Add(t, A)
|
||||||
|
|
@ -203,7 +203,7 @@ func (c *curvePoint) Double(a *curvePoint, pool *bnPool) {
|
||||||
t.Add(d, d)
|
t.Add(d, d)
|
||||||
c.x.Sub(f, t)
|
c.x.Sub(f, t)
|
||||||
|
|
||||||
t.Add(C_, C_)
|
t.Add(_C, _C)
|
||||||
t2.Add(t, t)
|
t2.Add(t, t)
|
||||||
t.Add(t2, t2)
|
t.Add(t2, t2)
|
||||||
c.y.Sub(d, c.x)
|
c.y.Sub(d, c.x)
|
||||||
|
|
@ -217,7 +217,7 @@ func (c *curvePoint) Double(a *curvePoint, pool *bnPool) {
|
||||||
|
|
||||||
pool.Put(A)
|
pool.Put(A)
|
||||||
pool.Put(B)
|
pool.Put(B)
|
||||||
pool.Put(C_)
|
pool.Put(_C)
|
||||||
pool.Put(t)
|
pool.Put(t)
|
||||||
pool.Put(t2)
|
pool.Put(t2)
|
||||||
pool.Put(d)
|
pool.Put(d)
|
||||||
|
|
|
||||||
|
|
@ -130,7 +130,7 @@ func (e *gfP12) MulScalar(a *gfP12, b *gfP6, pool *bnPool) *gfP12 {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gfP12) Exp(a *gfP12, power *big.Int, pool *bnPool) *gfP12 {
|
func (e *gfP12) Exp(a *gfP12, power *big.Int, pool *bnPool) *gfP12 {
|
||||||
sum := newGFp12(pool)
|
sum := newGFp12(pool)
|
||||||
sum.SetOne()
|
sum.SetOne()
|
||||||
t := newGFp12(pool)
|
t := newGFp12(pool)
|
||||||
|
|
@ -144,12 +144,12 @@ func (c *gfP12) Exp(a *gfP12, power *big.Int, pool *bnPool) *gfP12 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set(sum)
|
e.Set(sum)
|
||||||
|
|
||||||
sum.Put(pool)
|
sum.Put(pool)
|
||||||
t.Put(pool)
|
t.Put(pool)
|
||||||
|
|
||||||
return c
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *gfP12) Square(a *gfP12, pool *bnPool) *gfP12 {
|
func (e *gfP12) Square(a *gfP12, pool *bnPool) *gfP12 {
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ func (e *gfP2) Double(a *gfP2) *gfP2 {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *gfP2) Exp(a *gfP2, power *big.Int, pool *bnPool) *gfP2 {
|
func (e *gfP2) Exp(a *gfP2, power *big.Int, pool *bnPool) *gfP2 {
|
||||||
sum := newGFp2(pool)
|
sum := newGFp2(pool)
|
||||||
sum.SetOne()
|
sum.SetOne()
|
||||||
t := newGFp2(pool)
|
t := newGFp2(pool)
|
||||||
|
|
@ -116,12 +116,12 @@ func (c *gfP2) Exp(a *gfP2, power *big.Int, pool *bnPool) *gfP2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set(sum)
|
e.Set(sum)
|
||||||
|
|
||||||
sum.Put(pool)
|
sum.Put(pool)
|
||||||
t.Put(pool)
|
t.Put(pool)
|
||||||
|
|
||||||
return c
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// See "Multiplication and Squaring in Pairing-Friendly Fields",
|
// See "Multiplication and Squaring in Pairing-Friendly Fields",
|
||||||
|
|
|
||||||
|
|
@ -266,13 +266,13 @@ func (e *gfP6) Invert(a *gfP6, pool *bnPool) *gfP6 {
|
||||||
t1.Mul(a.y, a.z, pool)
|
t1.Mul(a.y, a.z, pool)
|
||||||
B.Sub(B, t1)
|
B.Sub(B, t1)
|
||||||
|
|
||||||
C_ := newGFp2(pool)
|
_C := newGFp2(pool)
|
||||||
C_.Square(a.y, pool)
|
_C.Square(a.y, pool)
|
||||||
t1.Mul(a.x, a.z, pool)
|
t1.Mul(a.x, a.z, pool)
|
||||||
C_.Sub(C_, t1)
|
_C.Sub(_C, t1)
|
||||||
|
|
||||||
F := newGFp2(pool)
|
F := newGFp2(pool)
|
||||||
F.Mul(C_, a.y, pool)
|
F.Mul(_C, a.y, pool)
|
||||||
F.MulXi(F, pool)
|
F.MulXi(F, pool)
|
||||||
t1.Mul(A, a.z, pool)
|
t1.Mul(A, a.z, pool)
|
||||||
F.Add(F, t1)
|
F.Add(F, t1)
|
||||||
|
|
@ -282,14 +282,14 @@ func (e *gfP6) Invert(a *gfP6, pool *bnPool) *gfP6 {
|
||||||
|
|
||||||
F.Invert(F, pool)
|
F.Invert(F, pool)
|
||||||
|
|
||||||
e.x.Mul(C_, F, pool)
|
e.x.Mul(_C, F, pool)
|
||||||
e.y.Mul(B, F, pool)
|
e.y.Mul(B, F, pool)
|
||||||
e.z.Mul(A, F, pool)
|
e.z.Mul(A, F, pool)
|
||||||
|
|
||||||
t1.Put(pool)
|
t1.Put(pool)
|
||||||
A.Put(pool)
|
A.Put(pool)
|
||||||
B.Put(pool)
|
B.Put(pool)
|
||||||
C_.Put(pool)
|
_C.Put(pool)
|
||||||
F.Put(pool)
|
F.Put(pool)
|
||||||
|
|
||||||
return e
|
return e
|
||||||
|
|
|
||||||
|
|
@ -88,12 +88,12 @@ func lineFunctionDouble(r *twistPoint, q *curvePoint, pool *bnPool) (a, b, c *gf
|
||||||
|
|
||||||
A := newGFp2(pool).Square(r.x, pool)
|
A := newGFp2(pool).Square(r.x, pool)
|
||||||
B := newGFp2(pool).Square(r.y, pool)
|
B := newGFp2(pool).Square(r.y, pool)
|
||||||
C_ := newGFp2(pool).Square(B, pool)
|
_C := newGFp2(pool).Square(B, pool)
|
||||||
|
|
||||||
D := newGFp2(pool).Add(r.x, B)
|
D := newGFp2(pool).Add(r.x, B)
|
||||||
D.Square(D, pool)
|
D.Square(D, pool)
|
||||||
D.Sub(D, A)
|
D.Sub(D, A)
|
||||||
D.Sub(D, C_)
|
D.Sub(D, _C)
|
||||||
D.Add(D, D)
|
D.Add(D, D)
|
||||||
|
|
||||||
E := newGFp2(pool).Add(A, A)
|
E := newGFp2(pool).Add(A, A)
|
||||||
|
|
@ -112,7 +112,7 @@ func lineFunctionDouble(r *twistPoint, q *curvePoint, pool *bnPool) (a, b, c *gf
|
||||||
|
|
||||||
rOut.y.Sub(D, rOut.x)
|
rOut.y.Sub(D, rOut.x)
|
||||||
rOut.y.Mul(rOut.y, E, pool)
|
rOut.y.Mul(rOut.y, E, pool)
|
||||||
t := newGFp2(pool).Add(C_, C_)
|
t := newGFp2(pool).Add(_C, _C)
|
||||||
t.Add(t, t)
|
t.Add(t, t)
|
||||||
t.Add(t, t)
|
t.Add(t, t)
|
||||||
rOut.y.Sub(rOut.y, t)
|
rOut.y.Sub(rOut.y, t)
|
||||||
|
|
@ -142,7 +142,7 @@ func lineFunctionDouble(r *twistPoint, q *curvePoint, pool *bnPool) (a, b, c *gf
|
||||||
|
|
||||||
A.Put(pool)
|
A.Put(pool)
|
||||||
B.Put(pool)
|
B.Put(pool)
|
||||||
C_.Put(pool)
|
_C.Put(pool)
|
||||||
D.Put(pool)
|
D.Put(pool)
|
||||||
E.Put(pool)
|
E.Put(pool)
|
||||||
G.Put(pool)
|
G.Put(pool)
|
||||||
|
|
|
||||||
|
|
@ -171,12 +171,12 @@ func (c *twistPoint) Double(a *twistPoint, pool *bnPool) {
|
||||||
// See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
|
// See http://hyperelliptic.org/EFD/g1p/auto-code/shortw/jacobian-0/doubling/dbl-2009-l.op3
|
||||||
A := newGFp2(pool).Square(a.x, pool)
|
A := newGFp2(pool).Square(a.x, pool)
|
||||||
B := newGFp2(pool).Square(a.y, pool)
|
B := newGFp2(pool).Square(a.y, pool)
|
||||||
C_ := newGFp2(pool).Square(B, pool)
|
_C := newGFp2(pool).Square(B, pool)
|
||||||
|
|
||||||
t := newGFp2(pool).Add(a.x, B)
|
t := newGFp2(pool).Add(a.x, B)
|
||||||
t2 := newGFp2(pool).Square(t, pool)
|
t2 := newGFp2(pool).Square(t, pool)
|
||||||
t.Sub(t2, A)
|
t.Sub(t2, A)
|
||||||
t2.Sub(t, C_)
|
t2.Sub(t, _C)
|
||||||
d := newGFp2(pool).Add(t2, t2)
|
d := newGFp2(pool).Add(t2, t2)
|
||||||
t.Add(A, A)
|
t.Add(A, A)
|
||||||
e := newGFp2(pool).Add(t, A)
|
e := newGFp2(pool).Add(t, A)
|
||||||
|
|
@ -185,7 +185,7 @@ func (c *twistPoint) Double(a *twistPoint, pool *bnPool) {
|
||||||
t.Add(d, d)
|
t.Add(d, d)
|
||||||
c.x.Sub(f, t)
|
c.x.Sub(f, t)
|
||||||
|
|
||||||
t.Add(C_, C_)
|
t.Add(_C, _C)
|
||||||
t2.Add(t, t)
|
t2.Add(t, t)
|
||||||
t.Add(t2, t2)
|
t.Add(t2, t2)
|
||||||
c.y.Sub(d, c.x)
|
c.y.Sub(d, c.x)
|
||||||
|
|
@ -197,7 +197,7 @@ func (c *twistPoint) Double(a *twistPoint, pool *bnPool) {
|
||||||
|
|
||||||
A.Put(pool)
|
A.Put(pool)
|
||||||
B.Put(pool)
|
B.Put(pool)
|
||||||
C_.Put(pool)
|
_C.Put(pool)
|
||||||
t.Put(pool)
|
t.Put(pool)
|
||||||
t2.Put(pool)
|
t2.Put(pool)
|
||||||
d.Put(pool)
|
d.Put(pool)
|
||||||
|
|
|
||||||
|
|
@ -58,12 +58,12 @@ type PublicKey struct {
|
||||||
Params *ECIESParams
|
Params *ECIESParams
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export an ECIES public key as an ECDSA public key.
|
//ExportECDSA exports an ECIES public key as an ECDSA public key.
|
||||||
func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey {
|
func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey {
|
||||||
return &ecdsa.PublicKey{Curve: pub.Curve, X: pub.X, Y: pub.Y}
|
return &ecdsa.PublicKey{Curve: pub.Curve, X: pub.X, Y: pub.Y}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Import an ECDSA public key as an ECIES public key.
|
//ImportECDSAPublic imports an ECDSA public key as an ECIES public key.
|
||||||
func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey {
|
func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey {
|
||||||
return &PublicKey{
|
return &PublicKey{
|
||||||
X: pub.X,
|
X: pub.X,
|
||||||
|
|
@ -79,21 +79,21 @@ type PrivateKey struct {
|
||||||
D *big.Int
|
D *big.Int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export an ECIES private key as an ECDSA private key.
|
// ExportECDSA exports an ECIES private key as an ECDSA private key.
|
||||||
func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey {
|
func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey {
|
||||||
pub := &prv.PublicKey
|
pub := &prv.PublicKey
|
||||||
pubECDSA := pub.ExportECDSA()
|
pubECDSA := pub.ExportECDSA()
|
||||||
return &ecdsa.PrivateKey{PublicKey: *pubECDSA, D: prv.D}
|
return &ecdsa.PrivateKey{PublicKey: *pubECDSA, D: prv.D}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Import an ECDSA private key as an ECIES private key.
|
//ImportECDSA imports an ECDSA private key as an ECIES private key.
|
||||||
func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
|
func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey {
|
||||||
pub := ImportECDSAPublic(&prv.PublicKey)
|
pub := ImportECDSAPublic(&prv.PublicKey)
|
||||||
return &PrivateKey{*pub, prv.D}
|
return &PrivateKey{*pub, prv.D}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate an elliptic curve public / private keypair. If params is nil,
|
// GenerateKey generates an elliptic curve public / private keypair.
|
||||||
// the recommended default parameters for the key will be chosen.
|
// If params is nil,the recommended default parameters for the key will be chosen.
|
||||||
func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) {
|
func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error) {
|
||||||
pb, x, y, err := elliptic.GenerateKey(curve, rand)
|
pb, x, y, err := elliptic.GenerateKey(curve, rand)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -117,7 +117,7 @@ func MaxSharedKeyLength(pub *PublicKey) int {
|
||||||
return (pub.Curve.Params().BitSize + 7) / 8
|
return (pub.Curve.Params().BitSize + 7) / 8
|
||||||
}
|
}
|
||||||
|
|
||||||
// ECDH key agreement method used to establish secret keys for encryption.
|
//GenerateShared is the ECDH key agreement method used to establish secret keys for encryption.
|
||||||
func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error) {
|
func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error) {
|
||||||
if prv.PublicKey.Curve != pub.Curve {
|
if prv.PublicKey.Curve != pub.Curve {
|
||||||
return nil, ErrInvalidCurve
|
return nil, ErrInvalidCurve
|
||||||
|
|
|
||||||
|
|
@ -101,9 +101,8 @@ func cmpPrivate(prv1, prv2 *PrivateKey) bool {
|
||||||
return false
|
return false
|
||||||
} else if prv1.D.Cmp(prv2.D) != 0 {
|
} else if prv1.D.Cmp(prv2.D) != 0 {
|
||||||
return false
|
return false
|
||||||
} else {
|
|
||||||
return cmpPublic(prv1.PublicKey, prv2.PublicKey)
|
|
||||||
}
|
}
|
||||||
|
return cmpPublic(prv1.PublicKey, prv2.PublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate the ECDH component.
|
// Validate the ECDH component.
|
||||||
|
|
|
||||||
|
|
@ -351,7 +351,7 @@ func (api *PrivateDebugAPI) Preimage(ctx context.Context, hash common.Hash) (hex
|
||||||
return nil, errors.New("unknown preimage")
|
return nil, errors.New("unknown preimage")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBadBLocks returns a list of the last 'bad blocks' that the client has seen on the network
|
// GetBadBlocks returns a list of the last 'bad blocks' that the client has seen on the network
|
||||||
// and returns them as a JSON list of block-hashes
|
// and returns them as a JSON list of block-hashes
|
||||||
func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) {
|
func (api *PrivateDebugAPI) GetBadBlocks(ctx context.Context) ([]core.BadBlockArgs, error) {
|
||||||
return api.eth.BlockChain().BadBlocks()
|
return api.eth.BlockChain().BadBlocks()
|
||||||
|
|
@ -406,7 +406,7 @@ func storageRangeAt(st state.Trie, start []byte, maxResult int) (StorageRangeRes
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetModifiedAccountsByumber returns all accounts that have changed between the
|
// GetModifiedAccountsByNumber returns all accounts that have changed between the
|
||||||
// two blocks specified. A change is defined as a difference in nonce, balance,
|
// two blocks specified. A change is defined as a difference in nonce, balance,
|
||||||
// code hash, or storage hash.
|
// code hash, or storage hash.
|
||||||
//
|
//
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ type EthAPIBackend struct {
|
||||||
gpo *gasprice.Oracle
|
gpo *gasprice.Oracle
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ChainConfig returns the the core config which determined the blockchain settings
|
||||||
func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
|
func (b *EthAPIBackend) ChainConfig() *params.ChainConfig {
|
||||||
return b.eth.chainConfig
|
return b.eth.chainConfig
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ type Ethereum struct {
|
||||||
gasPrice *big.Int
|
gasPrice *big.Int
|
||||||
etherbase common.Address
|
etherbase common.Address
|
||||||
|
|
||||||
networkId uint64
|
networkID uint64
|
||||||
netRPCService *ethapi.PublicNetAPI
|
netRPCService *ethapi.PublicNetAPI
|
||||||
|
|
||||||
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
|
lock sync.RWMutex // Protects the variadic fields (e.g. gas price and etherbase)
|
||||||
|
|
@ -126,7 +126,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
|
||||||
accountManager: ctx.AccountManager,
|
accountManager: ctx.AccountManager,
|
||||||
engine: CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
|
engine: CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
|
||||||
shutdownChan: make(chan bool),
|
shutdownChan: make(chan bool),
|
||||||
networkId: config.NetworkId,
|
networkID: config.NetworkId,
|
||||||
gasPrice: config.GasPrice,
|
gasPrice: config.GasPrice,
|
||||||
etherbase: config.Etherbase,
|
etherbase: config.Etherbase,
|
||||||
bloomRequests: make(chan chan *bloombits.Retrieval),
|
bloomRequests: make(chan chan *bloombits.Retrieval),
|
||||||
|
|
@ -369,7 +369,7 @@ func (s *Ethereum) Engine() consensus.Engine { return s.engine }
|
||||||
func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb }
|
func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb }
|
||||||
func (s *Ethereum) IsListening() bool { return true } // Always listening
|
func (s *Ethereum) IsListening() bool { return true } // Always listening
|
||||||
func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
||||||
func (s *Ethereum) NetVersion() uint64 { return s.networkId }
|
func (s *Ethereum) NetVersion() uint64 { return s.networkID }
|
||||||
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
||||||
|
|
||||||
// Protocols implements node.Service, returning all the currently configured
|
// Protocols implements node.Service, returning all the currently configured
|
||||||
|
|
|
||||||
|
|
@ -900,7 +900,7 @@ func (d *Downloader) fillHeaderSkeleton(from uint64, skeleton []*types.Header) (
|
||||||
var (
|
var (
|
||||||
deliver = func(packet dataPack) (int, error) {
|
deliver = func(packet dataPack) (int, error) {
|
||||||
pack := packet.(*headerPack)
|
pack := packet.(*headerPack)
|
||||||
return d.queue.DeliverHeaders(pack.peerId, pack.headers, d.headerProcCh)
|
return d.queue.DeliverHeaders(pack.peerID, pack.headers, d.headerProcCh)
|
||||||
}
|
}
|
||||||
expire = func() map[string]int { return d.queue.ExpireHeaders(d.requestTTL()) }
|
expire = func() map[string]int { return d.queue.ExpireHeaders(d.requestTTL()) }
|
||||||
throttle = func() bool { return false }
|
throttle = func() bool { return false }
|
||||||
|
|
@ -930,7 +930,7 @@ func (d *Downloader) fetchBodies(from uint64) error {
|
||||||
var (
|
var (
|
||||||
deliver = func(packet dataPack) (int, error) {
|
deliver = func(packet dataPack) (int, error) {
|
||||||
pack := packet.(*bodyPack)
|
pack := packet.(*bodyPack)
|
||||||
return d.queue.DeliverBodies(pack.peerId, pack.transactions, pack.uncles)
|
return d.queue.DeliverBodies(pack.peerID, pack.transactions, pack.uncles)
|
||||||
}
|
}
|
||||||
expire = func() map[string]int { return d.queue.ExpireBodies(d.requestTTL()) }
|
expire = func() map[string]int { return d.queue.ExpireBodies(d.requestTTL()) }
|
||||||
fetch = func(p *peerConnection, req *fetchRequest) error { return p.FetchBodies(req) }
|
fetch = func(p *peerConnection, req *fetchRequest) error { return p.FetchBodies(req) }
|
||||||
|
|
@ -954,7 +954,7 @@ func (d *Downloader) fetchReceipts(from uint64) error {
|
||||||
var (
|
var (
|
||||||
deliver = func(packet dataPack) (int, error) {
|
deliver = func(packet dataPack) (int, error) {
|
||||||
pack := packet.(*receiptPack)
|
pack := packet.(*receiptPack)
|
||||||
return d.queue.DeliverReceipts(pack.peerId, pack.receipts)
|
return d.queue.DeliverReceipts(pack.peerID, pack.receipts)
|
||||||
}
|
}
|
||||||
expire = func() map[string]int { return d.queue.ExpireReceipts(d.requestTTL()) }
|
expire = func() map[string]int { return d.queue.ExpireReceipts(d.requestTTL()) }
|
||||||
fetch = func(p *peerConnection, req *fetchRequest) error { return p.FetchReceipts(req) }
|
fetch = func(p *peerConnection, req *fetchRequest) error { return p.FetchReceipts(req) }
|
||||||
|
|
|
||||||
|
|
@ -596,21 +596,21 @@ func (q *queue) cancel(request *fetchRequest, taskQueue *prque.Prque, pendPool m
|
||||||
// Revoke cancels all pending requests belonging to a given peer. This method is
|
// Revoke cancels all pending requests belonging to a given peer. This method is
|
||||||
// meant to be called during a peer drop to quickly reassign owned data fetches
|
// meant to be called during a peer drop to quickly reassign owned data fetches
|
||||||
// to remaining nodes.
|
// to remaining nodes.
|
||||||
func (q *queue) Revoke(peerId string) {
|
func (q *queue) Revoke(peerID string) {
|
||||||
q.lock.Lock()
|
q.lock.Lock()
|
||||||
defer q.lock.Unlock()
|
defer q.lock.Unlock()
|
||||||
|
|
||||||
if request, ok := q.blockPendPool[peerId]; ok {
|
if request, ok := q.blockPendPool[peerID]; ok {
|
||||||
for _, header := range request.Headers {
|
for _, header := range request.Headers {
|
||||||
q.blockTaskQueue.Push(header, -float32(header.Number.Uint64()))
|
q.blockTaskQueue.Push(header, -float32(header.Number.Uint64()))
|
||||||
}
|
}
|
||||||
delete(q.blockPendPool, peerId)
|
delete(q.blockPendPool, peerID)
|
||||||
}
|
}
|
||||||
if request, ok := q.receiptPendPool[peerId]; ok {
|
if request, ok := q.receiptPendPool[peerID]; ok {
|
||||||
for _, header := range request.Headers {
|
for _, header := range request.Headers {
|
||||||
q.receiptTaskQueue.Push(header, -float32(header.Number.Uint64()))
|
q.receiptTaskQueue.Push(header, -float32(header.Number.Uint64()))
|
||||||
}
|
}
|
||||||
delete(q.receiptPendPool, peerId)
|
delete(q.receiptPendPool, peerID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,22 +34,22 @@ type dataPack interface {
|
||||||
|
|
||||||
// headerPack is a batch of block headers returned by a peer.
|
// headerPack is a batch of block headers returned by a peer.
|
||||||
type headerPack struct {
|
type headerPack struct {
|
||||||
peerId string
|
peerID string
|
||||||
headers []*types.Header
|
headers []*types.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *headerPack) PeerId() string { return p.peerId }
|
func (p *headerPack) PeerId() string { return p.peerID }
|
||||||
func (p *headerPack) Items() int { return len(p.headers) }
|
func (p *headerPack) Items() int { return len(p.headers) }
|
||||||
func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) }
|
func (p *headerPack) Stats() string { return fmt.Sprintf("%d", len(p.headers)) }
|
||||||
|
|
||||||
// bodyPack is a batch of block bodies returned by a peer.
|
// bodyPack is a batch of block bodies returned by a peer.
|
||||||
type bodyPack struct {
|
type bodyPack struct {
|
||||||
peerId string
|
peerID string
|
||||||
transactions [][]*types.Transaction
|
transactions [][]*types.Transaction
|
||||||
uncles [][]*types.Header
|
uncles [][]*types.Header
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *bodyPack) PeerId() string { return p.peerId }
|
func (p *bodyPack) PeerId() string { return p.peerID }
|
||||||
func (p *bodyPack) Items() int {
|
func (p *bodyPack) Items() int {
|
||||||
if len(p.transactions) <= len(p.uncles) {
|
if len(p.transactions) <= len(p.uncles) {
|
||||||
return len(p.transactions)
|
return len(p.transactions)
|
||||||
|
|
@ -60,20 +60,20 @@ func (p *bodyPack) Stats() string { return fmt.Sprintf("%d:%d", len(p.transactio
|
||||||
|
|
||||||
// receiptPack is a batch of receipts returned by a peer.
|
// receiptPack is a batch of receipts returned by a peer.
|
||||||
type receiptPack struct {
|
type receiptPack struct {
|
||||||
peerId string
|
peerID string
|
||||||
receipts [][]*types.Receipt
|
receipts [][]*types.Receipt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *receiptPack) PeerId() string { return p.peerId }
|
func (p *receiptPack) PeerId() string { return p.peerID }
|
||||||
func (p *receiptPack) Items() int { return len(p.receipts) }
|
func (p *receiptPack) Items() int { return len(p.receipts) }
|
||||||
func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) }
|
func (p *receiptPack) Stats() string { return fmt.Sprintf("%d", len(p.receipts)) }
|
||||||
|
|
||||||
// statePack is a batch of states returned by a peer.
|
// statePack is a batch of states returned by a peer.
|
||||||
type statePack struct {
|
type statePack struct {
|
||||||
peerId string
|
peerID string
|
||||||
states [][]byte
|
states [][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *statePack) PeerId() string { return p.peerId }
|
func (p *statePack) PeerId() string { return p.peerID }
|
||||||
func (p *statePack) Items() int { return len(p.states) }
|
func (p *statePack) Items() int { return len(p.states) }
|
||||||
func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }
|
func (p *statePack) Stats() string { return fmt.Sprintf("%d", len(p.states)) }
|
||||||
|
|
|
||||||
|
|
@ -64,7 +64,7 @@ func errResp(code errCode, format string, v ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProtocolManager struct {
|
type ProtocolManager struct {
|
||||||
networkId uint64
|
networkID uint64
|
||||||
|
|
||||||
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
|
fastSync uint32 // Flag whether fast sync is enabled (gets disabled if we already have blocks)
|
||||||
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
|
acceptTxs uint32 // Flag whether we're considered synchronised (enables transaction processing)
|
||||||
|
|
@ -98,10 +98,10 @@ type ProtocolManager struct {
|
||||||
|
|
||||||
// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
// NewProtocolManager returns a new Ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
||||||
// with the Ethereum network.
|
// with the Ethereum network.
|
||||||
func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkId uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
|
func NewProtocolManager(config *params.ChainConfig, mode downloader.SyncMode, networkID uint64, mux *event.TypeMux, txpool txPool, engine consensus.Engine, blockchain *core.BlockChain, chaindb ethdb.Database) (*ProtocolManager, error) {
|
||||||
// Create the protocol manager with the base fields
|
// Create the protocol manager with the base fields
|
||||||
manager := &ProtocolManager{
|
manager := &ProtocolManager{
|
||||||
networkId: networkId,
|
networkID: networkID,
|
||||||
eventMux: mux,
|
eventMux: mux,
|
||||||
txpool: txpool,
|
txpool: txpool,
|
||||||
blockchain: blockchain,
|
blockchain: blockchain,
|
||||||
|
|
@ -263,7 +263,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
|
||||||
number = head.Number.Uint64()
|
number = head.Number.Uint64()
|
||||||
td = pm.blockchain.GetTd(hash, number)
|
td = pm.blockchain.GetTd(hash, number)
|
||||||
)
|
)
|
||||||
if err := p.Handshake(pm.networkId, td, hash, genesis.Hash()); err != nil {
|
if err := p.Handshake(pm.networkID, td, hash, genesis.Hash()); err != nil {
|
||||||
p.Log().Debug("Ethereum handshake failed", "err", err)
|
p.Log().Debug("Ethereum handshake failed", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -770,7 +770,7 @@ type NodeInfo struct {
|
||||||
func (pm *ProtocolManager) NodeInfo() *NodeInfo {
|
func (pm *ProtocolManager) NodeInfo() *NodeInfo {
|
||||||
currentBlock := pm.blockchain.CurrentBlock()
|
currentBlock := pm.blockchain.CurrentBlock()
|
||||||
return &NodeInfo{
|
return &NodeInfo{
|
||||||
Network: pm.networkId,
|
Network: pm.networkID,
|
||||||
Difficulty: pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
|
Difficulty: pm.blockchain.GetTd(currentBlock.Hash(), currentBlock.NumberU64()),
|
||||||
Genesis: pm.blockchain.Genesis().Hash(),
|
Genesis: pm.blockchain.Genesis().Hash(),
|
||||||
Config: pm.blockchain.Config(),
|
Config: pm.blockchain.Config(),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue