diff --git a/ecc/ecc.go b/ecc/ecc.go index b45637946..fb4dc0918 100644 --- a/ecc/ecc.go +++ b/ecc/ecc.go @@ -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 @@ -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. diff --git a/ecc/ecc_field.go b/ecc/ecc_field.go new file mode 100644 index 000000000..5f9179e31 --- /dev/null +++ b/ecc/ecc_field.go @@ -0,0 +1,123 @@ +// Copyright 2020-2025 Consensys Software Inc. +// Licensed under the Apache License, Version 2.0. See the LICENSE file for details. + +// Code generated by consensys/gnark-crypto DO NOT EDIT + +package ecc + +import ( + "errors" + "math/big" + "strings" +) + +var mID = map[string]ID{ + "bls12_377": BLS12_377, + "bls12_381": BLS12_381, + "bls24_315": BLS24_315, + "bls24_317": BLS24_317, + "bn254": BN254, + "bw6_633": BW6_633, + "bw6_761": BW6_761, + "grumpkin": GRUMPKIN, + "secp256k1": SECP256K1, + "stark_curve": STARK_CURVE, +} + +// ScalarField returns the scalar field of the curve +func (id ID) ScalarField() *big.Int { + f := new(big.Int) + switch id { + case BLS12_377: + f.SetString("8444461749428370424248824938781546531375899335154063827935233455917409239041", 10) + case BLS12_381: + f.SetString("52435875175126190479447740508185965837690552500527637822603658699938581184513", 10) + case BLS24_315: + f.SetString("11502027791375260645628074404575422495959608200132055716665986169834464870401", 10) + case BLS24_317: + f.SetString("30869589236456844204538189757527902584594726589286811523515204428962673459201", 10) + case BN254: + f.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10) + case BW6_633: + f.SetString("39705142709513438335025689890408969744933502416914749335064285505637884093126342347073617133569", 10) + case BW6_761: + f.SetString("258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177", 10) + case GRUMPKIN: + f.SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) + case SECP256K1: + f.SetString("115792089237316195423570985008687907852837564279074904382605163141518161494337", 10) + case STARK_CURVE: + f.SetString("3618502788666131213697322783095070105526743751716087489154079457884512865583", 10) + 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 { + case BLS12_377: + f.SetString("258664426012969094010652733694893533536393512754914660539884262666720468348340822774968888139573360124440321458177", 10) + case BLS12_381: + f.SetString("4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787", 10) + case BLS24_315: + f.SetString("39705142709513438335025689890408969744933502416914749335064285505637884093126342347073617133569", 10) + case BLS24_317: + f.SetString("136393071104295911515099765908274057061945112121419593977210139303905973197232025618026156731051", 10) + case BN254: + f.SetString("21888242871839275222246405745257275088696311157297823662689037894645226208583", 10) + case BW6_633: + f.SetString("20494478644167774678813387386538961497669590920908778075528754551012016751717791778743535050360001387419576570244406805463255765034468441182772056330021723098661967429339971741066259394985997", 10) + case BW6_761: + f.SetString("6891450384315732539396789682275657542479668912536150109513790160209623422243491736087683183289411687640864567753786613451161759120554247759349511699125301598951605099378508850372543631423596795951899700429969112842764913119068299", 10) + case GRUMPKIN: + f.SetString("21888242871839275222246405745257275088548364400416034343698204186575808495617", 10) + case SECP256K1: + f.SetString("115792089237316195423570985008687907853269984665640564039457584007908834671663", 10) + case STARK_CURVE: + f.SetString("3618502788666131213697322783095070105623107215331596699973092056135872020481", 10) + default: + panic("unimplemented ecc ID") + } + return f +} + +// String returns the string representation of the ID +func (id ID) String() string { + switch id { + case BLS12_377: + return "bls12_377" + case BLS12_381: + return "bls12_381" + case BLS24_315: + return "bls24_315" + case BLS24_317: + return "bls24_317" + case BN254: + return "bn254" + case BW6_633: + return "bw6_633" + case BW6_761: + return "bw6_761" + case GRUMPKIN: + return "grumpkin" + case SECP256K1: + return "secp256k1" + case STARK_CURVE: + return "stark_curve" + 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") +} diff --git a/internal/generator/config/template/ecc_field.go.tmpl b/internal/generator/config/template/ecc_field.go.tmpl new file mode 100644 index 000000000..33a50e71c --- /dev/null +++ b/internal/generator/config/template/ecc_field.go.tmpl @@ -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") +} diff --git a/internal/generator/main.go b/internal/generator/main.go index 1ff9db822..973fef1bb 100644 --- a/internal/generator/main.go +++ b/internal/generator/main.go @@ -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)