Module: ECDSA::Format::PointOctetString

Defined in:
lib/schnorr/ec_point_ext.rb

Class Method Summary collapse

Class Method Details

.decode(string, group) ⇒ Object

Raises:

  • (DecodeError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/schnorr/ec_point_ext.rb', line 31

def self.decode(string, group)
  string = string.dup.force_encoding('BINARY')

  raise DecodeError, 'Point octet string is empty.' if string.empty?

  if string.bytesize == 32
    decode_from_x(string, group)
  else
    case string[0].ord
    when 0
      check_length string, 33
      raise DecodeError, 'Unrecognized infinity point.' unless ['00' * 33].pack('H*') == string
      return group.infinity
    when 2
      decode_compressed string, group, 0
    when 3
      decode_compressed string, group, 1
    when 4
      decode_uncompressed string, group
    else
      raise DecodeError, 'Unrecognized start byte for point octet string: 0x%x' % string[0].ord
    end
  end
end

.decode_from_x(x_string, group) ⇒ ECDSA::Point

decode from x coordinate.

Parameters:

  • x_string (String)

    X-coordinate binary string

  • group (ECDSA::Group)

    A group of elliptic curves to use.

Returns:

Raises:

  • (DecodeError)


60
61
62
63
64
65
66
# File 'lib/schnorr/ec_point_ext.rb', line 60

def self.decode_from_x(x_string, group)
  x = ECDSA::Format::FieldElementOctetString.decode(x_string, group.field)
  y_sq = group.field.mod(x.pow(3, group.field.prime) + 7)
  y = y_sq.pow((group.field.prime + 1)/4, group.field.prime)
  raise DecodeError, 'Public key not on the curve.' unless y.pow(2, group.field.prime) == y_sq
  finish_decode(x, y.even? ? y : group.field.prime - y, group)
end