|
| 1 | +//! This test tries to load various kind of corrupted serialized inputs and see that they are |
| 2 | +//! handled without crashes. |
| 3 | +
|
| 4 | +use std::fs::File; |
| 5 | +use std::io::Read; |
| 6 | +use std::ops::{Add, Mul, Sub}; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use tfhe::conformance::ListSizeConstraint; |
| 10 | +use tfhe::integer::ciphertext::IntegerProvenCompactCiphertextListConformanceParams; |
| 11 | +use tfhe::prelude::{CiphertextList, Tagged}; |
| 12 | +use tfhe::safe_serialization::{safe_deserialize, safe_deserialize_conformant}; |
| 13 | +use tfhe::zk::CompactPkeCrs; |
| 14 | +use tfhe::{ |
| 15 | + set_server_key, CompactCiphertextList, CompactCiphertextListConformanceParams, |
| 16 | + CompactCiphertextListExpander, CompactPublicKey, FheAsciiString, HlExpandable, |
| 17 | + ProvenCompactCiphertextList, ServerKey, |
| 18 | +}; |
| 19 | + |
| 20 | +const DATA_DIR: &str = "./corrupted_inputs_deserialization/data"; |
| 21 | +const AUX_DIR: &str = "aux_data"; |
| 22 | +const GIGA: u64 = 1024 * 1024 * 1024; |
| 23 | + |
| 24 | +fn load_server_key(aux_dir: &Path) -> ServerKey { |
| 25 | + let path = aux_dir.join("server_key.bcode"); |
| 26 | + let f = File::open(&path).unwrap_or_else(|e| panic!("failed to open {}: {e}", path.display())); |
| 27 | + safe_deserialize(f, 4 * GIGA).unwrap() |
| 28 | +} |
| 29 | + |
| 30 | +fn load_crs(aux_dir: &Path) -> CompactPkeCrs { |
| 31 | + let path = aux_dir.join("crs.bcode"); |
| 32 | + let f = File::open(&path).unwrap_or_else(|e| panic!("failed to open {}: {e}", path.display())); |
| 33 | + safe_deserialize(f, 4 * GIGA).unwrap() |
| 34 | +} |
| 35 | + |
| 36 | +fn load_public_key(aux_dir: &Path) -> CompactPublicKey { |
| 37 | + let path = aux_dir.join("pubkey.bcode"); |
| 38 | + let f = File::open(&path).unwrap_or_else(|e| panic!("failed to open {}: {e}", path.display())); |
| 39 | + safe_deserialize(f, 4 * GIGA).unwrap() |
| 40 | +} |
| 41 | + |
| 42 | +fn load_metadata(aux_dir: &Path) -> Vec<u8> { |
| 43 | + let path = aux_dir.join("metadata.txt"); |
| 44 | + std::fs::read(&path).unwrap_or_else(|e| panic!("failed to open {}: {e}", path.display())) |
| 45 | +} |
| 46 | + |
| 47 | +fn list_subdirs(dir: &Path) -> Vec<std::path::PathBuf> { |
| 48 | + std::fs::read_dir(dir) |
| 49 | + .unwrap_or_else(|e| panic!("failed to read {}: {e}", dir.display())) |
| 50 | + .filter_map(|entry| { |
| 51 | + let entry = entry.unwrap(); |
| 52 | + entry.file_type().unwrap().is_dir().then(|| entry.path()) |
| 53 | + }) |
| 54 | + .collect() |
| 55 | +} |
| 56 | + |
| 57 | +/// Read all .bcode files in `dir` and call `handler` on each file. |
| 58 | +fn process_inputs(dir: &Path, mut handler: impl FnMut(&[u8])) -> u64 { |
| 59 | + let mut total_tests = 0; |
| 60 | + let entries = |
| 61 | + std::fs::read_dir(dir).unwrap_or_else(|e| panic!("failed to read {}: {e}", dir.display())); |
| 62 | + |
| 63 | + for entry in entries { |
| 64 | + let path = entry.unwrap().path(); |
| 65 | + if path.extension().and_then(|e| e.to_str()) == Some("bcode") { |
| 66 | + println!("Processing {}", path.display()); |
| 67 | + total_tests += 1; |
| 68 | + let input = std::fs::read(&path) |
| 69 | + .unwrap_or_else(|e| panic!("failed to read {}: {e}", path.display())); |
| 70 | + handler(&input); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + total_tests |
| 75 | +} |
| 76 | + |
| 77 | +fn test_integer<FheType>(exp: &CompactCiphertextListExpander, i: usize) |
| 78 | +where |
| 79 | + FheType: HlExpandable |
| 80 | + + Tagged |
| 81 | + + Clone |
| 82 | + + Add<FheType, Output = FheType> |
| 83 | + + Sub<FheType, Output = FheType> |
| 84 | + + Mul<FheType, Output = FheType>, |
| 85 | +{ |
| 86 | + let ct = match exp.get::<FheType>(i) { |
| 87 | + Ok(Some(ct)) => ct, |
| 88 | + Ok(None) => { |
| 89 | + println!("No ct found at idx {}\n", i); |
| 90 | + return; |
| 91 | + } |
| 92 | + Err(e) => { |
| 93 | + println!("Error caught while trying to get ct at idx {}:\n{e}\n", i); |
| 94 | + return; |
| 95 | + } |
| 96 | + }; |
| 97 | + let res = ct.clone() + ct.clone(); |
| 98 | + let res = res * ct.clone(); |
| 99 | + let _ = std::hint::black_box(res - ct); |
| 100 | +} |
| 101 | + |
| 102 | +fn test_string(exp: &CompactCiphertextListExpander, i: usize) { |
| 103 | + let ct = match exp.get::<FheAsciiString>(i) { |
| 104 | + Ok(Some(ct)) => ct, |
| 105 | + Ok(None) => { |
| 106 | + println!("No ct found at idx {i}\n"); |
| 107 | + return; |
| 108 | + } |
| 109 | + Err(e) => { |
| 110 | + println!("Error caught while trying to get ct at idx {i}:\n{e}\n"); |
| 111 | + return; |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + let _ = std::hint::black_box(ct.len()); |
| 116 | +} |
| 117 | + |
| 118 | +/// Try to expand each element and perform some operations, dispatching on the |
| 119 | +/// actual type reported by the expander. |
| 120 | +fn use_list(exp: &CompactCiphertextListExpander) { |
| 121 | + for i in 0..exp.len() { |
| 122 | + let Some(kind) = exp.get_kind_of(i) else { |
| 123 | + println!("No metadata for ct at idx {i}\n"); |
| 124 | + return; |
| 125 | + }; |
| 126 | + |
| 127 | + match kind { |
| 128 | + FheTypes::Uint8 => test_integer(exp, i), |
| 129 | + FheTypes::Uint16 => test_integer(exp, i), |
| 130 | + FheTypes::Uint32 => test_integer(exp, i), |
| 131 | + FheTypes::Uint64 => test_integer(exp, i), |
| 132 | + FheTypes::Int8 => test_integer(exp, i), |
| 133 | + FheTypes::Int16 => test_integer(exp, i), |
| 134 | + FheTypes::Int32 => test_integer(exp, i), |
| 135 | + FheTypes::Int64 => test_integer(exp, i), |
| 136 | + FheTypes::AsciiString => test_string(exp, i), |
| 137 | + other => panic!( |
| 138 | + "unsupported FheTypes variant {other:?} at index {i}, \ |
| 139 | + this test should be updated to handle it" |
| 140 | + ), |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +fn handle_ct_list(input: &[u8], conformance_params: &CompactCiphertextListConformanceParams) { |
| 146 | + let ct_list: CompactCiphertextList = match safe_deserialize_conformant::<CompactCiphertextList>( |
| 147 | + input, |
| 148 | + 4 * GIGA, |
| 149 | + conformance_params, |
| 150 | + ) { |
| 151 | + Ok(ct_list) => ct_list, |
| 152 | + Err(e) => { |
| 153 | + println!("Error caught during deserialization:\n{e}\n"); |
| 154 | + return; |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + let exp = match ct_list.expand() { |
| 159 | + Ok(exp) => exp, |
| 160 | + Err(e) => { |
| 161 | + println!("Error caught during expand:\n{e}\n"); |
| 162 | + return; |
| 163 | + } |
| 164 | + }; |
| 165 | + |
| 166 | + use_list(&exp); |
| 167 | + println!("List used without error\n") |
| 168 | +} |
| 169 | + |
| 170 | +fn handle_proven_ct_list( |
| 171 | + input: &[u8], |
| 172 | + conformance_params: &IntegerProvenCompactCiphertextListConformanceParams, |
| 173 | + crs: &CompactPkeCrs, |
| 174 | + public_key: &CompactPublicKey, |
| 175 | + metadata: &[u8], |
| 176 | +) { |
| 177 | + let ct_list: ProvenCompactCiphertextList = match safe_deserialize_conformant::< |
| 178 | + ProvenCompactCiphertextList, |
| 179 | + >(input, 4 * GIGA, conformance_params) |
| 180 | + { |
| 181 | + Ok(ct_list) => ct_list, |
| 182 | + Err(e) => { |
| 183 | + println!("Error caught during deserialization:\n{e}\n"); |
| 184 | + return; |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + let exp = match ct_list.verify_and_expand(crs, public_key, metadata) { |
| 189 | + Ok(exp) => exp, |
| 190 | + Err(e) => { |
| 191 | + println!("Error caught during verify_and_expand:\n{e}\n"); |
| 192 | + return; |
| 193 | + } |
| 194 | + }; |
| 195 | + |
| 196 | + use_list(&exp); |
| 197 | + println!("List used without error\n") |
| 198 | +} |
| 199 | + |
| 200 | +#[test] |
| 201 | +fn test_corrupted_inputs_deserialization() { |
| 202 | + let mut total_tests = 0; |
| 203 | + let data_dir = Path::new(DATA_DIR); |
| 204 | + |
| 205 | + let compact_list_dir = data_dir.join("compact_list"); |
| 206 | + for group_dir in list_subdirs(&compact_list_dir) { |
| 207 | + println!("compact_list group: {}", group_dir.display()); |
| 208 | + let aux_dir = group_dir.join(AUX_DIR); |
| 209 | + |
| 210 | + let server_key = load_server_key(&aux_dir); |
| 211 | + let pubkey = load_public_key(&aux_dir); |
| 212 | + |
| 213 | + let cpk_conformance_params = |
| 214 | + CompactCiphertextListConformanceParams::from_parameters_and_size_constraint( |
| 215 | + pubkey.parameters(), |
| 216 | + ListSizeConstraint::try_size_in_range(4, usize::MAX).unwrap(), |
| 217 | + ) |
| 218 | + .allow_unpacked(); |
| 219 | + |
| 220 | + set_server_key(server_key); |
| 221 | + |
| 222 | + total_tests += process_inputs(&group_dir, |input| { |
| 223 | + handle_ct_list(input, &cpk_conformance_params); |
| 224 | + }); |
| 225 | + } |
| 226 | + |
| 227 | + let proven_compact_list_dir = data_dir.join("proven_compact_list"); |
| 228 | + for group_dir in list_subdirs(&proven_compact_list_dir) { |
| 229 | + println!("proven_compact_list group: {}", group_dir.display()); |
| 230 | + let aux_dir = group_dir.join(AUX_DIR); |
| 231 | + |
| 232 | + let server_key = load_server_key(&aux_dir); |
| 233 | + let pubkey = load_public_key(&aux_dir); |
| 234 | + let crs = load_crs(&aux_dir); |
| 235 | + let metadata = load_metadata(&aux_dir); |
| 236 | + |
| 237 | + let proven_cpk_conformance_params = |
| 238 | + IntegerProvenCompactCiphertextListConformanceParams::from_public_key_encryption_parameters_and_crs_parameters( |
| 239 | + pubkey.parameters(), |
| 240 | + &crs, |
| 241 | + ) |
| 242 | + .allow_unpacked(); |
| 243 | + |
| 244 | + set_server_key(server_key); |
| 245 | + |
| 246 | + total_tests += process_inputs(&group_dir, |input| { |
| 247 | + handle_proven_ct_list( |
| 248 | + input, |
| 249 | + &proven_cpk_conformance_params, |
| 250 | + &crs, |
| 251 | + &pubkey, |
| 252 | + &metadata, |
| 253 | + ); |
| 254 | + }); |
| 255 | + } |
| 256 | + |
| 257 | + println!("Executed {} tests", total_tests); |
| 258 | + // If we ran 0 test, it is likely that something wrong happened |
| 259 | + assert!(total_tests != 0); |
| 260 | +} |
0 commit comments