Adapts tests to new way to use params

pull/159/head
David Núñez 2018-06-01 12:30:19 +02:00
parent c736370bcc
commit d5b434c013
7 changed files with 100 additions and 52 deletions

View File

@ -9,6 +9,7 @@ from umbral.keys import UmbralPrivateKey
from umbral.point import Point
from umbral.pre import Capsule
from umbral.signing import Signer
from umbral.config import default_params
def test_cannot_attach_cfrag_without_keys():
@ -16,7 +17,11 @@ def test_cannot_attach_cfrag_without_keys():
We need the proper keys to verify the correctness of CFrags
in order to attach them to a Capsule.
"""
capsule = Capsule(point_e=Point.gen_rand(),
params = default_params()
capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())
@ -59,7 +64,10 @@ def test_cannot_attach_cfrag_without_proof():
However, even when properly attaching keys, we can't attach the CFrag
if it is unproven.
"""
capsule = Capsule(point_e=Point.gen_rand(),
params = default_params()
capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())
@ -86,7 +94,10 @@ def test_cannot_set_different_keys():
"""
Once a key is set on a Capsule, it can't be changed to a different key.
"""
capsule = Capsule(point_e=Point.gen_rand(),
params = default_params()
capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())

View File

@ -6,14 +6,21 @@ from umbral.point import Point
from umbral.pre import Capsule
from umbral.signing import Signer
from umbral.keys import UmbralPrivateKey
from umbral.config import default_params
def test_capsule_creation(alices_keys):
params = default_params()
with pytest.raises(TypeError):
rare_capsule = Capsule() # Alice cannot make a capsule this way.
rare_capsule = Capsule(params) # Alice cannot make a capsule this way.
# Some users may create capsules their own way.
custom_capsule = Capsule(point_e=Point.gen_rand(),
custom_capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())
@ -28,17 +35,22 @@ def test_capsule_creation(alices_keys):
def test_capsule_equality():
one_capsule = Capsule(point_e=Point.gen_rand(),
params = default_params()
one_capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())
another_capsule = Capsule(point_e=Point.gen_rand(),
another_capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())
assert one_capsule != another_capsule
activated_capsule = Capsule(point_e_prime=Point.gen_rand(),
activated_capsule = Capsule(params,
point_e_prime=Point.gen_rand(),
point_v_prime=Point.gen_rand(),
point_noninteractive=Point.gen_rand())
@ -46,9 +58,11 @@ def test_capsule_equality():
def test_decapsulation_by_alice(alices_keys):
params = default_params()
delegating_privkey, _signing_privkey = alices_keys
sym_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
sym_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key, params)
assert len(sym_key) == 32
# The symmetric key sym_key is perhaps used for block cipher here in a real-world scenario.
@ -57,6 +71,7 @@ def test_decapsulation_by_alice(alices_keys):
def test_bad_capsule_fails_reencryption(alices_keys, bobs_keys):
params = default_params()
delegating_privkey, _signing_privkey = alices_keys
signer_alice = Signer(_signing_privkey)
@ -64,7 +79,8 @@ def test_bad_capsule_fails_reencryption(alices_keys, bobs_keys):
kfrags = pre.split_rekey(delegating_privkey, signer_alice, receiving_pubkey, 1, 2)
bollocks_capsule = Capsule(point_e=Point.gen_rand(),
bollocks_capsule = Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=CurveBN.gen_rand())

View File

