Best Python code snippet using assertpy_python
test_BN256G2.py
Source:test_BN256G2.py
...28 (result[0], result[1]),29 (result[2], result[3]),30 (result[4], result[5])31 )32 def is_inf(pt1):33 return pt1[2] == (0, 0)34 def add(self, pt1, pt2):35 return contractWrapperJ._to_tuples(self.bn256g2._ECTwistAddJacobian(36 pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1], pt1[2][0], pt1[2][1],37 pt2[0][0], pt2[0][1], pt2[1][0], pt2[1][1], pt2[2][0], pt2[2][1]38 ))39 def double(self, pt1):40 return contractWrapperJ._to_tuples(self.bn256g2._ECTwistDoubleJacobian(41 pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1], pt1[2][0], pt1[2][1]42 ))43 def multiply(self, pt1, s):44 return contractWrapperJ._to_tuples(self.bn256g2._ECTwistMulJacobian(45 s,46 pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1], pt1[2][0], pt1[2][1]47 ))48 def eq(self, pt1, pt2):49 x1, y1, z1 = pt150 x2, y2, z2 = pt251 return (52 self.bn256g2._FQ2Mul(x1[0], x1[1], z2[0], z2[1]) == self.bn256g2._FQ2Mul(x2[0], x2[1], z1[0], z1[1]) and53 self.bn256g2._FQ2Mul(y1[0], y1[1], z2[0], z2[1]) == self.bn256g2._FQ2Mul(y2[0], y2[1], z1[0], z1[1])54 )55class contractWrapper(object):56 def __init__(self, bn256g2):57 self.bn256g2 = bn256g258 def _to_tuples(result):59 return (60 (result[0], result[1]),61 (result[2], result[3])62 )63 def is_inf(pt1):64 return pt1 == ((0, 0), (0, 0))65 def add(self, pt1, pt2):66 return contractWrapper._to_tuples(self.bn256g2.ECTwistAdd(67 pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1],68 pt2[0][0], pt2[0][1], pt2[1][0], pt2[1][1]69 ))70 def multiply(self, pt1, s):71 return contractWrapper._to_tuples(self.bn256g2.ECTwistMul(72 s,73 pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1]74 ))75 def eq(self, pt1, pt2):76 return pt1 == pt277 def is_on_curve(self, pt1):78 return self.bn256g2._isOnCurve(pt1[0][0], pt1[0][1], pt1[1][0], pt1[1][1])79class TestBN256G2(unittest.TestCase):80 def setUp(self):81 chain = tester.Chain()82 bn256g2 = chain.contract(open('BN256G2.sol').read().replace('internal', 'public'), language='solidity')83 self.contractJ = contractWrapperJ(bn256g2)84 self.contract = contractWrapper(bn256g2)85 def test_G2J(self):86 G2, eq, add, double, multiply, is_inf = G2J, self.contractJ.eq, self.contractJ.add, self.contractJ.double, self.contractJ.multiply, contractWrapperJ.is_inf87 self.assertTrue(eq(add(add(double(G2), G2), G2), double(double(G2))))88 self.assertFalse(eq(double(G2), G2))89 self.assertTrue(eq(add(multiply(G2, 9), multiply(G2, 5)), add(multiply(G2, 12), multiply(G2, 2))))90 self.assertTrue(is_inf(multiply(G2, CURVE_ORDER)))91 self.assertFalse(is_inf(multiply(G2, 2 * FIELD_MODULUS - CURVE_ORDER)))92 self.assertTrue(is_inf(add(multiply(G2, CURVE_ORDER), multiply(G2, CURVE_ORDER))))93 self.assertTrue(eq(add(multiply(G2, CURVE_ORDER), multiply(G2, 5)), multiply(G2, 5)))94 self.assertTrue(eq(add(multiply(G2, 5), multiply(G2, CURVE_ORDER)), multiply(G2, 5)))95 self.assertTrue(is_inf(multiply(multiply(G2, CURVE_ORDER), 1)))96 self.assertTrue(is_inf(multiply(multiply(G2, CURVE_ORDER), 2)))97 self.assertTrue(eq(G2J_inf, add(G2J_inf, G2J_inf)))98 self.assertTrue(eq(G2J, add(G2J, G2J_inf)))99 self.assertTrue(eq(G2J, add(G2J_inf, G2J)))100 def test_G2(self):101 eq, add, multiply, is_inf, is_on_curve = self.contract.eq, self.contract.add, self.contract.multiply, contractWrapper.is_inf, self.contract.is_on_curve102 self.assertTrue(eq(add(multiply(G2, 9), multiply(G2, 5)), add(multiply(G2, 12), multiply(G2, 2))))103 self.assertTrue(is_inf(multiply(G2, CURVE_ORDER)))104 self.assertFalse(is_inf(multiply(G2, 2 * FIELD_MODULUS - CURVE_ORDER)))105 self.assertTrue(is_on_curve(multiply(G2, 9)))106 self.assertTrue(is_inf(add(multiply(G2, CURVE_ORDER), multiply(G2, CURVE_ORDER))))107 self.assertTrue(eq(add(multiply(G2, CURVE_ORDER), multiply(G2, 5)), multiply(G2, 5)))108 self.assertTrue(eq(add(multiply(G2, 5), multiply(G2, CURVE_ORDER)), multiply(G2, 5)))109 self.assertTrue(is_inf(multiply(multiply(G2, CURVE_ORDER), 1)))110 self.assertTrue(is_inf(multiply(multiply(G2, CURVE_ORDER), 2)))111 self.assertTrue(eq(G2_inf, add(G2_inf, G2_inf)))112 self.assertTrue(eq(G2, add(G2, G2_inf)))113 self.assertTrue(eq(G2, add(G2_inf, G2)))114 def test_invalid_curves_G2(self):115 eq, add, multiply, is_inf, is_on_curve = self.contract.eq, self.contract.add, self.contract.multiply, contractWrapper.is_inf, self.contract.is_on_curve116 with self.assertRaises(tester.TransactionFailed) as e:117 add(multiply(G2, 9), ((1, 1), (1, 1)))118 with self.assertRaises(tester.TransactionFailed) as e:119 add(((1, 1), (1, 1)), multiply(G2, 9))120 with self.assertRaises(tester.TransactionFailed) as e:121 add(((0, 0), (0, 0)), ((1, 1), (1, 1)))122 with self.assertRaises(tester.TransactionFailed) as e:123 add(((1, 1), (1, 1)), ((0, 0), (0, 0)))124 with self.assertRaises(tester.TransactionFailed) as e:...
points.py
Source:points.py
1from builtins import classmethod2from HEC import *3from GF import *4from ring import *5class PointHEC():6 curve: HEC7 field: GF8 x: int9 y: int10 is_inf: bool11 def __init__(self, x, y, curve):12 self.is_inf = False13 self.curve = curve14 self.field = curve.field15 self.x = x % self.field.p16 self.y = y % self.field.p17 def __str__(self):18 if self.is_inf:19 point_str = 'point = Infinity'20 else:21 point_str = 'x = ' + str(self.x) + ' y = ' + str(self.y)22 return point_str + ' on ' + str(self.curve)23 def __eq__(self, other):24 if self.is_inf:25 return self.curve == other.curve and other.is_inf26 return self.curve == other.curve and self.x == other.x and self.y == other.y27 @classmethod28 def infinite_point(cls, curve):29 elem = cls(0, 0, curve)30 elem.is_inf = True31 return elem32 def is_finite(self):33 return not self.is_inf34 def opposite(self):35 if self.is_inf:36 return self37 x = self.x38 y = -self.y - poly_calc(x, self.curve.h[::-1])39 return PointHEC(x, y, self.curve)40 def belong_curve(self):41 if self.is_inf:42 return True43 x = self.x44 y = self.y45 return 0 == y^2 + y * poly_calc(x, self.curve.h[::-1]) - poly_calc(x, self.curve.f[::-1])46 def is_spacial(self):47 opposite = self.opposite()48 return self.x == opposite.x and self.y == opposite.y and self.is_inf == self.is_inf49 def is_ordinary(self):50 return not self.is_spacial()51class PointRing(PointHEC):52 def __init__(self, x, y, poly):53 super().__init__(x, y, poly.curve)54 self.poly = poly55 @classmethod56 def infinit_point(cls, poly):57 elem = cls(0, 0, poly)58 elem.is_inf = True59 return elem60 def __eq__(self, other):61 if self.is_inf:62 return self.poly == other.poly and other.is_inf63 return self.poly == other.poly and self.x == other.x and self.y == other.y64 def __str__(self):65 if self.is_inf:66 point_str = 'point = Infinity'67 else:68 point_str = 'x = ' + str(self.x) + ' y = ' + str(self.y)69 return point_str + '\nof' + str(self.poly) + '\non ' + str(self.curve)70 def is_zero(self):71 if self.is_inf:72 return self.poly.get_value_inf() == 073 return self.poly.get_value(self.x, self.y) == 074 def is_pole(self):75 if self.is_inf:76 return self.poly.get_value_inf() == 'Inf'77 return self.poly.get_value(self.x, self.y) == 'Inf'78 def ord(self):79 if self.is_inf:80 return -max(2*poly_degree(self.poly.a), 2*self.curve.genus + 1 + 2*poly_degree(self.poly.b))81 a_deriv = self.poly.a[::-1]82 b_deriv = self.poly.b[::-1]83 times = 084 while poly_degree(a_deriv) and poly_degree(b_deriv) and \85 poly_calc(self.x, a_deriv) == 0 and poly_calc(self.x, b_deriv) == 0:86 times = times + 187 a_deriv = deriv_poly(a_deriv)88 b_deriv = deriv_poly(b_deriv)89 r = times90 s = 091 if poly_calc(self.x, a_deriv) + self.y * poly_calc(self.x, b_deriv) == 0:92 norm_deriv = self.poly.norm()[::-1]93 while poly_degree(norm_deriv) and poly_calc(self.x, norm_deriv) == 0:94 s = s + 195 norm_deriv = deriv_poly(norm_deriv)96 if self.is_ordinary():97 return r + s98 else:99 return 2*r + s100class PointFrac(PointHEC):101 def __init__(self, x, y, frac):102 super().__init__(x, y, frac.curve)103 self.frac = frac104 @classmethod105 def infinit_point(cls, frac):106 elem = cls(0, 0, frac)107 elem.is_inf = True108 return elem109 def __eq__(self, other):110 if self.is_inf:111 return self.frac == other.frac and other.is_inf112 return self.frac == other.frac and self.x == other.x and self.y == other.y113 def __str__(self):114 if self.is_inf:115 point_str = 'point = Infinity'116 else:117 point_str = 'x = ' + str(self.x) + ' y = ' + str(self.y)118 return point_str + '\nof' + str(self.frac) + '\non ' + str(self.curve)119 def is_zero(self):120 if self.is_inf:121 return self.frac.get_value_inf() == 0122 return self.frac.get_value(self.x, self.y) == 0123 def is_pole(self):124 if self.is_inf:125 return self.frac.get_value_inf() == 'Inf'126 return self.frac.get_value(self.x, self.y) == 'Inf'127 def ord(self):128 return self.top.ord() - self.button.ord()129# curve = HEC(7, 4, [4, 1], [3, 3, 3, 3, 3,3, 3])130# pnt = PointHEC(32, -1200, curve)131# print("POIONT", pnt)132# print(pnt.is_spacial())133# point_inf = PointHEC.infinite_point(curve)134# print(point_inf)135# print(point_inf.is_spacial())136# elem = PolyRing([4, 0, 0], [1, 1, 0], curve)137# print (elem)138# point_ring = PointRing(0, 0, elem)139# point_ring_inf = PointRing.infinit_point(elem)140# print (point_ring)141# print(point_ring.ord())142# print(point_ring_inf.ord())143curve = HEC(7, 1, [0], [1, 0, 0, 2, 1, 3])144print(curve)145pnt = PointHEC(3, 1, curve)146print(pnt)147elem = PolyRing([4, 1], [1], curve)148print(elem)149point_ring = PointRing(0, 0, elem)150point_ring_inf = PointRing.infinit_point(elem)151print (point_ring)152print(point_ring.ord())...
ecp.py
Source:ecp.py
1from ecp_functional import add, sub, mul, on_curve2class ECP:3 def __init__(self, x, y, a, b, q, is_inf=False):4 '''5 Elliptic curve point in affine form with the curve expressed6 in Weierstrass form.7 '''8 self.x = x9 self.y = y10 self.a = a11 self.b = b12 self.q = q13 self.is_inf = is_inf14 def __add__(self, Q):15 px, py = self.x, self.y16 qx, qy = Q.x, Q.y17 a, b = self.a, self.b18 q = self.q19 x, y, is_inf = add(px, py, qx, qy, a, b, q)20 return ECP(x, y, a, b, q, is_inf)21 def __sub__(self, Q):22 px, py = self.x, self.y23 qx, qy = Q.x, Q.y24 a, b = self.a, self.b25 q = self.q26 x, y, is_inf = add(px, py, qx, -qy, a, b, q)27 return ECP(x, y, a, b, q, is_inf)28 def __mul__(self, k):29 x, y = self.x, self.y30 a, b = self.a, self.b31 q = self.q32 x, y, is_inf = mul(k, x, y, a, b, q)33 return ECP(x, y, a, b, q, is_inf)34 def __rmul__(self, k):35 return self.__mul__(k)36 def __lmul__(self, k):37 return self.__mul__(k)38 def __eq__(self, Q):39 if self.is_inf and Q.is_inf:40 return True41 return self.x == Q.x and self.y == Q.y and self.is_inf == Q.is_inf42 def on_curve(self):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!