git.gammaspectra.live/P2Pool/consensus@v0.0.0-20240403173234-a039820b20c9/monero/address/crypto_test.go (about) 1 package address 2 3 import ( 4 "git.gammaspectra.live/P2Pool/consensus/monero/crypto" 5 "git.gammaspectra.live/P2Pool/consensus/types" 6 "os" 7 "path" 8 "runtime" 9 "strconv" 10 "strings" 11 "testing" 12 ) 13 14 func init() { 15 _, filename, _, _ := runtime.Caller(0) 16 // The ".." may change depending on you folder structure 17 dir := path.Join(path.Dir(filename), "../..") 18 err := os.Chdir(dir) 19 if err != nil { 20 panic(err) 21 } 22 23 } 24 25 func GetTestEntries(name string, n int) chan []string { 26 buf, err := os.ReadFile("testdata/crypto_tests.txt") 27 if err != nil { 28 return nil 29 } 30 result := make(chan []string) 31 go func() { 32 defer close(result) 33 for _, line := range strings.Split(string(buf), "\n") { 34 entries := strings.Split(strings.TrimSpace(line), " ") 35 if entries[0] == name && len(entries) >= (n+1) { 36 result <- entries[1:] 37 } 38 } 39 }() 40 return result 41 } 42 43 func TestDerivePublicKey(t *testing.T) { 44 results := GetTestEntries("derive_public_key", 4) 45 if results == nil { 46 t.Fatal() 47 } 48 for e := range results { 49 var expectedDerivedKey types.Hash 50 51 derivation := crypto.PublicKeyBytes(types.MustHashFromString(e[0])) 52 outputIndex, _ := strconv.ParseUint(e[1], 10, 0) 53 54 base := crypto.PublicKeyBytes(types.MustHashFromString(e[2])) 55 56 result := e[3] == "true" 57 if result { 58 expectedDerivedKey = types.MustHashFromString(e[4]) 59 } 60 61 point2 := base.AsPoint() 62 63 if result == false && point2 == nil { 64 //expected failure 65 continue 66 } else if point2 == nil { 67 t.Fatalf("invalid point %s / %s", derivation.String(), base.String()) 68 } 69 70 sharedData := crypto.GetDerivationSharedDataForOutputIndex(&derivation, outputIndex) 71 72 var addr PackedAddress 73 addr[0] = base 74 derivedKey := GetPublicKeyForSharedData(&addr, sharedData) 75 76 if result { 77 if expectedDerivedKey.String() != derivedKey.String() { 78 t.Fatalf("expected %s, got %s", expectedDerivedKey.String(), derivedKey.String()) 79 } 80 } 81 } 82 } 83 84 var testThrowawayAddress = FromBase58("42ecNLuoGtn1qC9SPSD9FPMNfsv35RE66Eu8WJJtyEHKfEsEiALVVp7GBCeAYFb7PHcSZmz9sDUtRMnKk2as1KfuLuTQJ3i") 85 var signatureMessage = []byte("test_message") 86 var signatureMessage2 = []byte("test_message2") 87 88 const ( 89 SignatureSpendSuccess = "SigV2DnQYF11xZ1ahLJxddohCroiEJRnUe1tgwD5ksmFMzQ9NcRdbxLPrEdQW3e8w4sLpqhSup5tU9igQqeAR8j7r7Sty" 90 SignatureSpendWrongMessage = "SigV26RKRd31efizGHrWHwtYG6EN2MmwvF1rjU4ygZQuDmSxvCJnky1GJTzaM49naQeKvXbaGcnpZ1b3k8gVQLaFMFiBJ" 91 SignatureViewSuccess = "SigV2b7LaAuXrFvPAXwU11SJwHbcXJoKfQ5aBJ9FwMJNxvMTu78AebqNUCWPH1BVfNRvy1f3GCTLjHfWvuRJMZtSHu5uj" 92 SignatureViewWrongMessage = "SigV2AxWUATswZvnHSR5mMRsn9GcJe2gSCv3SbFwHv6J8THkj5KvmR8gUnTidHovZVyxgNHcUuiunM2dfVhbZvBTS6sZZ" 93 ) 94 95 func TestSignature(t *testing.T) { 96 result := VerifyMessage(testThrowawayAddress, signatureMessage, SignatureSpendSuccess) 97 if result != ResultSuccessSpend { 98 t.Fatalf("unexpected %d", result) 99 } 100 result = VerifyMessage(testThrowawayAddress, signatureMessage, SignatureSpendWrongMessage) 101 if result != ResultFail { 102 t.Fatalf("unexpected %d", result) 103 } 104 result = VerifyMessage(testThrowawayAddress, signatureMessage2, SignatureSpendSuccess) 105 if result != ResultFail { 106 t.Fatalf("unexpected %d", result) 107 } 108 result = VerifyMessage(testThrowawayAddress, signatureMessage, SignatureViewSuccess) 109 if result != ResultFailZeroSpend { 110 t.Fatalf("unexpected %d", result) 111 } 112 result = VerifyMessage(testThrowawayAddress, signatureMessage, SignatureViewWrongMessage) 113 if result != ResultFail { 114 t.Fatalf("unexpected %d", result) 115 } 116 result = VerifyMessage(testThrowawayAddress, signatureMessage2, SignatureViewSuccess) 117 if result != ResultFail { 118 t.Fatalf("unexpected %d", result) 119 } 120 result = VerifyMessage(&ZeroPrivateKeyAddress, signatureMessage, SignatureViewSuccess) 121 if result != ResultFail { 122 t.Fatalf("unexpected %d", result) 123 } 124 }