Class: EllipticCurve::Curve::CurveFp

Inherits:
Object
  • Object
show all
Defined in:
lib/curve.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b, p, n, gx, gy, name, oid, nistName = nil) ⇒ CurveFp

Returns a new instance of CurveFp.



11
12
13
14
15
16
17
18
19
20
# File 'lib/curve.rb', line 11

def initialize(a, b, p, n, gx, gy, name, oid, nistName=nil)
    @a = a
    @b = b
    @p = p
    @n = n
    @g = Point.new(gx, gy)
    @name = name
    @oid = oid
    @nistName = nistName
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



9
10
11
# File 'lib/curve.rb', line 9

def a
  @a
end

#bObject

Returns the value of attribute b.



9
10
11
# File 'lib/curve.rb', line 9

def b
  @b
end

#gObject

Returns the value of attribute g.



9
10
11
# File 'lib/curve.rb', line 9

def g
  @g
end

#nObject

Returns the value of attribute n.



9
10
11
# File 'lib/curve.rb', line 9

def n
  @n
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/curve.rb', line 9

def name
  @name
end

#nistNameObject

Returns the value of attribute nistName.



9
10
11
# File 'lib/curve.rb', line 9

def nistName
  @nistName
end

#oidObject

Returns the value of attribute oid.



9
10
11
# File 'lib/curve.rb', line 9

def oid
  @oid
end

#pObject

Returns the value of attribute p.



9
10
11
# File 'lib/curve.rb', line 9

def p
  @p
end

Instance Method Details

#contains(p) ⇒ Object



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

def contains(p)
    # Verify if the point `p` is on the curve
    # :param p: point p = Point(x, y)
    # :return: boolean
    if not (0 <= p.x and p.x <= @p - 1)
        return false
    end
    if not (0 <= p.y and p.y <= @p - 1)
        return false
    end
    if (p.y ** 2 - (p.x ** 3 + @a * p.x + @b)) % @p != 0
        return false
    end
    return true
end

#lengthObject



38
39
40
# File 'lib/curve.rb', line 38

def length
    return (1 + ("%x" % @n).length).div(2)
end

#y(x, isEven) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/curve.rb', line 42

def y(x, isEven)
    ySquared = (x.pow(3, @p) + @a * x + @b) % @p
    y = Math::modularSquareRoot(ySquared, @p)
    if isEven != (y % 2 == 0)
        y = @p - y
    end
    return y
end