Repository navigation
Conversation
There was a problem hiding this comment.
Are you sure? Doesn't let mut r = *a copy (via Copy) a?
There was a problem hiding this comment.
You're right. I was confused and thinking of *a as if it was taking a raw pointer, but of course it's not.
I prefer #1. I like the DRY principle but IMO it is more important that the ECC code be as clear and as small as possible. I eventually want to literally prove the ECC code is correct.
No need to put this in the PR comments, just in the commit messages. |
1c339ac to
7490a0b
Compare
|
@briansmith I believe this is ready for re-review. |
69f8c63 to
ffbac22
Compare
|
Ready for re-review, unless you'd like me to squash the new (/ all) commits first. I'll do that before I merge in any case, and add the licensing agreement. |
2 similar comments
1 similar comment
briansmith
left a comment
There was a problem hiding this comment.
Please always squash all changes so that the commits are exactly what you want merged. I'll deal with interdiffing myself as needed. Or, at least, make sure to include the license blurb at the end of all commits so that I can do the squashing myself.
Earlier you said:
If we always copy into the extended buffers, that actually complicates our Range code, because it requires us to decide inside Range whether we are going to apply the mask to the most-significant limb or the next-most-significant limb. (Or pass in two masks, or something.)
But, I think maybe I'm not understanding what you mean or maybe I didn't make my idea clear. Please see https://github.com/briansmith/ring/commits/rsa-blinding-0b100, particularly the commit I added, "Simplify set_to_rand_mod." Is there something wrong with doing it this way that avoids the code duplication and branching? It's something that normally doesn't matter, but as a general rule in ring I'm trying to eliminate branches, not for constant-timedness, but also to simplify the analysis of the code. So, if this kind of simplification works I'd like to do it this way. But, again, maybe I'm missing something.
Finally, when I was making that edit I realized that the ordering of the output parameter dest relative to the other parameters of set_to_rand_mod() doesn't match the convention in the rest of the code. I see we have some conflicting conventions:
- The BoringSSL C/C++ code puts output parameters first.
- Rust code seems to put output parameters last.
- The
rngparameter is supposed to be last.
I guess the rule we should use is this:
- C/C++ code does whatever Google wants, which means output parameters first and
randparameter last. - Rust code should have input parameters first, then
rng, then output parameters.
I kind of just made this up but it seems to make the most sense. WDYT?
Finally, usually in ring we name output parameters with an out_ prefix or we use the name out if there is just one parameter. So, I think dest should be out.
Earlier today I made it so you don't have to use #![allow(unsafe_code)] any more; unsafe { } is allowed everywhere.
| if most_sig >> (LIMB_BITS - 3) == 0b100 { | ||
| SamplingParams { | ||
| most_sig_limb_mask: 1, // effectively a carry into a new, more-significant limb | ||
| most_sig_limb_mask: 1, // effectively a carry over into a new, more-significant limb |
There was a problem hiding this comment.
NIT: I think the added word "over" is redundant.
|
|
||
| limbs_reduce_once_constant_time(dest, self.max_exclusive); | ||
| if self.are_limbs_within(&dest) { | ||
| // `dest` was in [max, 2*max) |
There was a problem hiding this comment.
This should be (max, 2*max), right? If the value were max then are_limbs_within(&dest) would return false because max - max == 0, which isn't allowed.
|
|
||
| limbs_reduce_once_constant_time(dest, self.max_exclusive); | ||
| if self.are_limbs_within(&dest) { | ||
| // `dest` was in [2*max, 3*max) |
There was a problem hiding this comment.
Should we change are_limbs_with() to work like this?:
if limbs_less_than_limbs_constant_time(limbs, self.max_exclusive) != LimbMask::True {
return false;
}
return limbs_are_zero_constant_time(limbs) == LimbMask::False;
In particular this would reduce the number of calls to limbs_are_zero_constant_time for the cases where we have to do one or more reductions.
| return Ok(()); | ||
| } | ||
|
|
||
| // `dest` was in [3*max, 2**(n+1)) or zero |
There was a problem hiding this comment.
It is either in that subrange, or it was zero, or it was max or 2*max, right?
There was a problem hiding this comment.
I suggest that you append ". Continue looping to generate a new random value." Especially because the period is missing, it looks like there might be some code unintentionally missing here.
There was a problem hiding this comment.
Makes sense. I'd actually already gone back and forth on whether expanding this comment was redundant or not
| } | ||
|
|
||
| impl <'a> Range<'a> { | ||
|
|
| /// and stores it into `dest`. | ||
| fn set_to_rand_mod(dest: &mut [Limb], max_exclusive: &[Limb], rng: &rand::SecureRandom) | ||
| -> Result<(), error::Unspecified> { | ||
|
|
| use {c, core, rand, error, rsa}; | ||
| use limb::*; | ||
|
|
||
| const RSA_KEY_MAX_LIMBS: usize = rsa::RSA_PUBLIC_KEY_MODULUS_BITS_MAX / LIMB_BITS; |
There was a problem hiding this comment.
When re-reviewing this, I realized that this should be defined next to RSA_PUBLIC_KEY_MODULUS_BITS_MAX and we should add a comment explaining that we assume that RSA_PUBLIC_KEY_MODULUS_BITS_MAX is always a multiple of LIMB_BITS.
Preferably, use the rounding calculation:
(rsa::RSA_PUBLIC_KEY_MODULUS_BITS_MAX + LIMB_BITS - 1) / LIMB_BITS.
| return Ok(()); | ||
| } | ||
|
|
||
| // `dest` was in [3*max, 2**(n+1)) or zero |
There was a problem hiding this comment.
I suggest that you append ". Continue looping to generate a new random value." Especially because the period is missing, it looks like there might be some code unintentionally missing here.
|
|
||
| /// Chooses a positive integer less than `max_exclusive` uniformly at random | ||
| /// and stores it into `dest`. | ||
| fn set_to_rand_mod(dest: &mut [Limb], max_exclusive: &[Limb], rng: &rand::SecureRandom) |
There was a problem hiding this comment.
Double-check line lengths are <= 80 characters. This one, at least, is too long.
There was a problem hiding this comment.
Fixed by cherry-picking in your rewrite of the function.
| } | ||
| } | ||
|
|
||
| #[allow(unsafe_code)] |
There was a problem hiding this comment.
You can drop this #[allow(unsafe_code)] now.
| } | ||
|
|
||
| #[test] | ||
| #[allow(unused_results)] |
There was a problem hiding this comment.
Let's not use allow(unused_results), even in tests. Instead we can use the let _ = pattern.
| use limb::*; | ||
|
|
||
| #[test] | ||
| fn test_select_sampling_params() { |
There was a problem hiding this comment.
All of these test cases are one limb long, so they are not verifying that we're looking at the high limb instead of, say, the low limb.
|
|
||
| assert_normal!(i, l); | ||
| assert_normal!(i, l | 1); | ||
| assert_normal!(i, l | l >> 3); |
There was a problem hiding this comment.
I don't understand the significance of these test cases, especially the last one. Add some comments to show why these values are important to test.
There was a problem hiding this comment.
The last is testing that we don't treat 0b001001, 0b0001001, etc like we do 0b1001 and 0b01001. But every case I pick from the wide set of non-0b100 numbers feels a bit arbitrary to me; if you find these redundant, I'm happy to remove them.
| use super::{RSA_KEY_MAX_LIMBS, Range, SamplingParams, with_extended_buffers}; | ||
|
|
||
| #[bench] | ||
| fn bench_sample_into_limbs_simple(b: &mut bench::Bencher) { |
There was a problem hiding this comment.
Some comments about the purpose of this benchmark would be useful. In particular, why does the benchmark fill in its own SamplingParams and Range instead of having the real library code calculate them? What is meant by "simple" in the function name?
| } | ||
|
|
||
| #[bench] | ||
| fn bench_sample_into_limbs_reduce_no_copy(b: &mut bench::Bencher) { |
There was a problem hiding this comment.
Again, please add a comment about the purpose of this benchmark.
| #[bench] | ||
| fn bench_sample_into_limbs_simple(b: &mut bench::Bencher) { | ||
| let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS]; | ||
| max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 1); |
There was a problem hiding this comment.
Add a comment such as "construct a value such that the high limb starts with 0b100...
| #[bench] | ||
| fn bench_sample_into_limbs_reduce_no_copy(b: &mut bench::Bencher) { | ||
| let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS]; | ||
| max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 2); |
There was a problem hiding this comment.
Please add a comment and an assertion such as assert_eq!(max[RSA_KEY_MAX_LIMBS - 1] >> LIMB_BITS - 3, 0b100) to indicate the expected form of max.
| #[bench] | ||
| fn bench_sample_into_limbs_reduce_with_copy(b: &mut bench::Bencher) { | ||
| let mut max: [Limb; RSA_KEY_MAX_LIMBS] = [0; RSA_KEY_MAX_LIMBS]; | ||
| max[RSA_KEY_MAX_LIMBS - 1] = 1 << (LIMB_BITS - 1); |
There was a problem hiding this comment.
Please add a comment and an assertion such as assert_eq!(max[RSA_KEY_MAX_LIMBS - 1] >> LIMB_BITS - 3, 0b100) to indicate the expected form of max.
Just to follow-up on this: I went and looked and we're already not as consistent as I was hoping. In particular, it's more the case that we put the |
|
I'll squash the changes before requesting re-review, and I cherry-picked in your commit (I hadn't thought of your Re line length comments & some others, should I be running |
I agree to license my contributions to each file under the terms given at the top of each file I changed.
0b11350 to
b794e50
Compare
|
Ready for re-review, modulo possible |
Oxidize/restore special-case handling for `0b100...` range bounds, lost when OpenSSL code was oxidized, and move RSA blinding param generation out of limb.rs and into its own module I agree to license my contributions to each file under the terms given at the top of each file I changed.
b794e50 to
7626a91
Compare
No, it's not that I want you to remove the tests. More, it would be nice to find a way to make the tests more obvious, by naming some of the calculated values with clear names and/or adding comments. |
I have a rustfmt configuration file that makes things more like the BoringSSL style. Unfortunately, rustfmt likes to put function arguments each on their own line, so I end up having to reformat some things after running rustfmt. So, it isn't quite 100% formatted according to rustfmt but it might be helpful to use it. |
2 similar comments
|
Changes Unknown when pulling 7626a91 on djudd:rsa-blinding-0b100 into ** on briansmith:master**. |
|
Bump - AFAIK this is ready (apologies if I missed some comment). |
|
Yes, I've already reviewed it earlier this week and I'm planning to push it with minor changes sometime this weekend. |
|
Great. No hurry, just wasn't sure of the status. Thanks! |
|
Please see the tweaks at https://github.com/briansmith/ring/commits/rsa-blinding-0b100. The first commit I stacked on top of yours is just reordering your code so that the code is in top-down(-ish) order. The second commit is just a bunch of tweaks. LMK if I created any problem. |
|
Aside from a typo in a comment, looks good. Thanks! On Thu, Nov 17, 2016 at 2:19 PM, Brian Smith [email protected]
|
... in RSA blinding keygen, lost when OpenSSL code was oxidized
There are some TODOs in the code regarding
as, but also a larger one, where I want to agree on a strategy before moving forward.I pulled the
0b100check and mask calculation for Range sampling into the Range constructor, where it's easier to test and, I think, clearer. However, this means that the EC code which uses Range just for theare_limbs_withincheck now is unnecessarily initializing those values too. Even though it skips the complex path, I'm not all that happy with it. Two options:In the former case, we'd be able to move Range and everything associated out into
rsaland.In the latter case, we'll still in some sense be making the EC keygen code more complex than it is now, but using a different constructor with explicit flags should make it fairly clear that the complex paths (3 * 0b100 reduction and mask calculation) are not used.
Thoughts?
I am the sole copyright holder of all added code, and agree to license it per the terms in each file changed.