Class: CryptoGost::Point

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

Overview

EllipticCurvePoint

author WildDima

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, coords) ⇒ Point

Returns a new instance of Point.



8
9
10
11
# File 'lib/crypto_gost/point.rb', line 8

def initialize(group, coords)
  @group = group
  @x, @y = coords
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



6
7
8
# File 'lib/crypto_gost/point.rb', line 6

def group
  @group
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/crypto_gost/point.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/crypto_gost/point.rb', line 6

def y
  @y
end

Instance Method Details

#*(other) ⇒ Object

rubocop:enable Metrics/AbcSize



49
50
51
52
53
54
55
56
57
58
# File 'lib/crypto_gost/point.rb', line 49

def *(other)
  return unless other.is_a? Numeric
  if other == 1
    self
  elsif (other % 2).odd?
    self + (self * (other - 1))
  else
    double * (other / 2)
  end
end

#+(other) ⇒ Object

rubocop:disable Metrics/AbcSize



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crypto_gost/point.rb', line 18

def +(other)
  unless other.is_a? Point
    raise ArgumentError, "Invalid other: #{other.inspect}"
  end

  new_x = add_ec_module(other.x - x)
  new_y = add_ec_module(other.y - y)

  s = add_ec_module(
    (new_y * ModularArithmetic.invert(new_x, group.p)) % group.p
  )

  new_x = add_ec_module((s**2 - x - other.x) % group.p)
  new_y = add_ec_module((s * (x - new_x) - y) % group.p)

  self.class.new group, [new_x, new_y]
end

#add_ec_module(coord) ⇒ Object



60
61
62
# File 'lib/crypto_gost/point.rb', line 60

def add_ec_module(coord)
  coord < 0 ? coord + group.p : coord
end

#coordsObject



13
14
15
# File 'lib/crypto_gost/point.rb', line 13

def coords
  [x, y]
end

#doubleObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/crypto_gost/point.rb', line 36

def double
  new_x = add_ec_module(2 * y)
  new_y = add_ec_module(3 * x**2 + group.a)

  s = (new_y * ModularArithmetic.invert(new_x, group.p)) % group.p

  new_x = add_ec_module(s**2 - 2 * x) % group.p
  new_y = add_ec_module(s * (x - new_x) - y) % group.p

  self.class.new group, [new_x, new_y]
end