def test_point_decode_fail(self): curve = CurveDB().instantiate(name="sect113r1") with self.assertRaises(InvalidInputException): curve.decode_point(bytes.fromhex("04 0011 2233")) with self.assertRaises(UnsupportedEncodingException): curve.decode_point(bytes.fromhex("02 0011 2233"))
def test_point_decode(self): curve = CurveDB().instantiate(name="sect113r1") decoded_point = curve.decode_point( bytes.fromhex( "04 009d73616f35f4ab1407d73562c10f 00a52830277958ee84d1315ed31886" )) self.assertEqual(decoded_point, curve.G)
def from_subject_pubkey_info(cls, pk_alg, params_asn1, pubkey_data): params = ASN1Tools.safe_decode(params_asn1, asn1_spec=ECParameters()) accessible_parameters = {} if params.asn1 is not None: accessible_parameters["curve_source"] = params.asn1.getName() if accessible_parameters["curve_source"] == "namedCurve": # Named curve curve_oid = OID.from_asn1(params.asn1.getComponent()) curve = CurveDB().instantiate(oid=curve_oid) accessible_parameters.update({ "curve_oid": curve_oid, "curve": curve, }) elif accessible_parameters["curve_source"] == "specifiedCurve": # Explicit curve or implicit curve curve = EllipticCurve.from_asn1(params.asn1.getComponent()) accessible_parameters.update({ "curve": curve, }) else: # Implicit curve pass if accessible_parameters.get("curve") is not None: pk_point = curve.decode_point(pubkey_data) accessible_parameters.update({ "x": pk_point.x, "y": pk_point.y, }) else: pk_point = None return cls(accessible_parameters=accessible_parameters, decoding_details=[params, pk_point])
def from_subject_pubkey_info(cls, pk_alg, params_asn1, pubkey_data): curve = CurveDB().instantiate(oid=pk_alg.value.oid) pk_point = curve.decode_point(pubkey_data) accessible_parameters = dict(pk_alg.value.fixed_params) accessible_parameters.update({ "x": pk_point.x, "y": pk_point.y, "curve": curve, "point": pk_point, "curve_source": "namedCurve", }) return cls(accessible_parameters=accessible_parameters, decoding_details=[pk_point])
def test_point_decode_ed25519(self): curve = CurveDB().instantiate(name="ed25519") Q = curve.decode_point(bytes(32)) self.assertEqual( Q.x, 0x2b8324804fc1df0b2b4d00993dfbd7a72f431806ad2fe478c4ee1b274a0ea0b0) self.assertEqual(Q.y, 0) self.assertTrue(Q.on_curve()) self.assertEqual(Q.encode(), bytes(32)) Q = curve.decode_point( bytes.fromhex( "0000000000000000000000000000000000000000000000000000000000000080" )) self.assertEqual( Q.x, 0x547cdb7fb03e20f4d4b2ff66c2042858d0bce7f952d01b873b11e4d8b5f15f3d) self.assertEqual(Q.y, 0) self.assertTrue(Q.on_curve()) self.assertEqual( Q.encode(), bytes.fromhex( "0000000000000000000000000000000000000000000000000000000000000080" )) Q = curve.decode_point( bytes.fromhex( "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" )) self.assertEqual( Q.x, 0x55d0e09a2b9d34292297e08d60d0f620c513d47253187c24b12786bd777645ce) self.assertEqual( Q.y, 0x1a5107f7681a02af2523a6daf372e10e3a0764c9d3fe4bd5b70ab18201985ad7) self.assertTrue(Q.on_curve()) self.assertEqual( Q.encode(), bytes.fromhex( "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" )) Q = curve.decode_point( bytes.fromhex( "dfc9425e4f968f7f0c29f0259cf5f9aed6851c2bb4ad8bfb860cfee0ab248292" )) self.assertEqual( Q.x, 0x3493c89a1d42961795326fb77ddda9b1073eb50954eec3acc573cd718bed3093) self.assertEqual( Q.y, 0x128224abe0fe0c86fb8badb42b1c85d6aef9f59c25f0290c7f8f964f5e42c9df) self.assertTrue(Q.on_curve()) self.assertEqual( Q.encode(), bytes.fromhex( "dfc9425e4f968f7f0c29f0259cf5f9aed6851c2bb4ad8bfb860cfee0ab248292" )) Q = curve.decode_point( bytes.fromhex( "dcbfc4d2bd9b5b9b3f7cd673cf559fe3793946a6a904355c07a552991bdba7c5" )) self.assertEqual( Q.x, 0x5c71bfc23d23bb896be916c12e2b02aa5d22c1883ac097fe5ab604aa52020ec9) self.assertEqual( Q.y, 0x45a7db1b9952a5075c3504a9a6463979e39f55cf73d67c3f9b5b9bbdd2c4bfdc) self.assertTrue(Q.on_curve()) with self.assertRaises(InvalidInputException): curve.decode_point( bytes.fromhex( "0305334e381af78f141cb666f6199f57bc3495335a256a95bd2a55bf546663f6" ))