class ECDSA::Group

Constants

NAMES
Nistp192
Nistp224
Nistp256
Nistp384
Nistp521
Secp112r1
Secp112r2
Secp128r1
Secp128r2
Secp160k1
Secp160r1
Secp160r2
Secp192k1
Secp192r1
Secp224k1
Secp224r1
Secp256k1
Secp256r1
Secp384r1
Secp521r1

Attributes

cofactor[R]

The cofactor of the group. This is the number of points on the curve divided by the number of points in the group generated by the generator.

field[R]

The field that coordinates on the curve belong to. @return (PrimeField)

generator[R]

The generator point. @return (Point)

name[R]

The name of the group. @return (String)

order[R]

The order of the group. This is the smallest positive integer ‘i` such that the generator point multiplied by `i` is infinity. This is also the number of different points that are on the curve. @return (Order)

param_a[R]

The a parameter in the curve equation (*y^2 = x^3 + ax + b*). @option opts :a (Integer)

param_b[R]

The b parameter in the curve equation. @return (Integer)

Public Class Methods

new(opts) click to toggle source

These parameters are defined in www.secg.org/collateral/sec2_final.pdf

@param opts (Hash) @option opts :p (Integer) A prime number that defines the field used. The field will be *F<sub>p</sub>*. @option opts :a (Integer) The a parameter in the curve equation (*y^2 = x^3 + ax + b*). @option opts :b (Integer) The b parameter in the curve equation. @option opts :g (Array(Integer)) The coordinates of the generator point, with x first. @option opts :n (Integer) The order of g. This is the smallest positive

integer `i` such that the generator point multiplied by `i` is infinity.
This is also the number of different points that are on the curve.

@option opts :h (Integer) The cofactor (optional).

# File lib/ecdsa/group.rb, line 51
def initialize(opts)
  @opts = opts

  @name = opts.fetch(:name) { '%#x' % object_id }
  @field = PrimeField.new(opts[:p])
  @param_a = opts[:a]
  @param_b = opts[:b]
  @generator = new_point(@opts[:g])
  @order = opts[:n]
  @cofactor = opts[:h]

  @param_a.is_a?(Integer) or raise ArgumentError, 'Invalid a.'
  @param_b.is_a?(Integer) or raise ArgumentError, 'Invalid b.'

  @param_a = field.mod @param_a
  @param_b = field.mod @param_b
end

Public Instance Methods

bit_length() click to toggle source

The number of bits that it takes to represent a member of the field. Log base 2 of the prime p, rounded up.

@return (Integer)

# File lib/ecdsa/group.rb, line 97
def bit_length
  @bit_length ||= ECDSA.bit_length(field.prime)
end
byte_length() click to toggle source

The number of bytes that it takes to represent a member of the field. Log base 256 of the prime p, rounded up.

@return (Integer)

# File lib/ecdsa/group.rb, line 105
def byte_length
  @byte_length ||= ECDSA.byte_length(field.prime)
end
include?(point) click to toggle source

Returns true if the point is a solution to the curve’s defining equation or if it is the infinity point.

# File lib/ecdsa/group.rb, line 111
def include?(point)
  return false if point.group != self
  point.infinity? or point_satisfies_equation?(point)
end
infinity() click to toggle source

Returns the infinity point.

@return (Point)

# File lib/ecdsa/group.rb, line 89
def infinity
  @infinity ||= Point.new(self, :infinity)
end
Also aliased as: infinity_point
inspect() click to toggle source

@return (String)

# File lib/ecdsa/group.rb, line 143
def inspect
  "#<#{self.class}:#{name}>"
end
new_point(p) click to toggle source

Creates a new point. The argument can either be an array of integers representing the coordinates, with x first, or it can be ‘:infinity`.

# File lib/ecdsa/group.rb, line 72
def new_point(p)
  case p
  when :infinity
    infinity
  when Array
    x, y = p
    Point.new(self, x, y)
  when Integer
    generator.multiply_by_scalar(p)
  else
    raise ArgumentError, "Invalid point specifier #{p.inspect}."
  end
end
partially_valid_public_key?(point) click to toggle source

Returns true if the point is not infinity and it is a solution to the curve’s defining equation. This is defined in SEC1 2.0, Section 3.2.3.1: Elliptic Curve Public Key Partial Validation Primitive

# File lib/ecdsa/group.rb, line 129
def partially_valid_public_key?(point)
  return false if point.group != self
  return false if point.infinity?
  point_satisfies_equation?(point)
end
solve_for_y(x) click to toggle source

Given the x coordinate of a point, finds all possible corresponding y coordinates.

@return (Array)

# File lib/ecdsa/group.rb, line 138
def solve_for_y(x)
  field.square_roots equation_right_hand_side x
end
to_s() click to toggle source

@return (String)

# File lib/ecdsa/group.rb, line 148
def to_s
  inspect
end
valid_public_key?(point) click to toggle source

Returns true if the point is not infinity, it is a solution to the curve’s defining equation, and it is a multiple of G. This process is defined in SEC1 2.0, Section 3.2.2.1: Elliptic Curve Public Key Partial Validation Primitive

# File lib/ecdsa/group.rb, line 119
def valid_public_key?(point)
  return false if point.group != self
  return false if point.infinity?
  return false if !point_satisfies_equation?(point)
  point.multiply_by_scalar(order).infinity?
end

Private Instance Methods

equation_right_hand_side(x) click to toggle source
# File lib/ecdsa/group.rb, line 158
def equation_right_hand_side(x)
  field.mod(x * x * x + param_a * x + param_b)
end
infinity_point()

Group#infinity_point was deprecated in favor of infinity. This alias is for backwards compatibility with versions 0.1.4 and before.

Alias for: infinity
point_satisfies_equation?(point) click to toggle source
# File lib/ecdsa/group.rb, line 154
def point_satisfies_equation?(point)
  field.square(point.y) == equation_right_hand_side(point.x)
end