arcium-core-utils 0.8.1

Arcium core utils
Documentation
use primitives::{
    algebra::elliptic_curve::{Curve, Point, Scalar},
    types::PeerNumber,
};
use serde::Deserialize;
#[cfg(test)]
use serde::Serialize;

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

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

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

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

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

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

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

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

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

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

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

impl TryFrom<FieldPlaintextUnaryOp> for latest::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 latest::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)]
#[cfg_attr(test, derive(Serialize))]
#[serde(bound(
    deserialize = "Scalar<C>: Deserialize<'de>, Point<C>: Deserialize<'de>",
    serialize = "Scalar<C>: Serialize, Point<C>: Serialize"
))]
#[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)]
#[cfg_attr(test, derive(Serialize))]
#[repr(C)]
pub enum Batched {
    Yes(usize),
    No,
}

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