Skip to content

Commit ecf8ad1

Browse files
committed
Move expander out of group module.
1 parent aff140f commit ecf8ad1

12 files changed

+37
-25
lines changed

ecc/bls12381/g1.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package bls12381
22

33
import (
44
"crypto"
5+
_ "crypto/sha256" // to link library
56
"crypto/subtle"
67
"fmt"
78
"math/big"
89

910
"github.com/cloudflare/circl/ecc/bls12381/ff"
10-
"github.com/cloudflare/circl/group"
11+
"github.com/cloudflare/circl/expander"
1112
)
1213

1314
// G1Size is the length in bytes of an element in G1 in uncompressed form..
@@ -336,7 +337,7 @@ func (g *G1) toAffine() {
336337
// be used as a hash function, otherwise use G1.Hash instead.
337338
func (g *G1) Encode(input, dst []byte) {
338339
const L = 64
339-
pseudo := group.NewExpanderMD(crypto.SHA256, dst).Expand(input, L)
340+
pseudo := expander.NewExpanderMD(crypto.SHA256, dst).Expand(input, L)
340341

341342
bu := new(big.Int).SetBytes(pseudo)
342343
bu.Mod(bu, new(big.Int).SetBytes(ff.FpOrder()))
@@ -356,7 +357,7 @@ func (g *G1) Encode(input, dst []byte) {
356357
// random oracle returning points in G1 be required.
357358
func (g *G1) Hash(input, dst []byte) {
358359
const L = 64
359-
pseudo := group.NewExpanderMD(crypto.SHA256, dst).Expand(input, 2*L)
360+
pseudo := expander.NewExpanderMD(crypto.SHA256, dst).Expand(input, 2*L)
360361

361362
var u0, u1 ff.Fp
362363
fpOrder := new(big.Int).SetBytes(ff.FpOrder())

group/expander.go renamed to expander/expander.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package group
1+
// Package expander generates arbitrary bytes from an XOF or Hash function.
2+
package expander
23

34
import (
45
"crypto"

group/expander_test.go renamed to expander/expander_test.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package group_test
1+
package expander_test
22

33
import (
44
"bytes"
55
"crypto"
6+
_ "crypto/sha256" // to link libraries
7+
_ "crypto/sha512" // to link libraries
68
"encoding/hex"
79
"encoding/json"
810
"fmt"
@@ -11,13 +13,13 @@ import (
1113
"strconv"
1214
"testing"
1315

14-
"github.com/cloudflare/circl/group"
16+
"github.com/cloudflare/circl/expander"
1517
"github.com/cloudflare/circl/internal/test"
1618
"github.com/cloudflare/circl/xof"
1719
)
1820

1921
func TestExpander(t *testing.T) {
20-
fileNames, err := filepath.Glob("./testdata/expand*.json")
22+
fileNames, err := filepath.Glob("./testdata/*.json")
2123
if err != nil {
2224
t.Fatal(err)
2325
}
@@ -40,16 +42,16 @@ func TestExpander(t *testing.T) {
4042
}
4143

4244
func testExpander(t *testing.T, vs *vectorExpanderSuite) {
43-
var exp group.Expander
45+
var exp expander.Expander
4446
switch vs.Hash {
4547
case "SHA256":
46-
exp = group.NewExpanderMD(crypto.SHA256, []byte(vs.DST))
48+
exp = expander.NewExpanderMD(crypto.SHA256, []byte(vs.DST))
4749
case "SHA512":
48-
exp = group.NewExpanderMD(crypto.SHA512, []byte(vs.DST))
50+
exp = expander.NewExpanderMD(crypto.SHA512, []byte(vs.DST))
4951
case "SHAKE128":
50-
exp = group.NewExpanderXOF(xof.SHAKE128, vs.K, []byte(vs.DST))
52+
exp = expander.NewExpanderXOF(xof.SHAKE128, vs.K, []byte(vs.DST))
5153
case "SHAKE256":
52-
exp = group.NewExpanderXOF(xof.SHAKE256, vs.K, []byte(vs.DST))
54+
exp = expander.NewExpanderXOF(xof.SHAKE256, vs.K, []byte(vs.DST))
5355
default:
5456
t.Skip("hash not supported: " + vs.Hash)
5557
}
@@ -92,10 +94,10 @@ func BenchmarkExpander(b *testing.B) {
9294

9395
for _, v := range []struct {
9496
Name string
95-
Exp group.Expander
97+
Exp expander.Expander
9698
}{
97-
{"XMD", group.NewExpanderMD(crypto.SHA256, dst)},
98-
{"XOF", group.NewExpanderXOF(xof.SHAKE128, 0, dst)},
99+
{"XMD", expander.NewExpanderMD(crypto.SHA256, dst)},
100+
{"XOF", expander.NewExpanderXOF(xof.SHAKE128, 0, dst)},
99101
} {
100102
exp := v.Exp
101103
for l := 8; l <= 10; l++ {

group/hash.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package group
22

3-
import "math/big"
3+
import (
4+
"math/big"
5+
6+
"github.com/cloudflare/circl/expander"
7+
)
48

59
// HashToField generates a set of elements {u1,..., uN} = Hash(b) where each
610
// u in GF(p) and L is the security parameter.
7-
func HashToField(u []big.Int, b []byte, e Expander, p *big.Int, L uint) {
11+
func HashToField(u []big.Int, b []byte, e expander.Expander, p *big.Int, L uint) {
812
count := uint(len(u))
913
bytes := e.Expand(b, count*L)
1014
for i := range u {

group/ristretto255.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77

88
r255 "github.com/bwesterb/go-ristretto"
9+
"github.com/cloudflare/circl/expander"
910
)
1011

1112
var (
@@ -86,7 +87,7 @@ func (g ristrettoGroup) HashToElementNonUniform(b, dst []byte) Element {
8687
return g.HashToElement(b, dst)
8788
}
8889
func (g ristrettoGroup) HashToElement(msg, dst []byte) Element {
89-
xmd := NewExpanderMD(crypto.SHA512, dst)
90+
xmd := expander.NewExpanderMD(crypto.SHA512, dst)
9091
data := xmd.Expand(msg, 64)
9192
e := g.NewElement()
9293
e.(*ristrettoElement).p.Derive(data)

group/short.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package group
33
import (
44
"crypto"
55
"crypto/elliptic"
6-
_ "crypto/sha256" // to link libraries
7-
_ "crypto/sha512" // to link libraries
86
"crypto/subtle"
97
"fmt"
108
"io"
119
"math/big"
1210

1311
"github.com/cloudflare/circl/ecc/p384"
12+
"github.com/cloudflare/circl/expander"
1413
)
1514

1615
var (
@@ -36,12 +35,16 @@ func (g wG) Generator() Element { return &wElt{g, g.c.Params().Gx, g.c.Params()
3635
func (g wG) Order() Scalar { s := &wScl{g, nil}; s.fromBig(g.c.Params().N); return s }
3736
func (g wG) RandomElement(rd io.Reader) Element {
3837
b := make([]byte, (g.c.Params().BitSize+7)/8)
39-
mustReadFull(rd, b)
38+
if n, err := io.ReadFull(rd, b); err != nil || n != len(b) {
39+
panic(err)
40+
}
4041
return g.HashToElement(b, nil)
4142
}
4243
func (g wG) RandomScalar(rd io.Reader) Scalar {
4344
b := make([]byte, (g.c.Params().BitSize+7)/8)
44-
mustReadFull(rd, b)
45+
if n, err := io.ReadFull(rd, b); err != nil || n != len(b) {
46+
panic(err)
47+
}
4548
return g.HashToScalar(b, nil)
4649
}
4750
func (g wG) cvtElt(e Element) *wElt {
@@ -75,14 +78,14 @@ func (g wG) Params() *Params {
7578
func (g wG) HashToElementNonUniform(b, dst []byte) Element {
7679
var u [1]big.Int
7780
mapping, h, L := g.mapToCurveParams()
78-
xmd := NewExpanderMD(h, dst)
81+
xmd := expander.NewExpanderMD(h, dst)
7982
HashToField(u[:], b, xmd, g.c.Params().P, L)
8083
return mapping(&u[0])
8184
}
8285
func (g wG) HashToElement(b, dst []byte) Element {
8386
var u [2]big.Int
8487
mapping, h, L := g.mapToCurveParams()
85-
xmd := NewExpanderMD(h, dst)
88+
xmd := expander.NewExpanderMD(h, dst)
8689
HashToField(u[:], b, xmd, g.c.Params().P, L)
8790
Q0 := mapping(&u[0])
8891
Q1 := mapping(&u[1])
@@ -91,7 +94,7 @@ func (g wG) HashToElement(b, dst []byte) Element {
9194
func (g wG) HashToScalar(b, dst []byte) Scalar {
9295
var u [1]big.Int
9396
_, h, L := g.mapToCurveParams()
94-
xmd := NewExpanderMD(h, dst)
97+
xmd := expander.NewExpanderMD(h, dst)
9598
HashToField(u[:], b, xmd, g.c.Params().N, L)
9699
s := g.NewScalar().(*wScl)
97100
s.fromBig(&u[0])

0 commit comments

Comments
 (0)