Danger
Nothing here should be used for any security purposes.
Elliptic curves#
This module is imported with:
import toy_crypto.ec
I wrote this for the sole purposes of
Providing a working context to illustrate the double-and-add algorithm in the
Point.scaler_multiply()
method.Doing calculations over floats that I could use for diagrams. (That code has been removed.)
from toy_crypto.ec import Curve
from toy_crypto.nt import Modulus
# Example curve from Serious Cryptography
curve = Curve(-4, 0, 191)
assert str(curve) == "y^2 = x^3 - 4x + 0 (mod 191)"
# set a generator (base-point), G
G = curve.point(146, 131)
assert G.on_curve() is True
five_G = G.scaler_multiply(5)
assert five_G.x == 61
assert five_G.y == 163
The ec
classes#
- class toy_crypto.ec.Curve(a: int, b: int, p: int) None [source]#
Define a curve of the form \(y^2 = x^3 + ax + b \pmod p\).
- Parameters:
a (
int
)b (
int
)p (
int
)
- property a: int#
The ‘a’ of \(y^2 = x^3 + ax + b \pmod p\).
- property b: int#
The ‘b’ of \(y^2 = x^3 + ax + b \pmod p\).
- compute_y(x: int) tuple[int, int] | None [source]#
Returns pair of y values for x on curve. None otherwise.
- Parameters:
x (
int
)- Return type:
Optional
[tuple
[int
,int
]]
- property order: int#
The order of the group.
- class toy_crypto.ec.Point(x: int, y: int, curve: Curve) None [source]#
Point on elliptic curve over finite field.
- Parameters:
x (
int
)y (
int
)curve (
Curve
)
Create a mutable point on a curve.
- Parameters:
x (
int
)y (
int
)curve (
Curve
)
- iadd(Q: Self) Self [source]#
add point to self in place.
The Point at Infinity is not mutable. But you can make a mutable copy with
curve.PAI.cp()
. Or you could just usePoint.add()
.- Raises:
ValueError – if Q is not on its own curve
ValueError – if Q and self are on different curves
NotImplementedError – if self is not a mutable point.
- Parameters:
self (
Self
)Q (
Self
)
- Return type:
Self
- idouble() Self [source]#
Double point in place.
The Point at Infinity is not mutable. But you can make a mutable copy with
curve.PAI.cp()
or just usePoint.double()
.- Raises:
NotImplementedError – if self is not a mutable point.
- Return type:
Self
- property is_zero: bool#
True if point at infinity
- neg() Self [source]#
Return additive inverse.
- Returns:
Additive inverse
- Return type:
- Parameters:
self (
Self
)