Binary multiplication on elliptic curves in C++
n = mod(n, m);  // the %-operator doesn't return a non-negative integer in every case so I wrote a function



I would find a comment explaining m more useful than a comment explaining why you made a mod function, because everyone ends up writing a mod function which behaves sensibly.

I don't know much about elliptic curves, but I have worked with finite fields. Are you sure it makes sense to take n mod m as though they were integers? The only makes sense to me if m is also the characteristic (i.e. if it's a field of prime order).




    unsigned int mask = 1 &lt;&lt; (sizeof (n) - 1);  // more flexible and platform independent than 1 &lt;&lt; 31
    for (; mask; mask &gt;&gt;= 1)



Why loop down? If you loop up then you get the platform-independence for free, and also you loop fewer times when n is small. Obviously the loop invariant would change, but it would be closer to school long multiplication and so might also be more maintainable.

    for (; n; n &gt;&gt;= 1)
    {
        if (n &amp; 1)
            r = add(r, p);
        p = add(p, p);
    }