1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use super::{FPEError, Integer};
/// Struct representing a floating point number.
pub struct Float {
number: Integer,
}
impl Float {
/// Instantiates a new `Float` struct.
///
/// # Returns
///
/// Returns a new instance of `Float` if successful, otherwise returns an
/// error `AnoError`.
pub fn instantiate() -> Result<Self, FPEError> {
Ok(Self {
number: Integer::instantiate(16, 16)?,
})
}
/// Encrypts the floating point value.
///
/// # Arguments
///
/// * `value` - A floating point value to be encrypted.
/// * `key` - A 32-byte encryption key.
/// * `tweak` - A tweak value.
///
/// # Returns
///
/// Returns the encrypted floating point value if successful, otherwise
/// returns an error `AnoError`.
pub fn encrypt(&self, key: &[u8; 32], tweak: &[u8], value: f64) -> Result<f64, FPEError> {
// Convert the floating point value to a BigUint
let big_uint = BigUint::from(value.to_bits());
// Encrypt the BigUint
let ciphertext = self.number.encrypt_big(key, tweak, &big_uint)?;
// Convert the encrypted BigUint to u64
let num_bits = ciphertext.to_u64().ok_or_else(|| {
FPEError::ConversionError(format!(
"Failed converting the ciphertext value: {ciphertext}, to a number of bits as an \
u64"
))
})?;
// Convert the u64 to f64
Ok(f64::from_bits(num_bits))
}
/// Decrypts the floating point value.
///
/// # Arguments
///
/// * `value` - A floating point value to be decrypted.
/// * `key` - A 32-byte decryption key.
/// * `tweak` - A tweak value.
///
/// # Returns
///
/// Returns the decrypted floating point value if successful, otherwise
/// returns an error `AnoError`.
pub fn decrypt(&self, key: &[u8; 32], tweak: &[u8], value: f64) -> Result<f64, FPEError> {
// Convert the floating point value to a BigUint
let big_uint = BigUint::from(value.to_bits());
// Decrypt the BigUint
let ciphertext = self.number.decrypt_big(key, tweak, &big_uint)?;
// Convert the decrypted BigUint to u64
let num_bits = ciphertext.to_u64().ok_or_else(|| {
FPEError::ConversionError(format!(
"Failed converting the ciphertext value: {ciphertext}, to a number of bits as an \
u64"
))
})?;
Ok(f64::from_bits(num_bits))
}
}