Skip to content
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

Signed improvements #166

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Signed improvements #166

wants to merge 4 commits into from

Conversation

fjarri
Copy link
Member

@fjarri fjarri commented Dec 10, 2024

Remaining work (for other PRs):

@fjarri fjarri self-assigned this Dec 10, 2024
Copy link

codecov bot commented Dec 10, 2024

Codecov Report

Attention: Patch coverage is 97.85592% with 25 lines in your changes missing coverage. Please review.

Project coverage is 93.59%. Comparing base (4c04dcf) to head (2c242d0).

Files with missing lines Patch % Lines
synedrion/src/uint/secret_signed.rs 96.70% 14 Missing ⚠️
synedrion/src/uint/public_signed.rs 93.44% 8 Missing ⚠️
synedrion/src/cggmp21/interactive_signing.rs 94.73% 1 Missing ⚠️
synedrion/src/paillier/encryption.rs 99.15% 1 Missing ⚠️
synedrion/src/uint/secret_unsigned.rs 98.21% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #166      +/-   ##
==========================================
+ Coverage   93.44%   93.59%   +0.14%     
==========================================
  Files          34       36       +2     
  Lines        6849     6727     -122     
==========================================
- Hits         6400     6296     -104     
+ Misses        449      431      -18     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fjarri fjarri marked this pull request as ready for review December 13, 2024 03:01
@fjarri fjarri requested a review from dvdplm December 13, 2024 03:09
@fjarri fjarri force-pushed the better-signed branch 2 times, most recently from 47fd19e to 0b9eba1 Compare December 13, 2024 06:26
Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pheew, a big one!

Overall LGTM; left some nitpicks, suggestions and questions.

There's some inconsistency in what is documented and what isn't, and I think we should avoid asserts in favour of Result wherever possible, although I don't consider it a blocker for this PR.

synedrion/src/cggmp21/conversion.rs Show resolved Hide resolved
synedrion/src/cggmp21/conversion.rs Outdated Show resolved Hide resolved
synedrion/src/cggmp21/conversion.rs Outdated Show resolved Hide resolved
synedrion/src/cggmp21/conversion.rs Show resolved Hide resolved
synedrion/src/cggmp21/conversion.rs Show resolved Hide resolved
}
}

fn homomorphic_mul_unsigned(self, rhs: &Bounded<P::Uint>) -> Self {
let rhs_wide = rhs.to_wide();
pub fn homomorphic_mul_wide_public(&self, rhs: &PublicSigned<P::WideUint>) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing docs

synedrion/src/tools/secret.rs Outdated Show resolved Hide resolved
/// A wrapper over secret unsigned integers that treats two's complement numbers as negative.
#[derive(Debug, Clone)]
pub(crate) struct SecretSigned<T: Zeroize> {
/// bound on the bit size of the absolute value (that is, `abs(value) < 2^bound`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// bound on the bit size of the absolute value (that is, `abs(value) < 2^bound`)
// bound on the bit size of the absolute value (that is, `abs(value) < 2^bound`)

(or move this info to the docs)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean, isn't it in the docs already? It's a doc comment

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, my bad. Maybe capitalize to "Bound" though?

/// Asserts that the value is within the interval the paper denotes as $\pm 2^exp$.
/// Panics if it is not the case.
///
/// That is, the value must be within $[-2^{exp}, 2^{exp}]$
/// (See Section 2).
pub fn assert_exp_range(&self, exp: u32) {
let in_bound = self.in_bound(exp);
// Have to check for the ends of the range too
let is_end = self.abs().expose_secret().ct_eq(&(T::one() << exp));
assert!(bool::from(in_bound | is_end), "out of bounds $\\pm 2^{exp}$",)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should strive to avoid using asserts in non-test code, and instead prefer Result or Option. Doing this consistently is a lot of work and I am fine doing it bit by bit, but perhaps we can avoid adding new asserting code and instead return Result here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I thought of that too, we can make all the proof constructors (where this method is used) return Result<..., LocalError>, so they can fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how often this is called or how important it is for it to be CT, but I tinkered a bit with making it faster (made it return bool for convenience):

    pub fn assert_exp_range_fast(&self, exp: u32) -> bool {
        let abs = self.abs();
        let mask = T::one().wrapping_neg().wrapping_shl_vartime(exp);
        if abs.expose_secret() == &(mask.not() + T::one()) {
            return true;
        }
        let masked = abs & mask;
        masked.expose_secret() == &T::zero()
    }

Benchmark timings for U2048:

Bounds check/assert_exp_range, small exponent
                        time:   [420.31 ns 425.26 ns 431.98 ns]
                        change: [-1.3407% +0.3532% +2.1482%] (p = 0.72 > 0.05)
                        No change in performance detected.
Found 6 outliers among 100 measurements (6.00%)
  2 (2.00%) high mild
  4 (4.00%) high severe
Bounds check/assert_exp_range_fast, small exponent
                        time:   [266.69 ns 271.08 ns 277.42 ns]
                        change: [-0.9114% +0.2448% +1.4652%] (p = 0.71 > 0.05)
                        No change in performance detected.
Found 7 outliers among 100 measurements (7.00%)
  1 (1.00%) high mild
  6 (6.00%) high severe
Bounds check/assert_exp_range, big exponent
                        time:   [713.33 ns 769.78 ns 821.11 ns]
                        change: [-8.6114% +0.4042% +10.744%] (p = 0.94 > 0.05)
                        No change in performance detected.
Bounds check/assert_exp_range_fast, big exponent
                        time:   [383.40 ns 421.51 ns 462.13 ns]
                        change: [-6.4458% +2.8628% +12.866%] (p = 0.55 > 0.05)
                        No change in performance detected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how often this is called or how important it is for it to be CT

A bunch of times during the protocol, but on the order of dozens maybe. So given that it takes <1us, I don't think its performance is too important. It does handle secret data, so I think constant-timeness is appropriate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Do you happen to know if & is CT?

synedrion/src/uint/secret_signed.rs Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zeroizing follow-up: Signed Zeroize MontyParams of RSA primes
2 participants