Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/hyperledger/indy-plenum i…
Browse files Browse the repository at this point in the history
…nto task-2297-get-taa-digest
  • Loading branch information
Toktar committed Dec 9, 2019
2 parents 1644abe + aae307a commit fdc3d8f
Show file tree
Hide file tree
Showing 114 changed files with 1,579 additions and 1,232 deletions.
30 changes: 8 additions & 22 deletions crypto/bls/bls_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from collections import namedtuple
from typing import Sequence

from common.exceptions import PlenumTypeError

GroupParams = namedtuple('GroupParams',
'group_name, g')

Expand All @@ -15,27 +13,15 @@ def load_group_params(self) -> GroupParams:


class BlsCryptoSigner(metaclass=ABCMeta):
def __init__(self, sk: str, pk: str, params: GroupParams):

if not isinstance(sk, str):
raise PlenumTypeError('sk', sk, str)

if not sk:
raise ValueError("'sk' should be a non-empty string")

if not isinstance(pk, str):
raise PlenumTypeError('pk', pk, str)

if not pk:
raise ValueError("'pk' should be a non-empty string")

self._sk = sk
self.pk = pk
self._group_params = params
@staticmethod
@abstractmethod
def generate_keys(params: GroupParams, seed=None) -> (object, object, object):
pass

@staticmethod
@abstractmethod
def generate_keys(params: GroupParams, seed=None) -> (str, str, str):
def generate_key_proof(sk: object, pk: object) -> object:
pass

@abstractmethod
Expand All @@ -49,13 +35,13 @@ def create_multi_sig(self, signatures: Sequence[str]) -> str:
pass

@abstractmethod
def verify_sig(self, signature: str, message: bytes, pk: str) -> bool:
def verify_sig(self, signature: str, message: bytes, pk: object) -> bool:
pass

@abstractmethod
def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[str]) -> bool:
def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[object]) -> bool:
pass

@abstractmethod
def verify_key_proof_of_possession(self, key_proof, pk: str) -> bool:
def verify_key_proof_of_possession(self, key_proof: object, pk: object) -> bool:
pass
15 changes: 10 additions & 5 deletions crypto/bls/bls_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from crypto.bls.bls_bft import BlsBft
from crypto.bls.bls_bft_replica import BlsBftReplica
from crypto.bls.bls_crypto import BlsGroupParamsLoader, BlsCryptoSigner, BlsCryptoVerifier
from crypto.bls.bls_crypto import BlsGroupParamsLoader, BlsCryptoSigner, BlsCryptoVerifier, GroupParams
from crypto.bls.bls_key_manager import BlsKeyManager, LoadBLSKeyError
from crypto.bls.bls_key_register import BlsKeyRegister
from plenum.bls.bls_store import BlsStore
Expand All @@ -12,10 +12,10 @@


class BlsFactoryCrypto(metaclass=ABCMeta):

@abstractmethod
def generate_bls_keys(self, seed=None) -> (str, str, str):
return self._get_bls_crypto_signer_class().generate_keys(
self._load_group_params(),
seed)
pass

def generate_and_store_bls_keys(self, seed=None) -> (str, str):
bls_key_manager = self._create_key_manager(self._load_group_params())
Expand All @@ -35,6 +35,11 @@ def create_bls_crypto_verifier(self) -> BlsCryptoVerifier:
group_params = self._load_group_params()
return self._create_bls_crypto_verifier(group_params)

def _generate_raw_bls_keys(self, seed=None) -> (object, object, object):
return self._get_bls_crypto_signer_class().generate_keys(
self._load_group_params(),
seed)

def _load_group_params(self):
return self._create_group_params_loader().load_group_params()

Expand All @@ -51,7 +56,7 @@ def _get_bls_crypto_signer_class(self):
pass

