ref: 90baca8302fd8b6ccad7d06b61da9ecf451ccf83
parent: 3084c76351b83338becd8a36d691ce076ea31c89
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Aug 28 20:46:51 EDT 2016
libmp: mpnrand from 9front
--- a/libmp/mpnrand.c
+++ b/libmp/mpnrand.c
@@ -6,30 +6,18 @@
mpint*
mpnrand(mpint *n, void (*gen)(uchar*, int), mpint *b)
{
- mpint *m;
int bits;
- /* m = 2^bits - 1 */
bits = mpsignif(n);
- m = mpnew(bits+1);
- mpleft(mpone, bits, m);
- mpsub(m, mpone, m);
-
+ if(bits == 0)
+ abort();
if(b == nil){
b = mpnew(bits);
setmalloctag(b, getcallerpc(&n));
}
-
- /* m = m - (m % n) */
- mpmod(m, n, b);
- mpsub(m, b, m);
-
do {
mprand(bits, gen, b);
- } while(mpcmp(b, m) >= 0);
-
- mpmod(b, n, b);
- mpfree(m);
+ } while(mpmagcmp(b, n) >= 0);
return b;
}