Class: ECC::FiniteField::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/elliptic-lite/field.rb

Overview

FiniteFiledElement base class

Direct Known Subclasses

S256Field

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num) ⇒ Element

Returns a new instance of Element.

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
# File 'lib/elliptic-lite/field.rb', line 74

def initialize( num )
  raise ArgumentError, "number #{num} not in finite field range 0 to #{self.class.prime}"   unless self.class.include?( num )

  @num  = num
  self.freeze   ## make "immutable"
  self
end

Instance Attribute Details

#numObject (readonly)

base functionality



34
35
36
# File 'lib/elliptic-lite/field.rb', line 34

def num
  @num
end

Class Method Details

.[](num) ⇒ Object



68
69
70
# File 'lib/elliptic-lite/field.rb', line 68

def self.[]( num )
  new( num )
end

.add(a, b) ⇒ Object

note: assumes integer as arguments values



41
42
43
# File 'lib/elliptic-lite/field.rb', line 41

def self.add( a, b )  ## note: assumes integer as arguments values
  ( a + b ) % prime
end

.div(a, b) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/elliptic-lite/field.rb', line 58

def self.div( a, b )
  # use Fermat's little theorem:
  #      self.num ** (prime-1) % prime == 1
  #  this means:
  #      1/num == num.pow( prime-2, prime )
  ( a * b.pow( prime-2, prime )) % prime
end

.include?(num) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/elliptic-lite/field.rb', line 37

def self.include?( num )
   num >=0 && num < prime
end

.mul(a, b) ⇒ Object



49
50
51
# File 'lib/elliptic-lite/field.rb', line 49

def self.mul( a, b )
  ( a * b ) % prime
end

.pow(a, exponent) ⇒ Object



53
54
55
56
# File 'lib/elliptic-lite/field.rb', line 53

def self.pow( a, exponent )
  n = exponent % ( prime - 1 )   # note: make possible negative exponent ALWAYS positive
  a.pow( n, prime ) % prime
end

.sub(a, b) ⇒ Object



45
46
47
# File 'lib/elliptic-lite/field.rb', line 45

def self.sub( a, b )
  ( a - b ) % prime
end

Instance Method Details

#==(other) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/elliptic-lite/field.rb', line 99

def ==(other)
  if other.is_a?( Element ) && prime?( other )
    @num == other.num
  else
    false
  end
end

#add(other) ⇒ Object Also known as: +



107
108
109
110
111
112
# File 'lib/elliptic-lite/field.rb', line 107

def add( other )
  require_prime!( other )

  num = self.class.add( @num, other.num )
  self.class.new( num )
end

#div(other) ⇒ Object Also known as: /



133
134
135
136
137
138
# File 'lib/elliptic-lite/field.rb', line 133

def div( other )
  require_prime!( other )

  num = self.class.div( @num, other.num )
  self.class.new( num )
end

#inspectObject



84
85
86
# File 'lib/elliptic-lite/field.rb', line 84

def inspect
  "#{self.class.name}(#{@num})"
end

#mul(other) ⇒ Object Also known as: *



121
122
123
124
125
126
# File 'lib/elliptic-lite/field.rb', line 121

def mul( other )
  require_prime!( other )

  num = self.class.mul( @num, other.num )
  self.class.new( num )
end

#pow(exponent) ⇒ Object Also known as: **



128
129
130
131
# File 'lib/elliptic-lite/field.rb', line 128

def pow( exponent )
  num = self.class.pow( @num, exponent )
  self.class.new( num )
end

#primeObject

convenience helper



82
# File 'lib/elliptic-lite/field.rb', line 82

def prime() self.class.prime; end

#prime?(other) ⇒ Boolean

check for matching prime

Returns:

  • (Boolean)


90
91
92
# File 'lib/elliptic-lite/field.rb', line 90

def prime?( other )  ## check for matching prime
  self.class.prime == other.class.prime
end

#require_prime!(other) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
# File 'lib/elliptic-lite/field.rb', line 94

def require_prime!( other )
  raise ArgumentError, "cannot operate on different finite fields; expected #{self.class.prime} got #{other.class.prime}"  unless prime?( other )
end

#sub(other) ⇒ Object Also known as: -



114
115
116
117
118
119
# File 'lib/elliptic-lite/field.rb', line 114

def sub( other )
  require_prime!( other )

  num = self.class.sub( @num, other.num )
  self.class.new( num )
end