From 37da2899f40661e3e9631e497da8dc59b971cbd0 Mon Sep 17 00:00:00 2001 From: "Charles.Forsyth" Date: Fri, 22 Dec 2006 17:07:39 +0000 Subject: 20060303a --- libsec/port/genprime.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 libsec/port/genprime.c (limited to 'libsec/port/genprime.c') diff --git a/libsec/port/genprime.c b/libsec/port/genprime.c new file mode 100644 index 00000000..c0e16d92 --- /dev/null +++ b/libsec/port/genprime.c @@ -0,0 +1,27 @@ +#include "os.h" +#include +#include + +// generate a probable prime. accuracy is the miller-rabin interations +void +genprime(mpint *p, int n, int accuracy) +{ + mpdigit x; + + // generate n random bits with high and low bits set + mpbits(p, n); + genrandom((uchar*)p->p, (n+7)/8); + p->top = (n+Dbits-1)/Dbits; + x = 1; + x <<= ((n-1)%Dbits); + p->p[p->top-1] &= (x-1); + p->p[p->top-1] |= x; + p->p[0] |= 1; + + // keep icrementing till it looks prime + for(;;){ + if(probably_prime(p, accuracy)) + break; + mpadd(p, mptwo, p); + } +} -- cgit v1.2.3