Skip to content

Commit dd5d437

Browse files
ValeriodeSathehoul
authored andcommitted
Mod Int Migrates to compatible
1 parent 056759a commit dd5d437

12 files changed

Lines changed: 139 additions & 89 deletions

File tree

compatible/compatible_mod/const_mod.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ type Mod struct {
2525
func (m *Mod) Nat() *bigmod.Nat {
2626
return m.Nat()
2727
}
28+
29+
func (z *Mod) SetString(s string, base int) (*Mod, bool) { panic("implement me") }
30+
31+
func (z *Mod) SetUint64(v uint64) *Mod {
32+
panic("implement me")
33+
}
34+
35+
func (z *Mod) SetBytes(b []byte) (*Mod, error) {
36+
modulus, err := bigmod.NewModulus(b)
37+
if err != nil {
38+
return nil, err
39+
}
40+
return &Mod{modulus}, nil
41+
}

compatible/compatible_mod/var_mod.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ import (
66
"math/big"
77
)
88

9-
type Mod = big.Int
9+
type Mod struct {
10+
*big.Int
11+
}
1012

1113
func NewInt(x int64) *Mod {
12-
return big.NewInt(x)
14+
return &Mod{big.NewInt(x)}
15+
}
16+
17+
func (m *Mod) SetBytes(b []byte) (*Mod, error) {
18+
return &Mod{big.NewInt(0).SetBytes(b)}, nil
1319
}
1420

1521
// func Add(x, y *Int, _ int) *Int { return big.Add(x, y) }

compatible/const_int.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type Int struct {
1414
Int *bigmod.Nat
1515
}
1616

17+
func SetFromNat(x *bigmod.Nat) *Int {
18+
return &Int{x}
19+
}
20+
1721
func NewInt(x uint) *Int {
1822
var z = bigmod.NewNat().SetUint(x)
1923
//
@@ -98,6 +102,10 @@ func (z *Int) SetUint(v uint) *Int {
98102
z.Int = bigmod.NewNat().SetUint(v)
99103
return z
100104
}
105+
func (z *Int) SetUint64(v uint64) *Int {
106+
z.Int = bigmod.NewNat().SetUint(uint(v))
107+
return z
108+
}
101109

102110
func (z *Int) zero() *Int {
103111
z.Int.SetUint(0)

group.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package kyber
22

33
import (
44
"crypto/cipher"
5-
"math/big"
5+
"go.dedis.ch/kyber/v4/compatible/compatible_mod"
66
)
77

88
// ByteOrder denotes the endianness of the operation.
@@ -72,8 +72,8 @@ type Scalar interface {
7272
ByteOrder() ByteOrder
7373

7474
// GroupOrder returns the order of the underlying group
75-
// todo should this return a compatible.Int? Is the size of the groupOrder secret?
76-
GroupOrder() *big.Int
75+
// todo should this return a compatible_mod.Mod? Is the size of the groupOrder secret?
76+
GroupOrder() *compatible_mod.Mod
7777
}
7878

7979
// Point represents an element of a public-key cryptographic Group.

group/edwards25519/const.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package edwards25519
55

66
import (
77
"go.dedis.ch/kyber/v4/compatible"
8+
"go.dedis.ch/kyber/v4/compatible/compatible_mod"
89
)
910

1011
// prime modulus of underlying field = 2^255 - 19
11-
var prime, _ = new(compatible.Int).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10)
12+
var prime, _ = new(compatible_mod.Mod).SetString("57896044618658097711785492504343953926634992332820282019728792003956564819949", 10)
1213

1314
// prime order of base point = 2^252 + 27742317777372353535851937790883648493
1415
var primeOrder, _ = new(compatible.Int).SetString("7237005577332262213973186563042994240857116359379907606001950938285454250989", 10)
@@ -19,10 +20,10 @@ var primeOrder, _ = new(compatible.Int).SetString("72370055773322622139731865630
1920
var lMinus2, _ = new(compatible.Int).SetString("7237005577332262213973186563042994240857116359379907606001950938285454250987", 10)
2021

2122
// cofactor of the curve, as a ModInt
22-
var cofactor = new(compatible.Int).SetInt64(8)
23+
var cofactor = new(compatible.Int).SetUint64(8)
2324

2425
// order of the full group including the cofactor
25-
var fullOrder = new(compatible.Int).Mul(primeOrder, cofactor)
26+
var fullOrder = new(compatible.Int).Mul(primeOrder, cofactor, prime)
2627

2728
// scalar versions of these, usable for multiplication
2829
var primeOrderScalar = newScalarInt(primeOrder)

group/edwards25519/constant_scalar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build constantTime
1+
//go:build constantTime && a
22

33
// Copyright 2013 The Go Authors. All rights reserved.
44
// Use of this source code is governed by a BSD-style

group/edwards25519/scalar.go

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build !constantTime
2-
31
// Copyright 2013 The Go Authors. All rights reserved.
42
// Use of this source code is governed by a BSD-style
53
// license that can be found in the LICENSE file.
@@ -11,8 +9,8 @@ import (
119
"encoding/hex"
1210
"errors"
1311
"go.dedis.ch/kyber/v4/compatible"
12+
"go.dedis.ch/kyber/v4/compatible/compatible_mod"
1413
"io"
15-
"math/big"
1614

1715
"go.dedis.ch/kyber/v4"
1816
"go.dedis.ch/kyber/v4/group/internal/marshalling"
@@ -59,11 +57,19 @@ func (s *scalar) setInt(i *mod.Int) kyber.Scalar {
5957

6058
// SetInt64 sets the scalar to a small integer value.
6159
func (s *scalar) SetInt64(v int64) kyber.Scalar {
62-
return s.setInt(mod.NewInt64(v, primeOrder))
60+
modulusPrimeOrder, err := new(compatible_mod.Mod).SetBytes(primeOrder.Bytes())
61+
if err != nil {
62+
panic(err)
63+
}
64+
return s.setInt(mod.NewInt64(v, modulusPrimeOrder))
6365
}
6466

6567
func (s *scalar) toInt() *mod.Int {
66-
return mod.NewIntBytes(s.v[:], primeOrder, defaultEndianess)
68+
modulusPrimeOrder, err := new(compatible_mod.Mod).SetBytes(primeOrder.Bytes())
69+
if err != nil {
70+
panic(err)
71+
}
72+
return mod.NewIntBytes(s.v[:], modulusPrimeOrder, defaultEndianess)
6773
}
6874

6975
// Set to the additive identity (0)
@@ -137,13 +143,21 @@ func (s *scalar) Inv(a kyber.Scalar) kyber.Scalar {
137143

138144
// Set to a fresh random or pseudo-random scalar
139145
func (s *scalar) Pick(rand cipher.Stream) kyber.Scalar {
140-
i := mod.NewInt(random.Int(primeOrder, rand), primeOrder)
146+
modulusPrimeOrder, err := new(compatible_mod.Mod).SetBytes(primeOrder.Bytes())
147+
if err != nil {
148+
panic(err)
149+
}
150+
i := mod.NewInt(random.Int(primeOrder, rand), modulusPrimeOrder)
141151
return s.setInt(i)
142152
}
143153

144154
// SetBytes s to b, interpreted as a little endian integer.
145155
func (s *scalar) SetBytes(b []byte) kyber.Scalar {
146-
return s.setInt(mod.NewIntBytes(b, primeOrder, defaultEndianess))
156+
modulusPrimeOrder, err := new(compatible_mod.Mod).SetBytes(primeOrder.Bytes())
157+
if err != nil {
158+
panic(err)
159+
}
160+
return s.setInt(mod.NewIntBytes(b, modulusPrimeOrder, defaultEndianess))
147161
}
148162

149163
// ByteOrder return the byte representation type (big or little endian)
@@ -152,8 +166,13 @@ func (s *scalar) ByteOrder() kyber.ByteOrder {
152166
}
153167

154168
// GroupOrder returns the order of the underlying group
155-
func (s *scalar) GroupOrder() *big.Int {
156-
return big.NewInt(0).SetBytes(primeOrder.Bytes())
169+
func (s *scalar) GroupOrder() *compatible_mod.Mod {
170+
171+
modulusPrimeOrder, err := new(compatible_mod.Mod).SetBytes(primeOrder.Bytes())
172+
if err != nil {
173+
panic(err)
174+
}
175+
return modulusPrimeOrder
157176
}
158177

159178
// String returns the string representation of this scalar (fixed length of 32 bytes, little endian).
@@ -202,8 +221,13 @@ func (s *scalar) UnmarshalFrom(r io.Reader) (int, error) {
202221
}
203222

204223
func newScalarInt(i *compatible.Int) *scalar {
224+
modulusFullOrder, err := new(compatible_mod.Mod).SetBytes(fullOrder.Bytes())
225+
if err != nil {
226+
panic(err)
227+
}
228+
205229
s := scalar{}
206-
s.setInt(mod.NewInt(i, fullOrder))
230+
s.setInt(mod.NewInt(i, modulusFullOrder))
207231
return &s
208232
}
209233

group/mod/constant_time_int.go

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@ import (
88
"crypto/cipher"
99
"encoding/hex"
1010
"errors"
11-
"github.com/cronokirby/saferith"
11+
"go.dedis.ch/kyber/v4"
1212
"go.dedis.ch/kyber/v4/compatible"
1313
"go.dedis.ch/kyber/v4/compatible/compatible_mod"
14-
"io"
15-
"math/big"
16-
17-
"go.dedis.ch/kyber/v4"
1814
"go.dedis.ch/kyber/v4/group/internal/marshalling"
1915
"go.dedis.ch/kyber/v4/util/random"
16+
"io"
2017
)
2118

2219
var Cap = 64
2320
var marshalScalarID = [8]byte{'m', 'o', 'd', '.', 'i', 'n', 't', ' '}
2421

2522
// Int is a generic implementation of finite field arithmetic
2623
// on integer finite fields with a given constant modulus,
27-
// built using Go's built-in big.Int or with the saferith package,
24+
// built using Go's built-in big.Int or with the filosottile/bigmod package,
2825
// depending on whether the constantTime build tag is chosen.
2926
// Int satisfies the kyber.Scalar interface,
3027
// and hence serves as a basic implementation of kyber.Scalar,
@@ -66,17 +63,18 @@ func (i *Int) SetString(n, d string, base int) (*Int, bool) {
6663
return i, true
6764
}
6865

69-
// Jacobi computes the Jacobi symbol of (a/M), which indicates whether a is
70-
// zero (0), a positive square in M (1), or a non-square in M (-1).
71-
func (i *Int) Jacobi(as kyber.Scalar) kyber.Scalar {
72-
ai := as.(*Int) //nolint:errcheck // Design pattern to emulate generics
73-
i.M = ai.M
74-
i.V.SetUint(uint(big.Jacobi(&ai.V, i.M)))
75-
return i
76-
}
66+
// Not used in constant time
67+
//// Jacobi computes the Jacobi symbol of (a/M), which indicates whether a is
68+
//// zero (0), a positive square in M (1), or a non-square in M (-1).
69+
//func (i *Int) Jacobi(as kyber.Scalar) kyber.Scalar {
70+
// ai := as.(*Int) //nolint:errcheck // Design pattern to emulate generics
71+
// i.M = ai.M
72+
// i.V.SetUint(uint(big.Jacobi(&ai.V, i.M)))
73+
// return i
74+
//}
7775

7876
// NewInt creaters a new Int with a given compatible.Int and a compatible.Int modulus.
79-
func NewInt(v *saferith.Nat, m *compatible_mod.Mod) *Int {
77+
func NewInt(v *compatible.Int, m *compatible_mod.Mod) *Int {
8078
return new(Int).Init(v, m)
8179
}
8280

@@ -182,13 +180,13 @@ func (i *Int) Clone() kyber.Scalar {
182180

183181
// Zero set the Int to the value 0. The modulus must already be initialized.
184182
func (i *Int) Zero() kyber.Scalar {
185-
i.V.SetUint64(0)
183+
i.V.SetUint(0)
186184
return i
187185
}
188186

189187
// One sets the Int to the value 1. The modulus must already be initialized.
190188
func (i *Int) One() kyber.Scalar {
191-
i.V.SetUint64(1)
189+
i.V.SetUint(1)
192190
return i
193191
}
194192

@@ -295,28 +293,25 @@ func (i *Int) Exp(a kyber.Scalar, e *compatible.Int) kyber.Scalar {
295293
return i
296294
}
297295

298-
// Sqrt computes some square root of a mod M of one exists.
299-
// Assumes the modulus M is an odd prime.
300-
// Returns true on success, false if input a is not a square.
301-
func (i *Int) Sqrt(as kyber.Scalar) bool {
302-
ai := as.(*Int) //nolint:errcheck // Design pattern to emulate generics
303-
out := i.V.ModSqrt(ai.V, ai.M)
304-
i.M = ai.M
305-
return out != nil
306-
}
296+
//not used in constant time
297+
//// Sqrt computes some square root of a mod M of one exists.
298+
//// Assumes the modulus M is an odd prime.
299+
//// Returns true on success, false if input a is not a square.
300+
//func (i *Int) Sqrt(as kyber.Scalar) bool {
301+
// ai := as.(*Int) //nolint:errcheck // Design pattern to emulate generics
302+
// out := i.V.ModSqrt(ai.V, ai.M)
303+
// i.M = ai.M
304+
// return out != nil
305+
//}
307306

308307
// Pick a [pseudo-]random integer, modulo M,
309308
// using bits from the given stream cipher.
310309
func (i *Int) Pick(rand cipher.Stream) kyber.Scalar {
311-
moduleInt := &saferith.Int{}
312-
moduleInt.SetNat(i.M.Nat())
313-
314-
compInt := &compatible.Int{
315-
moduleInt,
316-
}
310+
moduleInt := i.M.Nat()
311+
moduleCompatible := compatible.SetFromNat(moduleInt)
317312

318313
i.V.Set(
319-
random.Int(compInt, rand),
314+
random.Int(moduleCompatible, rand),
320315
)
321316
return i
322317
}
@@ -328,8 +323,8 @@ func (i *Int) ByteOrder() kyber.ByteOrder {
328323

329324
// GroupOrder returns the order of the underlying group
330325
// todo change to compatible_mod.Mod
331-
func (i *Int) GroupOrder() *big.Int {
332-
return big.NewInt(0).Set(i.M.Modulus)
326+
func (i *Int) GroupOrder() *compatible_mod.Mod {
327+
return i.M
333328
}
334329

335330
// MarshalSize returns the length in bytes of encoded integers with modulus M.

0 commit comments

Comments
 (0)