-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: centralize encoding in shortint
The plaintext encoding in shortint was duplicated all over the code This commit centralize the encoding used for shortint, so that if an encoding fix is needed there should be one place to do it.
- Loading branch information
Showing
17 changed files
with
293 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::core_crypto::entities::{Cleartext, Plaintext}; | ||
use crate::core_crypto::prelude::CiphertextModulusKind; | ||
use crate::shortint::{CarryModulus, CiphertextModulus, MessageModulus, ShortintParameterSet}; | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
pub(crate) enum PaddingBit { | ||
No = 0, | ||
Yes = 1, | ||
} | ||
|
||
fn compute_delta( | ||
ciphertext_modulus: CiphertextModulus, | ||
message_modulus: MessageModulus, | ||
carry_modulus: CarryModulus, | ||
padding_bit: PaddingBit, | ||
) -> u64 { | ||
match ciphertext_modulus.kind() { | ||
CiphertextModulusKind::Native => { | ||
(1u64 << (u64::BITS - 1 - padding_bit as u32)) / (carry_modulus.0 * message_modulus.0) | ||
* 2 | ||
} | ||
CiphertextModulusKind::Other | CiphertextModulusKind::NonNativePowerOfTwo => { | ||
ciphertext_modulus.get_custom_modulus() as u64 | ||
/ (carry_modulus.0 * message_modulus.0) | ||
/ if padding_bit == PaddingBit::Yes { 2 } else { 1 } | ||
* 2 | ||
} | ||
} | ||
} | ||
|
||
pub(crate) struct ShortintEncoding { | ||
pub(crate) ciphertext_modulus: CiphertextModulus, | ||
pub(crate) message_modulus: MessageModulus, | ||
pub(crate) carry_modulus: CarryModulus, | ||
pub(crate) padding_bit: PaddingBit, | ||
} | ||
|
||
impl ShortintEncoding { | ||
pub(crate) fn delta(&self) -> u64 { | ||
compute_delta( | ||
self.ciphertext_modulus, | ||
self.message_modulus, | ||
self.carry_modulus, | ||
self.padding_bit, | ||
) | ||
} | ||
} | ||
|
||
impl ShortintEncoding { | ||
pub(crate) fn from_parameters( | ||
params: impl Into<ShortintParameterSet>, | ||
padding_bit: PaddingBit, | ||
) -> Self { | ||
let params = params.into(); | ||
Self { | ||
ciphertext_modulus: params.ciphertext_modulus(), | ||
message_modulus: params.message_modulus(), | ||
carry_modulus: params.carry_modulus(), | ||
padding_bit, | ||
} | ||
} | ||
|
||
pub(crate) fn encode(&self, value: Cleartext<u64>) -> Plaintext<u64> { | ||
let delta = compute_delta( | ||
self.ciphertext_modulus, | ||
self.message_modulus, | ||
self.carry_modulus, | ||
self.padding_bit, | ||
); | ||
|
||
Plaintext(value.0.wrapping_mul(delta)) | ||
} | ||
|
||
pub(crate) fn decode(&self, value: Plaintext<u64>) -> Cleartext<u64> { | ||
assert!(self.ciphertext_modulus.is_native_modulus()); | ||
let delta = self.delta(); | ||
|
||
// The bit before the message | ||
let rounding_bit = delta >> 1; | ||
|
||
// Compute the rounding bit | ||
let rounding = (value.0 & rounding_bit) << 1; | ||
|
||
Cleartext(value.0.wrapping_add(rounding) / delta) | ||
} | ||
} |
Oops, something went wrong.