fss_rs/group/
byte.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2023 Yulong Ming (myl7)
3
4//! Byte vectors as a group.
5//!
6//! - Associative operation: XOR.
7//! - Identity element: All bits zero.
8//! - Inverse element: `x` itself.
9
10use std::ops::{Add, AddAssign, Neg};
11
12use super::Group;
13use crate::utils::xor_inplace;
14
15/// See [`self`].
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct ByteGroup<const BLEN: usize>(pub [u8; BLEN]);
18
19impl<const BLEN: usize> Add for ByteGroup<BLEN> {
20    type Output = Self;
21
22    fn add(mut self, rhs: Self) -> Self::Output {
23        xor_inplace(&mut self.0, &[&rhs.0]);
24        self
25    }
26}
27
28impl<const BLEN: usize> AddAssign for ByteGroup<BLEN> {
29    fn add_assign(&mut self, rhs: Self) {
30        xor_inplace(&mut self.0, &[&rhs.0])
31    }
32}
33
34impl<const BLEN: usize> Neg for ByteGroup<BLEN> {
35    type Output = Self;
36
37    fn neg(self) -> Self::Output {
38        self
39    }
40}
41
42impl<const BLEN: usize> Group<BLEN> for ByteGroup<BLEN> {
43    fn zero() -> Self {
44        ByteGroup([0; BLEN])
45    }
46}
47
48impl<const BLEN: usize> From<[u8; BLEN]> for ByteGroup<BLEN> {
49    fn from(value: [u8; BLEN]) -> Self {
50        Self(value)
51    }
52}
53
54impl<const BLEN: usize> From<ByteGroup<BLEN>> for [u8; BLEN] {
55    fn from(value: ByteGroup<BLEN>) -> Self {
56        value.0
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::test_group_axioms;
64
65    test_group_axioms!(test_group_axioms, ByteGroup<16>, 16);
66}