<commit_before>"""Beaver Triples Protocol.

<NAME>. *Efficient multiparty protocols using circuit randomization*.
In <NAME>, editor, CRYPTO, volume **576** of Lecture Notes in
Computer Science, pages 420–432. Springer, 1991.
"""


# stdlib
import operator
import secrets
from typing import Any
from typing import Dict
from typing import List
from typing import Tuple

# third party
import numpy as np

# relative
from ....tensor.smpc.mpc_tensor import MPCTensor
from ....tensor.smpc.share_tensor import ShareTensor
from ...store import register_primitive_generator
from ...store import register_primitive_store_add
from ...store import register_primitive_store_get
from ...store.exceptions import EmptyPrimitiveStore

ttp_generator = np.random.default_rng()


def _get_triples(
    op_str: str,
    nr_parties: int,
    parties_info: List[Any],
    a_shape: Tuple[int],
    b_shape: Tuple[int],
    ring_size: int = 2 ** 32,
    **kwargs: Dict[Any, Any],
) -> Tuple[Tuple[Tuple[ShareTensor, ShareTensor, ShareTensor]]]:
    """Get triples.

    The Trusted Third Party (TTP) or Crypto Provider should provide this triples Currently,
    the one that orchestrates the communication provides those triples.".

    Args:
        op_str (str): Operator string.
        nr_parties (int): Number of parties
        parties_info (List[Any]): Parties connection information.
        a_shape (Tuple[int]): Shape of a from beaver triples protocol.
        b_shape (Tuple[int]): Shape of b part from beaver triples protocol.
        ring_size (int) : Ring Size of the triples to generate.
        kwargs: Arbitrary keyword arguments for commands.

    Returns:
        List[List[3 x List[ShareTensor, ShareTensor, ShareTensor]]]:
        The generated triples a,b,c for each party.

    Raises:
        ValueError: If the triples are not consistent.
        ValueError: If the share class is invalid.
    """
    # relative
    from ..... import Tensor

    cmd = getattr(operator, op_str)
    min_value, max_value = ShareTensor.compute_min_max_from_ring(ring_size)
    seed_przs = secrets.randbits(32)

    a_rand = Tensor(
        ttp_generator.integers(
            low=min_value, high=max_value, size=a_shape, endpoint=True, dtype=np.int32
        )
    )
    a_shares = MPCTensor._get_shares_from_local_secret(
        secret=a_rand,
        parties_info=parties_info,  # type: ignore
        shape=a_shape,
        seed_przs=seed_przs,
    )

    b_rand = Tensor(
        ttp_generator.integers(
            low=min_value, high=max_value, size=b_shape, endpoint=True, dtype=np.int32
        )
    )

    b_shares = MPCTensor._get_shares_from_local_secret(
        secret=b_rand,
        parties_info=parties_info,  # type: ignore
        shape=b_shape,
        seed_przs=seed_przs,
    )

    c_val = cmd(a_rand, b_rand, **kwargs)

    c_shares = MPCTensor._get_shares_from_local_secret(
        secret=c_val,
        parties_info=parties_info,  # type: ignore
        shape=c_val.shape,
        seed_przs=seed_przs,
    )
    print("a", a_shares)
    print("b", b_shares)
    print("c", c_shares)
    # We are always creating an instance
    triple_sequential = [(a_shares, b_shares, c_shares)]<commit_msg>Initialize new seed for each local secret computation<commit_after>"""Beaver Triples Protocol.

<NAME>. *Efficient multiparty protocols using circuit randomization*.
In <NAME>, editor, CRYPTO, volume **576** of Lecture Notes in
Computer Science, pages 420–432. Springer, 1991.
"""


# stdlib
import operator
import secrets
from typing import Any
from typing import Dict
from typing import List
from typing import Tuple

# third party
import numpy as np

# relative
from ....tensor.smpc.mpc_tensor import MPCTensor
from ....tensor.smpc.share_tensor import ShareTensor
from ...store import register_primitive_generator
from ...store import register_primitive_store_add
from ...store import register_primitive_store_get
from ...store.exceptions import EmptyPrimitiveStore

ttp_generator = np.random.default_rng()


def _get_triples(
    op_str: str,
    nr_parties: int,
    parties_info: List[Any],
    a_shape: Tuple[int],
    b_shape: Tuple[int],
    ring_size: int = 2 ** 32,
    **kwargs: Dict[Any, Any],
) -> Tuple[Tuple[Tuple[ShareTensor, ShareTensor, ShareTensor]]]:
    """Get triples.

    The Trusted Third Party (TTP) or Crypto Provider should provide this triples Currently,
    the one that orchestrates the communication provides those triples.".

    Args:
        op_str (str): Operator string.
        nr_parties (int): Number of parties
        parties_info (List[Any]): Parties connection information.
        a_shape (Tuple[int]): Shape of a from beaver triples protocol.
        b_shape (Tuple[int]): Shape of b part from beaver triples protocol.
        ring_size (int) : Ring Size of the triples to generate.
        kwargs: Arbitrary keyword arguments for commands.

    Returns:
        List[List[3 x List[ShareTensor, ShareTensor, ShareTensor]]]:
        The generated triples a,b,c for each party.

    Raises:
        ValueError: If the triples are not consistent.
        ValueError: If the share class is invalid.
    """
    # relative
    from ..... import Tensor

    cmd = getattr(operator, op_str)
    min_value, max_value = ShareTensor.compute_min_max_from_ring(ring_size)
    seed_przs = secrets.randbits(32)

    a_rand = Tensor(
        ttp_generator.integers(
            low=min_value, high=max_value, size=a_shape, endpoint=True, dtype=np.int32
        )
    )
    a_shares = MPCTensor._get_shares_from_local_secret(
        secret=a_rand,
        parties_info=parties_info,  # type: ignore
        shape=a_shape,
        seed_przs=seed_przs,
    )
    seed_przs = secrets.randbits(32)
    b_rand = Tensor(
        ttp_generator.integers(
            low=min_value, high=max_value, size=b_shape, endpoint=True, dtype=np.int32
        )
    )

    b_shares = MPCTensor._get_shares_from_local_secret(
        secret=b_rand,
        parties_info=parties_info,  # type: ignore
        shape=b_shape,
        seed_przs=seed_przs,
    )
    seed_przs = secrets.randbits(32)
    c_val = cmd(a_rand, b_rand, **kwargs)

    c_shares = MPCTensor._get_shares_from_local_secret(
        secret=c_val,
        parties_info=parties_info,  # type: ignore
        shape=c_val.shape,
        seed_przs=seed_przs,
    )
    print("a", a_shares)
    print("b", b_shares)
    print("c", c_shares)
    # We are always creating an instance
    triple_sequential = [(a_shares, b_shares, c_shares)]
{'fraction_non_alphanumeric': 0.0905463474524248, 'fraction_numerical': 0.007673419275629221, 'mean_word_length': 3.1775641025641024, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5710013', 'n_tokens_mistral': 2136, 'n_tokens_neox': 2062, 'n_words': 560}