Initial put

This commit is contained in:
Alienscience 2021-12-28 17:56:42 +01:00
parent 68493181cc
commit 42232bd8f1
6 changed files with 104 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
Cargo.lock

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "cyclic-poly-23"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-traits = "0.2"

8
src/README.md Normal file
View file

@ -0,0 +1,8 @@
# Cyclic Poly
From section 2.3
## Rolling
## Decomposable

54
src/cyclic_poly.rs Normal file
View file

@ -0,0 +1,54 @@
use crate::hash_value::HashValue;
pub struct CyclicPoly<T: HashValue> {
current: T,
reverse: [T; 256],
}
impl<T: HashValue> CyclicPoly<T> {
pub fn from_block(block: &[u8]) -> Self {
let n: u32 = block.len() as u32;
let mut ret = Self {
current: T::zero(),
reverse: [T::zero(); 256],
};
for i in 0..ret.reverse.len() {
ret.reverse[i] = T::TRANSFORMATION[i].rotate_left(n);
}
ret.reset(block);
ret
}
pub fn value(&self) -> T {
self.current
}
pub fn rotate(&mut self, old_byte: u8, new_byte: u8) {
let t_in = T::TRANSFORMATION[usize::from(new_byte)];
let t_out = self.reverse[usize::from(old_byte)];
let current = self.current.rotate_left(1);
let current = current ^ t_in;
self.current = current ^ t_out;
}
fn reset(&mut self, block: &[u8]) {
for v in block.iter() {
let current = self.current.rotate_left(1);
let b = usize::from(*v);
let t = T::TRANSFORMATION[b];
self.current = current ^ t;
}
}
pub fn first_child(parent: T, second_child: T, second_block_size: usize) -> T {
let second_block_size = second_block_size as u32;
let first_brotl = parent ^ second_child;
first_brotl.rotate_right(second_block_size)
}
pub fn second_child(parent: T, first_child: T, second_block_size: usize) -> T {
let second_block_size = second_block_size as u32;
let first_brotl = first_child.rotate_left(second_block_size);
parent ^ first_brotl
}
}

15
src/hash_value.rs Normal file
View file

@ -0,0 +1,15 @@
use num_traits::{PrimInt, Unsigned};
pub trait HashValue: Unsigned + PrimInt {
const TRANSFORMATION: [Self; 256];
}
impl HashValue for u32 {
// TODO: Calculate
const TRANSFORMATION: [Self; 256] = [0; 256];
}
impl HashValue for u64 {
// TODO: Calculate
const TRANSFORMATION: [Self; 256] = [0; 256];
}

16
src/lib.rs Normal file
View file

@ -0,0 +1,16 @@
#![forbid(unsafe_code)]
mod cyclic_poly;
mod hash_value;
pub type CyclicPoly32 = cyclic_poly::CyclicPoly<u32>;
pub type CyclicPoly64 = cyclic_poly::CyclicPoly<u64>;
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}