extra: make Scalar.Invert(0) return 0

We don't want panics scattered around.
This commit is contained in:
Filippo Valsorda 2021-06-04 16:55:16 +02:00
parent dd0c73fa20
commit 0307d66a90
2 changed files with 6 additions and 5 deletions

View file

@ -64,12 +64,8 @@ func (s *Scalar) pow2k(k int) {
// Invert sets s to the inverse of a nonzero scalar v, and returns s.
//
// If t is zero, Invert will panic.
// If t is zero, Invert returns zero.
func (s *Scalar) Invert(t *Scalar) *Scalar {
if t.s == [32]byte{} {
panic("edwards25519: zero Scalar passed to Invert")
}
// Uses a hardcoded sliding window of width 4.
var table [8]Scalar
var tt Scalar

View file

@ -109,6 +109,11 @@ func TestScalarInvert(t *testing.T) {
if err := quick.Check(invertWorks, quickCheckConfig32); err != nil {
t.Error(err)
}
zero := NewScalar()
if xx := NewScalar().Invert(zero); xx.Equal(zero) != 1 {
t.Errorf("inverting zero did not return zero")
}
}
func TestMultiScalarMultMatchesBaseMult(t *testing.T) {