Skip to content

IntModMul

Description

Compute (a * b) mod modulus.

Parameters

Name Direction Description
result out Destination for the reduced product
a in Left operand
b in Right operand
modulus in Modulus

Usage example (from documentation)

  IntModMul(&result, &a, &b, &modulus);

Success

Returns true. *result holds (a * b) mod modulus.

Failure

Returns false on modulus == 0 or allocator OOM. *result is left untouched.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    bool IntModMul(Int *result, const Int *a, const Int *b, const Int *modulus) {
        ValidateInt(result);
        ValidateInt(a);
        }
    
        if (!IntModMul(&value, a, &inverse, modulus)) {
            IntDeinit(&inverse);
            IntDeinit(&value);
    
    bool IntSquareMod(Int *result, const Int *value, const Int *modulus) {
        return IntModMul(result, value, value, modulus);
    }
        while (exponent > 0) {
            if (exponent & 1u) {
                if (!IntModMul(&scratch, &acc, &base_mod, modulus)) {
                    IntDeinit(&acc);
                    IntDeinit(&base_mod);
            exponent >>= 1u;
            if (exponent > 0) {
                if (!IntModMul(&scratch, &base_mod, &base_mod, modulus)) {
                    IntDeinit(&acc);
                    IntDeinit(&base_mod);
        while (!IntIsZero(&exp)) {
            if (int_is_odd(&exp)) {
                if (!IntModMul(&scratch, &acc, &base_mod, modulus)) {
                    IntDeinit(&acc);
                    IntDeinit(&base_mod);
            }
            if (!IntIsZero(&exp)) {
                if (!IntModMul(&scratch, &base_mod, &base_mod, modulus)) {
                    IntDeinit(&acc);
                    IntDeinit(&base_mod);
                    }
    
                    if (!IntModMul(&next, &r, &b, modulus)) {
                        IntDeinit(&b);
                        IntDeinit(&b_sq);
                    }
                    next = IntInit(IntAllocator(result));
                    if (!IntModMul(&next, &t, &b_sq, modulus)) {
                        IntDeinit(&b);
                        IntDeinit(&b_sq);
    
    bool test_int_mod_mul(void) {
        WriteFmt("Testing IntModMul\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntModMul(&result_value, &a, &b, &m);
    
        bool result = IntToU64(&result_value) == 22;
        result      = result && (IntToU64(&result_value) == 12);
    
        IntModMul(&check, &result_value, &b, &m);
        result = result && (IntCompare(&check, 10) == 0);
        result      = result && (IntToU64(&result_value) == 4);
    
        IntModMul(&check, &value, &result_value, &mod);
        result = result && (IntToU64(&check) == 1);
    ///
    static bool test_m14_mod_mul_null_result_deadend(void) {
        WriteFmt("Testing IntModMul NULL result validation\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int mod = IntFrom(7, &alloc.base);
    
        IntModMul(NULL, &a, &b, &mod);
    
        DefaultAllocatorDeinit(&alloc);
        pass = pass && (IntToU64(&result) == 4);
    
        IntModMul(&check, &value, &result, &mod);
        pass = pass && (IntToU64(&check) == 1);
        pass      = pass && (IntToU64(&result) == 9);
    
        IntModMul(&check, &value, &result, &mod);
        pass = pass && (IntToU64(&check) == 1);
        Int r = IntFrom(7u, a);
    
        bool ok = IntModMul(&r, &x, &y, &m); // frees ar/br/prod (2126-2128)
        ok      = ok && IntToU64(&r) == (12345ull * 67890ull) % 1009ull;
Last updated on