Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
crypto/ed25519: Use curve25519-voi's public key cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Yawning committed May 20, 2021
1 parent 2aa4f63 commit 63134b8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"

"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/cache"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"
Expand All @@ -25,6 +26,8 @@ var (
verifyOptions = &ed25519.Options{
Verify: ed25519.VerifyOptionsZIP_215,
}

cachingVerifier = cache.NewVerifier(cache.NewLRUCache(cacheSize))
)

const (
Expand All @@ -42,6 +45,14 @@ const (
SeedSize = 32

KeyType = "ed25519"

// cacheSize is the number of public keys that will be cached in
// an expanded format for repeated signature verification.
//
// TODO/perf: Either this should exclude single verification, or be
// tuned to `> validatorSize + maxTxnsPerBlock` to avoid cache
// thrashing.
cacheSize = 4096
)

func init() {
Expand Down Expand Up @@ -159,7 +170,7 @@ func (pubKey PubKey) VerifySignature(msg []byte, sig []byte) bool {
return false
}

return ed25519.VerifyWithOptions(ed25519.PublicKey(pubKey), msg, sig, verifyOptions)
return cachingVerifier.VerifyWithOptions(ed25519.PublicKey(pubKey), msg, sig, verifyOptions)
}

func (pubKey PubKey) String() string {
Expand Down Expand Up @@ -199,7 +210,7 @@ func (b *BatchVerifier) Add(key crypto.PubKey, msg, signature []byte) error {
return errors.New("invalid signature")
}

b.BatchVerifier.AddWithOptions(ed25519.PublicKey(key.Bytes()), msg, signature, verifyOptions)
cachingVerifier.AddWithOptions(b.BatchVerifier, ed25519.PublicKey(key.Bytes()), msg, signature, verifyOptions)

return nil
}
Expand Down

0 comments on commit 63134b8

Please sign in to comment.