@ -3,14 +3,15 @@ import pytest
from umbral import pre, keys
from umbral.curvebn import CurveBN
from umbral.point import Point
from umbral.config import default_curve
from umbral.config import default_curve, default_params
from umbral.signing import Signer
def test_capsule_serialization(alices_keys):
delegating_privkey, _signing_privkey = alices_keys
params = delegating_privkey.params
_symmetric_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
_symmetric_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key, params)
capsule_bytes = capsule.to_bytes()
capsule_bytes_casted = bytes(capsule)
assert capsule_bytes == capsule_bytes_casted
@ -18,7 +19,7 @@ def test_capsule_serialization(alices_keys):
# 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.expected_bytes_length()
new_capsule = pre.Capsule.from_bytes(capsule_bytes)
new_capsule = pre.Capsule.from_bytes(capsule_bytes, params)
# Three ways to think about equality.
# First, the public approach for the Capsule. Simply:
@ -39,9 +40,11 @@ def test_activated_capsule_serialization(alices_keys, bobs_keys):
delegating_pubkey = delegating_privkey.get_pubkey()
signer_alice = Signer(signing_privkey)
params = delegating_privkey.params
receiving_privkey, receiving_pubkey = bobs_keys
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key, params)
capsule.set_correctness_keys(delegating=delegating_pubkey,
receiving=receiving_pubkey,
@ -58,7 +61,7 @@ def test_activated_capsule_serialization(alices_keys, bobs_keys):
assert len(rec_capsule_bytes) == pre.Capsule.expected_bytes_length(activated=True)
new_rec_capsule = pre.Capsule.from_bytes(rec_capsule_bytes)
new_rec_capsule = pre.Capsule.from_bytes(rec_capsule_bytes, params)
# Again, the same three perspectives on equality.
assert new_rec_capsule == capsule
@ -71,22 +74,28 @@ def test_activated_capsule_serialization(alices_keys, bobs_keys):
def test_cannot_create_capsule_from_bogus_material(alices_keys):
params = alices_keys[0].params
with pytest.raises(TypeError):
capsule_of_questionable_parentage = pre.Capsule(point_e=Point.gen_rand(),
capsule_of_questionable_parentage = pre.Capsule(params,
point_e=Point.gen_rand(),
point_v=42,
bn_sig=CurveBN.gen_rand())
with pytest.raises(TypeError):
capsule_of_questionable_parentage = pre.Capsule(point_e=Point.gen_rand(),
capsule_of_questionable_parentage = pre.Capsule(params,
point_e=Point.gen_rand(),
point_v=Point.gen_rand(),
bn_sig=42)
with pytest.raises(TypeError):
capsule_of_questionable_parentage = pre.Capsule(point_e_prime=Point.gen_rand(),
capsule_of_questionable_parentage = pre.Capsule(params,
point_e_prime=Point.gen_rand(),
point_v_prime=42,
point_noninteractive=Point.gen_rand())
with pytest.raises(TypeError):
capsule_of_questionable_parentage = pre.Capsule(point_e_prime=Point.gen_rand(),
capsule_of_questionable_parentage = pre.Capsule(params,
point_e_prime=Point.gen_rand(),
point_v_prime=Point.gen_rand(),
point_noninteractive=42)

View File

@ -9,12 +9,15 @@ from .conftest import parameters
def test_correctness_proof_serialization(alices_keys):
delegating_privkey, signing_privkey = alices_keys
delegating_pubkey = delegating_privkey.get_pubkey()
signer = Signer(signing_privkey)
priv_key_bob = keys.UmbralPrivateKey.gen_key()
pub_key_bob = priv_key_bob.get_pubkey()
_unused_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
params = delegating_privkey.params
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key, params)
kfrags = pre.split_rekey(delegating_privkey, signer, pub_key_bob, 1, 2)
# Example of potential metadata to describe the re-encryption request
@ -42,15 +45,18 @@ def test_correctness_proof_serialization(alices_keys):
@pytest.mark.parametrize("N, M", parameters)
def test_cheating_ursula_replays_old_reencryption(N, M, alices_keys):
delegating_privkey, signing_privkey = alices_keys
delegating_pubkey = delegating_privkey.get_pubkey()
signer = Signer(signing_privkey)
priv_key_bob = keys.UmbralPrivateKey.gen_key()
pub_key_bob = priv_key_bob.get_pubkey()
sym_key_alice1, capsule_alice1 = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
sym_key_alice2, capsule_alice2 = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
params = delegating_privkey.params
capsule_alice1.set_correctness_keys(delegating=delegating_privkey.get_pubkey(),
_unused_key1, capsule_alice1 = pre._encapsulate(delegating_pubkey.point_key, params)
_unused_key2, capsule_alice2 = pre._encapsulate(delegating_pubkey.point_key, params)
capsule_alice1.set_correctness_keys(delegating=delegating_pubkey,
receiving=pub_key_bob,
verifying=signing_privkey.get_pubkey())
@ -79,12 +85,12 @@ def test_cheating_ursula_replays_old_reencryption(N, M, alices_keys):
with pytest.raises(pre.GenericUmbralError):
sym_key = pre._decapsulate_reencrypted(pub_key_bob.point_key,
priv_key_bob.bn_key,
delegating_privkey.get_pubkey().point_key,
delegating_pubkey.point_key,
capsule_alice1
)
assert not cfrags[0].verify_correctness(capsule_alice1,
delegating_privkey.get_pubkey(),
delegating_pubkey,
signing_privkey.get_pubkey(),
pub_key_bob,
)
@ -94,7 +100,7 @@ def test_cheating_ursula_replays_old_reencryption(N, M, alices_keys):
correct_cases = 0
for cfrag_i, metadata_i in zip(cfrags[1:], metadata[1:]):
if cfrag_i.verify_correctness(capsule_alice1,
delegating_privkey.get_pubkey(),
delegating_pubkey,
signing_privkey.get_pubkey(),
pub_key_bob,
):
@ -108,7 +114,7 @@ def test_cheating_ursula_replays_old_reencryption(N, M, alices_keys):
with pytest.raises(pre.UmbralCorrectnessError) as exception_info:
_ = pre._open_capsule(capsule_alice1,
priv_key_bob,
delegating_privkey.get_pubkey(),
delegating_pubkey,
signing_privkey.get_pubkey(),
)
correctness_error = exception_info.value
@ -119,15 +125,17 @@ def test_cheating_ursula_replays_old_reencryption(N, M, alices_keys):
@pytest.mark.parametrize("N, M", parameters)
def test_cheating_ursula_sends_garbage(N, M, alices_keys):
delegating_privkey, signing_privkey = alices_keys
delegating_pubkey = delegating_privkey.get_pubkey()
signer = Signer(signing_privkey)
# Bob
priv_key_bob = keys.UmbralPrivateKey.gen_key()
pub_key_bob = priv_key_bob.get_pubkey()
sym_key, capsule_alice = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
params = delegating_privkey.params
capsule_alice.set_correctness_keys(delegating=delegating_privkey.get_pubkey(),
sym_key, capsule_alice = pre._encapsulate(delegating_pubkey.point_key, params)
capsule_alice.set_correctness_keys(delegating=delegating_pubkey,
receiving=pub_key_bob,
verifying=signing_privkey.get_pubkey())
@ -153,11 +161,11 @@ def test_cheating_ursula_sends_garbage(N, M, alices_keys):
with pytest.raises(pre.GenericUmbralError):
_unused_key = pre._decapsulate_reencrypted(pub_key_bob.point_key,
priv_key_bob.bn_key,
delegating_privkey.get_pubkey().point_key,
delegating_pubkey.point_key,
capsule_alice)
assert not cfrags[0].verify_correctness(capsule_alice,
delegating_privkey.get_pubkey(),
delegating_pubkey,
signing_privkey.get_pubkey(),
pub_key_bob,
)
@ -166,7 +174,7 @@ def test_cheating_ursula_sends_garbage(N, M, alices_keys):
# so the rest of CFrags chould be correct:
for cfrag_i, metadata_i in zip(cfrags[1:], metadata[1:]):
assert cfrag_i.verify_correctness(capsule_alice,
delegating_privkey.get_pubkey(),
delegating_pubkey,
signing_privkey.get_pubkey(),
pub_key_bob,
)
@ -174,7 +182,8 @@ def test_cheating_ursula_sends_garbage(N, M, alices_keys):
# Alternatively, we can try to open the capsule directly.
# We should get an exception with an attached list of incorrect cfrags
with pytest.raises(pre.UmbralCorrectnessError) as exception_info:
_decapsulated_key = pre._open_capsule(capsule_alice, priv_key_bob, delegating_privkey.get_pubkey(),
_decapsulated_key = pre._open_capsule(capsule_alice, priv_key_bob,
delegating_pubkey,
signing_privkey.get_pubkey())
correctness_error = exception_info.value
assert cfrags[0] in correctness_error.offending_cfrags
@ -186,6 +195,7 @@ def test_decryption_fails_when_it_expects_a_proof_and_there_isnt(N, M, alices_ke
"""Manually injects umbralparameters for multi-curve testing."""
delegating_privkey, signing_privkey = alices_keys
delegating_pubkey = delegating_privkey.get_pubkey()
signer = Signer(signing_privkey)
priv_key_bob, pub_key_bob = bobs_keys
@ -215,11 +225,14 @@ def test_decryption_fails_when_it_expects_a_proof_and_there_isnt(N, M, alices_ke
@pytest.mark.parametrize("N, M", parameters)
def test_m_of_n(N, M, alices_keys, bobs_keys):
delegating_privkey, signing_privkey = alices_keys
delegating_pubkey = delegating_privkey.get_pubkey()
signer = Signer(signing_privkey)
priv_key_bob, pub_key_bob = bobs_keys
sym_key, capsule = pre._encapsulate(delegating_privkey.get_pubkey().point_key)
params = delegating_privkey.params
sym_key, capsule = pre._encapsulate(delegating_pubkey.point_key, params)
capsule.set_correctness_keys(delegating=delegating_privkey.get_pubkey(),
receiving=pub_key_bob,
@ -238,10 +251,10 @@ def test_m_of_n(N, M, alices_keys, bobs_keys):
cfrag = pre.reencrypt(kfrag, capsule, metadata=metadata)
capsule.attach_cfrag(cfrag)
assert cfrag.verify_correctness(capsule,
delegating_privkey.get_pubkey(), signing_privkey.get_pubkey(), pub_key_bob,
assert cfrag.verify_correctness(capsule, delegating_pubkey,
signing_privkey.get_pubkey(), pub_key_bob,
)
sym_key_from_capsule = pre._open_capsule(capsule, priv_key_bob, delegating_privkey.get_pubkey(),
sym_key_from_capsule = pre._open_capsule(capsule, priv_key_bob, delegating_pubkey,
signing_privkey.get_pubkey())
assert sym_key == sym_key_from_capsule

View File

@ -32,7 +32,7 @@ def test_cfrag_serialization_with_proof_and_metadata(alices_keys, bobs_keys):
_receiving_privkey, receiving_pubkey = bobs_keys
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key, delegating_pubkey.params)
kfrags = pre.split_rekey(delegating_privkey, signer_alice,
receiving_pubkey, 1, 2)
@ -70,7 +70,7 @@ def test_cfrag_serialization_with_proof_but_no_metadata(alices_keys, bobs_keys):
_receiving_privkey, receiving_pubkey = bobs_keys
signer_alice = Signer(signing_privkey)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key, delegating_pubkey.params)
kfrags = pre.split_rekey(delegating_privkey, signer_alice,
receiving_pubkey, 1, 2)
@ -108,7 +108,7 @@ def test_cfrag_serialization_no_proof_no_metadata(alices_keys, bobs_keys):
_receiving_privkey, receiving_pubkey = bobs_keys
signer_alice = Signer(signing_privkey)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key)
_unused_key, capsule = pre._encapsulate(delegating_pubkey.point_key, delegating_pubkey.params)
kfrags = pre.split_rekey(delegating_privkey, signer_alice,
receiving_pubkey, 1, 2)

View File

@ -46,7 +46,7 @@ def test_derive_key_from_label():
def test_private_key_serialization(random_ec_curvebn1):
priv_key = random_ec_curvebn1
umbral_key = keys.UmbralPrivateKey(priv_key)
umbral_key = keys.UmbralPrivateKey(priv_key, default_params())
encoded_key = umbral_key.to_bytes()
@ -56,7 +56,7 @@ def test_private_key_serialization(random_ec_curvebn1):
def test_private_key_serialization_with_encryption(random_ec_curvebn1):
priv_key = random_ec_curvebn1
umbral_key = keys.UmbralPrivateKey(priv_key)
umbral_key = keys.UmbralPrivateKey(priv_key, default_params())
encoded_key = umbral_key.to_bytes(password=b'test')
@ -70,7 +70,7 @@ def test_public_key_serialization(random_ec_curvebn1):
params = default_params()
pub_key = priv_key * params.g
umbral_key = keys.UmbralPublicKey(pub_key)
umbral_key = keys.UmbralPublicKey(pub_key, params)
encoded_key = umbral_key.to_bytes()
@ -84,7 +84,7 @@ def test_public_key_to_bytes(random_ec_curvebn1):
params = default_params()
pub_key = priv_key * params.g
umbral_key = keys.UmbralPublicKey(pub_key)
umbral_key = keys.UmbralPublicKey(pub_key, params)
key_bytes = bytes(umbral_key)
assert type(key_bytes) == bytes
@ -92,7 +92,7 @@ def test_public_key_to_bytes(random_ec_curvebn1):
def test_key_encoder_decoder(random_ec_curvebn1):
priv_key = random_ec_curvebn1
umbral_key = keys.UmbralPrivateKey(priv_key)
umbral_key = keys.UmbralPrivateKey(priv_key, default_params())
encoded_key = umbral_key.to_bytes(encoder=base64.urlsafe_b64encode)

View File

@ -31,24 +31,23 @@ def test_simple_api(N, M, curve=default_curve()):
receiving_pubkey = receiving_privkey.get_pubkey()
plain_data = b'peace at dawn'
ciphertext, capsule = pre.encrypt(delegating_pubkey, plain_data, params=params)
ciphertext, capsule = pre.encrypt(delegating_pubkey, plain_data)
cleartext = pre.decrypt(ciphertext, capsule, delegating_privkey, params=params)
cleartext = pre.decrypt(ciphertext, capsule, delegating_privkey)
assert cleartext == plain_data
capsule.set_correctness_keys(delegating=delegating_pubkey,
receiving=receiving_pubkey,
verifying=signing_pubkey)
kfrags = pre.split_rekey(delegating_privkey, signer, receiving_pubkey, M, N, params=params)
kfrags = pre.split_rekey(delegating_privkey, signer, receiving_pubkey, M, N)
for kfrag in kfrags:
cfrag = pre.reencrypt(kfrag, capsule, params=params)
cfrag = pre.reencrypt(kfrag, capsule)
capsule.attach_cfrag(cfrag)
reenc_cleartext = pre.decrypt(ciphertext, capsule, receiving_privkey,
delegating_pubkey, signing_pubkey,
params=params)
delegating_pubkey, signing_pubkey)
assert reenc_cleartext == plain_data