I have a c struct as
typedef struct u128b {
uint64_t hi,
uint64_t lo
} u128b
How would I perform a modulo expression x^e mod m when all 3 variables, x, ė, and m are all u128b structs?
I've attempted to run the code from https://stackoverflow.com/a/20114154/11667622
u128b mul_mod(u128b a, u128b b, u128b m)
{
if (m == 0)
return a * b;
u128b r = {0, 0};
while(a > 0)
{
if (a & 1)
if ((r += b) > m) r %= m;
a >>= 1;
if ((b <<= 1) > m) b %= m;
}
return r;
}
//https://stackoverflow.com/questions/20111827/various-questions-about-rsa-encryption/20114154#20114154
u128b pow_mod(u128b base, u128b expon, u128b mod)
{
u128b remainder = {0, 1};
while (expon > 0) {
if (expon & 1)
remainder = mul_mod(r, base, mod);
base = mul_mod(base, base, mod);
expon >>= 1;
}
return remainder;
}
This works when all the parameters and variables are uint64_t but how would I apply this to two separate 64 bit variables hi and low for all 3 variables? shifting bits across hi and lo? and how would the modulo work with the hi and lo bits?