gmp_libmpn_sec_add_1 Method |
Namespace: Math.Gmp.Native
public static mp_limb_t mpn_sec_add_1( mp_ptr rp, mp_ptr ap, mp_size_t n, mp_limb_t b, mp_ptr tp )
This function takes O(N) time, unlike the leaky functions mpn_add_1(mp_ptr, mp_ptr, mp_size_t, mp_limb_t) which is O(1) on average. It requires scratch space of mpn_sec_add_1_itch(mp_size_t)(n) limbs, to be passed in the tp parameter. The scratch space requirements are guaranteed to be at most n limbs, and increase monotonously in the operand size.
// Create multi-precision operands, and expected result. mp_ptr ap = new mp_ptr(new uint[] { 0xffffffff, 0xffffffff }); mp_ptr result = new mp_ptr(new uint[] { 0x00000000, 0x00000000 }); mp_ptr rp = new mp_ptr(result.Size); // Create scratch space. mp_size_t size = gmp_lib.mpn_sec_add_1_itch(ap.Size); mp_ptr tp = new mp_ptr(size); // Set rp = ap + 1. mp_limb_t carry = gmp_lib.mpn_sec_add_1(rp, ap, ap.Size, 1, tp); // Assert result of operation. Assert.IsTrue(carry == 1); Assert.IsTrue(rp.SequenceEqual(result)); // Release unmanaged memory. gmp_lib.free(rp, ap, tp, result);