PageRenderTime 26ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 2module 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
16      # Order of the standard group
17      STANDARD_GROUP_ORDER = 2**252 + 27742317777372353535851937790883648493
18
19      include KeyComparator
20      include Serializable
21
22      extend Sodium
23
24      sodium_type      :scalarmult
25      sodium_primitive :curve25519
26
27      sodium_function  :scalarmult_curve25519,
28                       :crypto_scalarmult_curve25519,
29                       [:pointer, :pointer, :pointer]
30
31      # Number of bytes in a scalar on this curve
32      SCALARBYTES = 32
33      BYTES       = 32
34
35      # Number of bytes in a scalar on this curve
36
37      # Creates a new Point from the given serialization
38      #
39      # @param [String] point location of a group element (32-bytes)
40      #
41      # @return [RbNaCl::Point] the Point at this location
42      def initialize(point)
43        @point = point.to_str
44
45        # FIXME: really should have a separate constant here for group element size
46        # Group elements and scalars are both 32-bits, but that's for convenience
47        Util.check_length(@point, SCALARBYTES, "group element")
48      end
49
50      # Multiply the given integer by this point
51      # This ordering is a bit confusing because traditionally the point
52      # would be the right-hand operand.
53      #
54      # @param [String] integer value to multiply with this Point (32-bytes)
55      #
56      # @return [RbNaCl::Point] result as a Point object
57      def mult(integer)
58        integer = integer.to_str
59        Util.check_length(integer, SCALARBYTES, "integer")
60
61        result = Util.zeros(SCALARBYTES)
62        self.class.scalarmult_curve25519(result, integer, @point)
63
64        self.class.new(result)
65      end
66
67      # Return the point serialized as bytes
68      #
69      # @return [String] 32-byte string representing this point
70      def to_bytes; @point; end
71
72      @base_point = new(STANDARD_GROUP_ELEMENT)
73
74      # NaCl's standard base point for all Curve25519 public keys
75      #
76      # @return [RbNaCl::Point] standard base point (a.k.a. standard group element)
77      def self.base; @base_point; end
78      def self.base_point; @base_point; end
79    end
80  end
81end