@abstractmethod
def _create_bls_crypto_signer(self, sk, pk, group_params) -> BlsCryptoSigner:
def _create_bls_crypto_signer(self, sk: str, pk: str, group_params: GroupParams) -> BlsCryptoSigner:
pass

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/bls_key_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class BlsKeyRegister(metaclass=ABCMeta):
@abstractmethod
def get_key_by_name(self, node_name, pool_state_root_hash=None):
def get_key_by_name(self, node_name, pool_state_root_hash=None) -> object:
'''
Gets Public BLS key for a node with the specified name.
The key is associated with a state of a pool as defined by pool_state_root_hash.
Expand Down
47 changes: 20 additions & 27 deletions crypto/bls/indy_crypto/bls_crypto_indy_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def bls_from_str(v: str, cls) -> Optional[BlsEntity]:
logger.warning('BLS: Indy Crypto error: {}'.format(e))
return None

@staticmethod
def bls_pk_from_str(v: str) -> Optional[VerKey]:
return IndyCryptoBlsUtils.bls_from_str(v, VerKey)

@staticmethod
def prepare_seed(seed):
seed_bytes = None
Expand All @@ -66,21 +70,20 @@ def __init__(self, params: GroupParams):
self._generator = \
IndyCryptoBlsUtils.bls_from_str(params.g, Generator) # type: Generator

def verify_sig(self, signature: str, message: bytes, pk: str) -> bool:
def verify_sig(self, signature: str, message: bytes, bls_pk: Optional[VerKey]) -> bool:
bls_signature = IndyCryptoBlsUtils.bls_from_str(signature, Signature)
if bls_signature is None:
return False
bls_pk = IndyCryptoBlsUtils.bls_from_str(pk, VerKey)
if bls_pk is None:
return False
return Bls.verify(bls_signature,
message,
bls_pk,
self._generator)

def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[str]) -> bool:
epks = [IndyCryptoBlsUtils.bls_from_str(p, VerKey) for p in pks]
if None in epks:
def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[Optional[VerKey]]) -> bool:
# TODO: is it expected that we return False if one of the keys is None?
if None in pks:
return False

multi_signature = \
Expand All @@ -90,52 +93,42 @@ def verify_multi_sig(self, signature: str, message: bytes, pks: Sequence[str]) -

return Bls.verify_multi_sig(multi_sig=multi_signature,
message=message,
ver_keys=epks,
ver_keys=pks,
gen=self._generator)

def create_multi_sig(self, signatures: Sequence[str]) -> str:
sigs = [IndyCryptoBlsUtils.bls_from_str(s, Signature) for s in signatures]
bts = MultiSignature.new(sigs)
return IndyCryptoBlsUtils.bls_to_str(bts)

def verify_key_proof_of_possession(self, key_proof, pk: str) -> bool:
bls_key_proof = IndyCryptoBlsUtils.bls_from_str(key_proof, ProofOfPossession)
bls_pk = IndyCryptoBlsUtils.bls_from_str(pk, VerKey)
if None in [bls_key_proof, bls_pk]:
def verify_key_proof_of_possession(self, key_proof: Optional[ProofOfPossession], bls_pk: Optional[VerKey]) -> bool:
if None in [key_proof, bls_pk]:
return False
return Bls.verify_pop(bls_key_proof,
return Bls.verify_pop(key_proof,
bls_pk,
self._generator)


class BlsCryptoSignerIndyCrypto(BlsCryptoSigner):
def __init__(self, sk: str, pk: str, params: GroupParams):
super().__init__(sk, pk, params)
self._sk_bls = IndyCryptoBlsUtils.bls_from_str(sk, SignKey)
self._pk_bls = IndyCryptoBlsUtils.bls_from_str(pk, VerKey)
def __init__(self, sk: SignKey, pk: VerKey, params: GroupParams):
self._sk = sk # type: SignKey
self.pk = pk # type: VerKey
self._generator = \
IndyCryptoBlsUtils.bls_from_str(params.g, Generator) # type: Generator

@staticmethod
def generate_keys(params: GroupParams, seed=None) -> (str, str, str):
def generate_keys(params: GroupParams, seed=None) -> (SignKey, VerKey, ProofOfPossession):
seed = IndyCryptoBlsUtils.prepare_seed(seed)
gen = IndyCryptoBlsUtils.bls_from_str(params.g, Generator)
sk = SignKey.new(seed)
vk = VerKey.new(gen, sk)
key_proof = ProofOfPossession.new(ver_key=vk, sign_key=sk)
sk_str = IndyCryptoBlsUtils.bls_to_str(sk)
vk_str = IndyCryptoBlsUtils.bls_to_str(vk)
key_proof_str = IndyCryptoBlsUtils.bls_to_str(key_proof)
return sk_str, vk_str, key_proof_str
return sk, vk, key_proof

@staticmethod
def generate_key_proof(sk: str, pk: str):
sk_bls = IndyCryptoBlsUtils.bls_from_str(sk, SignKey)
pk_bls = IndyCryptoBlsUtils.bls_from_str(pk, VerKey)
key_proof = ProofOfPossession.new(ver_key=pk_bls, sign_key=sk_bls)
key_proof_str = IndyCryptoBlsUtils.bls_to_str(key_proof)
return key_proof_str
def generate_key_proof(sk: SignKey, pk: VerKey) -> ProofOfPossession:
return ProofOfPossession.new(ver_key=pk, sign_key=sk)

def sign(self, message: bytes) -> str:
sign = Bls.sign(message, self._sk_bls)
sign = Bls.sign(message, self._sk)
return IndyCryptoBlsUtils.bls_to_str(sign)
Loading

0 comments on commit fdc3d8f

Please sign in to comment.