git.gammaspectra.live/P2Pool/consensus@v0.0.0-20240403173234-a039820b20c9/monero/address/address_test.go (about)

     1  package address
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"git.gammaspectra.live/P2Pool/consensus/monero/crypto"
     7  	"git.gammaspectra.live/P2Pool/consensus/types"
     8  	"git.gammaspectra.live/P2Pool/consensus/utils"
     9  	"git.gammaspectra.live/P2Pool/edwards25519"
    10  	"sync/atomic"
    11  	"testing"
    12  )
    13  
    14  var privateKey = edwards25519.NewScalar()
    15  
    16  var testAddress = FromBase58("42HEEF3NM9cHkJoPpDhNyJHuZ6DFhdtymCohF9CwP5KPM1Mp3eH2RVXCPRrxe4iWRogT7299R8PP7drGvThE8bHmRDq1qWp")
    17  var testAddress2 = FromBase58("4AQ3YkqG2XdWsPHEgrDGdyQLq1qMMGFqWTFJfrVQW99qPmCzZKvJqzxgf5342KC17o9bchfJcUzLhVW9QgNKTYUBLg876Gt")
    18  var testAddress3 = FromBase58("47Eqp7fsvVnPPSU4rsXrKJhyAme6LhDRZDzFky9xWsWUS9pd6FPjJCMDCNX1NnNiDzTwfbAgGMk2N6A1aucNcrkhLffta1p")
    19  
    20  var ephemeralPubKey, _ = hex.DecodeString("20efc1310db960b0e8d22c8b85b3414fcaa1ed9aab40cf757321dd6099a62d5e")
    21  
    22  func init() {
    23  	h, _ := hex.DecodeString("74b98b1e7ce5fc50d1634f8634622395ec2a19a4698a016fedd8139df374ac00")
    24  	if _, err := privateKey.SetCanonicalBytes(h); err != nil {
    25  		utils.Panic(err)
    26  	}
    27  }
    28  
    29  func TestAddress(t *testing.T) {
    30  	derivation := crypto.PrivateKeyFromScalar(privateKey).GetDerivationCofactor(testAddress.ViewPublicKey())
    31  
    32  	sharedData := crypto.GetDerivationSharedDataForOutputIndex(derivation, 37)
    33  	ephemeralPublicKey := GetPublicKeyForSharedData(testAddress, sharedData)
    34  
    35  	if bytes.Compare(ephemeralPublicKey.AsSlice(), ephemeralPubKey) != 0 {
    36  		t.Fatalf("ephemeral key mismatch, expected %s, got %s", hex.EncodeToString(ephemeralPubKey), ephemeralPublicKey.String())
    37  	}
    38  }
    39  
    40  var previousId, _ = types.HashFromString("d59abce89ce8131eba025988d1ea372937f2acf85b86b46993b80c4354563a8b")
    41  var detTxPriv, _ = types.HashFromString("10f3941fd50ca266d3350984004a804c887c36ec11620080fe0b7c4a2a208605")
    42  
    43  func TestDeterministic(t *testing.T) {
    44  	detTx := types.HashFromBytes(GetDeterministicTransactionPrivateKey(types.Hash(testAddress2.SpendPublicKey().AsBytes()), previousId).AsSlice())
    45  	if detTx != detTxPriv {
    46  		t.Fatal()
    47  	}
    48  }
    49  
    50  func TestSort(t *testing.T) {
    51  	if testAddress2.Compare(testAddress3) != -1 {
    52  		t.Fatalf("expected address2 < address3, got %d", testAddress2.Compare(testAddress3))
    53  	}
    54  }
    55  
    56  func BenchmarkCoinbaseDerivation(b *testing.B) {
    57  	b.ReportAllocs()
    58  	packed := testAddress3.ToPackedAddress()
    59  	txKey := crypto.PrivateKeyFromScalar(privateKey)
    60  	var i atomic.Uint64
    61  	b.RunParallel(func(pb *testing.PB) {
    62  		for pb.Next() {
    63  			GetEphemeralPublicKeyAndViewTag(&packed, txKey, i.Add(1))
    64  		}
    65  	})
    66  	b.ReportAllocs()
    67  }
    68  
    69  func BenchmarkCoinbaseDerivationInline(b *testing.B) {
    70  	spendPub, viewPub := testAddress3.SpendPublicKey().AsPoint().Point(), testAddress3.ViewPublicKey().AsPoint().Point()
    71  
    72  	var i atomic.Uint64
    73  	b.RunParallel(func(pb *testing.PB) {
    74  		p := new(edwards25519.Point)
    75  		for pb.Next() {
    76  			getEphemeralPublicKeyInline(spendPub, viewPub, privateKey, i.Add(1), p)
    77  		}
    78  	})
    79  	b.ReportAllocs()
    80  }
    81  
    82  func BenchmarkCoinbaseDerivationNoAllocate(b *testing.B) {
    83  
    84  	spendPub, viewPub := testAddress3.SpendPublicKey().AsPoint().Point(), testAddress3.ViewPublicKey().AsPoint().Point()
    85  
    86  	txKey := privateKey
    87  
    88  	var i atomic.Uint64
    89  	b.RunParallel(func(pb *testing.PB) {
    90  		hasher := crypto.GetKeccak256Hasher()
    91  		defer crypto.PutKeccak256Hasher(hasher)
    92  		for pb.Next() {
    93  			GetEphemeralPublicKeyAndViewTagNoAllocate(spendPub, GetDerivationNoAllocate(viewPub, txKey), txKey, i.Add(1), hasher)
    94  		}
    95  	})
    96  	b.ReportAllocs()
    97  }
    98  
    99  func BenchmarkPackedAddress_ComparePacked(b *testing.B) {
   100  	a1, a2 := testAddress.ToPackedAddress(), testAddress2.ToPackedAddress()
   101  
   102  	b.RunParallel(func(pb *testing.PB) {
   103  		for pb.Next() {
   104  			if a1.ComparePacked(&a2) == 0 {
   105  				panic("cannot be equal")
   106  			}
   107  		}
   108  	})
   109  	b.ReportAllocs()
   110  }