Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_crypto/src/pqc/macros_dilithium.rs

3.3 KiB, 1 run

created by r1870400018:238, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1#[macro_export]
2macro_rules! shake128 {
3 ( $output:expr ; $( $input:expr ),* ) => {
4 let mut hasher = sha3::Shake128::default();
5 $(
6 digest::Input::process(&mut hasher, $input);
7 )*
8 let mut reader = digest::ExtendableOutput::xof_result(hasher);
9 digest::XofReader::read(&mut reader, $output);
10 }
11}
12
13#[macro_export]
14macro_rules! shake256 {
15 ( $output:expr ; $( $input:expr ),* ) => {
16 let mut hasher = sha3::Shake256::default();
17 $(
18 digest::Input::process(&mut hasher, $input);
19 )*
20 let mut reader = digest::ExtendableOutput::xof_result(hasher);
21 digest::XofReader::read(&mut reader, $output);
22 }
23}
24
25#[macro_export]
26macro_rules! polyvec {
27 ( $polyvec:ident, $len:expr ) => {
28
29 #[derive(Copy, Clone)]
30 pub struct $polyvec(pub [Poly; $len]);
31
32 impl $polyvec {
33 pub fn reduce(&mut self) {
34 self.0.iter_mut()
35 .for_each(poly::reduce)
36 }
37
38 pub fn csubq(&mut self) {
39 self.0.iter_mut()
40 .for_each(poly::csubq)
41 }
42
43 pub fn freeze(&mut self) {
44 self.0.iter_mut()
45 .for_each(poly::freeze)
46 }
47
48 pub fn with_add(&mut self, u: &Self, v: &Self) {
49 for i in 0..$len {
50 poly::add(&mut self[i], &u[i], &v[i]);
51 }
52 }
53
54 pub fn add_assign(&mut self, u: &Self) {
55 for i in 0..$len {
56 poly::add_assign(&mut self[i], &u[i]);
57 }
58 }
59
60 pub fn with_sub(&mut self, u: &Self, v: &Self) {
61 for i in 0..$len {
62 poly::sub(&mut self[i], &u[i], &v[i]);
63 }
64 }
65
66 pub fn shift_left(&mut self, k: u32) {
67 self.0.iter_mut()
68 .for_each(|p| poly::shift_left(p, k));
69 }
70
71 pub fn ntt(&mut self) {
72 self.0.iter_mut()
73 .for_each(poly::ntt);
74 }
75
76 pub fn invntt_montgomery(&mut self) {
77 self.0.iter_mut()
78 .for_each(poly::invntt_montgomery)
79 }
80
81 pub fn chknorm(&self, bound: u32) -> bool {
82 self.0.iter()
83 .map(|p| poly::chknorm(p, bound))
84 .fold(false, |x, y| x | y)
85 }
86 }
87
88 impl ::core::ops::Index<usize> for $polyvec {
89 type Output = Poly;
90
91 #[inline(always)]
92 fn index(&self, i: usize) -> &Self::Output {
93 self.0.index(i)
94 }
95 }
96
97 impl ::core::ops::IndexMut<usize> for $polyvec {
98 #[inline(always)]
99 fn index_mut(&mut self, i: usize) -> &mut Self::Output {
100 self.0.index_mut(i)
101 }
102 }
103
104 impl ::core::cmp::PartialEq for $polyvec {
105 fn eq(&self, other: &Self) -> bool {
106 self.0.iter().zip(&other.0)
107 .flat_map(|(x, y)| x.iter().zip(y.iter()))
108 .all(|(x, y)| x == y)
109 }
110 }
111
112 impl Eq for $polyvec {}
113
114 impl Default for $polyvec {
115 fn default() -> Self {
116 $polyvec([[0; N]; $len])
117 }
118 }
119 }
120}