Class: JOSE::JWA::Edwards25519Point

Inherits:
EdwardsPoint show all
Defined in:
lib/jose/jwa/edwards_point.rb

Overview

A point on Edwards25519.

Constant Summary collapse

BASE_FIELD =

Create a new point on curve.

JOSE::JWA::FieldElement.new(1, (2**255)-19).freeze
D =
(-BASE_FIELD.make(121665)/BASE_FIELD.make(121666)).freeze
F0 =
BASE_FIELD.make(0).freeze
F1 =
BASE_FIELD.make(1).freeze
XB =
BASE_FIELD.make(15112221349535400772501151409588531511454012693041857206046113283949847762202).freeze
YB =
BASE_FIELD.make(46316835694926478169428394003475163141307993866256225615783033603165251855960).freeze
L =

Order of basepoint.

7237005577332262213973186563042994240857116359379907606001950938285454250989
C =

The logarithm of cofactor.

3
N =

The highest set bit

254
B =

The coding length

256

Instance Attribute Summary collapse

Attributes inherited from EdwardsPoint

#x, #y, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EdwardsPoint

#*, #<=>, #decode_base, #encode_base, #initpoint

Constructor Details

#initialize(x, y) ⇒ Edwards25519Point

Returns a new instance of Edwards25519Point.

Raises:

  • (ArgumentError)


94
95
96
97
98
99
# File 'lib/jose/jwa/edwards_point.rb', line 94

def initialize(x, y)
  # Check the point is actually on the curve.
  raise ArgumentError, "Invalid point" if y*y-x*x != F1+D*x*x*y*y
  initpoint(x, y)
  @t = x*y
end

Instance Attribute Details

#tObject

Returns the value of attribute t.



87
88
89
# File 'lib/jose/jwa/edwards_point.rb', line 87

def t
  @t
end

Class Method Details

.stdbaseObject

The standard base point.



90
91
92
# File 'lib/jose/jwa/edwards_point.rb', line 90

def self.stdbase
  return new(XB, YB)
end

Instance Method Details

#+(y) ⇒ Object

Point addition.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jose/jwa/edwards_point.rb', line 131

def +(y)
  # The formulas are from EFD.
  tmp = zero_elem
  zcp = @z * y.z
  a = (@y - @x) * (y.y - y.x)
  b = (@y + @x) * (y.y + y.x)
  c = (D + D) * @t * y.t
  d = zcp + zcp
  e, h = b - a, b + a
  f, g = d - c, d + c
  tmp.x, tmp.y, tmp.z, tmp.t = e * f, g * h, f * g, e * h
  return tmp
end

#decode(s) ⇒ Object

Decode a point representation.



102
103
104
105
106
# File 'lib/jose/jwa/edwards_point.rb', line 102

def decode(s)
  x, y = decode_base(s, B)
  return nil if x.nil?
  return JOSE::JWA::Edwards25519Point.new(x, y)
end

#doubleObject

Point doubling.



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/jose/jwa/edwards_point.rb', line 146

def double
  # The formulas are from EFD.
  tmp = zero_elem
  x1s, y1s, z1s = @x * @x, @y * @y, @z * @z
  xys = @x + @y
  h = -(x1s + y1s)
  e = xys * xys + h
  g = y1s - x1s
  f = g - (z1s + z1s)
  tmp.x, tmp.y, tmp.z, tmp.t = e * f, g * h, f * g, e * h
  return tmp
end

#encodeObject

Encode a point representation.



109
110
111
# File 'lib/jose/jwa/edwards_point.rb', line 109

def encode
  return encode_base(B)
end

#inspectObject



159
160
161
162
163
164
# File 'lib/jose/jwa/edwards_point.rb', line 159

def inspect
  "\n{#{@x.x},\n"\
  " #{@y.x},\n"\
  " #{@z.x},\n"\
  " #{@t.x}}"
end

#normalizeObject



113
114
115
116
117
118
# File 'lib/jose/jwa/edwards_point.rb', line 113

def normalize
  xp, yp, zp = @x / @z, @y / @z, @z / @z
  tmp = zero_elem
  tmp.x, tmp.y, tmp.z, tmp.t = xp, yp, zp, xp * yp
  return tmp
end

#solve_x2(y) ⇒ Object

Solve for x^2.



126
127
128
# File 'lib/jose/jwa/edwards_point.rb', line 126

def solve_x2(y)
  return ((y*y-F1)/(D*y*y+F1))
end

#zero_elemObject

Construct neutral point on this curve.



121
122
123
# File 'lib/jose/jwa/edwards_point.rb', line 121

def zero_elem
  return JOSE::JWA::Edwards25519Point.new(F0, F1)
end