-
Notifications
You must be signed in to change notification settings - Fork 4
Optimize homomorphic encryption (precomputations and other optimizations) #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,9 +22,55 @@ pub struct RsaPK { | |
| e: Integer, | ||
| } | ||
|
|
||
| /// Decryption key for RSA | ||
| pub struct RsaSK { | ||
| /// Precomputed decryption key for RSA | ||
| pub struct PrecomputedRsaSK { | ||
| p: Integer, | ||
| q: Integer, | ||
| d: Integer, | ||
| d_p: Integer, | ||
| d_q: Integer, | ||
| q_inv: Integer, | ||
| } | ||
|
|
||
| impl PrecomputedRsaSK { | ||
| /// Creates a new precomputed secret key using p, q, and d | ||
| pub fn new(p: Integer, q: Integer, d: Integer) -> Self { | ||
| let d_p = Integer::from(&d).rem(Integer::from(&p - 1)); | ||
| let d_q = Integer::from(&d).rem(Integer::from(&q - 1)); | ||
| let q_inv = Integer::from(q.invert_ref(&p).unwrap()); | ||
|
|
||
| PrecomputedRsaSK { | ||
| p, | ||
| q, | ||
| d, | ||
| d_p, | ||
| d_q, | ||
| q_inv, | ||
| } | ||
| } | ||
|
|
||
| /// Compresses the precomputed secret key to a smaller format without precomputations | ||
| pub fn compress(self) -> CompressedRsaSK { | ||
| CompressedRsaSK { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool rust feature, you can use |
||
| p: self.p, | ||
| q: self.q, | ||
| d: self.d, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Small secret key for RSA, which is less fast than a `PrecomputedRsaSK`. | ||
| pub struct CompressedRsaSK { | ||
| p: Integer, | ||
| q: Integer, | ||
| d: Integer, | ||
| } | ||
|
|
||
| impl CompressedRsaSK { | ||
| /// Creates a `PrecomputedRsaSK` from this key. | ||
| pub fn precompute(self) -> PrecomputedRsaSK { | ||
| PrecomputedRsaSK::new(self.p, self.q, self.d) | ||
| } | ||
| } | ||
|
|
||
| /// Ciphertext of the RSA cryptosystem, which is multiplicatively homomorphic. | ||
|
|
@@ -36,21 +82,21 @@ impl Associable<RsaPK> for RsaCiphertext {} | |
|
|
||
| impl AsymmetricCryptosystem for Rsa { | ||
| type PublicKey = RsaPK; | ||
| type SecretKey = RsaSK; | ||
| type SecretKey = PrecomputedRsaSK; | ||
|
|
||
| fn setup(security_param: &BitsOfSecurity) -> Self { | ||
| Rsa { | ||
| modulus_size: security_param.to_public_key_bit_length(), | ||
| } | ||
| } | ||
|
|
||
| fn generate_keys<R: SecureRng>(&self, rng: &mut GeneralRng<R>) -> (RsaPK, RsaSK) { | ||
| let (n, lambda) = gen_rsa_modulus(self.modulus_size, rng); | ||
| fn generate_keys<R: SecureRng>(&self, rng: &mut GeneralRng<R>) -> (RsaPK, PrecomputedRsaSK) { | ||
| let (n, lambda, p, q) = gen_rsa_modulus(self.modulus_size, rng); | ||
|
|
||
| let e = Integer::from(65537); | ||
| let d = Integer::from(e.invert_ref(&lambda).unwrap()); | ||
|
|
||
| (RsaPK { n, e }, RsaSK { d }) | ||
| (RsaPK { n, e }, PrecomputedRsaSK::new(p, q, d)) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -70,12 +116,26 @@ impl EncryptionKey for RsaPK { | |
| } | ||
| } | ||
|
|
||
| impl DecryptionKey<RsaPK> for RsaSK { | ||
| impl DecryptionKey<RsaPK> for CompressedRsaSK { | ||
| fn decrypt_raw(&self, public_key: &RsaPK, ciphertext: &RsaCiphertext) -> Integer { | ||
| Integer::from(ciphertext.c.secure_pow_mod_ref(&self.d, &public_key.n)) | ||
| } | ||
| } | ||
|
|
||
| impl DecryptionKey<RsaPK> for PrecomputedRsaSK { | ||
| fn decrypt_raw( | ||
| &self, | ||
| _public_key: &RsaPK, | ||
| ciphertext: &<RsaPK as EncryptionKey>::Ciphertext, | ||
| ) -> <RsaPK as EncryptionKey>::Plaintext { | ||
| let m_p = Integer::from(ciphertext.c.secure_pow_mod_ref(&self.d_p, &self.p)); | ||
| let m_q = Integer::from(ciphertext.c.secure_pow_mod_ref(&self.d_q, &self.q)); | ||
| let h = (&self.q_inv * (m_p - &m_q)).rem(&self.p); | ||
|
|
||
| m_q + h * &self.q | ||
| } | ||
| } | ||
|
|
||
| impl HomomorphicMultiplication for RsaPK { | ||
| fn mul( | ||
| &self, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,29 @@ pub trait DecryptionKey<PK: EncryptionKey> { | |
|
|
||
| /// Decrypt the ciphertext using the secret key and its related public key. | ||
| fn decrypt_raw(&self, public_key: &PK, ciphertext: &PK::Ciphertext) -> PK::Plaintext; | ||
|
|
||
| /// Uses both the secret material from the decryption key and the encryption key to encrypt faster than with only the encryption key. | ||
| /// This method is always implemented, but it defaults to simply encrypting without the secret key. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, maybe I misunderstand, but if this is a trait, is there a need to specify that this method is always implemented? |
||
| fn encrypt_fast<'pk, R: SecureRng>( | ||
| &'pk self, | ||
| public_key: &'pk PK, | ||
| plaintext: &PK::Plaintext, | ||
| rng: &mut GeneralRng<R>, | ||
| ) -> AssociatedCiphertext<'pk, PK::Ciphertext, PK> { | ||
| self.encrypt_fast_raw(public_key, plaintext, rng) | ||
| .associate(public_key) | ||
| } | ||
|
|
||
| /// Uses both the secret material from the decryption key and the encryption key to encrypt faster than with only the encryption key. | ||
| /// This method is always implemented, but it defaults to simply encrypting without the secret key. | ||
| fn encrypt_fast_raw<R: SecureRng>( | ||
| &self, | ||
| public_key: &PK, | ||
| plaintext: &PK::Plaintext, | ||
| rng: &mut GeneralRng<R>, | ||
| ) -> PK::Ciphertext { | ||
| public_key.encrypt_raw(plaintext, rng) | ||
| } | ||
| } | ||
|
|
||
| #[derive(PartialEq, Debug)] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can actually use
..shorthand here:let (n, lambda, ..) =