#include "allgbreak.c"

/* this file has a silly main function to test the primitive root routines */
/* it needs those routines from allgbreak.c */

main(int argc, char *argv[])
{
  int pint;

  BIGNUM *p, *g, *pminusone;

  BN_CTX *ctx;

  char *pstring, *gstring;

  if ( argc >= 3 ) debug_level=atoi(argv[2]);
  
  prog = argv[0];

  debug("Checking usage");

  if (argc < 2) myerror("Need one argument: p");

  pint=atoi(argv[1]);

  debug("initializing");

  ctx=BN_CTX_new();
  if (ctx==NULL) myerror("error allocating ctx");
  /* temporary structure */

  p=BN_new();
  if (p==NULL) myerror("error allocating p");
  /* the prime we are considering */

  g=BN_new();
  if (g==NULL) myerror("error allocating g");
  /* the primitive root */

  pminusone=BN_new();
  if (pminusone==NULL) myerror("error allocating pminusone");
  /* p-1 */

  if ( !BN_set_word(p, pint) ) myerror("error initializing p to pint");

  trace_bignum("p", p); 

  if ( !BN_sub(pminusone, p, BN_value_one()) )
    myerror("error initializing pminusone to p-1");

  trace_bignum("p-1", pminusone); 

  if ( !BN_one(g) ) myerror("error initializing g to 1");

  debug("doing first primroot");
  if ( !primroot(g, p, pminusone, ctx) ) myerror("error in primroot");
  /* first primitive root */
  debug("first primroot successful");

  
  pstring=BN_bn2dec(p);
  printf("prime = %s\n\n", pstring);
  OPENSSL_free(pstring);

  while ( BN_cmp(g,p) == -1 )
    /* g less than p */
    {
      gstring=BN_bn2dec(g);
      printf("primitive root = %s\n", gstring);
      OPENSSL_free(gstring);
      if ( !primroot(g, p, pminusone, ctx) ) myerror("error in primroot");
    }

}