Skip to content

Commit 793ad90

Browse files
Merge #1010: doc: Minor fixes in safegcd_implementation.md
dc9b685 doc: Minor fixes in safegcd_implementation.md (Elliott Jin) Pull request description: ACKs for top commit: sipa: ACK dc9b685 real-or-random: ACK dc9b685 Tree-SHA512: 990c969806b9abf42e5554093aa573911bbdf28a68c26f60e03e2a754506b1c714f784c673d862b973c5d0a38576605b14aff9d4bd3df176d535ca8ebfe4c0bd
2 parents ea5e8a9 + dc9b685 commit 793ad90

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

doc/safegcd_implementation.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,14 @@ bits efficiently, which is possible on most platforms; it is abstracted here as
569569

570570
```python
571571
def count_trailing_zeros(v):
572-
"""For a non-zero value v, find z such that v=(d<<z) for some odd d."""
573-
return (v & -v).bit_length() - 1
572+
"""
573+
When v is zero, consider all N zero bits as "trailing".
574+
For a non-zero value v, find z such that v=(d<<z) for some odd d.
575+
"""
576+
if v == 0:
577+
return N
578+
else:
579+
return (v & -v).bit_length() - 1
574580

575581
i = N # divsteps left to do
576582
while True:
@@ -601,7 +607,7 @@ becomes negative, or when *i* reaches *0*. Combined, this is equivalent to addin
601607
It is easy to find what that multiple is: we want a number *w* such that *g+w&thinsp;f* has a few bottom
602608
zero bits. If that number of bits is *L*, we want *g+w&thinsp;f mod 2<sup>L</sup> = 0*, or *w = -g/f mod 2<sup>L</sup>*. Since *f*
603609
is odd, such a *w* exists for any *L*. *L* cannot be more than *i* steps (as we'd finish the loop before
604-
doing more) or more than *&eta;+1* steps (as we'd run `eta, f, g = -eta, g, f` at that point), but
610+
doing more) or more than *&eta;+1* steps (as we'd run `eta, f, g = -eta, g, -f` at that point), but
605611
apart from that, we're only limited by the complexity of computing *w*.
606612

607613
This code demonstrates how to cancel up to 4 bits per step:
@@ -618,7 +624,7 @@ while True:
618624
break
619625
# We know g is odd now
620626
if eta < 0:
621-
eta, f, g = -eta, g, f
627+
eta, f, g = -eta, g, -f
622628
# Compute limit on number of bits to cancel
623629
limit = min(min(eta + 1, i), 4)
624630
# Compute w = -g/f mod 2**limit, using the table value for -1/f mod 2**4. Note that f is

0 commit comments

Comments
 (0)