2022-10-24 12:36:50 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2023-05-20 08:49:05 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/edwards25519"
|
2022-10-30 16:40:56 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/moneroutil"
|
2022-11-07 22:59:52 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
|
2023-05-12 06:55:01 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/sha3"
|
2022-10-24 12:36:50 +00:00
|
|
|
)
|
|
|
|
|
2022-10-30 16:40:56 +00:00
|
|
|
func BytesToScalar(buf []byte) *edwards25519.Scalar {
|
2022-11-08 13:25:29 +00:00
|
|
|
_ = buf[31] // bounds check hint to compiler; see golang.org/issue/14808
|
2022-11-07 08:52:35 +00:00
|
|
|
var bytes [32]byte
|
|
|
|
copy(bytes[:], buf[:])
|
|
|
|
scReduce32(bytes[:])
|
2022-11-07 22:59:52 +00:00
|
|
|
c, _ := GetEdwards25519Scalar().SetCanonicalBytes(bytes[:])
|
2022-10-24 12:36:50 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-11-07 22:59:52 +00:00
|
|
|
func Keccak256(data ...[]byte) (result types.Hash) {
|
|
|
|
h := sha3.NewLegacyKeccak256()
|
|
|
|
for _, b := range data {
|
|
|
|
h.Write(b)
|
|
|
|
}
|
|
|
|
HashFastSum(h, result[:])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 02:16:56 +00:00
|
|
|
func Keccak256Single(data []byte) (result types.Hash) {
|
|
|
|
h := sha3.NewLegacyKeccak256()
|
|
|
|
h.Write(data)
|
|
|
|
HashFastSum(h, result[:])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-30 16:40:56 +00:00
|
|
|
func HashToScalar(data ...[]byte) *edwards25519.Scalar {
|
2022-11-07 22:59:52 +00:00
|
|
|
h := PooledKeccak256(data...)
|
2022-11-07 08:52:35 +00:00
|
|
|
scReduce32(h[:])
|
2022-11-07 22:59:52 +00:00
|
|
|
c, _ := GetEdwards25519Scalar().SetCanonicalBytes(h[:])
|
2022-10-24 12:36:50 +00:00
|
|
|
return c
|
|
|
|
}
|
2022-11-01 11:22:00 +00:00
|
|
|
|
2023-05-10 02:16:56 +00:00
|
|
|
func HashToScalarNoAllocate(data ...[]byte) edwards25519.Scalar {
|
|
|
|
h := Keccak256(data...)
|
|
|
|
scReduce32(h[:])
|
|
|
|
|
|
|
|
var c edwards25519.Scalar
|
|
|
|
_, _ = c.SetCanonicalBytes(h[:])
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func HashToScalarNoAllocateSingle(data []byte) edwards25519.Scalar {
|
|
|
|
h := Keccak256Single(data)
|
|
|
|
scReduce32(h[:])
|
|
|
|
|
|
|
|
var c edwards25519.Scalar
|
|
|
|
_, _ = c.SetCanonicalBytes(h[:])
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2022-11-07 22:59:52 +00:00
|
|
|
// HashFastSum sha3.Sum clones the state by allocating memory. prevent that. b must be pre-allocated to the expected size, or larger
|
2023-05-12 06:55:01 +00:00
|
|
|
func HashFastSum(hash *sha3.HasherState, b []byte) []byte {
|
2022-11-08 13:25:29 +00:00
|
|
|
_ = b[31] // bounds check hint to compiler; see golang.org/issue/14808
|
2023-05-12 06:55:01 +00:00
|
|
|
_, _ = hash.Read(b[:hash.Size()])
|
|
|
|
return b
|
2022-11-07 22:59:52 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 06:57:36 +00:00
|
|
|
func HashToPoint(publicKey PublicKey) *edwards25519.Point {
|
|
|
|
//TODO: make this work with existing edwards25519 library
|
|
|
|
input := moneroutil.Key(publicKey.AsBytes())
|
|
|
|
var key moneroutil.Key
|
|
|
|
(&input).HashToEC().ToBytes(&key)
|
2022-11-07 22:59:52 +00:00
|
|
|
p, _ := GetEdwards25519Point().SetBytes(key[:])
|
2022-11-06 06:57:36 +00:00
|
|
|
return p
|
2022-11-01 11:22:00 +00:00
|
|
|
}
|