Skip to content

Commit

Permalink
Added a testcase, and added a shares check in combine_shares
Browse files Browse the repository at this point in the history
  • Loading branch information
amiller committed Apr 10, 2018
1 parent 6e88261 commit b28b835
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions honeybadgerbft/crypto/threshenc/tpke.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,17 @@ def combine_shares(self, (U,V,W), shares):
""" """
# sigs: a mapping from idx -> sig
S = set(shares.keys())
print S
assert S.issubset(range(self.l))

# ASSUMPTION
# assert self.verify_ciphertext((U,V,W))

# ASSUMPTION
for j,share in shares.iteritems():
if j == 0: print share
self.verify_share( j, share, (U,V,W) )

mul = lambda a,b: a*b
res = reduce(mul,
[share ** self.lagrange(S, j)
Expand Down
52 changes: 52 additions & 0 deletions test/test_threshenc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
from charm.toolbox.pairinggroup import PairingGroup,ZR,G1,G2,GT,pair
import random
from honeybadgerbft.crypto.threshenc.tpke import TPKEPublicKey, TPKEPrivateKey, dealer
from Crypto.Hash import SHA256
from Crypto import Random
from Crypto.Cipher import AES

def test_threshenc():
PK, SKs = dealer(players=100,k=35)

m = SHA256.new('hello world').digest()
C = PK.encrypt(m)

assert PK.verify_ciphertext(C)

shares = [sk.decrypt_share(C) for sk in SKs]
for i,share in enumerate(shares):
assert PK.verify_share(i, share, C)

SS = range(PK.l)
for i in range(1):
random.shuffle(SS)
S = set(SS[:PK.k])

m_ = PK.combine_shares(C, dict((s,shares[s]) for s in S))
assert m_ == m

def test_threshenc2():
# Failure cases
PK, SKs = dealer(players=100,k=35)

m = SHA256.new('hello world').digest()
C = PK.encrypt(m)

assert PK.verify_ciphertext(C)

shares = [sk.decrypt_share(C) for sk in SKs]
for i,share in enumerate(shares):
assert PK.verify_share(i, share, C)

SS = range(PK.l)
random.shuffle(SS)
# Perturb one of the keys
shares[SS[0]] += shares[SS[0]]
S = set(SS[:PK.k])

try:
m_ = PK.combine_shares(C, dict((s,shares[s]) for s in S))
assert m_ == m
except AssertionError: pass
else: assert False, "Combine shares should have raised an error"

0 comments on commit b28b835

Please sign in to comment.