Click or drag to resize
gmp_libmpn_sec_invert Method
Set R to the inverse of A modulo M, where R = {rp, n}, A = {ap, n}, and M = {mp, n}. This function’s interface is preliminary.

Namespace:  Math.Gmp.Native
Assembly:  Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
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
)

Parameters

rp
Type: Math.Gmp.Nativemp_ptr
The result integer.
ap
Type: Math.Gmp.Nativemp_ptr
The first operand integer.
mp
Type: Math.Gmp.Nativemp_ptr
The second operand integer.
n
Type: Math.Gmp.Nativemp_size_t
The number of limbs of ap and mp.
nbcnt
Type: Math.Gmp.Nativemp_bitcnt_t
The third operand integer.
tp
Type: Math.Gmp.Nativemp_ptr
The scratch operand integer.

Return Value

Type: Int32
If an inverse exists, return 1, otherwise return 0 and leave R undefined.
Remarks

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.

Examples
// 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);
See Also