`import std/[strformat, options] import math type TypeError = object of ValueError type Point* = object x,y,a,b: int func initPoint(x,y,a,b:int):Point = result.x = x result.y = y result.a = a result.b = b if some(x).isNone and some(y).isNone: return if not(y^2 == x ^ 3 + a*x+b): raise newException(ValueError, fmt"({x}, {y}) is not on the curve") proc `==`(x, other:Point):bool = return x.x == other.x and x.y == other.y and x.a == other.a and x.b == other.b proc `!=`(x,other:Point):bool = return not(x.x == other.x) and not(x.y == other.y) and not(x.a == other.a) and not(x.b == other.b) proc `+`(x, other:Point): Point = if x.a != other.a or x.b != other.b: raise newException(TypeError, fmt"Points {x}, {other} are not on the same curve") #if some(x.x).isNone: if option[Point](nil).isNone: return other if some(other.x).isNone: return x var b = initPoint(-1,-1,5,7) echo b var p1 = initPoint(-1,1, 5,7) var infi = initPoint(5, 7) echo infi + p1 `
Run