Skip to content

Commit

Permalink
Simplify types with overflow detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Sep 8, 2022
1 parent 4ad9106 commit 292d13e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions base_layer/common_types/src/dammsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,25 @@ pub enum ChecksumError {
}

// Fixed for a dictionary size of `2^8 == 256`
const K: u8 = 8;
const COEFFICIENTS: [u8; 4] = [8, 4, 3, 1];
const COEFFICIENTS: [u8; 3] = [4, 3, 1];

/// Compute the DammSum checksum for an array, each of whose elements are in the range `[0, 2^8)`
pub fn compute_checksum(data: &Vec<u8>) -> u8 {
let mut mask = 1u16;
let mut mask = 1u8;

// Compute the bitmask (if possible)
for bit in COEFFICIENTS {
mask += 1u16 << bit;
mask += 1u8 << bit;
}

// Perform the Damm algorithm
let mut result = 0u16;
let mut result = 0u8;

for digit in data {
result ^= u16::from(*digit); // add
result <<= 1u16; // double
if result & (1u16 << K) > 0 {
result ^= *digit; // add
let overflow = (result & (1 << 7)) != 0;
result <<= 1; // double
if overflow {
// reduce
result ^= mask;
}
Expand Down

0 comments on commit 292d13e

Please sign in to comment.