Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group: adds Set/Copy methods for group elements and scalars #326

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Group interface {

// Element represents an abstract element of a prime-order group.
type Element interface {
Set(Element) Element
Copy() Element
IsIdentity() bool
IsEqual(Element) bool
Add(Element, Element) Element
Expand All @@ -45,6 +47,8 @@ type Element interface {

// Scalar represents an integer scalar.
type Scalar interface {
Set(Scalar) Scalar
Copy() Scalar
IsEqual(Scalar) bool
SetUint64(uint64)
Add(Scalar, Scalar) Scalar
Expand Down
18 changes: 18 additions & 0 deletions group/ristretto255.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ func (e *ristrettoElement) IsEqual(x Element) bool {
return e.p.Equals(&x.(*ristrettoElement).p)
}

func (e *ristrettoElement) Set(x Element) Element {
e.p.Set(&x.(*ristrettoElement).p)
return e
}

func (e *ristrettoElement) Copy() Element {
return &ristrettoElement{*new(r255.Point).Set(&e.p)}
armfazh marked this conversation as resolved.
Show resolved Hide resolved
}

func (e *ristrettoElement) Add(x Element, y Element) Element {
e.p.Add(&x.(*ristrettoElement).p, &y.(*ristrettoElement).p)
return e
Expand Down Expand Up @@ -164,6 +173,15 @@ func (s *ristrettoScalar) IsEqual(x Scalar) bool {
return s.s.Equals(&x.(*ristrettoScalar).s)
}

func (s *ristrettoScalar) Set(x Scalar) Scalar {
s.s.Set(&x.(*ristrettoScalar).s)
return s
}

func (s *ristrettoScalar) Copy() Scalar {
return &ristrettoScalar{*new(r255.Scalar).Set(&s.s)}
}

func (s *ristrettoScalar) Add(x Scalar, y Scalar) Scalar {
s.s.Add(&x.(*ristrettoScalar).s, &y.(*ristrettoScalar).s)
return s
Expand Down
17 changes: 17 additions & 0 deletions group/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func (e *wElt) IsEqual(o Element) bool {
return e.x.Cmp(oo.x) == 0 && e.y.Cmp(oo.y) == 0
}

func (e *wElt) Set(a Element) Element {
aa := e.cvtElt(a)
e.x.Set(aa.x)
e.y.Set(aa.y)
return e
}

func (e *wElt) Copy() Element { return e.wG.zeroElement().Set(e) }
func (e *wElt) Add(a, b Element) Element {
aa, bb := e.cvtElt(a), e.cvtElt(b)
e.x, e.y = e.c.Add(aa.x, aa.y, bb.x, bb.y)
Expand Down Expand Up @@ -225,6 +233,15 @@ func (s *wScl) fromBig(b *big.Int) {
}
}

func (s *wScl) Set(a Scalar) Scalar {
aa := s.cvtScl(a)
if err := s.UnmarshalBinary(aa.k); err != nil {
panic(err)
}
return s
}

func (s *wScl) Copy() Scalar { return s.wG.zeroScalar().Set(s) }
func (s *wScl) Add(a, b Scalar) Scalar {
aa, bb := s.cvtScl(a), s.cvtScl(b)
r := new(big.Int)
Expand Down