Esempio n. 1
0
    def przs_generate_random_share(
        self,
        shape: Union[tuple, torch.Size],
        generators: List[torch.Generator],
    ) -> Any:
        """Generate a random share using the two generators hold by a party.

        Args:
            shape (Union[tuple, torch.Size]): Shape.
            generators (List[torch.Generator]): Torch generator.

        Returns:
            Any: ShareTensor

        """
        from sympc.tensor import ShareTensor

        gen0, gen1 = generators

        current_share = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen0,
            shape=shape,
        )

        next_share = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen1,
            shape=shape,
        )

        share = ShareTensor(session=self)
        share.tensor = current_share - next_share

        return share
Esempio n. 2
0
    def _generate_random_share(
        self, shape: Union[tuple, torch.Size], ring_size: int
    ) -> List[torch.Tensor]:
        """Generate random tensor share for the given shape and ring_size.

        Args:
            shape (Union[tuple, torch.Size]): Shape for the share.
            ring_size (int): ring size to generate share.

        The generators are invoked in Counter(CTR) mode as parties with the same initial seeds
        could generate correlated random numbers on subsequent invocations.

        Returns:
            List[torch.Tensor] : shares generated by the generators.
        """
        from sympc.tensor import PRIME_NUMBER

        gen1, gen2 = self.przs_generators
        tensor_type = get_type_from_ring(ring_size)
        max_val = PRIME_NUMBER if ring_size == PRIME_NUMBER else None

        tensor_share1 = generate_random_element(
            tensor_type=tensor_type, generator=gen1, shape=shape, max_val=max_val
        )

        tensor_share2 = generate_random_element(
            tensor_type=tensor_type, generator=gen2, shape=shape, max_val=max_val
        )

        return [tensor_share1, tensor_share2]
Esempio n. 3
0
    def _generate_random_share(
        self,
        shape: Union[tuple, torch.Size],
    ) -> List[torch.Tensor]:
        """Generate random tensor share for the given shape.

        Args:
            shape (Union[tuple, torch.Size]): Shape for the share.

        The generators are invoked in Counter(CTR) mode as parties with the same initial seeds
        could generate the same random number on subsequent invocations.

        Returns:
            List[torch.Tensor] : shares generated by the generators.
        """
        gen1, gen2 = self.przs_generators

        tensor_share1 = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen1,
            shape=shape,
        )

        tensor_share2 = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen2,
            shape=shape,
        )

        return [tensor_share1, tensor_share2]
Esempio n. 4
0
def test_przs_share_tensor() -> None:
    """Test przs_generate_random_share method from Session for ShareTensor."""
    session = Session()  # default protocol: FSS
    SessionManager.setup_mpc(session)
    seed1 = secrets.randbits(32)
    seed2 = secrets.randbits(32)
    gen1 = get_new_generator(seed1)
    gen2 = get_new_generator(seed2)
    session.przs_generators = [gen1, gen2]
    shape = (2, 1)
    share = session.przs_generate_random_share(shape=shape)
    assert isinstance(share, ShareTensor)

    new_gen1 = get_new_generator(seed1)
    new_gen2 = get_new_generator(seed2)
    share1 = generate_random_element(generator=new_gen1,
                                     shape=shape,
                                     tensor_type=session.tensor_type)
    share2 = generate_random_element(generator=new_gen2,
                                     shape=shape,
                                     tensor_type=session.tensor_type)
    target_tensor = share1 - share2
    assert (share.tensor == target_tensor).all()
Esempio n. 5
0
    def przs_generate_random_share(
        self,
        shape: Union[tuple, torch.Size],
    ) -> Any:
        """Generate a random share using the two generators hold by a party.

        Args:
            shape (Union[tuple, torch.Size]): Shape for the share.

        Returns:
            Any: ShareTensor

        """
        from sympc.tensor import ShareTensor

        gen0, gen1 = self.przs_generators

        current_share = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen0,
            shape=shape,
        )

        next_share = generate_random_element(
            tensor_type=self.tensor_type,
            generator=gen1,
            shape=shape,
        )

        # It has encoder_precision = 0 such that the value would not be encoded
        share = ShareTensor(
            data=current_share - next_share,
            session_uuid=self.uuid,
            config=Config(encoder_precision=0),
        )
        return share
Esempio n. 6
0
def test_przs_rs_tensor() -> None:
    """Test przs_generate_random_share method from Session for ReplicatedSharedTensor."""
    falcon = Falcon(security_type="malicious")
    session = Session(protocol=falcon)
    SessionManager.setup_mpc(session)
    seed1 = secrets.randbits(32)
    seed2 = secrets.randbits(32)
    gen1 = get_new_generator(seed1)
    gen2 = get_new_generator(seed2)
    session.przs_generators = [gen1, gen2]
    shape = (2, 1)
    share = session.przs_generate_random_share(shape=shape)
    assert isinstance(share, ReplicatedSharedTensor)

    new_gen1 = get_new_generator(seed1)
    new_gen2 = get_new_generator(seed2)
    share1 = generate_random_element(generator=new_gen1,
                                     shape=shape,
                                     tensor_type=session.tensor_type)
    share2 = generate_random_element(generator=new_gen2,
                                     shape=shape,
                                     tensor_type=session.tensor_type)
    target_tensor = share1 - share2
    assert (share.shares[0] == target_tensor).all()