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, glvParams = nil) ⇒ CurveFp

Returns a new instance of CurveFp.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/curve.rb', line 12

def initialize(a, b, p, n, gx, gy, name, oid, nistName=nil, glvParams=nil)
    @a = a
    @b = b
    @p = p
    @n = n
    @nBitLength = n.bit_length
    @g = Point.new(gx, gy)
    @name = name
    @oid = oid
    @nistName = nistName
    # GLV endomorphism parameters (only for curves that support one,
    # e.g. secp256k1). nil means no endomorphism; fall back to Shamir+JSF.
    @glvParams = glvParams
    @_generatorPowersTable = nil
end

Instance Attribute Details

#_generatorPowersTableObject

Returns the value of attribute _generatorPowersTable.



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

def _generatorPowersTable
  @_generatorPowersTable
end

#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

#glvParamsObject

Returns the value of attribute glvParams.



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

def glvParams
  @glvParams
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

#nBitLengthObject

Returns the value of attribute nBitLength.



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

def nBitLength
  @nBitLength
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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/curve.rb', line 28

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



44
45
46
# File 'lib/curve.rb', line 44

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

#y(x, isEven) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/curve.rb', line 48

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