ore_rs/
encrypt.rs

1use crate::ciphertext::*;
2use crate::convert::ToOrderedInteger;
3use crate::PlainText;
4use crate::{OreCipher, OreError};
5
6pub trait OreEncrypt<T: OreCipher> {
7    type LeftOutput: OreOutput;
8    type FullOutput: OreOutput;
9
10    fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError>;
11    fn encrypt(&self, input: &T) -> Result<Self::FullOutput, OreError>;
12}
13
14impl<T: OreCipher> OreEncrypt<T> for u64 {
15    /* Note that Rust currently doesn't allow
16     * generic associated types so this ia a bit verbose! */
17    type LeftOutput = Left<T, 8>;
18    type FullOutput = CipherText<T, 8>;
19
20    fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError>
21    where
22        T::LeftBlockType: CipherTextBlock,
23    {
24        let bytes = self.to_be_bytes();
25        cipher.encrypt_left(&bytes)
26    }
27
28    fn encrypt(&self, cipher: &T) -> Result<Self::FullOutput, OreError>
29    where
30        T::LeftBlockType: CipherTextBlock,
31        T::RightBlockType: CipherTextBlock,
32    {
33        let bytes = self.to_be_bytes();
34        cipher.encrypt(&bytes)
35    }
36}
37
38impl<T: OreCipher> OreEncrypt<T> for u32 {
39    type LeftOutput = Left<T, 4>;
40    type FullOutput = CipherText<T, 4>;
41
42    fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError> {
43        let bytes = self.to_be_bytes();
44        cipher.encrypt_left(&bytes)
45    }
46
47    fn encrypt(&self, cipher: &T) -> Result<Self::FullOutput, OreError> {
48        let bytes = self.to_be_bytes();
49        cipher.encrypt(&bytes)
50    }
51}
52
53impl<T: OreCipher> OreEncrypt<T> for f64 {
54    type LeftOutput = Left<T, 8>;
55    type FullOutput = CipherText<T, 8>;
56
57    fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError> {
58        let plaintext: u64 = self.map_to();
59        plaintext.encrypt_left(cipher)
60    }
61
62    fn encrypt(&self, cipher: &T) -> Result<Self::FullOutput, OreError> {
63        let plaintext: u64 = self.map_to();
64        plaintext.encrypt(cipher)
65    }
66}
67
68impl<T: OreCipher, const N: usize> OreEncrypt<T> for PlainText<N> {
69    type LeftOutput = Left<T, N>;
70    type FullOutput = CipherText<T, N>;
71
72    fn encrypt_left(&self, cipher: &T) -> Result<Self::LeftOutput, OreError> {
73        cipher.encrypt_left(self)
74    }
75
76    fn encrypt(&self, cipher: &T) -> Result<Self::FullOutput, OreError> {
77        cipher.encrypt(self)
78    }
79}