Class: BitcoinCigs::Point

Inherits:
Object
  • Object
show all
Includes:
CryptoHelper
Defined in:
lib/bitcoin_cigs/point.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CryptoHelper

#decode58, #decode64, #decode_hex, #encode58, #encode64, #inverse_mod, #leftmost_bit, #ripemd160, #sha256, #sqrt_mod, #str_to_num

Constructor Details

#initialize(curve, x, y, order = nil) ⇒ Point

Returns a new instance of Point.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/bitcoin_cigs/point.rb', line 11

def initialize(curve, x, y, order = nil)
  self.curve = curve
  self.x = x
  self.y = y
  self.order = order
  
  return if infinite?
  
  raise ::BitcoinCigs::Error.new if curve && !curve.contains_point(x, y)
  raise ::BitcoinCigs::Error.new if order && !(self * order).infinite?
end

Instance Attribute Details

#curveObject

Returns the value of attribute curve.



5
6
7
# File 'lib/bitcoin_cigs/point.rb', line 5

def curve
  @curve
end

#orderObject

Returns the value of attribute order.



5
6
7
# File 'lib/bitcoin_cigs/point.rb', line 5

def order
  @order
end

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/bitcoin_cigs/point.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/bitcoin_cigs/point.rb', line 5

def y
  @y
end

Class Method Details

.infinityObject



7
8
9
# File 'lib/bitcoin_cigs/point.rb', line 7

def self.infinity
  ::BitcoinCigs::Point.new(nil, nil, nil)
end

Instance Method Details

#*(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bitcoin_cigs/point.rb', line 45

def *(other)
  e = other
  
  e = e % order if order
  
  return ::BitcoinCigs::Point.infinity if e == 0
  return ::BitcoinCigs::Point.infinity if infinite?
  
  raise ::BitcoinCigs::Error.new unless e > 0
  
  e3 = 3 * e
  negative_self = ::BitcoinCigs::Point.new(curve, x, -y, order)
  i = leftmost_bit(e3) / 2
  result = self
  
  while i > 1
    result = result.double
    result += self if (e3 & i) != 0 && (e & i) == 0
    result += negative_self if (e3 & i) == 0 && (e & i) != 0
    i = i / 2
  end
  
  result
end

#+(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bitcoin_cigs/point.rb', line 27

def +(other)
  return self if other.infinite?
  return other if infinite?
  
  raise ::BitcoinCigs::Error.new if curve != other.curve

  if x == other.x
    return (y + other.y) % curve.p == 0 ? ::BitcoinCigs::Point.infinity : double
  end

  p = curve.p
  l = ( ( other.y - y ) * inverse_mod( other.x - x, p ) ) % p
  x3 = ( l * l - x - other.x ) % p
  y3 = ( l * ( x - x3 ) - y ) % p
  
  Point.new(curve, x3, y3)
end

#==(other) ⇒ Object



70
71
72
# File 'lib/bitcoin_cigs/point.rb', line 70

def ==(other)
  curve == other.curve && x == other.x && y == other.y && order == other.order
end

#doubleObject



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bitcoin_cigs/point.rb', line 78

def double
  return ::BitcoinCigs::Point.infinity if infinite?

  p = curve.p
  a = curve.a
  l = ( ( 3 * x * x + a ) * \
        inverse_mod( 2 * y, p ) ) % p
  x3 = ( l * l - 2 * x ) % p
  y3 = ( l * ( x - x3 ) - y ) % p
  
  ::BitcoinCigs::Point.new(curve, x3, y3)
end

#infinite?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/bitcoin_cigs/point.rb', line 23

def infinite?
  curve.nil? && x.nil? && y.nil? && order.nil?
end

#to_sObject



74
75
76
# File 'lib/bitcoin_cigs/point.rb', line 74

def to_s
  infinite? ? "infinity" : "(#{x},#{y})"
end