Skip to content

group: add SetUint64 method to Group.Scalar. #301

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

Merged
merged 1 commit into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Element interface {
// Scalar represents an integer scalar.
type Scalar interface {
IsEqual(Scalar) bool
SetUint64(uint64)
Add(Scalar, Scalar) Scalar
Sub(Scalar, Scalar) Scalar
Mul(Scalar, Scalar) Scalar
Expand Down
11 changes: 11 additions & 0 deletions group/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,18 +183,22 @@ func testScalar(t *testing.T, testTimes int, g group.Group) {
d := g.NewScalar()
e := g.NewScalar()
f := g.NewScalar()
one := g.NewScalar()
one.SetUint64(1)
params := g.Params()
for i := 0; i < testTimes; i++ {
a := g.RandomScalar(rand.Reader)
b := g.RandomScalar(rand.Reader)
c.Add(a, b)
d.Sub(a, b)
e.Mul(c, d)
e.Add(e, one)

c.Mul(a, a)
d.Mul(b, b)
d.Neg(d)
f.Add(c, d)
f.Add(f, one)
enc1, err1 := e.MarshalBinary()
enc2, err2 := f.MarshalBinary()
if err1 != nil || err2 != nil || !bytes.Equal(enc1, enc2) {
Expand All @@ -204,6 +208,13 @@ func testScalar(t *testing.T, testTimes int, g group.Group) {
test.ReportError(t, l, params.ScalarLength)
}
}

a := g.RandomScalar(rand.Reader)
c.Inv(a)
c.Mul(c, a)
if !one.IsEqual(c) {
test.ReportError(t, c, one, a)
}
}

func BenchmarkElement(b *testing.B) {
Expand Down
3 changes: 3 additions & 0 deletions group/ristretto255.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto"
_ "crypto/sha512" // to link libraries
"io"
"math/big"

r255 "github.com/bwesterb/go-ristretto"
)
Expand Down Expand Up @@ -145,6 +146,8 @@ func (e *ristrettoElement) UnmarshalBinary(data []byte) error {
return e.p.UnmarshalBinary(data)
}

func (s *ristrettoScalar) SetUint64(n uint64) { s.s.SetBigInt(new(big.Int).SetUint64(n)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use SetBytes. Scalars are encoded little endian, so simply write the uint64 into the [32]byte at the first 8 bytes using encoding/binary.LittleEndian.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also add a SetUint64.


func (s *ristrettoScalar) IsEqual(x Scalar) bool {
return s.s.Equals(&x.(*ristrettoScalar).s)
}
Expand Down
3 changes: 2 additions & 1 deletion group/short.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ type wScl struct {
k []byte
}

func (s *wScl) String() string { return fmt.Sprintf("0x%x", s.k) }
func (s *wScl) String() string { return fmt.Sprintf("0x%x", s.k) }
func (s *wScl) SetUint64(n uint64) { s.fromBig(new(big.Int).SetUint64(n)) }
func (s *wScl) IsEqual(a Scalar) bool {
aa := s.cvtScl(a)
return subtle.ConstantTimeCompare(s.k, aa.k) == 1
Expand Down