nimiq-utils 1.2.2

Various utilities (e.g., CRC, Merkle proofs, timers) for Nimiq's Rust implementation
Documentation
use std::io;

#[derive(Default)]
pub struct Crc8Computer {
    value: u8,
}

#[allow(clippy::unreadable_literal)]
impl Crc8Computer {
    const TABLE: [u8; 256] = [
        0, 151, 185, 46, 229, 114, 92, 203, 93, 202, 228, 115, 184, 47, 1, 150, 186, 45, 3, 148,
        95, 200, 230, 113, 231, 112, 94, 201, 2, 149, 187, 44, 227, 116, 90, 205, 6, 145, 191, 40,
        190, 41, 7, 144, 91, 204, 226, 117, 89, 206, 224, 119, 188, 43, 5, 146, 4, 147, 189, 42,
        225, 118, 88, 207, 81, 198, 232, 127, 180, 35, 13, 154, 12, 155, 181, 34, 233, 126, 80,
        199, 235, 124, 82, 197, 14, 153, 183, 32, 182, 33, 15, 152, 83, 196, 234, 125, 178, 37, 11,
        156, 87, 192, 238, 121, 239, 120, 86, 193, 10, 157, 179, 36, 8, 159, 177, 38, 237, 122, 84,
        195, 85, 194, 236, 123, 176, 39, 9, 158, 162, 53, 27, 140, 71, 208, 254, 105, 255, 104, 70,
        209, 26, 141, 163, 52, 24, 143, 161, 54, 253, 106, 68, 211, 69, 210, 252, 107, 160, 55, 25,
        142, 65, 214, 248, 111, 164, 51, 29, 138, 28, 139, 165, 50, 249, 110, 64, 215, 251, 108,
        66, 213, 30, 137, 167, 48, 166, 49, 31, 136, 67, 212, 250, 109, 243, 100, 74, 221, 22, 129,
        175, 56, 174, 57, 23, 128, 75, 220, 242, 101, 73, 222, 240, 103, 172, 59, 21, 130, 20, 131,
        173, 58, 241, 102, 72, 223, 16, 135, 169, 62, 245, 98, 76, 219, 77, 218, 244, 99, 168, 63,
        17, 134, 170, 61, 19, 132, 79, 216, 246, 97, 247, 96, 78, 217, 18, 133, 171, 60,
    ];

    pub fn update(&mut self, buf: &[u8]) -> &mut Self {
        for &i in buf {
            self.value = Crc8Computer::TABLE[(self.value ^ i) as usize];
        }
        self
    }

    pub fn result(&self) -> u8 {
        self.value
    }
}

impl io::Write for Crc8Computer {
    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
        self.update(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> Result<(), io::Error> {
        Ok(())
    }
}