Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing multiplication operation that has overflowed

Given the code:

uint Function(uint value)
{
  return value * 0x123456D;
}

Inputting the value 0x300 yields the result 0x69D04700. This is only the lower 32 bits of the result. Given the result 0x69D04700 and the factor 0x123456D, is it possible to retrieve all numbers such that (value * 0x123456D) & 0xFFFFFFFF = 0x69D04700 in a fast way?

Edit: The code I show is pseudocode - I can't widen the return type.

like image 915
jakobbotsch Avatar asked Aug 04 '11 16:08

jakobbotsch


1 Answers

What you need is modular division, which can be computed with a version of Euclid's algorithm. In this case the result is 768.

This is very fast -- time (log n)2 even for a naive implementation. (If you needed to work with large numbers I could give references to better algorithms.)

See extended Euclidean algorithm for a sketch of how to implement this.

like image 182
Charles Avatar answered Oct 23 '22 10:10

Charles