Class: Rb25519::FField::EC
- Inherits:
-
Object
- Object
- Rb25519::FField::EC
- Defined in:
- lib/rb-pure25519.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
Instance Method Summary collapse
-
#initialize(field, coeffs = nil) ⇒ EC
constructor
A new instance of EC.
- #naive_points ⇒ Object
- #on_curve(x, y) ⇒ Object
- #point_add(point_a, point_b) ⇒ Object
- #scale_double_add(k, point_a) ⇒ Object
- #scale_naive(k, point_a) ⇒ Object
Constructor Details
#initialize(field, coeffs = nil) ⇒ EC
Returns a new instance of EC.
332 333 334 335 |
# File 'lib/rb-pure25519.rb', line 332 def initialize(field, coeffs=nil) @coeffs = coeffs @field = field end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
331 332 333 |
# File 'lib/rb-pure25519.rb', line 331 def field @field end |
Instance Method Details
#naive_points ⇒ Object
341 342 343 344 345 346 347 348 349 |
# File 'lib/rb-pure25519.rb', line 341 def naive_points points = [ECInfinity] @field.p.times do |x| @field.p.times do |y| points << [x,y] if on_curve(x,y) end end points end |
#on_curve(x, y) ⇒ Object
337 338 339 |
# File 'lib/rb-pure25519.rb', line 337 def on_curve(x,y) raise NotImplementedError.new end |
#point_add(point_a, point_b) ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/rb-pure25519.rb', line 351 def point_add(point_a, point_b) xa = point_a[0].kind_of?(FFieldValue) ? point_a[0] : @field[point_a[0]] xb = point_b[0].kind_of?(FFieldValue) ? point_b[0] : @field[point_b[0]] ya = point_a[1].kind_of?(FFieldValue) ? point_a[1] : @field[point_a[1]] yb = point_b[1].kind_of?(FFieldValue) ? point_b[1] : @field[point_b[1]] if xa == xb and ya == yb return double_point(point_a) end #puts "point_add: #{point_a.inspect} + #{point_b.inspect}" # All the following operations are in F_p (eg, "mod p") s = (yb - ya) / (xb - xa) #puts "Slope: #{s}" xc = s**2 - xa - xb yc = (ya * -1) + (xa - xc) * s [xc, yc] end |
#scale_double_add(k, point_a) ⇒ Object
384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/rb-pure25519.rb', line 384 def scale_double_add(k, point_a) t = point_a bits = k.bit_length (bits-1).times.to_a.reverse.each do |bit| t = point_add( t, t ) if (k >> bit) & 0x1 == 1 t = point_add(t, point_a) end end t end |
#scale_naive(k, point_a) ⇒ Object
374 375 376 377 378 379 380 381 382 |
# File 'lib/rb-pure25519.rb', line 374 def scale_naive(k, point_a) point = point_a (k-1).times do point = point_add(point, point_a) end point end |