github.com/Evanesco-Labs/go-evanesco@v1.0.1/zkpminer/keypair/utils.go (about)

     1  package keypair
     2  
     3  import (
     4  	"crypto/sha512"
     5  	"encoding/binary"
     6  	"math/big"
     7  )
     8  
     9  var one = big.NewInt(1)
    10  
    11  func HashToCurve(m []byte) (x, y *big.Int) {
    12  	h := sha512.New()
    13  	var i uint32
    14  
    15  	byteLen := (Params.BitSize + 7) >> 3
    16  	for x == nil && i < 100 {
    17  		h.Reset()
    18  		binary.Write(h, binary.BigEndian, i)
    19  		h.Write(m)
    20  		r := []byte{2} // Set point encoding to "compressed", y=0.
    21  		r = h.Sum(r)
    22  		x, y = Unmarshal(Curve, r[:byteLen+1])
    23  		i++
    24  	}
    25  	return
    26  }
    27  
    28  // HashToField hashes to an integer [1,N-1]
    29  func HashToField(m []byte) *big.Int {
    30  	// NIST SP 800-90A ยง A.5.1: Simple discard method.
    31  	byteLen := (Params.BitSize + 7) >> 3
    32  	h := sha512.New()
    33  	for i := uint32(0); ; i++ {
    34  		// TODO: Use a NIST specified DRBG.
    35  		h.Reset()
    36  		binary.Write(h, binary.BigEndian, i)
    37  		h.Write(m)
    38  		b := h.Sum(nil)
    39  		k := new(big.Int).SetBytes(b[:byteLen])
    40  		if k.Cmp(new(big.Int).Sub(Params.N, one)) == -1 {
    41  			return k.Add(k, one)
    42  		}
    43  	}
    44  }