Merge pull request #150 from jMyles/class_length

expected_bytes_length
pull/160/head
Justin Holmes 2018-05-29 14:45:24 -04:00 committed by GitHub
commit ca5cc4b272
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 39 deletions

View File

@ -16,7 +16,7 @@ def test_capsule_serialization(alices_keys):
assert capsule_bytes == capsule_bytes_casted
# A Capsule can be represented as the 98 total bytes of two Points (33 each) and a CurveBN (32).
assert len(capsule_bytes) == pre.Capsule.get_size()
assert len(capsule_bytes) == pre.Capsule.expected_bytes_length()
new_capsule = pre.Capsule.from_bytes(capsule_bytes)
@ -50,7 +50,7 @@ def test_activated_capsule_serialization(alices_keys, bobs_keys):
capsule._reconstruct_shamirs_secret(priv_key_bob)
rec_capsule_bytes = capsule.to_bytes()
assert len(rec_capsule_bytes) == pre.Capsule.get_size(activated=True)
assert len(rec_capsule_bytes) == pre.Capsule.expected_bytes_length(activated=True)
new_rec_capsule = pre.Capsule.from_bytes(rec_capsule_bytes)

View File

@ -14,7 +14,7 @@ def test_kfrag_serialization(alices_keys):
kfrag_bytes = kfrags[0].to_bytes()
curve = default_curve()
assert len(kfrag_bytes) == KFrag.get_size(curve)
assert len(kfrag_bytes) == KFrag.expected_bytes_length(curve)
new_frag = pre.KFrag.from_bytes(kfrag_bytes)
assert new_frag._id == kfrags[0]._id
@ -109,7 +109,7 @@ def test_cfrag_serialization_no_proof_no_metadata(alices_keys):
assert proof is None
curve = default_curve()
assert len(cfrag_bytes) == CapsuleFrag.get_size(curve)
assert len(cfrag_bytes) == CapsuleFrag.expected_bytes_length(curve)
new_cfrag = pre.CapsuleFrag.from_bytes(cfrag_bytes)
assert new_cfrag._point_e1 == cfrag._point_e1

View File

@ -30,7 +30,7 @@ class CurveBN(object):
self.order = order
@classmethod
def get_size(cls, curve: ec.EllipticCurve=None):
def expected_bytes_length(cls, curve: ec.EllipticCurve=None):
"""
Returns the size (in bytes) of a CurveBN given the curve.
If no curve is provided, it uses the default.

View File

@ -22,14 +22,14 @@ class KFrag(object):
self.signature = signature
@classmethod
def get_size(cls, curve: ec.EllipticCurve = None):
def expected_bytes_length(cls, curve: ec.EllipticCurve = None):
"""
Returns the size (in bytes) of a KFrag given the curve.
If no curve is provided, it will use the default curve.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
return (bn_size * 4) + (point_size * 3)
@ -40,8 +40,8 @@ class KFrag(object):
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
splitter = BytestringSplitter(
bn_size, # id
@ -49,7 +49,7 @@ class KFrag(object):
(Point, point_size), # point_noninteractive
(Point, point_size), # point_commitment
(Point, point_size), # point_xcoord
(Signature, Signature.get_size(curve))
(Signature, Signature.expected_bytes_length(curve))
)
components = splitter(data)
@ -95,14 +95,14 @@ class CorrectnessProof(object):
self.kfrag_signature = kfrag_signature
@classmethod
def get_size(cls, curve: ec.EllipticCurve = None):
def expected_bytes_length(cls, curve: ec.EllipticCurve = None):
"""
Returns the size (in bytes) of a CorrectnessProof without the metadata.
If no curve is given, it will use the default curve.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve=curve)
point_size = Point.get_size(curve=curve)
bn_size = CurveBN.expected_bytes_length(curve=curve)
point_size = Point.expected_bytes_length(curve=curve)
return (bn_size * 3) + (point_size * 4)
@ -112,8 +112,8 @@ class CorrectnessProof(object):
Instantiate CorrectnessProof from serialized data.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
splitter = BytestringSplitter(
(Point, point_size), # point_e2
@ -121,7 +121,7 @@ class CorrectnessProof(object):
(Point, point_size), # point_kfrag_commitment
(Point, point_size), # point_kfrag_pok
(CurveBN, bn_size), # bn_sig
(Signature, Signature.get_size()), # kfrag_signature
(Signature), # kfrag_signature
)
components = splitter(data, return_remainder=True)
metadata = components.pop(-1) or None
@ -168,15 +168,15 @@ class CapsuleFrag(object):
"""
@classmethod
def get_size(cls, curve: ec.EllipticCurve = None):
def expected_bytes_length(cls, curve: ec.EllipticCurve = None):
"""
Returns the size (in bytes) of a CapsuleFrag given the curve without
the CorrectnessProof.
If no curve is provided, it will use the default curve.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
return (bn_size * 1) + (point_size * 4)
@ -187,8 +187,8 @@ class CapsuleFrag(object):
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
splitter = BytestringSplitter(
(Point, point_size), # point_e1

View File

@ -20,7 +20,7 @@ class Point(object):
self.group = group
@classmethod
def get_size(cls, curve: ec.EllipticCurve=None):
def expected_bytes_length(cls, curve: ec.EllipticCurve=None):
"""
Returns the size (in bytes) of a compressed Point given a curve.
If no curve is provided, it uses the default curve.

