def test_getattr(self): curves = ECurve._CurveNames() for c in curves: cva = ECurve(c) p = cva.p n = cva.n h = cva.h G = cva.G bits = cva.bits ctype = cva.ctype if ctype == 'ShortWeierstrass': a = cva.a b = cva.b cvb = ECurve.ShortWeierstrass(p, a, b, n, h, G[0], G[1], bits) elif ctype == 'Edwards': c = cva.c d = cva.d cvb = ECurve.Edwards(p, c, d, n, h, G[0], G[1], bits) elif ctype == 'Montgomery': B = cva.B A = cva.A cvb = ECurve.Montgomery(p, B, A, n, h, G[0], G[1], bits) else: self.assertEqual(ctype, "TwistedEdwards") a = cva.a d = cva.d cvb = ECurve.TwistedEdwards(p, a, d, n, h, G[0], G[1], bits) self.assertEqual(cva, cvb)
def _der_decode_curve(decoder): # curve description ensure_tag(decoder, asn1.Numbers.Sequence) decoder.enter() # field order ensure_tag(decoder, asn1.Numbers.OctetString) _, p = decoder.read() p = _bebytes_to_int(p) # curve type (enum) ensure_tag(decoder, asn1.Numbers.Enumerated) _, ctype = decoder.read() # curve equation coefficients ensure_tag(decoder, asn1.Numbers.Sequence) decoder.enter() ensure_tag(decoder, asn1.Numbers.OctetString) _, cp0 = decoder.read() cp0 = _bebytes_to_int(cp0) ensure_tag(decoder, asn1.Numbers.OctetString) _, cp1 = decoder.read() cp1 = _bebytes_to_int(cp1) decoder.leave() # curve order ensure_tag(decoder, asn1.Numbers.OctetString) _, n = decoder.read() n = _bebytes_to_int(n) # curve cofactor ensure_tag(decoder, asn1.Numbers.OctetString) _, h = decoder.read() h = _bebytes_to_int(h) # generator point ensure_tag(decoder, asn1.Numbers.Sequence) decoder.enter() ensure_tag(decoder, asn1.Numbers.OctetString) _, gx = decoder.read() gx = _bebytes_to_int(gx) ensure_tag(decoder, asn1.Numbers.OctetString) _, gy = decoder.read() gy = _bebytes_to_int(gy) decoder.leave() # curve field bitsize ensure_tag(decoder, asn1.Numbers.Integer) _, bits = decoder.read() decoder.leave() if ctype == 2: curve = ECurve.ShortWeierstrass(p, cp0, cp1, n, h, gx, gy, bits) elif ctype == 3: curve = ECurve.Edwards(p, cp0, cp1, n, h, gx, gy, bits) elif ctype == 4: curve = ECurve.Montgomery(p, cp0, cp1, n, h, gx, gy, bits) elif ctype == 5: curve = ECurve.TwistedEdwards(p, cp0, cp1, n, h, gx, gy, bits) else: raise ValueError('Unknown Curve Type') return curve
def test_parameters(self): cvws1 = ECurve('secp112r1') self.assertIsNotNone(cvws1) cvws2 = ECurve.ShortWeierstrass(0xDB7C2ABF62E35E668076BEAD208B, 0xDB7C2ABF62E35E668076BEAD2088, 0x659EF8BA043916EEDE8911702B22, 0xDB7C2ABF62E35E7628DFAC6561C5, 1, 0x09487239995A5EE76B55F9C2F098, 0xA89CE5AF8724C0A23E0E0FF77500, 112) self.assertIsNotNone(cvws2) self.assertEqual(cvws1, cvws2) # cved1 = ECurve('E-222') self.assertIsNotNone(cved1) cved2 = ECurve.Edwards( 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffff8b, 1, 160102, 0xffffffffffffffffffffffffffff70cbc95e932f802f31423598cbf, 4, 0x19b12bb156a389e55c9768c303316d07c23adab3736eb2bc3eb54e51, 0x1c, 222) self.assertIsNotNone(cved2) self.assertEqual(cved1, cved2) # cvmo1 = ECurve('M-221') self.assertIsNotNone(cvmo1) cvmo2 = ECurve.Montgomery( 0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffd, 1, 117050, 0x40000000000000000000000000015a08ed730e8a2f77f005042605b, 8, 0x4, 0xf7acdd2a4939571d1cef14eca37c228e61dbff10707dc6c08c5056d, 221) self.assertIsNotNone(cvmo2) self.assertEqual(cvmo1, cvmo2) # cvte1 = ECurve('Ed25519') self.assertIsNotNone(cvte1) cvte2 = ECurve.TwistedEdwards( 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEC, 0x52036CEE2B6FFE738CC740797779E89800700A4D4141D8AB75EB4DCA135978A3, 0x1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED, 8, 0x216936D3CD6E53FEC0A4E231FDD6DC5C692CC7609525A7B2C9562D608F25D51A, 0x6666666666666666666666666666666666666666666666666666666666666658, 255) self.assertIsNotNone(cvte2) self.assertEqual(cvte1, cvte2)