Update crypto tests to verify NoAllocate implementations as well

This commit is contained in:
DataHoarder 2024-04-11 02:37:49 +02:00
parent 71dbf635d1
commit e390e970e6
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
6 changed files with 34 additions and 7 deletions

View file

@ -90,7 +90,7 @@ func BenchmarkCoinbaseDerivationNoAllocate(b *testing.B) {
hasher := crypto.GetKeccak256Hasher()
defer crypto.PutKeccak256Hasher(hasher)
for pb.Next() {
GetEphemeralPublicKeyAndViewTagNoAllocate(spendPub, GetDerivationNoAllocate(viewPub, txKey), txKey, i.Add(1), hasher)
GetEphemeralPublicKeyAndViewTagNoAllocate(spendPub, GetDerivationNoAllocate(viewPub, txKey), i.Add(1), hasher)
}
})
b.ReportAllocs()

View file

@ -51,7 +51,7 @@ func GetEphemeralPublicKeyAndViewTag(a Interface, txKey crypto.PrivateKey, outpu
}
// GetEphemeralPublicKeyAndViewTagNoAllocate Special version of GetEphemeralPublicKeyAndViewTag
func GetEphemeralPublicKeyAndViewTagNoAllocate(spendPublicKeyPoint *edwards25519.Point, derivation crypto.PublicKeyBytes, txKey *edwards25519.Scalar, outputIndex uint64, hasher *sha3.HasherState) (crypto.PublicKeyBytes, uint8) {
func GetEphemeralPublicKeyAndViewTagNoAllocate(spendPublicKeyPoint *edwards25519.Point, derivation crypto.PublicKeyBytes, outputIndex uint64, hasher *sha3.HasherState) (crypto.PublicKeyBytes, uint8) {
var intermediatePublicKey, ephemeralPublicKey edwards25519.Point
derivationSharedData, viewTag := crypto.GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate(derivation, outputIndex, hasher)

View file

@ -45,6 +45,10 @@ func TestDerivePublicKey(t *testing.T) {
if results == nil {
t.Fatal()
}
hasher := crypto.GetKeccak256Hasher()
defer crypto.PutKeccak256Hasher(hasher)
for e := range results {
var expectedDerivedKey types.Hash
@ -64,18 +68,31 @@ func TestDerivePublicKey(t *testing.T) {
//expected failure
continue
} else if point2 == nil {
t.Fatalf("invalid point %s / %s", derivation.String(), base.String())
t.Errorf("invalid point %s / %s", derivation.String(), base.String())
continue
}
sharedData := crypto.GetDerivationSharedDataForOutputIndex(&derivation, outputIndex)
sharedData2, _ := crypto.GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate(derivation, outputIndex, hasher)
if sharedData.AsBytes() != crypto.PrivateKeyFromScalar(&sharedData2).AsBytes() {
t.Errorf("derive_public_key differs from no_allocate: %s != %s", sharedData, crypto.PrivateKeyFromScalar(&sharedData2))
}
var addr PackedAddress
addr[0] = base
derivedKey := GetPublicKeyForSharedData(&addr, sharedData)
derivedKey2, _ := GetEphemeralPublicKeyAndViewTagNoAllocate(base.AsPoint().Point(), derivation, outputIndex, hasher)
if derivedKey.AsBytes() != derivedKey2 {
t.Errorf("derive_public_key differs from no_allocate: %s != %s", derivedKey, &derivedKey2)
}
if result {
if expectedDerivedKey.String() != derivedKey.String() {
t.Fatalf("expected %s, got %s", expectedDerivedKey.String(), derivedKey.String())
t.Errorf("expected %s, got %s", expectedDerivedKey.String(), derivedKey.String())
}
}
}

View file

@ -79,6 +79,10 @@ func TestDeriveViewTag(t *testing.T) {
if results == nil {
t.Fatal()
}
hasher := GetKeccak256Hasher()
defer PutKeccak256Hasher(hasher)
for e := range results {
derivation := PublicKeyBytes(types.MustHashFromString(e[0]))
outputIndex, _ := strconv.ParseUint(e[1], 10, 0)
@ -86,8 +90,14 @@ func TestDeriveViewTag(t *testing.T) {
viewTag := GetDerivationViewTagForOutputIndex(&derivation, outputIndex)
_, viewTag2 := GetDerivationSharedDataAndViewTagForOutputIndexNoAllocate(derivation, outputIndex, hasher)
if viewTag != viewTag2 {
t.Errorf("derive_view_tag differs from no_allocate: %d != %d", viewTag, &viewTag2)
}
if result[0] != viewTag {
t.Fatalf("expected %s, got %s", fasthex.EncodeToString(result), fasthex.EncodeToString([]byte{viewTag}))
t.Errorf("expected %s, got %s", fasthex.EncodeToString(result), fasthex.EncodeToString([]byte{viewTag}))
}
}
}

View file

@ -74,7 +74,7 @@ func (d *DerivationCache) GetEphemeralPublicKey(a *address.PackedAddress, txKeyS
viewTable := d.getPublicKeyTable(*a.ViewPublicKey())
spendPoint := d.getPublicKeyPoint(*a.SpendPublicKey())
derivation := d.getDerivation(*a.ViewPublicKey(), txKeySlice, viewTable, txKeyScalar.Scalar())
pKB, viewTag := address.GetEphemeralPublicKeyAndViewTagNoAllocate(spendPoint, derivation, txKeyScalar.Scalar(), outputIndex, hasher)
pKB, viewTag := address.GetEphemeralPublicKeyAndViewTagNoAllocate(spendPoint, derivation, outputIndex, hasher)
d.ephemeralPublicKeyCache.Set(key, ephemeralPublicKeyWithViewTag{PublicKey: pKB, ViewTag: viewTag})
return pKB, viewTag
}

View file

@ -15,7 +15,7 @@ func (d *NilDerivationCache) Clear() {
}
func (d *NilDerivationCache) GetEphemeralPublicKey(a *address.PackedAddress, _ crypto.PrivateKeySlice, txKeyScalar *crypto.PrivateKeyScalar, outputIndex uint64, hasher *sha3.HasherState) (crypto.PublicKeyBytes, uint8) {
ephemeralPubKey, viewTag := address.GetEphemeralPublicKeyAndViewTagNoAllocate(a.SpendPublicKey().AsPoint().Point(), address.GetDerivationNoAllocate(a.ViewPublicKey().AsPoint().Point(), txKeyScalar.Scalar()), txKeyScalar.Scalar(), outputIndex, hasher)
ephemeralPubKey, viewTag := address.GetEphemeralPublicKeyAndViewTagNoAllocate(a.SpendPublicKey().AsPoint().Point(), address.GetDerivationNoAllocate(a.ViewPublicKey().AsPoint().Point(), txKeyScalar.Scalar()), outputIndex, hasher)
return ephemeralPubKey.AsBytes(), viewTag
}