Module: BLS::Field

Included in:
Fp, Fr
Defined in:
lib/bls/field.rb

Overview

Finite field

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
# File 'lib/bls/field.rb', line 67

def ==(other)
  return false unless other.is_a?(self.class)
  value == other.value
end

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



31
32
33
# File 'lib/bls/field.rb', line 31

def add(other)
  self.class.new(value + other.value)
end

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



57
58
59
60
# File 'lib/bls/field.rb', line 57

def div(other)
  v = other.is_a?(Field) ? other.invert : self.class.new(other).invert
  multiply(v)
end

#invertObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bls/field.rb', line 16

def invert
  x0 = 1
  x1 = 0
  y0 = 0
  y1 = 1
  a = self.class.const_get(:ORDER)
  b = value
  until a.zero?
    q, b, a = [b / a, a, b % a]
    x0, x1 = [x1, x0 - q * x1]
    y0, y1 = [y1, y0 - q * y1]
  end
  self.class.new(x0)
end

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



51
52
53
54
# File 'lib/bls/field.rb', line 51

def multiply(other)
  v = other.is_a?(Field) ? other.value : other
  self.class.new(value * v)
end

#negateObject



12
13
14
# File 'lib/bls/field.rb', line 12

def negate
  self.class.new(-value)
end

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



40
41
42
43
# File 'lib/bls/field.rb', line 40

def pow(n)
  v = value.pow(n, self.class.const_get(:ORDER))
  self.class.new(v)
end

#squareObject



36
37
38
# File 'lib/bls/field.rb', line 36

def square
  self.class.new(value**2)
end

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



46
47
48
# File 'lib/bls/field.rb', line 46

def subtract(other)
  self.class.new(value - other.value)
end

#to_sObject



63
64
65
# File 'lib/bls/field.rb', line 63

def to_s
  value.to_s
end

#zero?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/bls/field.rb', line 8

def zero?
  value.zero?
end