Class: H2C::M2C::SSWU

Inherits:
Object
  • Object
show all
Defined in:
lib/h2c/m2c/sswu.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curve, z) ⇒ SSWU

Constructor



12
13
14
15
16
17
18
# File 'lib/h2c/m2c/sswu.rb', line 12

def initialize(curve, z)
  @curve = curve
  @z = z
  f = curve.field
  @c1 = f.mod(-curve.param_b * f.inverse(curve.param_a))
  @c2 = f.mod(-f.inverse(z))
end

Instance Attribute Details

#c1Object (readonly)

Returns the value of attribute c1.



7
8
9
# File 'lib/h2c/m2c/sswu.rb', line 7

def c1
  @c1
end

#c2Object (readonly)

Returns the value of attribute c2.



7
8
9
# File 'lib/h2c/m2c/sswu.rb', line 7

def c2
  @c2
end

#curveObject (readonly)

Returns the value of attribute curve.



7
8
9
# File 'lib/h2c/m2c/sswu.rb', line 7

def curve
  @curve
end

#zObject (readonly)

Returns the value of attribute z.



7
8
9
# File 'lib/h2c/m2c/sswu.rb', line 7

def z
  @z
end

Instance Method Details

#map(u) ⇒ Array(Integer, Integer)

Outputs x and y are elements of the field F.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/h2c/m2c/sswu.rb', line 23

def map(u)
  f = curve.field
  t1 = f.mod(f.power(u, 2) * f.mod(z))
  t2 = f.power(t1, 2)
  x1 = f.mod(t1 + t2)
  e1 = x1.zero?
  # inv0(x) is the multiplicative inverse of x, with inv0(0) = 0.
  x1 = e1 ? 0 : f.inverse(x1)
  x1 = f.mod(x1 + 1)
  x1 = e1 ? c2 : x1
  x1 = f.mod(x1 * c1)
  gx1 = f.power(x1, 2)
  gx1 = f.mod(gx1 + curve.param_a)
  gx1 = f.mod(gx1 * x1)
  gx1 = f.mod(gx1 + curve.param_b)
  x2 = f.mod(t1 * x1)
  t2 = f.mod(t1 * t2)
  gx2 = f.mod(gx1 * t2)
  e2 = square?(gx1)
  x = e2 ? x1 : x2
  y2 = e2 ? gx1 : gx2
  y = f.square_roots(y2)[0]
  e3 = sgn0(u) == sgn0(y)
  y = f.mod(e3 ? y : -y)
  curve.new_point([x, y])
end

#sgn0(x) ⇒ Object



55
56
57
58
# File 'lib/h2c/m2c/sswu.rb', line 55

def sgn0(x)
  res = x % 2
  curve.field.mod(1 - 2 * res)
end

#square?(x) ⇒ Boolean



50
51
52
53
# File 'lib/h2c/m2c/sswu.rb', line 50

def square?(x)
  test = curve.field.power(x, (curve.field.prime - 1) / 2)
  [0, 1].include?(test)
end