Skip to content

Commit

Permalink
Changed kilic naming convention to match circl
Browse files Browse the repository at this point in the history
  • Loading branch information
matteosz committed Feb 28, 2024
1 parent ae4016d commit 04cbb85
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 130 deletions.
78 changes: 39 additions & 39 deletions pairing/kilic_bls12381/g1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func DefaultDomainG1() []byte {
return domainG1
}

// KyberG1 is a kyber.Point holding a G1 point on BLS12-381 curve
type KyberG1 struct {
// G1Elt is a kyber.Point holding a G1 point on BLS12-381 curve
type G1Elt struct {
p *bls12381.PointG1
// domain separation tag. We treat a 0 len dst as the default value as per the RFC "Tags MUST have nonzero length"
dst []byte
Expand All @@ -28,108 +28,108 @@ type KyberG1 struct {
kyber.HashablePoint
}

func NullKyberG1(dst ...byte) *KyberG1 {
func NullG1(dst ...byte) *G1Elt {
var p bls12381.PointG1
return newKyberG1(&p, dst)
return newG1(&p, dst)
}
func newKyberG1(p *bls12381.PointG1, dst []byte) *KyberG1 {
func newG1(p *bls12381.PointG1, dst []byte) *G1Elt {
domain := dst
if bytes.Equal(dst, domainG1) {
domain = nil
}
return &KyberG1{p: p, dst: domain}
return &G1Elt{p: p, dst: domain}
}

func (k *KyberG1) Equal(k2 kyber.Point) bool {
k2g1, ok := k2.(*KyberG1)
func (k *G1Elt) Equal(k2 kyber.Point) bool {
k2g1, ok := k2.(*G1Elt)
if !ok {
return false
}
return bls12381.NewG1().Equal(k.p, k2g1.p) && bytes.Equal(k.dst, k2g1.dst)
}

func (k *KyberG1) Null() kyber.Point {
return newKyberG1(bls12381.NewG1().Zero(), k.dst)
func (k *G1Elt) Null() kyber.Point {
return newG1(bls12381.NewG1().Zero(), k.dst)
}

func (k *KyberG1) Base() kyber.Point {
return newKyberG1(bls12381.NewG1().One(), k.dst)
func (k *G1Elt) Base() kyber.Point {
return newG1(bls12381.NewG1().One(), k.dst)
}

func (k *KyberG1) Pick(rand cipher.Stream) kyber.Point {
func (k *G1Elt) Pick(rand cipher.Stream) kyber.Point {
var dst, src [32]byte
rand.XORKeyStream(dst[:], src[:])
return k.Hash(dst[:])
}

func (k *KyberG1) Set(q kyber.Point) kyber.Point {
k.p.Set(q.(*KyberG1).p)
func (k *G1Elt) Set(q kyber.Point) kyber.Point {
k.p.Set(q.(*G1Elt).p)
return k
}

func (k *KyberG1) Clone() kyber.Point {
func (k *G1Elt) Clone() kyber.Point {
var p bls12381.PointG1
p.Set(k.p)
return newKyberG1(&p, k.dst)
return newG1(&p, k.dst)
}

func (k *KyberG1) EmbedLen() int {
func (k *G1Elt) EmbedLen() int {
panic("bls12-381: unsupported operation")
}

func (k *KyberG1) Embed(data []byte, rand cipher.Stream) kyber.Point {
func (k *G1Elt) Embed(data []byte, rand cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

func (k *KyberG1) Data() ([]byte, error) {
func (k *G1Elt) Data() ([]byte, error) {
panic("bls12-381: unsupported operation")
}

func (k *KyberG1) Add(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG1)
bb := b.(*KyberG1)
func (k *G1Elt) Add(a, b kyber.Point) kyber.Point {
aa := a.(*G1Elt)
bb := b.(*G1Elt)
bls12381.NewG1().Add(k.p, aa.p, bb.p)
return k
}

func (k *KyberG1) Sub(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG1)
bb := b.(*KyberG1)
func (k *G1Elt) Sub(a, b kyber.Point) kyber.Point {
aa := a.(*G1Elt)
bb := b.(*G1Elt)
bls12381.NewG1().Sub(k.p, aa.p, bb.p)
return k
}

func (k *KyberG1) Neg(a kyber.Point) kyber.Point {
aa := a.(*KyberG1)
func (k *G1Elt) Neg(a kyber.Point) kyber.Point {
aa := a.(*G1Elt)
bls12381.NewG1().Neg(k.p, aa.p)
return k
}

func (k *KyberG1) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
func (k *G1Elt) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
if q == nil {
q = NullKyberG1(k.dst...).Base()
q = NullG1(k.dst...).Base()
}
bls12381.NewG1().MulScalarBig(k.p, q.(*KyberG1).p, &s.(*mod.Int).V)
bls12381.NewG1().MulScalarBig(k.p, q.(*G1Elt).p, &s.(*mod.Int).V)
return k
}

// MarshalBinary returns a compressed point, without any domain separation tag information
func (k *KyberG1) MarshalBinary() ([]byte, error) {
func (k *G1Elt) MarshalBinary() ([]byte, error) {
// we need to clone the point because of https://github.com/kilic/bls12-381/issues/37
// in order to avoid risks of race conditions.
t := new(bls12381.PointG1).Set(k.p)
return bls12381.NewG1().ToCompressed(t), nil
}

// UnmarshalBinary populates the point from a compressed point representation.
func (k *KyberG1) UnmarshalBinary(buff []byte) error {
func (k *G1Elt) UnmarshalBinary(buff []byte) error {
var err error
k.p, err = bls12381.NewG1().FromCompressed(buff)
return err
}

// MarshalTo writes a compressed point to the Writer, without any domain separation tag information
func (k *KyberG1) MarshalTo(w io.Writer) (int, error) {
func (k *G1Elt) MarshalTo(w io.Writer) (int, error) {
buf, err := k.MarshalBinary()
if err != nil {
return 0, err
Expand All @@ -138,7 +138,7 @@ func (k *KyberG1) MarshalTo(w io.Writer) (int, error) {
}

// UnmarshalFrom populates the point from a compressed point representation read from the Reader.
func (k *KyberG1) UnmarshalFrom(r io.Reader) (int, error) {
func (k *G1Elt) UnmarshalFrom(r io.Reader) (int, error) {
buf := make([]byte, k.MarshalSize())
n, err := io.ReadFull(r, buf)
if err != nil {
Expand All @@ -147,16 +147,16 @@ func (k *KyberG1) UnmarshalFrom(r io.Reader) (int, error) {
return n, k.UnmarshalBinary(buf)
}

func (k *KyberG1) MarshalSize() int {
func (k *G1Elt) MarshalSize() int {
return 48
}

func (k *KyberG1) String() string {
func (k *G1Elt) String() string {
b, _ := k.MarshalBinary()
return "bls12-381.G1: " + hex.EncodeToString(b)
}

func (k *KyberG1) Hash(m []byte) kyber.Point {
func (k *G1Elt) Hash(m []byte) kyber.Point {
domain := domainG1
// We treat a 0 len dst as the default value as per the RFC "Tags MUST have nonzero length"
if len(k.dst) != 0 {
Expand All @@ -167,6 +167,6 @@ func (k *KyberG1) Hash(m []byte) kyber.Point {
return k
}

func (k *KyberG1) IsInCorrectGroup() bool {
func (k *G1Elt) IsInCorrectGroup() bool {
return bls12381.NewG1().InCorrectSubgroup(k.p)
}
78 changes: 39 additions & 39 deletions pairing/kilic_bls12381/g2.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,116 +19,116 @@ func DefaultDomainG2() []byte {
return domainG2
}

// KyberG2 is a kyber.Point holding a G2 point on BLS12-381 curve
type KyberG2 struct {
// G2Elt is a kyber.Point holding a G2 point on BLS12-381 curve
type G2Elt struct {
p *bls12381.PointG2
// domain separation tag. We treat a 0 len dst as the default value as per the RFC "Tags MUST have nonzero length"
dst []byte
}

func NullKyberG2(dst ...byte) *KyberG2 {
func NullG2(dst ...byte) *G2Elt {
var p bls12381.PointG2
return newKyberG2(&p, dst)
return newG2(&p, dst)
}

func newKyberG2(p *bls12381.PointG2, dst []byte) *KyberG2 {
func newG2(p *bls12381.PointG2, dst []byte) *G2Elt {
domain := dst
if bytes.Equal(dst, domainG2) {
domain = nil
}
return &KyberG2{p: p, dst: domain}
return &G2Elt{p: p, dst: domain}
}

func (k *KyberG2) Equal(k2 kyber.Point) bool {
k2g2, ok := k2.(*KyberG2)
func (k *G2Elt) Equal(k2 kyber.Point) bool {
k2g2, ok := k2.(*G2Elt)
if !ok {
return false
}
return bls12381.NewG2().Equal(k.p, k2g2.p) && bytes.Equal(k.dst, k2g2.dst)
}

func (k *KyberG2) Null() kyber.Point {
return newKyberG2(bls12381.NewG2().Zero(), k.dst)
func (k *G2Elt) Null() kyber.Point {
return newG2(bls12381.NewG2().Zero(), k.dst)
}

func (k *KyberG2) Base() kyber.Point {
return newKyberG2(bls12381.NewG2().One(), k.dst)
func (k *G2Elt) Base() kyber.Point {
return newG2(bls12381.NewG2().One(), k.dst)
}

func (k *KyberG2) Pick(rand cipher.Stream) kyber.Point {
func (k *G2Elt) Pick(rand cipher.Stream) kyber.Point {
var dst, src [32]byte
rand.XORKeyStream(dst[:], src[:])
return k.Hash(dst[:])
}

func (k *KyberG2) Set(q kyber.Point) kyber.Point {
k.p.Set(q.(*KyberG2).p)
func (k *G2Elt) Set(q kyber.Point) kyber.Point {
k.p.Set(q.(*G2Elt).p)
return k
}

func (k *KyberG2) Clone() kyber.Point {
func (k *G2Elt) Clone() kyber.Point {
var p bls12381.PointG2
p.Set(k.p)
return newKyberG2(&p, k.dst)
return newG2(&p, k.dst)
}

func (k *KyberG2) EmbedLen() int {
func (k *G2Elt) EmbedLen() int {
panic("bls12-381: unsupported operation")
}

func (k *KyberG2) Embed(data []byte, rand cipher.Stream) kyber.Point {
func (k *G2Elt) Embed(data []byte, rand cipher.Stream) kyber.Point {
panic("bls12-381: unsupported operation")
}

func (k *KyberG2) Data() ([]byte, error) {
func (k *G2Elt) Data() ([]byte, error) {
panic("bls12-381: unsupported operation")
}

func (k *KyberG2) Add(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG2)
bb := b.(*KyberG2)
func (k *G2Elt) Add(a, b kyber.Point) kyber.Point {
aa := a.(*G2Elt)
bb := b.(*G2Elt)
bls12381.NewG2().Add(k.p, aa.p, bb.p)
return k
}

func (k *KyberG2) Sub(a, b kyber.Point) kyber.Point {
aa := a.(*KyberG2)
bb := b.(*KyberG2)
func (k *G2Elt) Sub(a, b kyber.Point) kyber.Point {
aa := a.(*G2Elt)
bb := b.(*G2Elt)
bls12381.NewG2().Sub(k.p, aa.p, bb.p)
return k
}

func (k *KyberG2) Neg(a kyber.Point) kyber.Point {
aa := a.(*KyberG2)
func (k *G2Elt) Neg(a kyber.Point) kyber.Point {
aa := a.(*G2Elt)
bls12381.NewG2().Neg(k.p, aa.p)
return k
}

func (k *KyberG2) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
func (k *G2Elt) Mul(s kyber.Scalar, q kyber.Point) kyber.Point {
if q == nil {
q = NullKyberG2(k.dst...).Base()
q = NullG2(k.dst...).Base()
}
bls12381.NewG2().MulScalarBig(k.p, q.(*KyberG2).p, &s.(*mod.Int).V)
bls12381.NewG2().MulScalarBig(k.p, q.(*G2Elt).p, &s.(*mod.Int).V)
return k
}

// MarshalBinary returns a compressed point, without any domain separation tag information
func (k *KyberG2) MarshalBinary() ([]byte, error) {
func (k *G2Elt) MarshalBinary() ([]byte, error) {
// we need to clone the point because of https://github.com/kilic/bls12-381/issues/37
// in order to avoid risks of race conditions.
t := new(bls12381.PointG2).Set(k.p)
return bls12381.NewG2().ToCompressed(t), nil
}

// UnmarshalBinary populates the point from a compressed point representation.
func (k *KyberG2) UnmarshalBinary(buff []byte) error {
func (k *G2Elt) UnmarshalBinary(buff []byte) error {
var err error
k.p, err = bls12381.NewG2().FromCompressed(buff)
return err
}

// MarshalTo writes a compressed point to the Writer, without any domain separation tag information
func (k *KyberG2) MarshalTo(w io.Writer) (int, error) {
func (k *G2Elt) MarshalTo(w io.Writer) (int, error) {
buf, err := k.MarshalBinary()
if err != nil {
return 0, err
Expand All @@ -137,7 +137,7 @@ func (k *KyberG2) MarshalTo(w io.Writer) (int, error) {
}

// UnmarshalFrom populates the point from a compressed point representation read from the Reader.
func (k *KyberG2) UnmarshalFrom(r io.Reader) (int, error) {
func (k *G2Elt) UnmarshalFrom(r io.Reader) (int, error) {
buf := make([]byte, k.MarshalSize())
n, err := io.ReadFull(r, buf)
if err != nil {
Expand All @@ -146,16 +146,16 @@ func (k *KyberG2) UnmarshalFrom(r io.Reader) (int, error) {
return n, k.UnmarshalBinary(buf)
}

func (k *KyberG2) MarshalSize() int {
func (k *G2Elt) MarshalSize() int {
return 96
}

func (k *KyberG2) String() string {
func (k *G2Elt) String() string {
b, _ := k.MarshalBinary()
return "bls12-381.G2: " + hex.EncodeToString(b)
}

func (k *KyberG2) Hash(m []byte) kyber.Point {
func (k *G2Elt) Hash(m []byte) kyber.Point {
domain := domainG2
// We treat a 0 len dst as the default value as per the RFC "Tags MUST have nonzero length"
if len(k.dst) != 0 {
Expand All @@ -166,6 +166,6 @@ func (k *KyberG2) Hash(m []byte) kyber.Point {
return k
}

func (k *KyberG2) IsInCorrectGroup() bool {
func (k *G2Elt) IsInCorrectGroup() bool {
return bls12381.NewG2().InCorrectSubgroup(k.p)
}
Loading

0 comments on commit 04cbb85

Please sign in to comment.