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

     1  // Copyright 2020 ConsenSys Software Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package hash
    16  
    17  import (
    18  	"hash"
    19  
    20  	bls377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/mimc"
    21  	bls378 "github.com/consensys/gnark-crypto/ecc/bls12-378/fr/mimc"
    22  	bls381 "github.com/consensys/gnark-crypto/ecc/bls12-381/fr/mimc"
    23  	bls315 "github.com/consensys/gnark-crypto/ecc/bls24-315/fr/mimc"
    24  	bls317 "github.com/consensys/gnark-crypto/ecc/bls24-317/fr/mimc"
    25  	bn254 "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
    26  	bw633 "github.com/consensys/gnark-crypto/ecc/bw6-633/fr/mimc"
    27  	bw756 "github.com/consensys/gnark-crypto/ecc/bw6-756/fr/mimc"
    28  	bw761 "github.com/consensys/gnark-crypto/ecc/bw6-761/fr/mimc"
    29  )
    30  
    31  // Hash defines an unique identifier for a hash function.
    32  type Hash uint
    33  
    34  const (
    35  	// MIMC_BN254 is the MiMC hash function for the BN254 curve.
    36  	MIMC_BN254 Hash = iota
    37  	// MIMC_BLS12_381 is the MiMC hash function for the BLS12-381 curve.
    38  	MIMC_BLS12_381
    39  	// MIMC_BLS12_377 is the MiMC hash function for the BLS12-377 curve.
    40  	MIMC_BLS12_377
    41  	// MIMC_BLS12_378 is the MiMC hash function for the BLS12-378 curve.
    42  	MIMC_BLS12_378
    43  	// MIMC_BW6_761 is the MiMC hash function for the BW6-761 curve.
    44  	MIMC_BW6_761
    45  	// MIMC_BLS24_315 is the MiMC hash function for the BLS24-315 curve.
    46  	MIMC_BLS24_315
    47  	// MIMC_BLS24_317 is the MiMC hash function for the BLS24-317 curve.
    48  	MIMC_BLS24_317
    49  	// MIMC_BW6_633 is the MiMC hash function for the BW6-633 curve.
    50  	MIMC_BW6_633
    51  	// MIMC_BW6_756 is the MiMC hash function for the BW6-756 curve.
    52  	MIMC_BW6_756
    53  )
    54  
    55  // size of digests in bytes
    56  var digestSize = []uint8{
    57  	MIMC_BN254:     32,
    58  	MIMC_BLS12_381: 48,
    59  	MIMC_BLS12_377: 48,
    60  	MIMC_BLS12_378: 48,
    61  	MIMC_BW6_761:   96,
    62  	MIMC_BLS24_315: 48,
    63  	MIMC_BLS24_317: 48,
    64  	MIMC_BW6_633:   80,
    65  	MIMC_BW6_756:   96,
    66  }
    67  
    68  // New initializes the hash function.
    69  func (m Hash) New() hash.Hash {
    70  	switch m {
    71  	case MIMC_BN254:
    72  		return bn254.NewMiMC()
    73  	case MIMC_BLS12_381:
    74  		return bls381.NewMiMC()
    75  	case MIMC_BLS12_377:
    76  		return bls377.NewMiMC()
    77  	case MIMC_BLS12_378:
    78  		return bls378.NewMiMC()
    79  	case MIMC_BW6_761:
    80  		return bw761.NewMiMC()
    81  	case MIMC_BLS24_315:
    82  		return bls315.NewMiMC()
    83  	case MIMC_BLS24_317:
    84  		return bls317.NewMiMC()
    85  	case MIMC_BW6_633:
    86  		return bw633.NewMiMC()
    87  	case MIMC_BW6_756:
    88  		return bw756.NewMiMC()
    89  	default:
    90  		panic("Unknown mimc ID")
    91  	}
    92  }
    93  
    94  // String returns the unique identifier of the hash function.
    95  func (m Hash) String() string {
    96  	switch m {
    97  	case MIMC_BN254:
    98  		return "MIMC_BN254"
    99  	case MIMC_BLS12_381:
   100  		return "MIMC_BLS381"
   101  	case MIMC_BLS12_377:
   102  		return "MIMC_BLS377"
   103  	case MIMC_BLS12_378:
   104  		return "MIMC_BLS378"
   105  	case MIMC_BW6_761:
   106  		return "MIMC_BW761"
   107  	case MIMC_BLS24_315:
   108  		return "MIMC_BLS315"
   109  	case MIMC_BLS24_317:
   110  		return "MIMC_BLS317"
   111  	case MIMC_BW6_633:
   112  		return "MIMC_BW633"
   113  	case MIMC_BW6_756:
   114  		return "MIMC_BW756"
   115  	default:
   116  		panic("Unknown mimc ID")
   117  	}
   118  }
   119  
   120  // Size returns the size of the digest of
   121  // the corresponding hash function
   122  func (m Hash) Size() int {
   123  	return int(digestSize[m])
   124  }