Compute the inverse of x in GF(2^field_size).
(self, x)
| 117 | return self.mul(x, x) |
| 118 | |
| 119 | def inv(self, x): |
| 120 | """Compute the inverse of x in GF(2^field_size).""" |
| 121 | assert x != 0 |
| 122 | # Use the extended polynomial Euclidean GCD algorithm on (modulus, x), over GF(2). |
| 123 | # See https://en.wikipedia.org/wiki/Polynomial_greatest_common_divisor. |
| 124 | t1, t2 = 0, 1 |
| 125 | r1, r2 = self._modulus, x |
| 126 | r1l, r2l = self.field_size + 1, r2.bit_length() |
| 127 | while r2: |
| 128 | q = r1l - r2l |
| 129 | r1 ^= r2 << q |
| 130 | t1 ^= t2 << q |
| 131 | r1l = r1.bit_length() |
| 132 | if r1 < r2: |
| 133 | t1, t2 = t2, t1 |
| 134 | r1, r2 = r2, r1 |
| 135 | r1l, r2l = r2l, r1l |
| 136 | assert r1 == 1 |
| 137 | return t1 |
| 138 | |
| 139 | class TestGF2Ops(unittest.TestCase): |
| 140 | """Test class for basic arithmetic properties of GF2Ops.""" |
no outgoing calls
no test coverage detected