Skip to content

Commit

Permalink
Merge pull request #6 from survived/dk-from-bignumber
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelodder7 authored Dec 16, 2022
2 parents 5949fed + c306c66 commit 857e058
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
13 changes: 4 additions & 9 deletions src/decryptionkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,19 @@ impl DecryptionKey {
}
let pm1: BigNumber = p - 1;
let qm1: BigNumber = q - 1;
let n = p * q;
let nn = &n * &n;
let pk = EncryptionKey {
n: n.clone(),
nn: nn.clone(),
};
let pk = EncryptionKey::from_n(p * q);
let lambda = pm1.lcm(&qm1);
if lambda.is_zero() {
return None;
}
let totient = &pm1 * &qm1;

// (N+1)^lambda mod N^2
let t: BigNumber = &n + 1;
let tt = t.modpow(&lambda, &nn);
let t: BigNumber = pk.n() + 1;
let tt = t.modpow(&lambda, pk.nn());

// L((N+1)^lambda mod N^2)^-1 mod N
let uu = pk.l(&tt).map(|uu| uu.invert(&n));
let uu = pk.l(&tt).map(|uu| uu.invert(pk.n()));
match uu {
None => None,
Some(u_inv) => u_inv.map(|u| DecryptionKey {
Expand Down
15 changes: 7 additions & 8 deletions src/encryptionkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ impl<'de> Deserialize<'de> for EncryptionKey {
D: Deserializer<'de>,
{
let n = BigNumber::deserialize(deserializer)?;
Ok(Self {
n: n.clone(),
nn: &n * &n,
})
Ok(Self::from_n(n))
}
}

Expand Down Expand Up @@ -122,10 +119,12 @@ impl EncryptionKey {
pub fn from_bytes<B: AsRef<[u8]>>(data: B) -> Result<Self, String> {
let data = data.as_ref();
let n = BigNumber::from_slice(data);
Ok(Self {
n: n.clone(),
nn: &n * &n,
})
Ok(Self::from_n(n))
}

/// Constructs encryption key from the Paillier modulus
pub fn from_n(n: BigNumber) -> Self {
Self { nn: &n * &n, n }
}

/// The Paillier modulus
Expand Down

0 comments on commit 857e058

Please sign in to comment.