git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/monero/crypto/hash.go (about)

     1  package crypto
     2  
     3  import (
     4  	"git.gammaspectra.live/P2Pool/consensus/v3/types"
     5  	"git.gammaspectra.live/P2Pool/edwards25519"
     6  	"git.gammaspectra.live/P2Pool/sha3"
     7  )
     8  
     9  func BytesToScalar(buf []byte) *edwards25519.Scalar {
    10  	_ = buf[31] // bounds check hint to compiler; see golang.org/issue/14808
    11  	var bytes [32]byte
    12  	copy(bytes[:], buf[:])
    13  	scReduce32(bytes[:])
    14  	c, _ := GetEdwards25519Scalar().SetCanonicalBytes(bytes[:])
    15  	return c
    16  }
    17  
    18  func Keccak256(data ...[]byte) (result types.Hash) {
    19  	h := sha3.NewLegacyKeccak256()
    20  	for _, b := range data {
    21  		h.Write(b)
    22  	}
    23  	HashFastSum(h, result[:])
    24  
    25  	return
    26  }
    27  
    28  func Keccak256Single(data []byte) (result types.Hash) {
    29  	h := sha3.NewLegacyKeccak256()
    30  	h.Write(data)
    31  	HashFastSum(h, result[:])
    32  
    33  	return
    34  }
    35  
    36  func HashToScalar(data ...[]byte) *edwards25519.Scalar {
    37  	h := PooledKeccak256(data...)
    38  	scReduce32(h[:])
    39  	c, _ := GetEdwards25519Scalar().SetCanonicalBytes(h[:])
    40  	return c
    41  }
    42  
    43  func HashToScalarNoAllocate(data ...[]byte) edwards25519.Scalar {
    44  	h := Keccak256(data...)
    45  	scReduce32(h[:])
    46  
    47  	var c edwards25519.Scalar
    48  	_, _ = c.SetCanonicalBytes(h[:])
    49  	return c
    50  }
    51  
    52  func HashToScalarNoAllocateSingle(data []byte) edwards25519.Scalar {
    53  	h := Keccak256Single(data)
    54  	scReduce32(h[:])
    55  
    56  	var c edwards25519.Scalar
    57  	_, _ = c.SetCanonicalBytes(h[:])
    58  	return c
    59  }
    60  
    61  // HashFastSum sha3.Sum clones the state by allocating memory. prevent that. b must be pre-allocated to the expected size, or larger
    62  func HashFastSum(hash *sha3.HasherState, b []byte) []byte {
    63  	_ = b[31] // bounds check hint to compiler; see golang.org/issue/14808
    64  	_, _ = hash.Read(b[:hash.Size()])
    65  	return b
    66  }
    67  
    68  /* TODO: wait for HashToPoint in edwards25519
    69  
    70  // HashToPoint Equivalent of Monero's HashToEC
    71  func HashToPointOld(publicKey PublicKey) *edwards25519.Point {
    72  
    73  	p := moneroutil.Key(publicKey.AsBytes())
    74  	var key moneroutil.Key
    75  
    76  	result := new(moneroutil.ExtendedGroupElement)
    77  	var p1 moneroutil.ProjectiveGroupElement
    78  	var p2 moneroutil.CompletedGroupElement
    79  	h := moneroutil.Key(Keccak256(p[:]))
    80  
    81  	log.Printf("old %s", hex.EncodeToString(h[:]))
    82  
    83  	p1.FromBytes(&h)
    84  
    85  	p1.ToBytes(&key)
    86  	log.Printf("old t %s", hex.EncodeToString(key[:]))
    87  
    88  	moneroutil.GeMul8(&p2, &p1)
    89  	p2.ToExtended(result)
    90  
    91  	result.ToBytes(&key)
    92  	log.Printf("old c %s", hex.EncodeToString(key[:]))
    93  	out, _ := GetEdwards25519Point().SetBytes(key[:])
    94  	return out
    95  }
    96  
    97  var cofactor = new(field.Element).Mult32(new(field.Element).One(), 8)
    98  
    99  // HashToPoint Equivalent of Monero's HashToEC
   100  func HashToPoint(publicKey PublicKey) *edwards25519.Point {
   101  	//TODO: make this work with existing edwards25519 library
   102  	h := Keccak256Single(publicKey.AsSlice())
   103  
   104  	log.Printf("new %s", hex.EncodeToString(h[:]))
   105  
   106  	e, err := new(field.Element).SetBytes(h[:])
   107  	if err != nil {
   108  		panic("hash to point failed")
   109  	}
   110  	log.Printf("new t %s", hex.EncodeToString(e.Bytes()))
   111  	e.Multiply(cofactor, e)
   112  
   113  	log.Printf("new c %s", hex.EncodeToString(e.Bytes()))
   114  	p, _ := GetEdwards25519Point().SetBytes(e.Bytes())
   115  	return p
   116  
   117  	var p1 edwards25519.Point
   118  	p1.MultByCofactor(&p1)
   119  	return p
   120  }
   121  
   122  */