2022-11-01 11:22:00 +00:00
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-05-20 08:49:05 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/edwards25519"
|
2023-05-11 07:51:00 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/p2pool-observer/types"
|
2023-05-12 06:55:01 +00:00
|
|
|
"git.gammaspectra.live/P2Pool/sha3"
|
2022-11-01 11:22:00 +00:00
|
|
|
)
|
|
|
|
|
2022-11-05 05:29:12 +00:00
|
|
|
func GetDerivationSharedDataForOutputIndex(derivation PublicKey, outputIndex uint64) PrivateKey {
|
2022-11-07 14:58:02 +00:00
|
|
|
var k = derivation.AsBytes()
|
|
|
|
var varIntBuf [binary.MaxVarintLen64]byte
|
|
|
|
return PrivateKeyFromScalar(HashToScalar(k[:], varIntBuf[:binary.PutUvarint(varIntBuf[:], outputIndex)]))
|
2022-11-01 11:22:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 07:51:00 +00:00
|
|
|
var viewTagDomain = []byte("view_tag")
|
|
|
|
|
2022-11-05 05:29:12 +00:00
|
|
|
func GetDerivationViewTagForOutputIndex(derivation PublicKey, outputIndex uint64) uint8 {
|
2022-11-07 14:58:02 +00:00
|
|
|
var k = derivation.AsBytes()
|
|
|
|
var varIntBuf [binary.MaxVarintLen64]byte
|
2023-05-11 07:51:00 +00:00
|
|
|
return PooledKeccak256(viewTagDomain, k[:], varIntBuf[:binary.PutUvarint(varIntBuf[:], outputIndex)])[0]
|
2022-11-01 11:22:00 +00:00
|
|
|
}
|
2022-11-06 06:57:36 +00:00
|
|
|
|
2022-11-07 14:58:02 +00:00
|
|
|
func GetDerivationSharedDataAndViewTagForOutputIndex(derivation PublicKey, outputIndex uint64) (PrivateKey, uint8) {
|
|
|
|
var k = derivation.AsBytes()
|
|
|
|
var varIntBuf [binary.MaxVarintLen64]byte
|
|
|
|
|
|
|
|
n := binary.PutUvarint(varIntBuf[:], outputIndex)
|
|
|
|
pK := PrivateKeyFromScalar(HashToScalar(k[:], varIntBuf[:n]))
|
2023-05-11 07:51:00 +00:00
|
|
|
return pK, PooledKeccak256(viewTagDomain, k[:], varIntBuf[:n])[0]
|
2022-11-07 14:58:02 +00:00
|
|
|
}
|
|
|
|
|
2023-05-11 07:51:00 +00:00
|
|
|
// GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate Special version of GetDerivationSharedDataAndViewTagForOutputIndex
|
2023-05-12 06:55:01 +00:00
|
|
|
func GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate(k PublicKeyBytes, outputIndex uint64, hasher *sha3.HasherState) (edwards25519.Scalar, uint8) {
|
2023-05-10 02:16:56 +00:00
|
|
|
var buf [PublicKeySize + binary.MaxVarintLen64]byte
|
|
|
|
copy(buf[:], k[:])
|
|
|
|
|
|
|
|
n := binary.PutUvarint(buf[PublicKeySize:], outputIndex)
|
2023-05-11 07:51:00 +00:00
|
|
|
var h types.Hash
|
|
|
|
hasher.Reset()
|
2023-05-17 15:29:01 +00:00
|
|
|
_, _ = hasher.Write(buf[:PublicKeySize+n])
|
2023-05-11 07:51:00 +00:00
|
|
|
HashFastSum(hasher, h[:])
|
|
|
|
scReduce32(h[:])
|
|
|
|
|
|
|
|
var c edwards25519.Scalar
|
|
|
|
_, _ = c.SetCanonicalBytes(h[:])
|
|
|
|
|
|
|
|
hasher.Reset()
|
2023-05-17 15:29:01 +00:00
|
|
|
_, _ = hasher.Write(viewTagDomain)
|
|
|
|
_, _ = hasher.Write(buf[:PublicKeySize+n])
|
2023-05-11 07:51:00 +00:00
|
|
|
HashFastSum(hasher, h[:])
|
|
|
|
|
|
|
|
return c, h[0]
|
2023-05-10 02:16:56 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 06:57:36 +00:00
|
|
|
func GetKeyImage(pair *KeyPair) PublicKey {
|
|
|
|
return PublicKeyFromPoint(HashToPoint(pair.PublicKey)).Multiply(pair.PrivateKey.AsScalar())
|
2022-11-07 22:59:52 +00:00
|
|
|
}
|