Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. use std::time::{Instant};
  2.  
  3. fn permute_eight_bits(ten_bit: u16) -> u8
  4. {
  5. // P8[6 3 7 4 8 5 10 9]
  6. // P8[5 2 6 3 7 4 9 8] (-1)
  7. let mut permuted_byte = 0b0000_0000_0000_0000;
  8.  
  9. let p_eight: [(u16,u16);8] = [(5, 0b0000_0000_0010_0000),
  10. (2, 0b0000_0000_0000_0100),
  11. (6, 0b0000_0000_0100_0000),
  12. (3, 0b0000_0000_0000_1000),
  13. (7, 0b0000_0000_1000_0000),
  14. (4, 0b0000_0000_0001_0000),
  15. (9, 0b0000_0010_0000_0000),
  16. (8, 0b0000_0001_0000_0000)];
  17.  
  18. for x in 0..8 {
  19. permuted_byte = permuted_byte << 1;
  20. if ten_bit & (1 << p_eight[x].0) == p_eight[x].1 {
  21. permuted_byte = permuted_byte | 1;
  22. }
  23. }
  24. permuted_byte
  25. }
  26.  
  27. fn permute_eight_str(ten_bit: u16) -> String
  28. {
  29. let init_key_str = format!("{:010b}", ten_bit);
  30. let mut permuted_chars: Vec<char> = Vec::new();
  31. let mut permuted_string = String::with_capacity(8);
  32. let p_eight: [usize;8] = [6,3,7,4,8,5,10,9];
  33.  
  34. for x in 0..8 {
  35. permuted_chars.push(init_key_str.chars().nth(p_eight[x]-1).unwrap() as char);
  36. }
  37.  
  38. for c in &permuted_chars // to avoid 'move' errors, we pass a reference
  39. { // as '&permuted_chars' and dereference '*c'
  40. permuted_string.push(*c);
  41. }
  42. permuted_string
  43. }
  44.  
  45. fn main() {
  46.  
  47. let bits = 0b0000_1111_0000_1111;
  48.  
  49. //call permutation using Strings
  50. let mut now = Instant::now();
  51. let string_result = permute_eight_str(bits);
  52. let mut new_now = Instant::now();
  53. let delta = new_now.duration_since(now);
  54.  
  55. //call permutation using Bitshifts
  56. now = Instant::now();
  57. let bits_result = permute_eight_bits(bits);
  58. new_now = Instant::now();
  59. let delta_two = new_now.duration_since(now);
  60.  
  61. println!("str: {:?}, took: {:?}", string_result, delta);
  62. println!("bits: {:08b}, took: {:?}", bits_result, delta_two);
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement