示例#1
0
def to_paillier(element, public_key):
    if isinstance(element, torch.Tensor) and isinstance(
            element.child, PaillierTensor):
        element.child.pubkey = public_key
        child = element.child.child
        if isinstance(child, ndarray):
            return element
        elif isinstance(child, EncryptedNumber):
            element.child.child = array([child])
            return element
        else:
            raise Exception(
                "The tensor does not have an EncryptedNumber or a np.ndarray as child"
            )
    elif isinstance(element, PaillierTensor):
        element.pubkey = public_key
        return element.wrap()
    elif isinstance(element, ndarray):
        tensor = PaillierTensor()
        tensor.child = element
        tensor.pubkey = public_key
        return tensor.wrap()
    elif isinstance(element, list):
        tensor = PaillierTensor()
        tensor.child = array(element)
        tensor.pubkey = public_key
        return tensor.wrap()
    elif isinstance(element, EncryptedNumber):
        tensor = PaillierTensor()
        tensor.child = array([element])
        tensor.pubkey = public_key
        return tensor.wrap()
    else:
        raise TypeError(type(element))
示例#2
0
def deserialize_paillier(struct, pub=None):
    # Case 1: dict recursion
    if isinstance(struct, dict):
        pub = PaillierPublicKey(n=int(struct['n']))
        child = [
            deserialize_paillier(substruct, pub)
            for substruct in struct['values']
        ]
        # Building Paillier Tensor
        tensor = PaillierTensor()
        tensor.child = array(child)
        tensor.pubkey = pub
        return tensor.wrap()

    # Case 2: list recursion
    elif isinstance(struct, list):
        return [deserialize_paillier(substruct, pub) for substruct in struct]

    # Case 3: Tuple deserialization
    elif isinstance(struct, tuple):
        return EncryptedNumber(pub, int(struct[0]), int(struct[1]))

    # Case 4: Unknown type
    else:
        raise TypeError(type(struct))