1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use num::{One, Zero};
use std::ops::{Add, Mul};
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum GF2 {
B0,
B1,
}
impl GF2 {
pub fn from_u8(b: u8) -> Self {
if b == 0 {
GF2::B0
} else if b == 1 {
GF2::B1
} else {
panic!("Input must be 0 or 1.");
}
}
}
impl Add for GF2 {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
if self == other {
GF2::B0
} else {
GF2::B1
}
}
}
impl Mul for GF2 {
type Output = Self;
fn mul(self, other: Self) -> Self::Output {
if self == GF2::B1 && other == GF2::B1 {
GF2::B1
} else {
GF2::B0
}
}
}
impl One for GF2 {
fn one() -> GF2 {
GF2::B1
}
}
impl Zero for GF2 {
fn is_zero(&self) -> bool {
self == &GF2::zero()
}
fn zero() -> GF2 {
GF2::B0
}
}