Skip to content

Instantly share code, notes, and snippets.

@Thiez
Forked from anonymous/optimization
Created November 12, 2016 09:36
Show Gist options
  • Save Thiez/b9c9af52a9906029133e97d6256c79c1 to your computer and use it in GitHub Desktop.
Save Thiez/b9c9af52a9906029133e97d6256c79c1 to your computer and use it in GitHub Desktop.
//replacement constant
pub const P: [u8; 256] = [
60, 63, 58, 160, 99, 193, 14, 189, 7, 116, 255, 230, 172,
201, 132, 186, 10, 203, 211, 20, 180, 106, 0, 21, 174, 119,
39, 225, 238, 38, 208, 76, 223, 216, 213, 150, 118, 77, 82,
42, 125, 68, 34, 232, 107, 252, 195, 247, 243, 169, 248, 120,
254, 123, 171, 103, 146, 65, 98, 61, 147, 114, 67, 134, 251,
59, 234, 87, 88, 53, 40, 196, 79, 168, 95, 113, 104, 83,
36, 73, 57, 190, 249, 75, 242, 129, 143, 110, 237, 94, 145,
97, 179, 197, 206, 24, 1, 16, 128, 89, 45, 101, 71, 192,
224, 185, 4, 209, 62, 3, 19, 198, 162, 205, 219, 159, 86,
144, 74, 90, 49, 18, 157, 246, 37, 127, 117, 108, 165, 93,
227, 15, 131, 66, 176, 222, 167, 182, 214, 212, 80, 207, 47,
170, 188, 78, 9, 121, 250, 235, 194, 85, 158, 181, 44, 161,
226, 163, 55, 215, 151, 191, 92, 173, 229, 64, 91, 17, 35,
218, 27, 43, 6, 202, 244, 220, 133, 81, 204, 72, 69, 155,
25, 111, 30, 12, 137, 228, 166, 231, 102, 221, 130, 152, 138,
140, 5, 2, 187, 29, 50, 48, 122, 13, 135, 22, 177, 136,
33, 51, 233, 141, 245, 139, 199, 54, 210, 239, 164, 184, 31,
52, 23, 148, 200, 253, 41, 124, 96, 70, 154, 126, 109, 236,
26, 142, 46, 149, 217, 105, 178, 241, 156, 153, 11, 115, 183,
8, 28, 240, 32, 56, 84, 100, 112, 175
];
//swapping constant
pub const T: [u8; 64] = [
63, 8, 16, 24, 32, 40, 48, 56,
1, 9, 17, 25, 33, 41, 49, 57,
2, 10, 18, 26, 34, 42, 50, 58,
3, 11, 19, 27, 35, 43, 51, 59,
4, 12, 20, 28, 36, 44, 52, 60,
5, 13, 21, 29, 37, 45, 53, 61,
6, 14, 22, 30, 38, 46, 54, 62,
7, 15, 23, 31, 39, 47, 55, 0
];
#[inline(never)]
fn original(input: [u8; 64]) -> [u8; 64] {
let mut block = [0; 64];
for (b, j) in input.iter().zip(T.iter()) {
block[*j as usize] = P[*b as usize];
}
block
}
#[inline(never)]
fn mine(input: [u8; 64]) -> [u8; 64] {
let mut block = [0u8; 64];
for n in 0..64 {
let original = input[n];;
let replacement = P[original as usize];
let index = T[n];
block[index as usize] = replacement;
}
block
}
#[inline(never)]
fn mine_alt(input: [u8; 64]) -> [u8; 64] {
fn add(a: &mut [u8; 8], b: [u8; 8]) {
for n in 0..8 {
let res = (a[n] + b[n]) % 64;
a[n] = res;
}
}
let mut block = [0u8; 64];
let mut indices = [63, 8, 16, 24, 32, 40, 48, 56];
for row in 0..8 {
for col in 0..8 {
let original = input[row * 8 + col];
let replacement = P[original as usize];
let index = indices[col];
block[index as usize] = replacement;
}
let offset = match row {
0 => [2, 1, 1, 1, 1, 1, 1, 1],
6 => [1, 1, 1, 1, 1, 1, 1, 2],
_ => [1, 1, 1, 1, 1, 1, 1, 1]
};
add(&mut indices, offset);
}
block
}
fn main() {
let mut h = [0u8; 64];
h.copy_from_slice(b"my data123313123123313312312364534534543534543453453312312313232");
// actual code
let block = original(h);
println!("Orig {:?}", &block[..]);
let block = mine(h);
println!("Mine {:?}", &block[..]);
let block = mine_alt(h);
println!("Mine_alt {:?}", &block[..]);
for n in 0..8 {
let offset = 8 * n;
for i in 0..8 {
print!("{} ", (T[(offset + i) % 64] - T[(offset + i - 8) % 64]) % 64);
}
println!("");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment