gmp_libmpn_sec_invert Method |
Namespace: Math.Gmp.Native
public static int mpn_sec_invert( mp_ptr rp, mp_ptr ap, mp_ptr mp, mp_size_t n, mp_bitcnt_t nbcnt, mp_ptr tp )
If an inverse exists, return 1, otherwise return 0 and leave R undefined. In either case, the input A is destroyed.
It is required that M is odd, and that nbcnt ≥ ceil(log(A + 1)) + ceil(log(M + 1)). A safe choice is nbcnt = 2 * n * mp_bits_per_limb, but a smaller value might improve performance if M or A are known to have leading zero bits.
This function requires scratch space of mpn_sec_invert_itch(n) limbs to be passed in the tp parameter.
// Create multi-precision operands, and expected result. mp_ptr ap = new mp_ptr(new uint[] { 3 }); mp_ptr mp = new mp_ptr(new uint[] { 11 }); mp_ptr rp = new mp_ptr(ap.Size); mp_ptr result = new mp_ptr(new uint[] { 4 }); // Create scratch space. mp_size_t size = gmp_lib.mpn_sec_invert_itch(ap.Size); mp_ptr tp = new mp_ptr(size); // Set rp = ap^-1 mod mp. gmp_lib.mpn_sec_invert(rp, ap, mp, ap.Size, (uint)(2 * ap.Size* gmp_lib.mp_bits_per_limb), tp); // Assert result of operation. Assert.IsTrue(rp[0] == result[0]); // Release unmanaged memory. gmp_lib.free(ap, mp, rp, result, tp);