Skip to content

Commit

Permalink
Create method SetIntString in the Scalar interface to allow the creat…
Browse files Browse the repository at this point in the history
…ion of a large Scalar from a string.

Previously, to create arbitrary numbers the methods SetInt64 or SetBytes had to be used.
SetInt64 only allows the creation of 64 bit integers, while SetBytes requires the byte array representation of the number, which is inconvenient.
  • Loading branch information
pmcampones committed Jul 24, 2024
1 parent 1a8e4d5 commit 9e72b53
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ type Scalar interface {
// SetInt64 sets the receiver to a small integer value.
SetInt64(v int64) Scalar

// SetIntString sets the receiver to a string encoded integer value.
SetIntString(v string) (Scalar, error)

// Set to the additive identity (0).
Zero() Scalar

Expand Down
8 changes: 8 additions & 0 deletions group/edwards25519/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ func (s *scalar) SetInt64(v int64) kyber.Scalar {
return s.setInt(mod.NewInt64(v, primeOrder))
}

func (s *scalar) SetIntString(v string) (kyber.Scalar, error) {
i, ok := new(big.Int).SetString(v, 0)
if !ok {
return nil, errors.New("invalid scalar string")
}
return s.setInt(mod.NewInt(i, primeOrder)), nil
}

func (s *scalar) toInt() *mod.Int {
return mod.NewIntBytes(s.v[:], primeOrder, defaultEndianess)
}
Expand Down
10 changes: 10 additions & 0 deletions group/mod/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/cipher"
"encoding/hex"
"errors"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -173,6 +174,15 @@ func (i *Int) SetInt64(v int64) kyber.Scalar {
return i
}

func (i *Int) SetIntString(v string) (kyber.Scalar, error) {
bigV := new(big.Int)
bigV, ok := bigV.SetString(v, 0)
if !ok {
return nil, fmt.Errorf("unable to set string number: %v", v)
}
return i.Init(bigV, i.M), nil
}

// Int64 returns the int64 representation of the value.
// If the value is not representable in an int64 the result is undefined.
func (i *Int) Int64() int64 {
Expand Down
9 changes: 9 additions & 0 deletions pairing/bls12381/circl/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package circl

import (
"crypto/cipher"
"fmt"
"io"
"math/big"

Expand Down Expand Up @@ -62,6 +63,14 @@ func (s *Scalar) SetInt64(v int64) kyber.Scalar {
return s
}

func (s *Scalar) SetIntString(v string) (kyber.Scalar, error) {
err := s.inner.SetString(v)
if err != nil {
return nil, fmt.Errorf("unable to set string number: %v", err)

Check failure on line 69 in pairing/bls12381/circl/scalar.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
return s, nil
}

func (s *Scalar) Zero() kyber.Scalar { s.inner.SetUint64(0); return s }

func (s *Scalar) Add(a, b kyber.Scalar) kyber.Scalar {
Expand Down
19 changes: 17 additions & 2 deletions share/vss/pedersen/vss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func TestMinimumT(t *testing.T) {

func TestVSSWhole(t *testing.T) {
dealer, verifiers := genAll()
vssWhole(t, dealer, verifiers, secret)
}

func vssWhole(t *testing.T, dealer *Dealer, verifiers []*Verifier, secret kyber.Scalar) {
// 1. dispatch deal
resps := make([]*Response, nbVerifiers)
encDeals, err := dealer.EncryptedDeals()
Expand Down Expand Up @@ -591,6 +594,15 @@ func TestVSSContext(t *testing.T) {
assert.Len(t, c, suite.Hash().Size())
}

func TestDeterministicStringSecret(t *testing.T) {
deterministicSec, err := suite.Scalar().SetIntString("0x123456789abcdef")
require.NoError(t, err)
dealer, err := NewDealer(suite, dealerSec, deterministicSec, verifiersPub, vssThreshold)
require.NoError(t, err)
verifiers := genVerifiers()
vssWhole(t, dealer, verifiers, deterministicSec)
}

func genPair() (kyber.Scalar, kyber.Point) {
secret := suite.Scalar().Pick(suite.RandomStream())
public := suite.Point().Mul(secret, nil)
Expand All @@ -612,13 +624,16 @@ func genDealer() *Dealer {
}

func genAll() (*Dealer, []*Verifier) {
dealer := genDealer()
return genDealer(), genVerifiers()
}

func genVerifiers() []*Verifier {
var verifiers = make([]*Verifier, nbVerifiers)
for i := 0; i < nbVerifiers; i++ {
v, _ := NewVerifier(suite, verifiersSec[i], dealerPub, verifiersPub)
verifiers[i] = v
}
return dealer, verifiers
return verifiers
}

func randomBytes(n int) []byte {
Expand Down

0 comments on commit 9e72b53

Please sign in to comment.