Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __produce_kfrags(M, N) -> list:
delegating_privkey = keys.UmbralPrivateKey.gen_key(params=PARAMS)
signing_privkey = keys.UmbralPrivateKey.gen_key(params=PARAMS)
signer = Signer(signing_privkey)
receiving_privkey = keys.UmbralPrivateKey.gen_key(params=PARAMS)
receiving_pubkey = receiving_privkey.get_pubkey()
kfrags = pre.split_rekey(delegating_privkey, signer, receiving_pubkey, M, N)
return kfrags
def test_activated_capsule_serialization():
priv_key = pre.gen_priv()
pub_key = pre.priv2pub(priv_key)
_unused_key, capsule = pre._encapsulate(pub_key)
kfrags, _unused_vkeys = pre.split_rekey(priv_key, pub_key, 1, 2)
cfrag = pre.reencrypt(kfrags[0], capsule)
capsule.attach_cfrag(cfrag)
capsule._reconstruct_shamirs_secret()
rec_capsule_bytes = capsule.to_bytes()
# An activated Capsule is:
# three points, representable as 33 bytes each (the original), and
# two points and a bignum (32 bytes) (the activated components), for 197 total.
assert len(rec_capsule_bytes) == (33 * 3) + (33 + 33 + 32)
new_rec_capsule = pre.Capsule.from_bytes(rec_capsule_bytes)
# Again, the same three perspectives on equality.
"""Manually injects umbralparameters for multi-curve testing."""
params = UmbralParameters(curve=curve)
priv_key_alice = keys.UmbralPrivateKey.gen_key(params=params)
pub_key_alice = priv_key_alice.get_pubkey()
priv_key_bob = keys.UmbralPrivateKey.gen_key(params=params)
pub_key_bob = priv_key_bob.get_pubkey()
plain_data = b'attack at dawn'
ciphertext, capsule = pre.encrypt(pub_key_alice, plain_data)
cleartext = pre.decrypt(capsule, priv_key_alice, ciphertext)
assert cleartext == plain_data
rekeys, _unused_vkeys = pre.split_rekey(priv_key_alice, pub_key_bob, M, N, params=params)
for rekey in rekeys:
c_frag = pre.reencrypt(rekey, capsule, params=params)
capsule.attach_cfrag(c_frag)
reenc_cleartext = pre.decrypt(capsule, priv_key_bob, ciphertext, pub_key_alice)
assert reenc_cleartext == plain_data
def test_kfrag_serialization(alices_keys):
priv_key_alice, pub_key_alice = alices_keys
kfrags, _unused_vkeys = pre.split_rekey(priv_key_alice, pub_key_alice, 1, 2)
kfrag_bytes = kfrags[0].to_bytes()
# A KFrag can be represented as the 194 total bytes of two Points (33 each) and four BigNums (32 each).
assert len(kfrag_bytes) == 33 + 33 + (32 * 4) == 194
new_frag = pre.KFrag.from_bytes(kfrag_bytes)
assert new_frag.bn_id == kfrags[0].bn_id
assert new_frag.bn_key == kfrags[0].bn_key
assert new_frag.point_eph_ni == kfrags[0].point_eph_ni
assert new_frag.point_commitment == kfrags[0].point_commitment
assert new_frag.bn_sig1 == kfrags[0].bn_sig1
assert new_frag.bn_sig2 == kfrags[0].bn_sig2
def test_challenge_response_serialization():
priv_key = pre.gen_priv()
pub_key = pre.priv2pub(priv_key)
_unused_key, capsule = pre._encapsulate(pub_key)
kfrags, _unused_vkeys = pre.split_rekey(priv_key, pub_key, 1, 2)
cfrag = pre.reencrypt(kfrags[0], capsule)
capsule.attach_cfrag(cfrag)
ch_resp = pre.challenge(kfrags[0], capsule, cfrag)
ch_resp_bytes = ch_resp.to_bytes()
# A ChallengeResponse can be represented as
# the 228 total bytes of four Points (33 each) and three BigNums (32 each).
assert len(ch_resp_bytes) == (33 * 4) + (32 * 3) == 228
new_ch_resp = pre.ChallengeResponse.from_bytes(ch_resp_bytes)
assert new_ch_resp.point_eph_e2 == ch_resp.point_eph_e2
assert new_ch_resp.point_eph_v2 == ch_resp.point_eph_v2
assert new_ch_resp.point_kfrag_commitment == ch_resp.point_kfrag_commitment
def test_m_of_n(N, M, alices_keys, bobs_keys):
priv_key_alice, pub_key_alice = alices_keys
priv_key_bob, pub_key_bob = bobs_keys
sym_key, capsule = pre._encapsulate(pub_key_alice.point_key)
kfrags, vkeys = pre.split_rekey(priv_key_alice, pub_key_bob, M, N)
for kfrag in kfrags:
assert kfrag.verify(pub_key_alice.point_key, pub_key_bob.point_key)
assert kfrag.is_consistent(vkeys)
for kfrag in kfrags[:M]:
cfrag = pre.reencrypt(kfrag, capsule)
capsule.attach_cfrag(cfrag)
ch = pre.challenge(kfrag, capsule, cfrag)
assert pre.check_challenge(capsule, cfrag, ch, pub_key_alice.point_key, pub_key_bob.point_key)
# assert capsule.is_openable_by_bob() # TODO: Is it possible to check here if >= m cFrags have been attached?
# capsule.open(pub_bob, priv_bob, pub_alice)
capsule._reconstruct_shamirs_secret()
sym_key_from_capsule = pre.decapsulate_reencrypted(pub_key_bob.point_key,
def test_bad_capsule_fails_reencryption(alices_keys):
priv_key_alice, pub_key_alice = alices_keys
k_frags, _unused_vkeys = pre.split_rekey(priv_key_alice, pub_key_alice, 1, 2)
bollocks_capsule = Capsule(point_eph_e=Point.gen_rand(),
point_eph_v=Point.gen_rand(),
bn_sig=BigNum.gen_rand())
with pytest.raises(Capsule.NotValid):
pre.reencrypt(k_frags[0], bollocks_capsule)
def test_cfrag_serialization(alices_keys):
priv_key_alice, pub_key_alice = alices_keys
_unused_key, capsule = pre._encapsulate(pub_key_alice.point_key)
k_frags, _unused_vkeys = pre.split_rekey(priv_key_alice, pub_key_alice, 1, 2)
c_frag = pre.reencrypt(k_frags[0], capsule)
c_frag_bytes = c_frag.to_bytes()
# A CFrag can be represented as the 131 total bytes of three Points (33 each) and a BigNum (32).
assert len(c_frag_bytes) == 33 + 33 + 33 + 32 == 131
new_cfrag = pre.CapsuleFrag.from_bytes(c_frag_bytes)
assert new_cfrag.point_eph_e1 == c_frag.point_eph_e1
assert new_cfrag.point_eph_v1 == c_frag.point_eph_v1
assert new_cfrag.bn_kfrag_id == c_frag.bn_kfrag_id
assert new_cfrag.point_eph_ni == c_frag.point_eph_ni
def test_alice_sends_fake_kfrag_to_ursula(N, M):
priv_key_alice = keys.UmbralPrivateKey.gen_key()
pub_key_alice = priv_key_alice.get_pubkey()
priv_key_bob = keys.UmbralPrivateKey.gen_key()
pub_key_bob = priv_key_bob.get_pubkey()
plaintext = b'attack at dawn'
ciphertext, capsule = pre.encrypt(pub_key_alice, plaintext)
cleartext = pre.decrypt(capsule, priv_key_alice, ciphertext)
assert cleartext == plaintext
k_frags, vkeys = pre.split_rekey(priv_key_alice, pub_key_bob, M, N)
# Alice tries to frame the first Ursula by sending her a random kFrag
k_frags[0].bn_key = BigNum.gen_rand()
for k_frag in k_frags:
c_frag = pre.reencrypt(k_frag, capsule)
capsule.attach_cfrag(c_frag)
with pytest.raises(Exception):
_ = pre.decrypt(capsule, priv_key_bob, ciphertext, pub_key_alice)