class CryptoGost3410::Point

EllipticCurvePoint

author WildDima

Attributes

group[RW]
x[RW]
y[RW]

Public Class Methods

new(group, coords) click to toggle source
# File lib/crypto_gost3410/point.rb, line 8
def initialize(group, coords)
  @group = group
  @x, @y = coords
end

Public Instance Methods

*(other) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/crypto_gost3410/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) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/crypto_gost3410/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) click to toggle source
# File lib/crypto_gost3410/point.rb, line 60
def add_ec_module(coord)
  coord < 0 ? coord + group.p : coord
end
coords() click to toggle source
# File lib/crypto_gost3410/point.rb, line 13
def coords
  [x, y]
end
double() click to toggle source
# File lib/crypto_gost3410/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