MCPcopy Create free account
hub / github.com/ElementsProject/elements / inv

Method inv

src/minisketch/tests/pyminisketch.py:119–137  ·  view source on GitHub ↗

Compute the inverse of x in GF(2^field_size).

(self, x)

Source from the content-addressed store, hash-verified

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
139class TestGF2Ops(unittest.TestCase):
140 """Test class for basic arithmetic properties of GF2Ops."""

Callers 3

field_size_testMethod · 0.95
poly_monicFunction · 0.80
berlekamp_masseyFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected