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-digit multi-comb for ecmult_gen #546

Closed
wants to merge 8 commits into from
9 changes: 5 additions & 4 deletions src/ecmult_gen_impl.h
Original file line number Diff line number Diff line change
@@ -225,13 +225,14 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
bit_pos = comb_off;
for (block = 0; block < COMB_BLOCKS; ++block) {
#if COMB_GROUPED
bits = (recoded[bit_pos >> 5] >> (bit_pos & 0x1F)) & ((1 << COMB_TEETH) - 1);
bit_pos += COMB_SPACING * COMB_TEETH;
bits = recoded[bit_pos >> 5] >> (bit_pos & 0x1F);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be done as part of a cmov ladder, like the old algorithm, to avoid cache-timing attacks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line is reading a window from the scalar. IIUC, the cmov ladder you mean is the _ge_storage_cmov loop at lines 255-257.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, yes! I missed that - you do indeed have a cmov ladder at the point where the secret data is actually used. Thanks.

bit_pos += COMB_TEETH;
#else
bits = 0;
for (tooth = 0; tooth < COMB_TEETH; ++tooth) {
bit = (recoded[bit_pos >> 5] >> (bit_pos & 0x1F)) & 1;
bits |= bit << tooth;
bit = recoded[bit_pos >> 5] >> (bit_pos & 0x1F);
bits &= ~(1 << tooth);
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 this bit-clearing line isn't actually needed, because bits starts at 0 and 1 << tooth is unique on each iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that ‘bit’ may now contain junk (or noise) bits beyond bit 0. Iterations after the first therefore need to clear the target bit (and we try to leave noise in the high bits). The change is in response to [1], which I think @gmaxwell mentioned on IRC a while back.

[1] https://www.usenix.org/system/files/conference/usenixsecurity18/sec18-alam.pdf

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! I understand, I was misreading bit << tooth as (bit & 1) << tooth.

Copy link
Contributor

Choose a reason for hiding this comment

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

Because the variable is named bit :P

bits ^= bit << tooth;
bit_pos += COMB_SPACING;
}
#endif