Skip to content

IntPowMod

Description

Generic modular exponentiation convenience macro.

Parameters

Name Direction Description
result out Destination for the reduced power
base in Base value
exponent in Exponent selected through generic dispatch
modulus in Modulus

Usage example (from documentation)

  IntPowMod(&result, &base, 65537u, &modulus);

Success

Returns true; *result holds base ** exponent mod modulus computed via repeated-squaring with intermediate reduction.

Failure

Returns false when modulus is zero (logged), when exponent is negative, or when an intermediate allocation fails; *result is unchanged.

Usage example (Cross-references)

Usage examples (Cross-references)
    
    bool test_int_pow_mod_scalar(void) {
        WriteFmt("Testing IntPowMod scalar-exponent dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntPowMod(&result_value, &base, 20u, &mod);
    
        bool result = IntToU64(&result_value) == 3;
    
    bool test_int_pow_mod_integer_exponent(void) {
        WriteFmt("Testing IntPowMod Int-exponent dispatch\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntPowMod(&result_value, &base, &exp, &mod);
    
        bool result = IntToU64(&result_value) == 445;
    
    bool test_int_pow_mod_scalar_zero_modulus(void) {
        WriteFmt("Testing IntPowMod scalar-exponent zero modulus handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntInit(&alloc.base);
    
        IntPowMod(&result_value, &base, 8u, &mod);
        DefaultAllocatorDeinit(&alloc);
        return false;
    
    bool test_int_pow_mod_integer_zero_modulus(void) {
        WriteFmt("Testing IntPowMod Int-exponent zero modulus handling\n");
    
        DefaultAllocator alloc = DefaultAllocatorInit();
        Int result_value = IntFrom(99, &alloc.base);
    
        bool result = !IntPowMod(&result_value, &base, &exp, &mod);
        result      = result && (IntCompare(&result_value, 99) == 0);
Last updated on