Update crypto tests to verify NoAllocate implementations as well
This commit is contained in:
parent
71dbf635d1
commit
e390e970e6
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue