lib/x25519.rb in x25519-1.0.1 vs lib/x25519.rb in x25519-1.0.2
- old
+ new
@@ -17,23 +17,26 @@
module_function
# Size of an X25519 key (public or private) in bytes
KEY_SIZE = 32
+ # Raised when the built-in self-test fails
+ SelfTestFailure = Class.new(StandardError)
+
+ class << self
+ # Obtain the backend provider module
+ attr_accessor :provider
+ end
+
# ref10 is the default provider
- @provider = X25519::Provider::Ref10
+ self.provider = X25519::Provider::Ref10
# X25519::Precomputed requires a 4th generation Intel Core CPU or newer,
# so only enable it if we detect we're on a supported platform. Otherwise,
# fall back to the ref10 portable C implementation.
- @provider = X25519::Provider::Precomputed if X25519::Provider::Precomputed.available?
+ self.provider = X25519::Provider::Precomputed if X25519::Provider::Precomputed.available?
- # Selected provider based on the logic above
- def provider
- @provider
- end
-
# Raw Diffie-Hellman function that acts directly on bytestrings. An
# alternative to the object-oriented API
#
# @param scalar_bytes [String] a serialized private scalar
# @param montgomery_u_bytes [String] a point we wish to multiply by the scalar
@@ -54,15 +57,15 @@
# Perform a self-test to ensure the selected provider is working
def self_test
X25519::TestVectors::VARIABLE_BASE.each do |v|
shared_secret = provider.scalarmult([v.scalar].pack("H*"), [v.input_coord].pack("H*"))
- raise "self test failed!" unless shared_secret.unpack("H*").first == v.output_coord
+ raise SelfTestFailure, "self test failed!" unless shared_secret.unpack("H*").first == v.output_coord
end
X25519::TestVectors::FIXED_BASE.each do |v|
public_key = provider.scalarmult_base([v.scalar].pack("H*"))
- raise "self test failed!" unless public_key.unpack("H*").first == v.output_coord
+ raise SelfTestFailure, "self test failed!" unless public_key.unpack("H*").first == v.output_coord
end
true
end
end