69718d1c1ba30507062149e65b2d6138e21ee669
1 from nmigen_gf
.reference
.state
import ST
5 # based on Algorithm ExtEucdInv from:
6 # https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.90.5233&rep=rep1&type=pdf
7 # designed to be dead-easy (and efficient) to implement in hardware
9 assert p
>= 2, "GFPRIME isn't a prime"
10 assert a
!= 0, "TODO: decide what happens for division by zero"
11 assert isinstance(a
, int) and 0 < a
< p
, "a out of range"
13 return 1 # the only value possible
23 # implementations could use count-zeros on
24 # both u and r to save cycles
31 # implementations could use count-zeros on
32 # both v and s to save cycles
39 # both LSB of u and v are 1
52 # make sure result r within modulo range 0 <= r <= p