Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 0 additions & 77 deletions ecc/ecc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@
// - EdDSA (on the "companion" twisted edwards curves)
package ecc

import (
"errors"
"math/big"
"strings"

"github.com/consensys/gnark-crypto/internal/generator/config"
)

// ID represent a unique ID for a curve
type ID uint16

Expand All @@ -39,75 +31,6 @@ const (
GRUMPKIN
)

// Implemented return the list of curves fully implemented in gnark-crypto
func Implemented() []ID {
return []ID{BN254, BLS12_377, BLS12_381, BW6_761, BLS24_315, BW6_633, BLS24_317, STARK_CURVE, SECP256K1, GRUMPKIN}
}

func IDFromString(s string) (ID, error) {
s = strings.ToLower(s)
for _, id := range Implemented() {
if s == id.String() {
return id, nil
}
}
return UNKNOWN, errors.New("unknown curve ID")
}

func (id ID) String() string {
cfg := id.config()
return strings.ToLower(cfg.EnumID)
}

// ScalarField returns the scalar field of the curve
func (id ID) ScalarField() *big.Int {
cfg := id.config()
return modulus(cfg, true)
}

// BaseField returns the base field of the curve
func (id ID) BaseField() *big.Int {
cfg := id.config()
return modulus(cfg, false)
}

func (id ID) config() *config.Curve {
// note to avoid circular dependency these are hard coded
// values are checked for non regression in code generation
switch id {
case BLS12_377:
return &config.BLS12_377
case BLS12_381:
return &config.BLS12_381
case BN254:
return &config.BN254
case BW6_761:
return &config.BW6_761
case BW6_633:
return &config.BW6_633
case BLS24_315:
return &config.BLS24_315
case BLS24_317:
return &config.BLS24_317
case STARK_CURVE:
return &config.STARK_CURVE
case SECP256K1:
return &config.SECP256K1
case GRUMPKIN:
return &config.GRUMPKIN
default:
panic("unimplemented ecc ID")
}
}

func modulus(c *config.Curve, scalarField bool) *big.Int {
if scalarField {
return new(big.Int).Set(c.FrInfo.Modulus())
}

return new(big.Int).Set(c.FpInfo.Modulus())
}

// MultiExpConfig enables to set optional configuration attribute to a call to MultiExp
type MultiExpConfig struct {
NbTasks int // go routines to be used in the multiexp. can be larger than num cpus.
Expand Down
123 changes: 123 additions & 0 deletions ecc/ecc_field.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions internal/generator/config/template/ecc_field.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import (
"math/big"
"errors"
"strings"
)

var mID = map[string]ID{
{{- range $curve := .}}
"{{toLower $curve.EnumID}}": {{toUpper $curve.EnumID}},
{{- end}}
}

// ScalarField returns the scalar field of the curve
func (id ID) ScalarField() *big.Int {
f := new(big.Int)
switch id {
{{- range $curve := .}}
case {{toUpper $curve.EnumID}}:
f.SetString("{{$curve.FrModulus}}", 10)
{{- end}}
default:
panic("unimplemented ecc ID")
}
return f
}

// BaseField returns the base field of the curve
func (id ID) BaseField() *big.Int {
f := new(big.Int)
switch id {
{{- range $curve := .}}
case {{toUpper $curve.EnumID}}:
f.SetString("{{$curve.FpModulus}}", 10)
{{- end}}
default:
panic("unimplemented ecc ID")
}
return f
}

// String returns the string representation of the ID
func (id ID) String() string {
switch id {
{{- range $curve := .}}
case {{toUpper $curve.EnumID}}:
return "{{toLower $curve.EnumID}}"
{{- end}}
default:
panic("unimplemented ecc ID")
}
}

// IDFromString returns the ID corresponding to the string representation of the curve ID.
// It returns UNKNOWN if the string does not match any known curve ID.
func IDFromString(s string) (ID, error) {
s = strings.ToLower(s)
if id, ok := mID[s]; ok {
return id, nil
}
return UNKNOWN, errors.New("unknown curve ID")
}
9 changes: 9 additions & 0 deletions internal/generator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ func main() {

wg.Wait()

// generate the ecc_field.go file
{
entries := []bavard.Entry{
{File: filepath.Join(baseDir, "ecc", "ecc_field.go"), Templates: []string{"ecc_field.go.tmpl"}},
}

assertNoError(bgen.Generate(config.Curves, "ecc", "./config/template", entries...))
}

// format the whole directory

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