git.gammaspectra.live/P2Pool/consensus@v0.0.0-20240403173234-a039820b20c9/monero/crypto/hash.go (about) 1 package crypto 2 3 import ( 4 "git.gammaspectra.live/P2Pool/consensus/types" 5 "git.gammaspectra.live/P2Pool/edwards25519" 6 "git.gammaspectra.live/P2Pool/moneroutil" 7 "git.gammaspectra.live/P2Pool/sha3" 8 ) 9 10 func BytesToScalar(buf []byte) *edwards25519.Scalar { 11 _ = buf[31] // bounds check hint to compiler; see golang.org/issue/14808 12 var bytes [32]byte 13 copy(bytes[:], buf[:]) 14 scReduce32(bytes[:]) 15 c, _ := GetEdwards25519Scalar().SetCanonicalBytes(bytes[:]) 16 return c 17 } 18 19 func Keccak256(data ...[]byte) (result types.Hash) { 20 h := sha3.NewLegacyKeccak256() 21 for _, b := range data { 22 h.Write(b) 23 } 24 HashFastSum(h, result[:]) 25 26 return 27 } 28 29 func Keccak256Single(data []byte) (result types.Hash) { 30 h := sha3.NewLegacyKeccak256() 31 h.Write(data) 32 HashFastSum(h, result[:]) 33 34 return 35 } 36 37 func HashToScalar(data ...[]byte) *edwards25519.Scalar { 38 h := PooledKeccak256(data...) 39 scReduce32(h[:]) 40 c, _ := GetEdwards25519Scalar().SetCanonicalBytes(h[:]) 41 return c 42 } 43 44 func HashToScalarNoAllocate(data ...[]byte) edwards25519.Scalar { 45 h := Keccak256(data...) 46 scReduce32(h[:]) 47 48 var c edwards25519.Scalar 49 _, _ = c.SetCanonicalBytes(h[:]) 50 return c 51 } 52 53 func HashToScalarNoAllocateSingle(data []byte) edwards25519.Scalar { 54 h := Keccak256Single(data) 55 scReduce32(h[:]) 56 57 var c edwards25519.Scalar 58 _, _ = c.SetCanonicalBytes(h[:]) 59 return c 60 } 61 62 // HashFastSum sha3.Sum clones the state by allocating memory. prevent that. b must be pre-allocated to the expected size, or larger 63 func HashFastSum(hash *sha3.HasherState, b []byte) []byte { 64 _ = b[31] // bounds check hint to compiler; see golang.org/issue/14808 65 _, _ = hash.Read(b[:hash.Size()]) 66 return b 67 } 68 69 func HashToPoint(publicKey PublicKey) *edwards25519.Point { 70 //TODO: make this work with existing edwards25519 library 71 input := moneroutil.Key(publicKey.AsBytes()) 72 var key moneroutil.Key 73 (&input).HashToEC().ToBytes(&key) 74 p, _ := GetEdwards25519Point().SetBytes(key[:]) 75 return p 76 }