Class: ECDSA::Ext::AbstractPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/ecdsa/ext/abstract_point.rb

Overview

Abstract class of point

Direct Known Subclasses

JacobianPoint, ProjectivePoint

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, *args) ⇒ ECDSA::Ext::AbstractPoint

Create new instance.

Parameters:

  • group (ECDSA::Group)
  • args (Array)
    x, y, z

13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ecdsa/ext/abstract_point.rb', line 13

def initialize(group, *args)
  @group = group
  if args == [:infinity]
    @infinity = true
  else
    @infinity = false
    @x, @y, @z = args
    raise ArgumentError, "Invalid x: #{x.inspect}" unless x.is_a?(Integer)
    raise ArgumentError, "Invalid y: #{y.inspect}" unless y.is_a?(Integer)
    raise ArgumentError, "Invalid z: #{z.inspect}" unless z.is_a?(Integer)
  end
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.


7
8
9
# File 'lib/ecdsa/ext/abstract_point.rb', line 7

def group
  @group
end

#infinityObject (readonly)

Returns the value of attribute infinity.


7
8
9
# File 'lib/ecdsa/ext/abstract_point.rb', line 7

def infinity
  @infinity
end

#xObject (readonly)

Returns the value of attribute x.


7
8
9
# File 'lib/ecdsa/ext/abstract_point.rb', line 7

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.


7
8
9
# File 'lib/ecdsa/ext/abstract_point.rb', line 7

def y
  @y
end

#zObject (readonly)

Returns the value of attribute z.


7
8
9
# File 'lib/ecdsa/ext/abstract_point.rb', line 7

def z
  @z
end

Class Method Details

.from_affine(point) ⇒ ECDSA::Ext::AbstractPoint

Convert coordinates from affine.

Parameters:

Returns:


35
36
37
38
39
40
41
# File 'lib/ecdsa/ext/abstract_point.rb', line 35

def self.from_affine(point)
  if point.infinity?
    infinity_point(point.group)
  else
    new(point.group, point.x, point.y, 1)
  end
end

.infinity_point(group) ⇒ ECDSA::Ext::AbstractPoint

Create infinity point


45
46
47
# File 'lib/ecdsa/ext/abstract_point.rb', line 45

def self.infinity_point(group)
  new(group, :infinity)
end

Instance Method Details

#==(other) ⇒ Object

Raises:

  • (NotImplementedError)

106
107
108
# File 'lib/ecdsa/ext/abstract_point.rb', line 106

def ==(other)
  raise NotImplementedError
end

#add_to_point(other) ⇒ Object

Raises:

  • (NotImplementedError)

94
95
96
# File 'lib/ecdsa/ext/abstract_point.rb', line 94

def add_to_point(other)
  raise NotImplementedError
end

#coordsArray

Return coordinates.

Returns:

  • (Array)

    (x, y , z)


68
69
70
# File 'lib/ecdsa/ext/abstract_point.rb', line 68

def coords
  [x, y, z]
end

#doubleObject

Raises:

  • (NotImplementedError)

98
99
100
# File 'lib/ecdsa/ext/abstract_point.rb', line 98

def double
  raise NotImplementedError
end

#fieldECDSA::PrimeField

Get filed of this group.

Returns:

  • (ECDSA::PrimeField)

28
29
30
# File 'lib/ecdsa/ext/abstract_point.rb', line 28

def field
  group.field
end

#infinity?Boolean

Check whether infinity point or not.

Returns:

  • (Boolean)

55
56
57
# File 'lib/ecdsa/ext/abstract_point.rb', line 55

def infinity?
  @infinity
end

#infinity_pointObject


49
50
51
# File 'lib/ecdsa/ext/abstract_point.rb', line 49

def infinity_point
  self.class.infinity_point(group)
end

#multiply_by_scalar(x) ⇒ ECDSA::Ext::ProjectivePoint Also known as: *

Return the point multiplied by a non-negative integer.

Parameters:

  • x (Integer)

Returns:

Raises:

  • (ArgumentError)

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ecdsa/ext/abstract_point.rb', line 75

def multiply_by_scalar(x)
  raise ArgumentError, "Scalar is not an integer." unless x.is_a?(Integer)
  raise ArgumentError, "Scalar is negative." if x.negative?

  q = self.class.infinity_point(group)
  v = self
  p, n = to_naf(x)
  len = [p.bit_length, n.bit_length].max
  len.times do
    q += v if p.odd?
    q += v.negate if n.odd?
    v = v.double
    p >>= 1
    n >>= 1
  end
  q
end

#negateECDSA::Ext::AbstractPoint

Return additive inverse of the point.


61
62
63
64
# File 'lib/ecdsa/ext/abstract_point.rb', line 61

def negate
  return self if infinity?
  self.class.new(group, x, field.mod(-y), z)
end

#to_affineObject

Raises:

  • (NotImplementedError)

102
103
104
# File 'lib/ecdsa/ext/abstract_point.rb', line 102

def to_affine
  raise NotImplementedError
end