Module: ECDSA::Format::PointOctetString

Defined in:
lib/ecdsa/format/point_octet_string.rb

Overview

This module provides methods for converting between Point objects and “octet strings”, which are just strings with binary data in them that have the coordinates of the point. The point octet string format is defined in two sections of [SEC1](www.secg.org/collateral/sec1_final.pdf):

  • Section 2.3.3: EllipticCurvePoint-to-OctetString Conversion

  • Section 2.3.4: OctetString-to-EllipticCurvePoint Conversion

Class Method Summary collapse

Class Method Details

.decode(string, group) ⇒ Point

Converts an octet string to a Point in the specified group. Raises a DecodeError if the string is invalid for any reason.

This is equivalent to [ec_GFp_simple_oct2point](github.com/openssl/openssl/blob/a898936/crypto/ec/ecp_oct.c#L325) in OpenSSL.

Parameters:

  • string (String)
  • group (Group)

Returns:

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ecdsa/format/point_octet_string.rb', line 44

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

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

  case string[0].ord
  when 0
    check_length string, 1
    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

.encode(point, opts = {}) ⇒ Object

Converts a Point to an octet string.

Parameters:

  • point (Point)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :compression (Object)

    Set this option to true to generate a compressed octet string, which only has one bit of the Y coordinate. (Default: false)



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ecdsa/format/point_octet_string.rb', line 21

def self.encode(point, opts = {})
  return "\x00" if point.infinity?

  if opts[:compression]
    start_byte = point.y.even? ? "\x02" : "\x03"
    start_byte + FieldElementOctetString.encode(point.x, point.group.field)
  else
    "\x04" +
      FieldElementOctetString.encode(point.x, point.group.field) +
      FieldElementOctetString.encode(point.y, point.group.field)
  end
end