PRIME(3)

πŸ“¦ version 0_git20260208, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

πŸ’… PRIME(3) β€” like, the prime(3) page from your Library Functions Manual, bestie

okay so first things first, this is section 3 of the Library Functions Manual, and it's all about prime numbers. yes, those primes. the big ones. the cryptographic ones. grab a strawberry πŸ“ and let's go.


✨ NAME ✨

genprime, gensafeprime, genstrongprime, DSAprimes, probably_prime, smallprimetest β€” prime number generation, babes. that's the whole vibe. πŸ”₯


🌸 SYNOPSIS 🌸

#include <u.h>
#include <libc.h>
#include <mp.h>
#include <libsec.h>

int  smallprimetest(mpint *p)

int  probably_prime(mpint *p, int nrep)

void genprime(mpint *p, int n, int nrep)

void gensafeprime(mpint *p, mpint *alpha, int n, int accuracy)

void genstrongprime(mpint *p, int n, int nrep)

void DSAprimes(mpint *q, mpint *p, uchar seed[SHA1dlen])

like, look at all those includes. u.h, libc.h, mp.h, libsec.h β€” you need them all, no skipping. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

Public key algorithms abound in prime numbers, like genuinely obsessed with them. so the following routines either generate primes or test numbers for primality. that's it. that's the whole job. πŸ’…

🌈 the vibes, function by function

πŸŽ€ smallprimetest

checks for divisibility by the first 10000 primes. it returns 0 if p is not divisible by the primes and -1 if it is. quick little vibe check, lowkey. ✨

πŸ’œ probably_prime

uses the Miller-Rabin test to test p. it returns non-zero if p is probably prime. the probability of it not being prime is 1/4**nrep. so like, crank up nrep if you want to be sure sure. ngl that's the whole deal. 😭

🌸 genprime

generates a random n bit prime. since it uses the Miller-Rabin test, nrep is the repetition count passed to probably_prime. easy. 🫢

⭐ gensafeprime

generates an n-bit prime p and a generator alpha of the multiplicative group of integers mod p. there is a prime q such that p-1=2*q. safe prime behavior, we love to see it. πŸ‘‘

πŸ”₯ genstrongprime

generates a prime, p, with the following properties, and yes you're getting all three:

  • (p-1)/2 is prime. therefore p-1 has a large prime factor, p'.
  • p'-1 has a large prime factor
  • p+1 has a large prime factor

strong prime said "i have layers" and honestly? iconic. πŸ’€

🧚 DSAprimes

generates two primes, q and p, using the NIST recommended algorithm for DSA primes. q divides p-1. the random seed used is also returned, so that skeptics can later confirm the computation. transparency slay. πŸ’–

⚠️ ok bestie listen: Be patient; this is a slow algorithm. like, actually slow. go make a snack, hydrate, come back. πŸ“

🐚 SOURCE 🐚

/src/libsec

πŸ‘€ SEE ALSO πŸ‘€

aes(3), blowfish(3), des(3), elgamal(3), rsa(3)


and that's the page!! πŸŽ€ it's PRIME(3) from the Library Functions Manual, section 3, straight outta /src/libsec β€” stay cryptographic, bestie, mwah πŸ’…πŸ’–βœ¨