Class: Ecc::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/ecc/point.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curve, x, y) ⇒ Point

Returns a new instance of Point.


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ecc/point.rb', line 7

def initialize(curve, x, y)

  if curve.class != Curve
    raise "1st argument type error: "
  end
  
  @curve = curve
  @x = x
  @y = y
  
end

Instance Attribute Details

#xObject

Returns the value of attribute x


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

def x
  @x
end

#yObject

Returns the value of attribute y


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

def y
  @y
end

Instance Method Details

#*(d) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ecc/point.rb', line 60

def *(d)
    
  sum = self
  
  (d - 1).times do
    sum = sum + self
  end
  
  sum
  
end

#+(other) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ecc/point.rb', line 37

def +(other)
  
  u = self
  v = other
  
  return u if v.zero?
  return v if u.zero?
  
  t = 0
  
  if u != v
    t = ((v.y - u.y) * (((v.x - u.x) ** (@curve.fp - 2)) % @curve.fp)) % @curve.fp
  else
    t = ((3 * u.x ** 2 + @curve.a) * (((2 * u.y) ** (@curve.fp - 2)) % @curve.fp)) % @curve.fp
  end
   
  x3 = t ** 2 - u.x - v.x
  y3 = t * (u.x - x3) - u.y
  
  Point.new(@curve, x3 % @curve.fp, y3 % @curve.fp)

end

#==(other) ⇒ Object


31
32
33
34
35
# File 'lib/ecc/point.rb', line 31

def ==(other)
  
  @x == other.x and @y == other.y
  
end

#to_sObject


19
20
21
22
23
# File 'lib/ecc/point.rb', line 19

def to_s
    
  "(#{@x}, #{@y})"
  
end

#zero?Boolean

Returns:

  • (Boolean)

25
26
27
28
29
# File 'lib/ecc/point.rb', line 25

def zero?
  
  @x == 0 and @y == 0
    
end