/vendor/bundle/jruby/2.1/gems/rbnacl-3.0.1/lib/rbnacl/group_elements/curve25519.rb

https://github.com/delowong/logstash · Ruby · 81 lines · 33 code · 15 blank · 33 comment · 0 complexity · 56cb4b85bbaebc00b7ebee2dcb7718b0 MD5 · raw file

  1. # encoding: binary
  2. module RbNaCl
  3. module GroupElements
  4. # Points provide the interface to NaCl's Curve25519 high-speed elliptic
  5. # curve cryptography, which can be used for implementing Diffie-Hellman
  6. # and other forms of public key cryptography (e.g. RbNaCl::Box)
  7. #
  8. # Objects of the Point class represent points on Edwards curves. NaCl
  9. # defines a base point (the "standard group element") which we can
  10. # multiply by an arbitrary integer. This is how NaCl computes public
  11. # keys from private keys.
  12. class Curve25519
  13. # NaCl's Curve25519 base point (a.k.a. standard group element), serialized as hex
  14. STANDARD_GROUP_ELEMENT = ["0900000000000000000000000000000000000000000000000000000000000000"].pack("H*").freeze
  15. # Order of the standard group
  16. STANDARD_GROUP_ORDER = 2**252 + 27742317777372353535851937790883648493
  17. include KeyComparator
  18. include Serializable
  19. extend Sodium
  20. sodium_type :scalarmult
  21. sodium_primitive :curve25519
  22. sodium_function :scalarmult_curve25519,
  23. :crypto_scalarmult_curve25519,
  24. [:pointer, :pointer, :pointer]
  25. # Number of bytes in a scalar on this curve
  26. SCALARBYTES = 32
  27. BYTES = 32
  28. # Number of bytes in a scalar on this curve
  29. # Creates a new Point from the given serialization
  30. #
  31. # @param [String] point location of a group element (32-bytes)
  32. #
  33. # @return [RbNaCl::Point] the Point at this location
  34. def initialize(point)
  35. @point = point.to_str
  36. # FIXME: really should have a separate constant here for group element size
  37. # Group elements and scalars are both 32-bits, but that's for convenience
  38. Util.check_length(@point, SCALARBYTES, "group element")
  39. end
  40. # Multiply the given integer by this point
  41. # This ordering is a bit confusing because traditionally the point
  42. # would be the right-hand operand.
  43. #
  44. # @param [String] integer value to multiply with this Point (32-bytes)
  45. #
  46. # @return [RbNaCl::Point] result as a Point object
  47. def mult(integer)
  48. integer = integer.to_str
  49. Util.check_length(integer, SCALARBYTES, "integer")
  50. result = Util.zeros(SCALARBYTES)
  51. self.class.scalarmult_curve25519(result, integer, @point)
  52. self.class.new(result)
  53. end
  54. # Return the point serialized as bytes
  55. #
  56. # @return [String] 32-byte string representing this point
  57. def to_bytes; @point; end
  58. @base_point = new(STANDARD_GROUP_ELEMENT)
  59. # NaCl's standard base point for all Curve25519 public keys
  60. #
  61. # @return [RbNaCl::Point] standard base point (a.k.a. standard group element)
  62. def self.base; @base_point; end
  63. def self.base_point; @base_point; end
  64. end
  65. end
  66. end