math/mpx-mul4-amd64-sse2.S: SSE2 multipliers for AMD64.
[catacomb] / math / mpbarrett.h
index 40e0fe4..b3ab798 100644 (file)
  * modexp routine provided which uses Barrett reduction rather than
  * Montgomery reduction.  This is handy when you're working on indices in an
  * even-order cyclic group or something.
+ *
+ * In more detail: suppose that %$b^{k-1} \le m < b^k$%.  Let %$\mu = {}$%
+ * %$\lfloor b^{2k}/m \rfloor$%; %$\mu$% is a scaled approximation to the
+ * reciprocal %$1/m$%.  Now, suppose we're given some %$a$% with
+ * %$0 \le a < b^{2k}$%.  The first step is to calculate an approximation
+ * %$q = \lfloor \mu \lfloor a/b^{k-1} \rfloor/b^{k+1} \rfloor$% to the
+ * quotient %$a/m$%.  Then we have:
+ *
+ *     %$\lfloor a/m - a/b^{2k} - b^{k-1}/m + 1/b^{k+1} \rfloor \le {}$%
+ *             %$q \le \lfloor a/m \rfloor
+ *
+ * But by assumption %$a < b^{2k}$% and %$2^{k-1} \le m$% so
+ *
+ *     %$\lfloor a/m \rfloor - 2 \le q \le \lfloor a/m \rfloor$%
+ *
+ * Now we approximate the remainder by calculating %$r = a - q m$%.
+ * Certainly %$r \equiv a \pmod{m}$%; and
+ *
+ *     %$0 \le r \le (a - m \lfloor a/m \rfloor) + 2 m < 3 m$%.
+ *
+ * So the remainder can be fixed up with at most two conditional
+ * subtractions.
  */
 
 #ifndef CATACOMB_MPBARRETT_H