Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fjarri committed Dec 3, 2024
1 parent 5f154ac commit 53f29f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
25 changes: 6 additions & 19 deletions synedrion/src/paillier/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,7 @@ impl<P: PaillierParams> Ciphertext<P> {
randomizer: &MaskedRandomizer<P>,
plaintext_is_negative: Choice,
) -> Self {
// Technically if `abs_plaintext` is greater than the modulus of `pk`,
// it will be effectively reduced modulo `pk`.
// But some ZK proofs with `TestParams` may still supply a value larger than `pk`
// because they are not planning on decrypting the resulting ciphertext;
// they just construct an encryption of the same value in two different ways
// and then compare the results.
// (And the value can be larger than `pk` because of some restrictions on
// `SchemeParameters`/`PaillierParameters` values in tests, which can only
// be overcome by fixing #27 and using a small 32- or 64-bit curve for tests)

// Calculate the ciphertext `C = (N + 1)^m * rho^N mod N^2`
// where `N` is the Paillier composite modulus, `m` is the plaintext,
// and `rho` is the randomizer.

// Simplify `(N + 1)^m mod N^2 == 1 + m * N mod N^2`.
// Since `m` can be negative, we calculate `m * N +- 1` (never overflows since `m < N`),
// then conditionally negate modulo N^2
// Same as `new_with_randomizer_inner`, but works on public data.

let prod = abs_plaintext.mul_wide(pk.modulus());
let mut prod_mod = prod.to_montgomery(pk.monty_params_mod_n_squared());
Expand Down Expand Up @@ -282,8 +266,11 @@ impl<P: PaillierParams> Ciphertext<P> {
assert_eq!(sk.public_key(), &self.pk);

let pk = sk.public_key();
let positive_result = self.decrypt(sk); // Note that this is in range `[0, N)`
// Can't define a `Sub<Secret>` for `Uint`, so have to re-wrap manually.

// Note that this is in range `[0, N)`
let positive_result = self.decrypt(sk);

// Can't define a `Sub<Secret>` for `Uint`, so have to re-wrap manually.
let negative_result = Secret::init_with(|| *pk.modulus() - positive_result.expose_secret());
let is_negative = positive_result
.expose_secret()
Expand Down
2 changes: 1 addition & 1 deletion synedrion/src/tools/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ where
}

pub fn to_signed(&self) -> Option<Secret<Signed<T>>> {
Secret::try_init_with(|| self.expose_secret().clone().into_signed().ok_or(|| ())).ok()
Secret::maybe_init_with(|| self.expose_secret().clone().into_signed())
}
}

Expand Down

0 comments on commit 53f29f6

Please sign in to comment.