public static BigInteger InverseGf(this BigInteger value, BigInteger modulus) { BigInteger b = 1, c = 0, u = value, v = modulus; while (u.GetBitCount() > 1) { var j = u.GetBitCount() - v.GetBitCount(); if (j < 0) { (u, v) = (v, u); (c, b) = (b, c); j = -j; } u = u.AddGf(v << j); b = b.AddGf(c << j); } return(b); }