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: u64Implementations§
Source§impl Field
impl Field
Sourcepub const MOD: u64 = 65_537u64
pub const MOD: u64 = 65_537u64
Set the prime modulus here. For prototype/testing you can change it.
Sourcepub fn new(x: u128) -> Self
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}pub fn zero() -> Self
Sourcepub fn one() -> Self
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
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}Trait Implementations§
impl Copy for Field
impl Eq for Field
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more