summaryrefslogtreecommitdiff
path: root/sys/src/cmd/unix/drawterm/libsec/genprime.c
diff options
context:
space:
mode:
authorTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
committerTaru Karttunen <taruti@taruti.net>2011-03-30 15:46:40 +0300
commite5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch)
treed8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/cmd/unix/drawterm/libsec/genprime.c
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/cmd/unix/drawterm/libsec/genprime.c')
-rwxr-xr-xsys/src/cmd/unix/drawterm/libsec/genprime.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/sys/src/cmd/unix/drawterm/libsec/genprime.c b/sys/src/cmd/unix/drawterm/libsec/genprime.c
new file mode 100755
index 000000000..c0e16d92b
--- /dev/null
+++ b/sys/src/cmd/unix/drawterm/libsec/genprime.c
@@ -0,0 +1,27 @@
+#include "os.h"
+#include <mp.h>
+#include <libsec.h>
+
+// 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);
+ }
+}