View File

@ -61,14 +61,14 @@ class Capsule(object):
self._attached_cfrags = list()
@classmethod
def get_size(cls, curve: ec.EllipticCurve = None, activated=False):
def expected_bytes_length(cls, curve: ec.EllipticCurve = None, activated=False):
"""
Returns the size (in bytes) of a Capsule given the curve.
If no curve is provided, it will use the default curve.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
if not activated:
return (bn_size * 1) + (point_size * 2)
@ -86,10 +86,10 @@ class Capsule(object):
Instantiates a Capsule object from the serialized data.
"""
curve = curve if curve is not None else default_curve()
bn_size = CurveBN.get_size(curve)
point_size = Point.get_size(curve)
bn_size = CurveBN.expected_bytes_length(curve)
point_size = Point.expected_bytes_length(curve)
if len(capsule_bytes) == cls.get_size(curve, activated=True):
if len(capsule_bytes) == cls.expected_bytes_length(curve, activated=True):
splitter = BytestringSplitter(
(Point, point_size), # point_e
(Point, point_size), # point_v
@ -275,7 +275,7 @@ def split_rekey(privkey_a_bn: Union[UmbralPrivateKey, CurveBN],
blake2b.update(dh_xcoord.to_bytes())
hashed_dh_tuple = blake2b.finalize()
bn_size = CurveBN.get_size(params.curve)
bn_size = CurveBN.expected_bytes_length(params.curve)
kfrags = []
for _ in range(N):
@ -428,9 +428,11 @@ def _open_capsule(capsule: Capsule,
if check_proof:
offending_cfrags = []
for cfrag in capsule._attached_cfrags:
if not cfrag.verify_correctness(capsule, delegating_pubkey,
alice_pubkey,
bob_pubkey, params):
if not cfrag.verify_correctness(capsule=capsule,
delegating_pubkey=delegating_pubkey,
signing_pubkey=alice_pubkey,
encrypting_pubkey=bob_pubkey,
params=params):
offending_cfrags.append(cfrag)
if offending_cfrags:
@ -447,7 +449,7 @@ def decrypt(ciphertext: bytes,
capsule: Capsule,
decrypting_key: UmbralPrivateKey,
delegating_pubkey: UmbralPublicKey = None,
alice_pub_key_sig: UmbralPublicKey = None,
verifying_key: UmbralPublicKey = None,
params: UmbralParameters = None, check_proof=True) -> bytes:
"""
Opens the capsule and gets what's inside.
@ -461,7 +463,7 @@ def decrypt(ciphertext: bytes,
# Since there are cfrags attached, we assume this is Bob opening the Capsule.
# (i.e., this is a re-encrypted capsule)
encapsulated_key = _open_capsule(capsule, decrypting_key, delegating_pubkey, alice_pub_key_sig,
encapsulated_key = _open_capsule(capsule, decrypting_key, delegating_pubkey, verifying_key,
params=params, check_proof=check_proof)
dem = UmbralDEM(encapsulated_key)

View File

@ -15,7 +15,7 @@ from umbral.utils import get_curve_keysize_bytes
_BLAKE2B = hashes.BLAKE2b(64)
class Signature(object):
class Signature:
"""
We store signatures as r and s; this class allows interoperation
between (r, s) and DER formatting.
@ -30,11 +30,11 @@ class Signature(object):
return "ECDSA Signature: {}".format(bytes(self).hex()[:15])
@classmethod
def get_size(cls, curve: ec.EllipticCurve = None):
def expected_bytes_length(cls, curve: ec.EllipticCurve = None):
curve = curve if curve is not None else default_curve()
return get_curve_keysize_bytes(curve) * 2
def verify(self, message: bytes, pubkey: UmbralPublicKey) -> bool:
def verify(self, message: bytes, verifying_key: UmbralPublicKey) -> bool:
"""
Verifies that a message's signature was valid.
@ -43,7 +43,7 @@ class Signature(object):
:return: True if valid, False if invalid
"""
cryptography_pub_key = pubkey.to_cryptography_pubkey()
cryptography_pub_key = verifying_key.to_cryptography_pubkey()
try:
cryptography_pub_key.verify(