prime 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. .TH PRIME 2
  2. .SH NAME
  3. genprime, gensafeprime, genstrongprime, DSAprimes, probably_prime, smallprimetest \- prime number generation
  4. .SH SYNOPSIS
  5. .B #include <u.h>
  6. .br
  7. .B #include <libc.h>
  8. .br
  9. .B #include <mp.h>
  10. .br
  11. .B #include <libsec.h>
  12. .PP
  13. .B
  14. int smallprimetest(mpint *p)
  15. .PP
  16. .B
  17. int probably_prime(mpint *p, int nrep)
  18. .PP
  19. .B
  20. void genprime(mpint *p, int n, int nrep)
  21. .PP
  22. .B
  23. void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy)
  24. .PP
  25. .B
  26. void genstrongprime(mpint *p, int n, int nrep)
  27. .PP
  28. .B
  29. void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen])
  30. .SH DESCRIPTION
  31. .PP
  32. Public key algorithms abound in prime numbers. The following routines
  33. generate primes or test numbers for primality.
  34. .PP
  35. .I Smallprimetest
  36. checks for divisibility by the first 10000 primes. It returns 0
  37. if
  38. .I p
  39. is not divisible by the primes and \-1 if it is.
  40. .PP
  41. .I Probably_prime
  42. uses the Miller-Rabin test to test
  43. .IR p .
  44. It returns non-zero if
  45. .I P
  46. is probably prime. The probability of it not being prime is
  47. 1/4**\fInrep\fR.
  48. .PP
  49. .I Genprime
  50. generates a random
  51. .I n
  52. bit prime. Since it uses the Miller-Rabin test,
  53. .I nrep
  54. is the repetition count passed to
  55. .IR probably_prime .
  56. .I Gensafegprime
  57. generates an
  58. .IR n -bit
  59. prime
  60. .I p
  61. and a generator
  62. .I alpha
  63. of the multiplicative group of integers mod \fIp\fR;
  64. there is a prime \fIq\fR such that \fIp-1=2*q\fR.
  65. .I Genstrongprime
  66. generates a prime,
  67. .IR p ,
  68. with the following properties:
  69. .IP \-
  70. .IR p -1
  71. has a large prime factor,
  72. .IR p '.
  73. A large factor is one close to 1/2
  74. the bit length of
  75. .IR p .
  76. .IP \-
  77. .IR p '-1
  78. has a large prime factor
  79. .IP \-
  80. .IR p +1
  81. has a large prime factor
  82. .IP \-
  83. (\fIp\fR-1)/2 is prime
  84. .PP
  85. .I DSAprimes
  86. generates two primes,
  87. .I q
  88. and
  89. .IR p,
  90. using the NIST recommended algorithm for DSA primes.
  91. .I q
  92. divides
  93. .IR p -1.
  94. The random seed used is also returned, so that skeptics
  95. can later confirm the computation. Be patient; this is a
  96. slow algorithm.
  97. .SH SOURCE
  98. .B /sys/src/libsec
  99. .SH SEE ALSO
  100. .IR aes (2)
  101. .IR blowfish (2),
  102. .IR des (2),
  103. .IR elgamal (2),
  104. .IR rsa (2),