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

     1  package crypto
     2  
     3  import (
     4  	"encoding/binary"
     5  	"git.gammaspectra.live/P2Pool/consensus/v3/types"
     6  	"git.gammaspectra.live/P2Pool/edwards25519"
     7  	"git.gammaspectra.live/P2Pool/sha3"
     8  )
     9  
    10  func GetDerivationSharedDataForOutputIndex(derivation PublicKey, outputIndex uint64) PrivateKey {
    11  	var k = derivation.AsBytes()
    12  	var varIntBuf [binary.MaxVarintLen64]byte
    13  	return PrivateKeyFromScalar(HashToScalar(k[:], varIntBuf[:binary.PutUvarint(varIntBuf[:], outputIndex)]))
    14  }
    15  
    16  var viewTagDomain = []byte("view_tag")
    17  
    18  func GetDerivationViewTagForOutputIndex(derivation PublicKey, outputIndex uint64) uint8 {
    19  	var k = derivation.AsBytes()
    20  	var varIntBuf [binary.MaxVarintLen64]byte
    21  	return PooledKeccak256(viewTagDomain, k[:], varIntBuf[:binary.PutUvarint(varIntBuf[:], outputIndex)])[0]
    22  }
    23  
    24  func GetDerivationSharedDataAndViewTagForOutputIndex(derivation PublicKey, outputIndex uint64) (PrivateKey, uint8) {
    25  	var k = derivation.AsBytes()
    26  	var varIntBuf [binary.MaxVarintLen64]byte
    27  
    28  	n := binary.PutUvarint(varIntBuf[:], outputIndex)
    29  	pK := PrivateKeyFromScalar(HashToScalar(k[:], varIntBuf[:n]))
    30  	return pK, PooledKeccak256(viewTagDomain, k[:], varIntBuf[:n])[0]
    31  }
    32  
    33  // GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate Special version of GetDerivationSharedDataAndViewTagForOutputIndex
    34  func GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate(k PublicKeyBytes, outputIndex uint64, hasher *sha3.HasherState) (edwards25519.Scalar, uint8) {
    35  	var buf [PublicKeySize + binary.MaxVarintLen64]byte
    36  	copy(buf[:], k[:])
    37  
    38  	n := binary.PutUvarint(buf[PublicKeySize:], outputIndex)
    39  	var h types.Hash
    40  	hasher.Reset()
    41  	_, _ = hasher.Write(buf[:PublicKeySize+n])
    42  	HashFastSum(hasher, h[:])
    43  	scReduce32(h[:])
    44  
    45  	var c edwards25519.Scalar
    46  	_, _ = c.SetCanonicalBytes(h[:])
    47  
    48  	hasher.Reset()
    49  	_, _ = hasher.Write(viewTagDomain)
    50  	_, _ = hasher.Write(buf[:PublicKeySize+n])
    51  	HashFastSum(hasher, h[:])
    52  
    53  	return c, h[0]
    54  }
    55  
    56  /* TODO: wait for HashToPoint in edwards25519
    57  func GetKeyImage(pair *KeyPair) PublicKey {
    58  	return PublicKeyFromPoint(HashToPoint(pair.PublicKey)).Multiply(pair.PrivateKey.AsScalar())
    59  }
    60  */