Added key image derivation

This commit is contained in:
DataHoarder 2022-11-06 07:57:36 +01:00
parent 39e34abed6
commit 75fea6aad3
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
3 changed files with 30 additions and 4 deletions

View file

@ -13,3 +13,7 @@ func GetDerivationViewTagForOutputIndex(derivation PublicKey, outputIndex uint64
h := moneroutil.Keccak256([]byte("view_tag"), derivation.AsSlice(), binary.AppendUvarint(nil, outputIndex))
return h[0]
}
func GetKeyImage(pair *KeyPair) PublicKey {
return PublicKeyFromPoint(HashToPoint(pair.PublicKey)).Multiply(pair.PrivateKey.AsScalar())
}

View file

@ -0,0 +1,18 @@
package crypto
import (
"encoding/hex"
"filippo.io/edwards25519"
"testing"
)
func TestKeyImageRaw(t *testing.T) {
sec, _ := hex.DecodeString("981d477fb18897fa1f784c89721a9d600bf283f06b89cb018a077f41dcefef0f")
scalar, _ := (&edwards25519.Scalar{}).SetCanonicalBytes(sec)
keyImage := GetKeyImage(NewKeyPairFromPrivate(PrivateKeyFromScalar(scalar)))
if keyImage.String() != "a637203ec41eab772532d30420eac80612fce8e44f1758bc7e2cb1bdda815887" {
t.Fatalf("key image expected %s, got %s", "a637203ec41eab772532d30420eac80612fce8e44f1758bc7e2cb1bdda815887", keyImage.String())
}
}

View file

@ -20,8 +20,12 @@ func HashToScalar(data ...[]byte) *edwards25519.Scalar {
return c
}
func HashToPoint(data ...[]byte) *edwards25519.Point {
h := moneroutil.Keccak256(data...)
p, _ := (&edwards25519.Point{}).SetBytes(h[:])
return p.ScalarMult(scalar8, p)
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)
p, _ := (&edwards25519.Point{}).SetBytes(key[:])
return p
}