strata/core/src/lib.rs

#![no_std]
//! strata-core โ€” algebraic trait hierarchy for verifiable computation.
//!
//! four tiers of traits organized by consumer need:
//!
//! ## tier 1: universal (this crate)
//!
//! every algebra implements at least one of these.
//!
//! ```text
//! Codec      encode, decode โ€” serialization
//! Semiring   add, mul, zero, one โ€” tropical lives here
//! Ring       + sub, neg โ€” polynomial rings live here
//! Field      + inv, sqrt, double, pow โ€” finite fields live here
//! ```
//!
//! ## tier 2: proof system (`strata-proof`)
//!
//! traits needed by lens (commitment) and zheng (verification).
//!
//! ```text
//! Reduce   reduce(bytes) โ†’ element โ€” Fiat-Shamir challenges
//! Dot      dot(a, b) โ†’ scalar โ€” inner product for constraint evaluation
//! ```
//!
//! ## tier 3: computation (`strata-compute`)
//!
//! traits needed by nox (execution) and jali (ring arithmetic).
//!
//! ```text
//! Spectral   roots_of_unity, two_adicity โ€” fields with NTT/transform domain
//! Bits       to_bits, from_bits โ€” bit decomposition for binary operations
//! ```
//!
//! ## tier 4: structure (`strata-ext`)
//!
//! traits for specific algebraic structures.
//!
//! ```text
//! Extension<Base>   base field, degree, frobenius โ€” tower fields
//! Batch             batch_inv โ€” Montgomery's trick
//! Blind             ct_eq, ct_select โ€” timing-safe operations
//! ```
//!
//! ## the five algebras
//!
//! | type | crate | tiers |
//! |------|-------|-------|
//! | Goldilocks | nebu | Field + Reduce + Dot + Spectral + Bits + Extension + Batch |
//! | Fโ‚‚ยนยฒโธ | kuro | Field + Reduce + Dot + Bits + Extension + Batch |
//! | RingElement | jali | (uses Goldilocks for scalar ops) |
//! | Tropical | trop | Semiring + Codec |
//! | Fq | genies | Field + Reduce + Dot + Batch + Blind |

pub mod testing;

use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};

// โ”€โ”€ tier 1: universal โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

/// serialize algebraic elements to and from bytes.
///
/// `encode` produces canonical bytes. `decode` rejects non-canonical input
/// (returns None) โ€” it NEVER silently reduces. this is critical for soundness:
/// a verifier accepting non-canonical elements can break proof security.
pub trait Codec: Sized {
    /// byte length of the canonical serialized form.
    fn byte_len() -> usize;

    /// serialize to canonical little-endian bytes.
    /// buffer must be at least `byte_len()` bytes.
    fn encode(&self, buf: &mut [u8]);

    /// deserialize from bytes.
    /// returns None if bytes are too short OR the value is non-canonical
    /// (e.g., >= modulus for prime fields).
    fn decode(bytes: &[u8]) -> Option<Self>;
}

/// semiring: two operations with identities. no subtraction.
///
/// addition and multiplication are associative and commutative.
/// multiplication distributes over addition. zero annihilates (a * 0 = 0).
///
/// the tropical semiring (min, +) satisfies this โ€” min has no inverse.
pub trait Semiring:
    Copy + Eq + Add<Output = Self> + Mul<Output = Self> + AddAssign + MulAssign
{
    const ZERO: Self;
    const ONE: Self;
}

/// ring: semiring with subtraction and negation.
///
/// the additive structure is a group (every element has an additive inverse).
/// polynomial ring R_q = F_p[x]/(x^n+1) satisfies this.
pub trait Ring: Semiring + Sub<Output = Self> + Neg<Output = Self> + SubAssign {}

/// field: ring with multiplicative inverse and related operations.
///
/// every nonzero element has a unique multiplicative inverse.
/// Goldilocks (nebu), Fโ‚‚ยนยฒโธ (kuro), F_q (genies) satisfy this.
pub trait Field: Ring {
    /// multiplicative inverse.
    ///
    /// # Panics
    /// panics if self is zero. use `try_inv` for non-panicking version.
    fn inv(self) -> Self;

    /// multiplicative inverse, returning None for zero.
    ///
    /// production code should prefer this over `inv` โ€” panicking on zero
    /// in a verifier is a denial-of-service vector.
    fn try_inv(self) -> Option<Self> {
        if self == Self::ZERO {
            None
        } else {
            Some(self.inv())
        }
    }

    /// aยฒ โ€” often faster than a * a (single squaring vs general multiply).
    fn square(self) -> Self {
        self * self
    }

    /// 2a โ€” via addition, avoids multiply overhead.
    fn double(self) -> Self {
        self + self
    }

