class ECC::FiniteField::Element
Attributes
num[R]
base functionality
Public Class Methods
[]( num )
click to toggle source
# File lib/elliptic-lite/field.rb, line 68 def self.[]( num ) new( num ) end
add( a, b )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# File lib/elliptic-lite/field.rb, line 37 def self.include?( num ) num >=0 && num < prime end
mul( a, b )
click to toggle source
# File lib/elliptic-lite/field.rb, line 49 def self.mul( a, b ) ( a * b ) % prime end
new( num )
click to toggle source
# 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
pow( a, exponent )
click to toggle source
# 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 )
click to toggle source
# File lib/elliptic-lite/field.rb, line 45 def self.sub( a, b ) ( a - b ) % prime end
Public Instance Methods
==(other)
click to toggle source
# 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 )
click to toggle source
# 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
Also aliased as: +
div( other )
click to toggle source
# 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
Also aliased as: /
inspect()
click to toggle source
# File lib/elliptic-lite/field.rb, line 84 def inspect "#{self.class.name}(#{@num})" end
mul( other )
click to toggle source
# 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
Also aliased as: *
pow( exponent )
click to toggle source
# File lib/elliptic-lite/field.rb, line 128 def pow( exponent ) num = self.class.pow( @num, exponent ) self.class.new( num ) end
Also aliased as: **
prime()
click to toggle source
# File lib/elliptic-lite/field.rb, line 82 def prime() self.class.prime; end
prime?( other )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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
Also aliased as: -