I need to create an algorithm implemented in C that performs modulo arithmetic between an arbitrary number of bytes and one byte. See this:
typedef struct{
u_int8_t * data;
u_int16_t length;
}UBigInt;
u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){
}
For powers of two, a and (b-1) can be used, but what about non-powers of two?
I understand that one of the methods is: a - b * (a / b)
This will require the use of UBigIntDivisionWithUInt8 and UBigIntMultiplicationWithUInt8 and UBigIntSubtractionWithUBigInt. Maybe a more efficient way to do this?
Thank.
This is the implementation I have now:
u_int8_t UBigIntModuloWithUInt8(UBigInt a,u_int8_t b){
if (!(b & (b - 1)))
return a.data[a.length - 1] & b - 1;
u_int16_t result = 0;
for(int x = 0; x < a.length; x++) {
result *= (256 % b);
result %= b;
result += a.data[x] % b;
result %= b;
}
return result;
}
source
share