    /// square root. returns None if self is a quadratic non-residue.
    ///
    /// for prime fields: Tonelli-Shanks or special-case (3 mod 4, 5 mod 8).
    /// for binary fields: inverse Frobenius (a^(2^(n-1))).
    fn sqrt(self) -> Option<Self>;

    /// a^e via square-and-multiply. e is a single u64.
    fn pow(self, mut e: u64) -> Self {
        let mut base = self;
        let mut result = Self::ONE;
        while e > 0 {
            if e & 1 == 1 {
                result *= base;
            }
            base = base.square();
            e >>= 1;
        }
        result
    }

    /// a^e where e is a multi-limb exponent (little-endian u64 limbs).
    ///
    /// required for genies (512-bit exponents) and any field where
    /// the group order exceeds u64.
    fn pow_bytes(self, exp: &[u64]) -> Self {
        let mut result = Self::ONE;
        for &limb in exp.iter().rev() {
            for bit in (0..64).rev() {
                result = result.square();
                if (limb >> bit) & 1 == 1 {
                    result *= self;
                }
            }
        }
        result
    }

    /// aยฒ in-place โ€” avoids allocation for large elements.
    fn square_in_place(&mut self) {
        *self = self.square();
    }

    /// double in-place.
    fn double_in_place(&mut self) {
        *self = self.double();
    }
}

Synonyms

bbg/src/lib.rs
optica/src/lib.rs
zheng/src/lib.rs
nox/rs/lib.rs
honeycrisp/src/lib.rs
trident/src/lib.rs
lens/src/lib.rs
strata/src/lib.rs
rs/macros/src/lib.rs
strata/nebu/rs/lib.rs
honeycrisp/rane/src/lib.rs
honeycrisp/acpu/src/lib.rs
lens/core/src/lib.rs
rs/mir-format/src/lib.rs
rs/core/src/lib.rs
hemera/wgsl/src/lib.rs
strata/kuro/rs/lib.rs
radio/iroh-ffi/src/lib.rs
cyb/src-tauri/src/lib.rs
radio/iroh-docs/src/lib.rs
strata/compute/src/lib.rs
lens/porphyry/src/lib.rs
radio/cyber-bao/src/lib.rs
radio/iroh-relay/src/lib.rs
lens/assayer/src/lib.rs
lens/brakedown/src/lib.rs
radio/iroh-car/src/lib.rs
honeycrisp/unimem/src/lib.rs
honeycrisp/aruminium/src/lib.rs
lens/binius/src/lib.rs
hemera/rs/src/lib.rs
strata/ext/src/lib.rs
radio/iroh/src/lib.rs
radio/iroh-gossip/src/lib.rs
strata/proof/src/lib.rs
radio/iroh-blobs/src/lib.rs
radio/iroh-base/src/lib.rs
radio/iroh-dns-server/src/lib.rs
radio/iroh-willow/src/lib.rs
lens/ikat/src/lib.rs
rs/tests/macro-integration/src/lib.rs
cw-cyber/contracts/hub-networks/src/lib.rs
radio/tests/integration/src/lib.rs
cw-cyber/contracts/litium-core/src/lib.rs
strata/trop/wgsl/src/lib.rs
strata/kuro/wgsl/src/lib.rs
cw-cyber/contracts/hub-protocols/src/lib.rs
cw-cyber/contracts/cw-cyber-gift/src/lib.rs
strata/trop/rs/src/lib.rs
cw-cyber/contracts/cybernet/src/lib.rs
cw-cyber/contracts/hub-channels/src/lib.rs
strata/nebu/wgsl/src/lib.rs
cw-cyber/contracts/graph-filter/src/lib.rs
cw-cyber/contracts/litium-stake/src/lib.rs
trident/editor/zed/src/lib.rs
radio/iroh-ffi/iroh-js/src/lib.rs
cw-cyber/contracts/hub-tokens/src/lib.rs
cyb/cyb/cyb-services/src/lib.rs
cw-cyber/packages/hub-base/src/lib.rs
strata/genies/rs/src/lib.rs
cw-cyber/contracts/std-test/src/lib.rs
cw-cyber/packages/cyber-std-test/src/lib.rs
cw-cyber/contracts/litium-refer/src/lib.rs
strata/jali/rs/src/lib.rs
cw-cyber/contracts/hub-libs/src/lib.rs
cw-cyber/contracts/litium-wrap/src/lib.rs
cw-cyber/packages/cyber-std/src/lib.rs
strata/genies/wgsl/src/lib.rs
cw-cyber/contracts/hub-skills/src/lib.rs
strata/jali/wgsl/src/lib.rs
cw-cyber/contracts/cw-cyber-subgraph/src/lib.rs
radio/iroh/bench/src/lib.rs
cw-cyber/contracts/litium-mine/src/lib.rs
cw-cyber/contracts/cw-cyber-passport/src/lib.rs

Neighbours