Skip to content

Commit 6a14241

Browse files
committed
doc: Minor fixes in safegcd_implementation.md
1 parent 74c34e7 commit 6a14241

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

doc/safegcd_implementation.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,10 @@ bits efficiently, which is possible on most platforms; it is abstracted here as
570570
```python
571571
def count_trailing_zeros(v):
572572
"""For a non-zero value v, find z such that v=(d<<z) for some odd d."""
573-
return (v & -v).bit_length() - 1
573+
if v == 0:
574+
return N
575+
else:
576+
return (v & -v).bit_length() - 1
574577

575578
i = N # divsteps left to do
576579
while True:
@@ -601,7 +604,7 @@ becomes negative, or when *i* reaches *0*. Combined, this is equivalent to addin
601604
It is easy to find what that multiple is: we want a number *w* such that *g+w&thinsp;f* has a few bottom
602605
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*
603606
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
607+
doing more) or more than *&eta;+1* steps (as we'd run `eta, f, g = -eta, g, -f` at that point), but
605608
apart from that, we're only limited by the complexity of computing *w*.
606609

607610
This code demonstrates how to cancel up to 4 bits per step:
@@ -618,7 +621,7 @@ while True:
618621
break
619622
# We know g is odd now
620623
if eta < 0:
621-
eta, f, g = -eta, g, f
624+
eta, f, g = -eta, g, -f
622625
# Compute limit on number of bits to cancel
623626
limit = min(min(eta + 1, i), 4)
624627
# 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)