Field

Struct Field 

Source
pub struct Field {
    pub v: u64,
}
Expand description

Simple finite field arithmetic modulo a prime p (u64). Uses u128 intermediates to avoid overflow on multiplication.

Fields§

§v: u64

Implementations§

Source§

impl Field

Source

pub const MOD: u64 = 65_537u64

Set the prime modulus here. For prototype/testing you can change it.

Source

pub fn new(x: u128) -> Self

Create element with reduction

Examples found in repository?
examples/channel.rs (line 13)
3fn main() {
4    // Example coefficients for recurrence: x^3 + 2x^2 + 3x + 4
5    let coeffs = vec![4, 3, 2];
6    let cm = CompanionMatrix::from_coeffs(&coeffs);
7
8    // Example polynomial for T: g(x) = 1 + 2x
9    let g_coeffs = vec![1, 2];
10    let t = StreamGenerator::build_t_from_poly(&cm, &g_coeffs);
11
12    // Initial state vector
13    let state = vec![Field::new(1), Field::new(0), Field::new(0)];
14
15    let mut sg = StreamGenerator::new(cm, t, state);
16
17    // Generate and print first 10 outputs
18    for _ in 0..10 {
19        let output = sg.step();
20        println!("{:?}", output);
21    }
22}
Source

pub fn zero() -> Self

Source

pub fn one() -> Self

Examples found in repository?
examples/net_receiver.rs (line 9)
5fn handle_client(mut socket: TcpStream) -> io::Result<()> {
6    let coeffs = vec![1, 2, 3];
7    let companion = CompanionMatrix::from_coeffs(&coeffs);
8    let t = StreamGenerator::build_t_from_poly(&companion, &coeffs);
9    let state = vec![Field::one(); companion.k];
10    let mut sg = StreamGenerator::new(companion, t, state);
11
12    let mut buf = [0u8; 1024];
13    let stdout = io::stdout();
14    let mut out = stdout.lock();
15
16    loop {
17        let n = socket.read(&mut buf)?;
18        if n == 0 { break; }
19
20        for &b in &buf[..n] {
21            let _mask = sg.step();
22            let decoded = b.wrapping_sub((_mask.v & 0xFF) as u8);
23            write!(out, "{}", decoded as char)?;
24        }
25        out.flush()?;
26    }
27
28    Ok(())
29}
More examples
Hide additional examples
examples/net_sender.rs (line 14)
6fn main() -> io::Result<()> {
7    // Connect to receiver
8    let mut stream = TcpStream::connect("127.0.0.1:4000")?;
9
10    // Example polynomial coefficients and state
11    let coeffs = vec![1, 2, 3];
12    let companion = CompanionMatrix::from_coeffs(&coeffs);
13    let t = StreamGenerator::build_t_from_poly(&companion, &coeffs);
14    let state = vec![Field::one(); companion.k];
15    let mut sg = StreamGenerator::new(companion, t, state);
16
17    // Read plain data from stdin
18    let mut input = Vec::new();
19    io::stdin().read_to_end(&mut input)?;
20
21    // Encode and send
22    for b in input {
23        let _mask = sg.step();
24        // very simple “encryption”: just add the mask
25        let encoded = b.wrapping_add((_mask.v & 0xFF) as u8);
26        stream.write_all(&[encoded])?;
27    }
28
29    Ok(())
30}
Source

pub fn pow(self, exp: u128) -> Self

fast exponentiation

Source

pub fn inv(self) -> Option<Self>

Multiplicative inverse using Extended Euclidean algorithm (since modulus is prime)

Trait Implementations§

Source§

impl Add for Field

Source§

type Output = Field

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Field

Source§

fn clone(&self) -> Field

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Field

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Mul for Field

Source§

type Output = Field

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Self

Performs the * operation. Read more
Source§

impl PartialEq for Field

Source§

fn eq(&self, other: &Field) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub for Field

Source§

type Output = Field

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self

Performs the - operation. Read more
Source§

impl Copy for Field

Source§

impl Eq for Field

Source§

impl StructuralPartialEq for Field

Auto Trait Implementations§

§

impl Freeze for Field

§

impl RefUnwindSafe for Field

§

impl Send for Field

§

impl Sync for Field

§

impl Unpin for Field

§

impl UnwindSafe for Field

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V