Skip to content

Commit 2b70394

Browse files
authored
feat: remove dependency on internal package in ecc.go (#693)
1 parent ca72a0f commit 2b70394

File tree

4 files changed

+193
-77
lines changed

4 files changed

+193
-77
lines changed

ecc/ecc.go

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@
1313
// - EdDSA (on the "companion" twisted edwards curves)
1414
package ecc
1515

16-
import (
17-
"errors"
18-
"math/big"
19-
"strings"
20-
21-
"github.com/consensys/gnark-crypto/internal/generator/config"
22-
)
23-
2416
// ID represent a unique ID for a curve
2517
type ID uint16
2618

@@ -39,75 +31,6 @@ const (
3931
GRUMPKIN
4032
)
4133

42-
// Implemented return the list of curves fully implemented in gnark-crypto
43-
func Implemented() []ID {
44-
return []ID{BN254, BLS12_377, BLS12_381, BW6_761, BLS24_315, BW6_633, BLS24_317, STARK_CURVE, SECP256K1, GRUMPKIN}
45-
}
46-
47-
func IDFromString(s string) (ID, error) {
48-
s = strings.ToLower(s)
49-
for _, id := range Implemented() {
50-
if s == id.String() {
51-
return id, nil
52-
}
53-
}
54-
return UNKNOWN, errors.New("unknown curve ID")
55-
}
56-
57-
func (id ID) String() string {
58-
cfg := id.config()
59-
return strings.ToLower(cfg.EnumID)
60-
}
61-
62-
// ScalarField returns the scalar field of the curve
63-
func (id ID) ScalarField() *big.Int {
64-
cfg := id.config()
65-
return modulus(cfg, true)
66-
}
67-
68-
// BaseField returns the base field of the curve
69-
func (id ID) BaseField() *big.Int {
70-
cfg := id.config()
71-
return modulus(cfg, false)
72-
}
73-
74-
func (id ID) config() *config.Curve {
75-
// note to avoid circular dependency these are hard coded
76-
// values are checked for non regression in code generation
77-
switch id {
78-
case BLS12_377:
79-
return &config.BLS12_377
80-
case BLS12_381:
81-
return &config.BLS12_381
82-
case BN254:
83-
return &config.BN254
84-
case BW6_761:
85-
return &config.BW6_761
86-
case BW6_633:
87-
return &config.BW6_633
88-
case BLS24_315:
89-
return &config.BLS24_315
90-
case BLS24_317:
91-
return &config.BLS24_317
92-
case STARK_CURVE:
93-
return &config.STARK_CURVE
94-
case SECP256K1:
95-
return &config.SECP256K1
96-
case GRUMPKIN:
97-
return &config.GRUMPKIN
98-
default:
99-
panic("unimplemented ecc ID")
100-
}
101-
}
102-
103-
func modulus(c *config.Curve, scalarField bool) *big.Int {
104-
if scalarField {
105-
return new(big.Int).Set(c.FrInfo.Modulus())
106-
}
107-
108-
return new(big.Int).Set(c.FpInfo.Modulus())
109-
}
110-
11134
// MultiExpConfig enables to set optional configuration attribute to a call to MultiExp
11235
type MultiExpConfig struct {
11336
NbTasks int // go routines to be used in the multiexp. can be larger than num cpus.

ecc/ecc_field.go

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import (
2+
"math/big"
3+
"errors"
4+
"strings"
5+
)
6+
7+
var mID = map[string]ID{
8+
{{- range $curve := .}}
9+
"{{toLower $curve.EnumID}}": {{toUpper $curve.EnumID}},
10+
{{- end}}
11+
}
12+
13+
// ScalarField returns the scalar field of the curve
14+
func (id ID) ScalarField() *big.Int {
15+
f := new(big.Int)
16+
switch id {
17+
{{- range $curve := .}}
18+
case {{toUpper $curve.EnumID}}:
19+
f.SetString("{{$curve.FrModulus}}", 10)
20+
{{- end}}
21+
default:
22+
panic("unimplemented ecc ID")
23+
}
24+
return f
25+
}
26+
27+
// BaseField returns the base field of the curve
28+
func (id ID) BaseField() *big.Int {
29+
f := new(big.Int)
30+
switch id {
31+
{{- range $curve := .}}
32+
case {{toUpper $curve.EnumID}}:
33+
f.SetString("{{$curve.FpModulus}}", 10)
34+
{{- end}}
35+
default:
36+
panic("unimplemented ecc ID")
37+
}
38+
return f
39+
}
40+
41+
// String returns the string representation of the ID
42+
func (id ID) String() string {
43+
switch id {
44+
{{- range $curve := .}}
45+
case {{toUpper $curve.EnumID}}:
46+
return "{{toLower $curve.EnumID}}"
47+
{{- end}}
48+
default:
49+
panic("unimplemented ecc ID")
50+
}
51+
}
52+
53+
// IDFromString returns the ID corresponding to the string representation of the curve ID.
54+
// It returns UNKNOWN if the string does not match any known curve ID.
55+
func IDFromString(s string) (ID, error) {
56+
s = strings.ToLower(s)
57+
if id, ok := mID[s]; ok {
58+
return id, nil
59+
}
60+
return UNKNOWN, errors.New("unknown curve ID")
61+
}

internal/generator/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ func main() {
187187

188188
wg.Wait()
189189

190+
// generate the ecc_field.go file
191+
{
192+
entries := []bavard.Entry{
193+
{File: filepath.Join(baseDir, "ecc", "ecc_field.go"), Templates: []string{"ecc_field.go.tmpl"}},
194+
}
195+
196+
assertNoError(bgen.Generate(config.Curves, "ecc", "./config/template", entries...))
197+
}
198+
190199
// format the whole directory
191200

192201
cmd := exec.Command("gofmt", "-s", "-w", baseDir)

0 commit comments

Comments
 (0)