github.com/consensys/gnark-crypto@v0.14.0/ecc/ecc.go (about)

     1  /*
     2  Copyright © 2020 ConsenSys
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package ecc provides bls12-381, bls12-377, bls12-378, bn254, bw6-761, bls24-315, bls24-317, bw6-633, bls12-378, bw6-756, secp256k1 and stark-curve elliptic curves implementation (+pairing).
    18  //
    19  // Also
    20  //
    21  //   - Multi exponentiation
    22  //   - FFT
    23  //   - Polynomial commitment schemes
    24  //   - MiMC
    25  //   - twisted edwards "companion curves"
    26  //   - EdDSA (on the "companion" twisted edwards curves)
    27  package ecc
    28  
    29  import (
    30  	"errors"
    31  	"math/big"
    32  	"strings"
    33  
    34  	"github.com/consensys/gnark-crypto/internal/generator/config"
    35  )
    36  
    37  // ID represent a unique ID for a curve
    38  type ID uint16
    39  
    40  // do not modify the order of this enum
    41  const (
    42  	UNKNOWN ID = iota
    43  	BN254
    44  	BLS12_377
    45  	BLS12_378
    46  	BLS12_381
    47  	BLS24_315
    48  	BLS24_317
    49  	BW6_761
    50  	BW6_633
    51  	BW6_756
    52  	STARK_CURVE
    53  	SECP256K1
    54  )
    55  
    56  // Implemented return the list of curves fully implemented in gnark-crypto
    57  func Implemented() []ID {
    58  	return []ID{BN254, BLS12_377, BLS12_381, BW6_761, BLS24_315, BW6_633, BLS12_378, BW6_756, BLS24_317, STARK_CURVE, SECP256K1}
    59  }
    60  
    61  func IDFromString(s string) (ID, error) {
    62  	for _, id := range Implemented() {
    63  		if strings.ToLower(s) == id.String() {
    64  			return id, nil
    65  		}
    66  	}
    67  	return UNKNOWN, errors.New("unknown curve ID")
    68  }
    69  
    70  func (id ID) String() string {
    71  	cfg := id.config()
    72  	return strings.ToLower(cfg.EnumID)
    73  }
    74  
    75  // ScalarField returns the scalar field of the curve
    76  func (id ID) ScalarField() *big.Int {
    77  	cfg := id.config()
    78  	return modulus(cfg, true)
    79  }
    80  
    81  // BaseField returns the base field of the curve
    82  func (id ID) BaseField() *big.Int {
    83  	cfg := id.config()
    84  	return modulus(cfg, false)
    85  }
    86  
    87  func (id ID) config() *config.Curve {
    88  	// note to avoid circular dependency these are hard coded
    89  	// values are checked for non regression in code generation
    90  	switch id {
    91  	case BLS12_377:
    92  		return &config.BLS12_377
    93  	case BLS12_378:
    94  		return &config.BLS12_378
    95  	case BLS12_381:
    96  		return &config.BLS12_381
    97  	case BN254:
    98  		return &config.BN254
    99  	case BW6_761:
   100  		return &config.BW6_761
   101  	case BW6_633:
   102  		return &config.BW6_633
   103  	case BLS24_315:
   104  		return &config.BLS24_315
   105  	case BLS24_317:
   106  		return &config.BLS24_317
   107  	case BW6_756:
   108  		return &config.BW6_756
   109  	case STARK_CURVE:
   110  		return &config.STARK_CURVE
   111  	case SECP256K1:
   112  		return &config.SECP256K1
   113  	default:
   114  		panic("unimplemented ecc ID")
   115  	}
   116  }
   117  
   118  func modulus(c *config.Curve, scalarField bool) *big.Int {
   119  	if scalarField {
   120  		return new(big.Int).Set(c.FrInfo.Modulus())
   121  	}
   122  
   123  	return new(big.Int).Set(c.FpInfo.Modulus())
   124  }
   125  
   126  // MultiExpConfig enables to set optional configuration attribute to a call to MultiExp
   127  type MultiExpConfig struct {
   128  	NbTasks int // go routines to be used in the multiexp. can be larger than num cpus.
   129  }