From c0782a26969062cd65679b4f03a91c4bb6d1983a Mon Sep 17 00:00:00 2001 From: jMyles Date: Fri, 26 Jan 2018 21:37:01 -0800 Subject: [PATCH] Single from_bytes method. See https://github.com/nucypher/pyUmbral/pull/24#discussion_r162469880 --- tests/test_umbral.py | 6 +++--- umbral/umbral.py | 29 +++++++++++------------------ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/tests/test_umbral.py b/tests/test_umbral.py index dc3d69d7..74e70faa 100644 --- a/tests/test_umbral.py +++ b/tests/test_umbral.py @@ -144,7 +144,7 @@ def test_capsule_serialization(): # TODO: Do we want to include the cfrags as well? See #20. assert len(capsule_bytes) == 33 + 33 + 32 == 98 - new_capsule = umbral.Capsule.from_original_bytes(capsule_bytes, + new_capsule = umbral.Capsule.from_bytes(capsule_bytes, umbral.UmbralParameters().curve) # TODO: Have method that gives us these attributes instead of needing to access them directly. assert new_capsule.original_components() == capsule.original_components() @@ -168,9 +168,9 @@ def test_reconstructed_capsule_serialization(): # A reconstructed Capsule is three points, representable as 33 bytes each. assert len(rec_capsule_bytes) == 99 - new_rec_capsule = umbral.Capsule.from_reconstructed_bytes( + new_rec_capsule = umbral.Capsule.from_bytes( rec_capsule_bytes, - umbral.UmbralParameters().curve) + umbral.UmbralParameters().curve, is_reconstructed=True) # TODO: Have method that gives us these attributes instead of needing to access them directly. assert new_rec_capsule._point_eph_e_prime == capsule._point_eph_e_prime assert new_rec_capsule._point_eph_v_prime == capsule._point_eph_v_prime diff --git a/umbral/umbral.py b/umbral/umbral.py index 1bc86592..02ce7b59 100644 --- a/umbral/umbral.py +++ b/umbral/umbral.py @@ -144,29 +144,22 @@ def __init__(self, self._contents = None @classmethod - def from_original_bytes(cls, data: bytes, curve: ec.EllipticCurve): + def from_bytes(cls, data: bytes, curve: ec.EllipticCurve, is_reconstructed=False): """ Instantiates a Capsule object from the serialized data. """ - eph_e = Point.from_bytes(data[0:33], curve) - eph_v = Point.from_bytes(data[33:66], curve) - sig = BigNum.from_bytes(data[66:98], curve) + if is_reconstructed: + e_prime = Point.from_bytes(data[0:33], curve) + v_prime = Point.from_bytes(data[33:66], curve) + eph_ni = Point.from_bytes(data[66:99], curve) - return cls(eph_e, eph_v, sig) - - @classmethod - def from_reconstructed_bytes(cls, data: bytes, curve: ec.EllipticCurve): - """ - Instantiate a Capsule from serialized data after reconstruction has occurred. - - The most obvious use case is Bob affixing at least m cFrags and then serializing the Capsule. - """ - # TODO: Seems like a job for BytestringSplitter ? - e_prime = Point.from_bytes(data[0:33], curve) - v_prime = Point.from_bytes(data[33:66], curve) - eph_ni = Point.from_bytes(data[66:99], curve) + return cls(e_prime=e_prime, v_prime=v_prime, noninteractive_point=eph_ni) + else: + eph_e = Point.from_bytes(data[0:33], curve) + eph_v = Point.from_bytes(data[33:66], curve) + sig = BigNum.from_bytes(data[66:98], curve) - return cls(e_prime=e_prime, v_prime=v_prime, noninteractive_point=eph_ni) + return cls(eph_e, eph_v, sig) @property def contents(self):