arcium-core-utils 0.4.5

Arcium core utils
Documentation
use primitives::{
    algebra::elliptic_curve::{Curve, Point, Scalar},
    types::PeerNumber,
};
use serde::Deserialize;

use crate::circuit::{
    v1::{
        constants::{
            BaseFieldPlaintext,
            BaseFieldPlaintextBatch,
            BitPlaintext,
            BitPlaintextBatch,
            Mersenne107Plaintext,
            Mersenne107PlaintextBatch,
            PointPlaintext,
            PointPlaintextBatch,
            ScalarPlaintext,
            ScalarPlaintextBatch,
        },
        errors::OpsConversionError,
        AlgebraicType,
    },
    v2,
};

/// Enum representing unary operations on field element plaintexts.
pub type FieldPlaintextUnaryOp = v2::FieldPlaintextUnaryOp;

/// Enum representing binary operations on field element plaintexts.
pub type FieldPlaintextBinaryOp = v2::FieldPlaintextBinaryOp;

/// Enum representing unary operations on a field share.
pub type FieldShareUnaryOp = v2::FieldShareUnaryOp;

/// Enum representing binary operations on field shares. This includes the case where the second
/// operand is a plaintext value.
pub type FieldShareBinaryOp = v2::FieldShareBinaryOp;

/// Enum representing unary operations on binary shares
pub type BitShareUnaryOp = v2::BitShareUnaryOp;

/// Enum representing binary operations on binary shares.
pub type BitShareBinaryOp = v2::BitShareBinaryOp;

/// Enum representing unary operations on plaintext points.
pub type PointPlaintextUnaryOp = v2::PointPlaintextUnaryOp;

/// Enum representing binary operations on plaintext points/scalars.
pub type PointPlaintextBinaryOp = v2::PointPlaintextBinaryOp;

/// Enum representing unary operations on point shares.
pub type PointShareUnaryOp = v2::PointShareUnaryOp;

/// Enum representing binary operations on point shares.
pub type PointShareBinaryOp = v2::PointShareBinaryOp;

impl TryFrom<FieldPlaintextUnaryOp> for v2::BitPlaintextUnaryOp {
    type Error = OpsConversionError;

    fn try_from(value: FieldPlaintextUnaryOp) -> Result<Self, Self::Error> {
        match value {
            FieldPlaintextUnaryOp::Neg => Ok(Self::Not),
            _ => Err(OpsConversionError::PlaintextUnaryOpFieldToBit(value)),
        }
    }
}

impl TryFrom<FieldPlaintextBinaryOp> for v2::BitPlaintextBinaryOp {
    type Error = OpsConversionError;

    fn try_from(value: FieldPlaintextBinaryOp) -> Result<Self, Self::Error> {
        match value {
            FieldPlaintextBinaryOp::Mul => Ok(Self::And),
            FieldPlaintextBinaryOp::Xor => Ok(Self::Xor),
            FieldPlaintextBinaryOp::Or => Ok(Self::Or),
            _ => Err(OpsConversionError::PlaintextBinaryOpFieldToBit(value)),
        }
    }
}

#[derive(Deserialize)]
#[serde(bound(deserialize = "Scalar<C>: Deserialize<'de>, Point<C>: Deserialize<'de>"))]
#[repr(C)]
pub enum Input<C: Curve> {
    SecretPlaintext {
        inputer: PeerNumber,
        algebraic_type: AlgebraicType,
        batched: Batched,
    },
    Share {
        algebraic_type: AlgebraicType,
        batched: Batched,
    },
    RandomShare {
        algebraic_type: AlgebraicType,
        batched: Batched,
    },
    Scalar(ScalarPlaintext<C>),
    ScalarBatch(ScalarPlaintextBatch<C>),
    BaseField(BaseFieldPlaintext<C>),
    BaseFieldBatch(BaseFieldPlaintextBatch<C>),
    Mersenne107(Mersenne107Plaintext),
    Mersenne107Batch(Mersenne107PlaintextBatch),
    Bit(BitPlaintext),
    BitBatch(BitPlaintextBatch),
    Point(PointPlaintext<C>),
    PointBatch(PointPlaintextBatch<C>),
}

/// Enum representing whether a gate is batched or not.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize)]
#[repr(C)]
pub enum Batched {
    Yes(usize),
    No,
}

impl Batched {
    pub fn count(&self) -> usize {
        match self {
            Batched::Yes(count) => *count,
            Batched::No => 1,
        }
    }
}