> static uint16 mul(register uint16 a, register uint16 b)
> {
>     register word32 p;
> 
>     p = (word32) a *b;
>     if (p) {
>         b = low16(p);
>         a = p >> 16;
>         return (b - a) + (b < a);
>     } else if (a) {
>         return 1 - a;
>     } else {
>         return 1 - b;
>     }
> }                               /* mul */

> There was some discussion of approaches to coding a constant time
> multiplication mod 65537 function on sci.crypt around October 96.

Note that on many microprocessors multiply is not a constant-time instruction,
leading to difficulties.

However, in more recent code there are two alternatives:

An optimized version of the above:
{
        register PGPUInt32 p;

        p = (uint32)a * b;
        if (p) {
                b = low16(p);
                a = p>>16;
                return (b - a) + (b < a);
        } else {
                return 1-a-b;
        }
}

and the AVOID_JUMPS case, which is constant-time if the multiply is
(it's also faster on some processors):

{
        register word32 p;

        a = low16(a-1);
        b = low16(b-1);
        p = (uint32)a*b + a + b;
        a = low16(p);
        b = p >> 16;
        return (a - b) + (a < b) + 1;
}

The only potential gotcha is the "a < b" part, which compilers
usually find branch-free code for, but sometimes they're dumb.
-- 
        -Colin

Reply via email to