Module: ECDSA::Format::FieldElementOctetString

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

Overview

This module provides methods for converting elements of a field to octet strings. The conversions are defined in these sections of [SEC1](www.secg.org/collateral/sec1_final.pdf):

  • Section 2.3.5: FieldElement-to-OctetString Conversion

  • Section 2.3.6: OctetString-to-FieldElement Conversion

Class Method Summary collapse

Class Method Details

.decode(string, field) ⇒ Integer

Converts an octet string to a field element. Raises a DecodeError if the input string is invalid for any reason.

Parameters:

Returns:

  • (Integer)


27
28
29
30
31
32
33
34
35
36
# File 'lib/ecdsa/format/field_element_octet_string.rb', line 27

def self.decode(string, field)
  int = IntegerOctetString.decode(string)

  if !field.include?(int)
    # The integer has to be non-negative, so it must be too big.
    raise DecodeError, 'Decoded integer is too large for field: 0x%x >= 0x%x.' % [int, field.prime]
  end

  int
end

.encode(element, field) ⇒ String

Converts a field element to an octet string.

Parameters:

  • element (Integer)

    The integer to encode.

  • field (PrimeField)

Returns:

  • (String)

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/ecdsa/format/field_element_octet_string.rb', line 15

def self.encode(element, field)
  raise ArgumentError, 'Given element is not an element of the field.' if !field.include?(element)
  length = ECDSA.byte_length(field.prime)
  IntegerOctetString.encode(element, length)
end