diff --git a/pairing/bn256/curve.go b/pairing/bn256/curve.go index bf959c9ce..cf2c6f3e6 100644 --- a/pairing/bn256/curve.go +++ b/pairing/bn256/curve.go @@ -1,6 +1,7 @@ package bn256 import ( + "fmt" "math/big" ) @@ -25,7 +26,7 @@ func (c *curvePoint) String() string { x, y := &gfP{}, &gfP{} montDecode(x, &c.x) montDecode(y, &c.y) - return x.String() + y.String() + return fmt.Sprintf("(%s, %s)", x.String(), y.String()) } func (c *curvePoint) Set(a *curvePoint) { diff --git a/pairing/bn256/point.go b/pairing/bn256/point.go index 1bfa5f981..4c20c5bac 100644 --- a/pairing/bn256/point.go +++ b/pairing/bn256/point.go @@ -184,7 +184,7 @@ func (p *pointG1) ElementSize() int { } func (p *pointG1) String() string { - return p.g.String() + return "bn256.G1:" + p.g.String() } type pointG2 struct { @@ -371,7 +371,7 @@ func (p *pointG2) ElementSize() int { } func (p *pointG2) String() string { - return p.g.String() + return "bn256.G2:" + p.g.String() } type pointGT struct { @@ -563,7 +563,7 @@ func (p *pointGT) ElementSize() int { } func (p *pointGT) String() string { - return p.g.String() + return "bn256.GT:" + p.g.String() } func (p *pointGT) Finalize() kyber.Point { diff --git a/pairing/bn256/suite.go b/pairing/bn256/suite.go index ed89375cd..4c51668d7 100644 --- a/pairing/bn256/suite.go +++ b/pairing/bn256/suite.go @@ -9,7 +9,6 @@ import ( "github.com/dedis/fixbuf" "github.com/dedis/kyber" - "github.com/dedis/kyber/group/mod" "github.com/dedis/kyber/util/random" "github.com/dedis/kyber/xof/blake2xb" ) @@ -31,10 +30,31 @@ func NewSuite() *Suite { return s } +// NewSuiteG1 returns a G1 suite. +func NewSuiteG1() *Suite { + s := NewSuite() + s.commonSuite.Group = &groupG1{commonSuite: &commonSuite{}} + return s +} + +// NewSuiteG2 returns a G2 suite. +func NewSuiteG2() *Suite { + s := NewSuite() + s.commonSuite.Group = &groupG2{commonSuite: &commonSuite{}} + return s +} + +// NewSuiteGT returns a GT suite. +func NewSuiteGT() *Suite { + s := NewSuite() + s.commonSuite.Group = &groupGT{commonSuite: &commonSuite{}} + return s +} + // NewSuiteRand generates and returns a new BN256 suite seeded by the // given cipher stream. func NewSuiteRand(rand cipher.Stream) *Suite { - s := &Suite{commonSuite: &commonSuite{rand}} + s := &Suite{commonSuite: &commonSuite{s: rand}} s.g1 = &groupG1{commonSuite: s.commonSuite} s.g2 = &groupG2{commonSuite: s.commonSuite} s.gt = &groupGT{commonSuite: s.commonSuite} @@ -63,26 +83,31 @@ func (s *Suite) Pair(p1 kyber.Point, p2 kyber.Point) kyber.Point { } // Not used other than for reflect.TypeOf() -var aScalar mod.Int +var aScalar kyber.Scalar +var aPoint kyber.Point var aPointG1 pointG1 var aPointG2 pointG2 var aPointGT pointGT var tScalar = reflect.TypeOf(&aScalar).Elem() +var tPoint = reflect.TypeOf(&aPoint).Elem() var tPointG1 = reflect.TypeOf(&aPointG1).Elem() var tPointG2 = reflect.TypeOf(&aPointG2).Elem() var tPointGT = reflect.TypeOf(&aPointGT).Elem() type commonSuite struct { s cipher.Stream + // kyber.Group is only set if we have a combined Suite + kyber.Group } // New implements the kyber.Encoding interface. func (c *commonSuite) New(t reflect.Type) interface{} { switch t { case tScalar: - g1 := groupG1{} - return g1.Scalar() + return c.Scalar() + case tPoint: + return c.Point() case tPointG1: g1 := groupG1{} return g1.Point() @@ -124,3 +149,11 @@ func (c *commonSuite) RandomStream() cipher.Stream { } return random.New() } + +// String returns a recognizable string that this is a combined suite. +func (c commonSuite) String() string { + if c.Group != nil { + return c.Group.String() + } + return "bn256" +} diff --git a/pairing/bn256/suite_test.go b/pairing/bn256/suite_test.go index ef7947cd9..0922b076f 100644 --- a/pairing/bn256/suite_test.go +++ b/pairing/bn256/suite_test.go @@ -237,3 +237,21 @@ func TestTripartiteDiffieHellman(t *testing.T) { require.Equal(t, k1, k2) require.Equal(t, k2, k3) } + +func TestCombined(t *testing.T) { + // Making sure we can do some basic arithmetic with the suites without having + // to extract the suite using .G1(), .G2(), .GT() + basicPointTest(t, NewSuiteG1()) + basicPointTest(t, NewSuiteG2()) + basicPointTest(t, NewSuiteGT()) +} + +func basicPointTest(t *testing.T, s *Suite) { + a := s.Scalar().Pick(random.New()) + pa := s.Point().Mul(a, nil) + + b := s.Scalar().Add(a, s.Scalar().One()) + pb1 := s.Point().Mul(b, nil) + pb2 := s.Point().Add(pa, s.Point().Base()) + require.True(t, pb1.Equal(pb2)) +} diff --git a/suites/all.go b/suites/all.go index e53bf5b76..aa78c2355 100644 --- a/suites/all.go +++ b/suites/all.go @@ -2,13 +2,8 @@ package suites import ( "github.com/dedis/kyber/group/edwards25519" - "github.com/dedis/kyber/pairing/bn256" ) func init() { register(edwards25519.NewBlakeSHA256Ed25519()) - register(bn256.NewSuite().G1().(Suite)) - register(bn256.NewSuite().G2().(Suite)) - register(bn256.NewSuite().GT().(Suite)) - } diff --git a/suites/all_vartime.go b/suites/all_vartime.go index 8af66f5db..cd21aeddd 100644 --- a/suites/all_vartime.go +++ b/suites/all_vartime.go @@ -13,7 +13,7 @@ func init() { register(curve25519.NewBlakeSHA256Curve25519(true)) register(nist.NewBlakeSHA256P256()) register(nist.NewBlakeSHA256QR512()) - register(bn256.NewSuite().G1().(Suite)) - register(bn256.NewSuite().G2().(Suite)) - register(bn256.NewSuite().GT().(Suite)) + register(bn256.NewSuiteG1()) + register(bn256.NewSuiteG2()) + register(bn256.NewSuiteGT()) }