oxedyne/fe2o3/fe2o3_crypto/tests/pad.rs
5.3 KiB, 1 run
created by r1870400018:76449, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Padmé padding (`oxedyne_fe2o3_crypto::pad`) against the shared vectors in |
| 2 | //! `tests/data/padme_vectors.txt`, which the JS twin (`js/test/pad.test.mjs`) checks too, and the |
| 3 | //! overhead bound of the PURBs paper (arXiv 1806.03160 §4.4, equation 1). |
| 4 | |
| 5 | use oxedyne_fe2o3_crypto::pad::{ |
| 6 | self, |
| 7 | PadConfig, |
| 8 | PadRule, |
| 9 | }; |
| 10 | |
| 11 | use oxedyne_fe2o3_core::prelude::*; |
| 12 | |
| 13 | const VECTORS: &str = include_str!("data/padme_vectors.txt"); |
| 14 | |
| 15 | fn rule_of(name: &str, floor: u64, block: u64) -> Outcome<PadRule> { |
| 16 | let cfg = PadConfig { rule: name.to_string(), floor, block }; |
| 17 | cfg.rule() |
| 18 | } |
| 19 | |
| 20 | #[test] |
| 21 | fn padme_len_matches_the_shared_vectors() -> Outcome<()> { |
| 22 | let mut seen = 0; |
| 23 | for line in VECTORS.lines() { |
| 24 | let f: Vec<&str> = line.split(' ').collect(); |
| 25 | if f.first() != Some(&"padme") { |
| 26 | continue; |
| 27 | } |
| 28 | let l: u64 = res!(f[1].parse().map_err(|e| err!("{:?}: {}", line, e; Invalid, Input))); |
| 29 | let want: u64 = res!(f[2].parse().map_err(|e| err!("{:?}: {}", line, e; Invalid, Input))); |
| 30 | let got = res!(pad::padme_len(l)); |
| 31 | if got != want { |
| 32 | return Err(err!("padme_len({}) is {}, the vectors say {}.", l, got, want; Mismatch)); |
| 33 | } |
| 34 | seen += 1; |
| 35 | } |
| 36 | if seen < 3000 { |
| 37 | return Err(err!("Only {} padme vectors were read.", seen; Missing)); |
| 38 | } |
| 39 | Ok(()) |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn framed_length_matches_the_shared_vectors() -> Outcome<()> { |
| 44 | let mut seen = 0; |
| 45 | for line in VECTORS.lines() { |
| 46 | let f: Vec<&str> = line.split(' ').collect(); |
| 47 | if f.first() != Some(&"frame") { |
| 48 | continue; |
| 49 | } |
| 50 | let num = |i: usize| -> Outcome<u64> { |
| 51 | f[i].parse::<u64>().map_err(|e| err!("{:?}: {}", line, e; Invalid, Input)) |
| 52 | }; |
| 53 | let rule = res!(rule_of(f[1], res!(num(2)), res!(num(3)))); |
| 54 | let n = res!(num(4)); |
| 55 | let want = res!(num(5)); |
| 56 | let got = res!(rule.framed_len(n)); |
| 57 | if got != want { |
| 58 | return Err(err!("{:?} frames {} body bytes in {}, the vectors say {}.", |
| 59 | rule, n, got, want; Mismatch)); |
| 60 | } |
| 61 | // Small bodies are padded for real, and come back whole. |
| 62 | if n < 5000 { |
| 63 | let body: Vec<u8> = (0..n).map(|i| (i * 7 + 1) as u8).collect(); |
| 64 | let framed = res!(rule.pad(&body)); |
| 65 | if framed.len() as u64 != want { |
| 66 | return Err(err!("pad gave {} bytes, framed_len {}.", framed.len(), want; Mismatch)); |
| 67 | } |
| 68 | let back = res!(pad::unpad(&framed)); |
| 69 | if back != &body[..] { |
| 70 | return Err(err!("unpad did not return the body of {} bytes.", n; Mismatch)); |
| 71 | } |
| 72 | } |
| 73 | seen += 1; |
| 74 | } |
| 75 | if seen < 1000 { |
| 76 | return Err(err!("Only {} frame vectors were read.", seen; Missing)); |
| 77 | } |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn padme_overhead_is_within_the_paper_bound() -> Outcome<()> { |
| 83 | // Equation 1: the added bytes are below 2^(E-S), and the overhead under 12% everywhere. The |
| 84 | // paper's text quotes +11.11% (9 bytes to 10) as the maximum, but its own Algorithm 1 pads 129 |
| 85 | // bytes to 144 (+11.63%), so the ceiling asserted is its "always less than 12%". |
| 86 | let mut worst = 0.0f64; |
| 87 | let mut l: u64 = 2; |
| 88 | while l < (1u64 << 22) { |
| 89 | let p = res!(pad::padme_len(l)); |
| 90 | if p < l { |
| 91 | return Err(err!("padme_len({}) = {} shrinks.", l, p; Invalid)); |
| 92 | } |
| 93 | let e = 63 - l.leading_zeros() as u64; |
| 94 | let s = 64 - e.leading_zeros() as u64; |
| 95 | if p - l >= (1u64 << (e - s)).max(1) { |
| 96 | return Err(err!("padme_len({}) = {} adds 2^(E-S) or more.", l, p; Invalid)); |
| 97 | } |
| 98 | let over = (p - l) as f64 / l as f64; |
| 99 | if over >= 0.12 { |
| 100 | return Err(err!("padme_len({}) = {}, overhead {} over 12%.", l, p, over; Invalid)); |
| 101 | } |
| 102 | if l > 256 { |
| 103 | worst = worst.max(over); |
| 104 | } |
| 105 | l += if l < 70_000 { 1 } else { 997 }; |
| 106 | } |
| 107 | if worst <= 0.0 { |
| 108 | return Err(err!("No overhead was measured."; Missing)); |
| 109 | } |
| 110 | Ok(()) |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn unpad_refuses_a_frame_that_lies() -> Outcome<()> { |
| 115 | let rule = res!(rule_of("padme", 64, 0)); |
| 116 | let mut framed = res!(rule.pad(b"hello")); |
| 117 | // Shorter than its prefix. |
| 118 | if pad::unpad(&framed[..3]).is_ok() { |
| 119 | return Err(err!("A 3-byte frame was accepted."; Invalid)); |
| 120 | } |
| 121 | // A prefix claiming more than the frame holds. |
| 122 | framed[0] = 0xff; |
| 123 | if pad::unpad(&framed).is_ok() { |
| 124 | return Err(err!("A prefix past the end was accepted."; Invalid)); |
| 125 | } |
| 126 | // Non-zero fill. |
| 127 | let mut framed = res!(rule.pad(b"hello")); |
| 128 | let last = framed.len() - 1; |
| 129 | framed[last] = 1; |
| 130 | if pad::unpad(&framed).is_ok() { |
| 131 | return Err(err!("A frame with non-zero fill was accepted."; Invalid)); |
| 132 | } |
| 133 | Ok(()) |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn config_names_the_rule() -> Outcome<()> { |
| 138 | if rule_of("block", 0, 0).is_ok() { |
| 139 | return Err(err!("A block rule of zero bytes was accepted."; Invalid)); |
| 140 | } |
| 141 | if rule_of("constant-ish", 0, 0).is_ok() { |
| 142 | return Err(err!("An unknown rule name was accepted."; Invalid)); |
| 143 | } |
| 144 | let mut cfg = PadConfig::default(); |
| 145 | if res!(cfg.rule()) != (PadRule::Padme { floor: cfg.floor }) { |
| 146 | return Err(err!("The default rule is not Padmé."; Invalid)); |
| 147 | } |
| 148 | cfg.rule = "pow2".to_string(); |
| 149 | match res!(cfg.rule()) { |
| 150 | PadRule::PowerOfTwo { .. } => Ok(()), |
| 151 | other => Err(err!("pow2 read as {:?}.", other; Invalid)), |
| 152 | } |
| 153 | } |