Debian Codegraph @

third_party/rust/clubcard/src/equation.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use std::cmp::min;
6
7 /// An Equation\<W\> is a representation of a GF(2) linear functional
8 /// a(x) = b + sum_i a_i x_i
9 /// where a_i is equal to zero except for i in a block of 64*W coefficients
10 /// starting at i=s. We say an Equation is /aligned/ if a_s = 1.
11 /// (Note: a_i above denotes the i-th bit, not the i'th 64-bit limb.)
12 #[derive(Clone, Debug, PartialEq, Eq)]
13 pub struct Equation<const W: usize> {
14 pub s: usize, // the row number
15 pub a: [u64; W], // the non-trivial columns
16 pub b: u8, // the constant term
17 }
18
19 impl<const W: usize> Equation<W> {
20 /// Construct the equation a(x) = sum_{i=s}^{s+64*W} a_i x^i.
21 /// The result is aligned.
22 pub fn homogeneous(s: usize, a: [u64; W]) -> Equation<W> {
23 Equation::inhomogeneous(s, a, 0)
24 }
25
26 /// Construct the equation a(x) = b + sum_{i=s}^{s+64*W} a_i x^i.
27 /// The result is aligned.
28 pub fn inhomogeneous(s: usize, a: [u64; W], b: u8) -> Equation<W> {
29 let mut eq = Equation { s: 0, a, b };
30 eq.add(&Equation::zero());
31 eq.s += s;
32 eq
33 }
34
35 /// Construct the equation a(x) = 0.
36 pub fn zero() -> Self {
37 Equation {
38 s: 0,
39 a: [0u64; W],
40 b: 0,
41 }
42 }
43
44 /// Is this a(x) = 1 or a(x) = 0?
45 pub fn is_zero(&self) -> bool {
46 // TODO: is_const? or maybe this gets the point across.
47 self.a == [0u64; W]
48 }
49
50 /// Adds `other` into `self`, i.e. sets self.a ^= other.a and self.b ^= other.b and then aligns
51 /// the result.
52 pub fn add(&mut self, other: &Equation<W>) {
53 assert!(self.s == other.s);
54 // Add the equations in GF(2)
55 for i in 0..W {
56 self.a[i] ^= other.a[i];
57 }
58 self.b ^= other.b;
59 // Exit early if this equation is now zero.
60 if self.is_zero() {
61 return;
62 }
63 // Shift until there is a non-zero bit in the lowest limb.
64 while self.a[0] == 0 {
65 self.a.rotate_left(1);
66 }
67 // Shift first non-zero bit to position 0.
68 let k = self.a[0].trailing_zeros();
69 if k == 0 {
70 return;
71 }
72 for i in 0..W - 1 {
73 self.a[i] >>= k;
74 self.a[i] |= self.a[i + 1] << (64 - k);
75 }
76 self.a[W - 1] >>= k;
77 // Update the starting position
78 self.s += k as usize;
79 }
80
81 /// Computes a(z) = sum a_i z_i.
82 pub fn eval(&self, z: &[u64]) -> u8 {
83 // Compute a(z), noting that this only depends
84 // on 64*W bits of z starting from position s.
85 let limb = self.s / 64;
86 let shift = self.s % 64;
87 let mut r = 0;
88 for i in limb..min(z.len(), limb + W) {
89 let mut tmp = z[i] >> shift;
90 if i + 1 < z.len() && shift != 0 {
91 tmp |= z[i + 1] << (64 - shift);
92 }
93 r ^= tmp & self.a[i - limb];
94 }
95 (r.count_ones() & 1) as u8
96 }
97 }
98
99 #[cfg(test)]
100 mod tests {
101 use crate::Equation;
102
103 #[test]
104 fn test_equation_add() {
105 let mut e1 = Equation {
106 s: 127,
107 a: [0b11],
108 b: 1,
109 };
110 let e2 = Equation {
111 s: 127,
112 a: [0b01],
113 b: 1,
114 };
115 e1.add(&e2);
116 // test that shifting works
117 assert!(e1.s == 128);
118 assert!(e1.a[0] == 0b1);
119 assert!(e1.b == 0);
120
121 let mut e1 = Equation {
122 s: 127,
123 a: [0b11, 0b1110, 0b1, 0],
124 b: 1,
125 };
126 let e2 = Equation {
127 s: 127,
128 a: [0b01, 0b0100, 0b0, 0],
129 b: 1,
130 };
131 e1.add(&e2);
132 // test that shifting works
133 assert!(e1.s == 128);
134 assert!(e1.a[0] == 0b1);
135 // test that bits move between limbs
136 assert!(e1.a[1] == (1 << 63) | 0b101);
137 assert!(e1.a[2] == 0);
138 assert!(e1.a[3] == 0);
139 assert!(e1.b == 0);
140 }
141
142 #[test]
143 fn test_equation_eval() {
144 for s in 0..64 {
145 let eq = Equation {
146 s,
147 a: [0xffffffffffffffff, 0, 0, 0],
148 b: 0,
149 };
150 assert!(0 == eq.eval(&[]));
151 for i in 0..64 {
152 assert!(((i >= eq.s) as u8) == eq.eval(&[1 << i, 0]));
153 assert!(((i < eq.s) as u8) == eq.eval(&[0, 1 << i]));
154 assert!(0 == eq.eval(&[0, 0, 1 << i]));
155 }
156 }
157 }
158 }