Class: JOSE::JWA::EdwardsPoint

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/jose/jwa/edwards_point.rb

Overview

A point on (twisted) Edwards curve.

Direct Known Subclasses

Edwards25519Point, Edwards448Point

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xObject

Returns the value of attribute x.



5
6
7
# File 'lib/jose/jwa/edwards_point.rb', line 5

def x
  @x
end

#yObject

Returns the value of attribute y.



5
6
7
# File 'lib/jose/jwa/edwards_point.rb', line 5

def y
  @y
end

#zObject

Returns the value of attribute z.



5
6
7
# File 'lib/jose/jwa/edwards_point.rb', line 5

def z
  @z
end

Instance Method Details

#*(x) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jose/jwa/edwards_point.rb', line 43

def *(x)
  r = zero_elem
  s = self
  while x > 0
    if (x % 2) > 0
      r = r + s
    end
    s = s.double
    x = x / 2
  end
  return r
end

#<=>(y) ⇒ Object

Check two points are equal.



57
58
59
60
61
62
63
64
65
66
# File 'lib/jose/jwa/edwards_point.rb', line 57

def <=>(y)
  # Need to check x1/z1 == x2/z2 and similarly for y, so cross-
  # multiply to eliminate divisions.
  xn1 = @x * y.z
  xn2 = y.x * @z
  yn1 = @y * y.z
  yn2 = y.y * @z
  return yn1 <=> yn2 if xn1 == xn2
  return xn1 <=> xn2
end

#decode_base(s, b) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jose/jwa/edwards_point.rb', line 13

def decode_base(s, b)
  # Check that point encoding is of correct length.
  raise ArgumentError, "s must be #{(b/8)} bytes" if s.bytesize != (b / 8)
  # Extract signbit.
  s = s.dup
  xs = s.getbyte((b-1)/8) >> ((b-1) & 7)
  s.setbyte((b-1)/8, s.getbyte((b-1)/8) & ~(1 << 7))
  # Decode y. If this fails, fail.
  y = self.class::BASE_FIELD.from_bytes(s, b)
  # Try to recover x. If it does not exist, or is zero and xs is
  # wrong, fail.
  x = solve_x2(y).sqrt
  raise ArgumentError, "decode error" if x.nil? or (x.zero? and xs != x.sign)
  # If sign of x isn't correct, flip it.
  x = -x if x.sign != xs
  # Return the constructed point.
  return x, y
end

#encode_base(b) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/jose/jwa/edwards_point.rb', line 32

def encode_base(b)
  xp, yp = @x / @z, @y / @z
  # Encode y.
  s = yp.to_bytes(b)
  # Add sign bit of x to encoding.
  if xp.sign != 0
    s.setbyte((b-1)/8, s.getbyte((b-1)/8) | (1 << ((b-1) % 8)))
  end
  return s
end

#initpoint(x, y) ⇒ Object



7
8
9
10
11
# File 'lib/jose/jwa/edwards_point.rb', line 7

def initpoint(x, y)
  @x = x
  @y = y
  @z = self.class::BASE_FIELD.make(1)
end