You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

crypto: Use a different library for ed25519/sr25519 (#6526) At Oasis we have spend some time writing a new Ed25519/X25519/sr25519 implementation called curve25519-voi. This PR switches the import from ed25519consensus/go-schnorrkel, which should lead to performance gains on most systems. Summary of changes: * curve25519-voi is now used for Ed25519 operations, following the existing ZIP-215 semantics. * curve25519-voi's public key cache is enabled (hardcoded size of 4096 entries, should be tuned, see the code comment) to accelerate repeated Ed25519 verification with the same public key(s). * (BREAKING) curve25519-voi is now used for sr25519 operations. This is a breaking change as the current sr25519 support does something decidedly non-standard when going from a MiniSecretKey to a SecretKey and or PublicKey (The expansion routine is called twice). While I believe the new behavior (that expands once and only once) to be more "correct", this changes the semantics as implemented. * curve25519-voi is now used for merlin since the included STROBE implementation produces much less garbage on the heap. Side issues fixed: * The version of go-schnorrkel that is currently imported by tendermint has a badly broken batch verification implementation. Upstream has fixed the issue after I reported it, so the version should be bumped in the interim. Open design questions/issues: * As noted, the public key cache size should be tuned. It is currently backed by a trivial thread-safe LRU cache, which is not scan-resistant, but replacing it with something better is a matter of implementing an interface. * As far as I can tell, the only reason why serial verification on batch failure is necessary is to provide more detailed error messages (that are only used in some unit tests). If you trust the batch verification to be consistent with serial verification then the fallback can be eliminated entirely (the BatchVerifier provided by the new library supports an option that omits the fallback if this is chosen as the way forward). * curve25519-voi's sr25519 support could use more optimization and more eyes on the code. The algorithm unfortunately is woefully under-specified, and the implementation was done primarily because I got really sad when I actually looked at go-schnorrkel, and we do not use the algorithm at this time.
4 years ago
  1. package crypto
  2. import (
  3. "github.com/tendermint/tendermint/crypto/tmhash"
  4. "github.com/tendermint/tendermint/internal/jsontypes"
  5. "github.com/tendermint/tendermint/libs/bytes"
  6. )
  7. const (
  8. // AddressSize is the size of a pubkey address.
  9. AddressSize = tmhash.TruncatedSize
  10. )
  11. // An address is a []byte, but hex-encoded even in JSON.
  12. // []byte leaves us the option to change the address length.
  13. // Use an alias so Unmarshal methods (with ptr receivers) are available too.
  14. type Address = bytes.HexBytes
  15. func AddressHash(bz []byte) Address {
  16. return Address(tmhash.SumTruncated(bz))
  17. }
  18. type PubKey interface {
  19. Address() Address
  20. Bytes() []byte
  21. VerifySignature(msg []byte, sig []byte) bool
  22. Equals(PubKey) bool
  23. Type() string
  24. // Implementations must support tagged encoding in JSON.
  25. jsontypes.Tagged
  26. }
  27. type PrivKey interface {
  28. Bytes() []byte
  29. Sign(msg []byte) ([]byte, error)
  30. PubKey() PubKey
  31. Equals(PrivKey) bool
  32. Type() string
  33. // Implementations must support tagged encoding in JSON.
  34. jsontypes.Tagged
  35. }
  36. type Symmetric interface {
  37. Keygen() []byte
  38. Encrypt(plaintext []byte, secret []byte) (ciphertext []byte)
  39. Decrypt(ciphertext []byte, secret []byte) (plaintext []byte, err error)
  40. }
  41. // If a new key type implements batch verification,
  42. // the key type must be registered in github.com/tendermint/tendermint/crypto/batch
  43. type BatchVerifier interface {
  44. // Add appends an entry into the BatchVerifier.
  45. Add(key PubKey, message, signature []byte) error
  46. // Verify verifies all the entries in the BatchVerifier, and returns
  47. // if every signature in the batch is valid, and a vector of bools
  48. // indicating the verification status of each signature (in the order
  49. // that signatures were added to the batch).
  50. Verify() (bool, []bool)
  51. }