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



75
76
77
78
# File 'lib/bls/field.rb', line 75

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

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



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

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

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



65
66
67
68
# File 'lib/bls/field.rb', line 65

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

#invertBLS::Field

Multiplicative inverse. Zero has none, and the extended Euclid below returns 0 for it rather than failing, which is a wrong answer that propagates: ProjectivePoint#to_affine inverts z, so the point at infinity would come back as the affine coordinates (0, 0) instead of being refused.

Returns:

Raises:

  • (BLS::Error)

    Occur when this element is zero.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/bls/field.rb', line 22

def invert
  raise BLS::Error, 'Zero has no multiplicative inverse.' if zero?

  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: *



59
60
61
62
# File 'lib/bls/field.rb', line 59

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: **



48
49
50
51
# File 'lib/bls/field.rb', line 48

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

#squareObject



44
45
46
# File 'lib/bls/field.rb', line 44

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

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



54
55
56
# File 'lib/bls/field.rb', line 54

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

#to_sObject



71
72
73
# File 'lib/bls/field.rb', line 71

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