Skip to content

Commit

Permalink
Single from_bytes method. See nucypher#24 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
jMyles committed Jan 27, 2018
1 parent feb967d commit c0782a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
6 changes: 3 additions & 3 deletions tests/test_umbral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
29 changes: 11 additions & 18 deletions umbral/umbral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit c0782a2

Please sign in to comment.