(root)/
gmp-6.3.0/
mini-gmp/
tests/
t-powm.c
       1  /*
       2  
       3  Copyright 2012, 2022, Free Software Foundation, Inc.
       4  
       5  This file is part of the GNU MP Library test suite.
       6  
       7  The GNU MP Library test suite is free software; you can redistribute it
       8  and/or modify it under the terms of the GNU General Public License as
       9  published by the Free Software Foundation; either version 3 of the License,
      10  or (at your option) any later version.
      11  
      12  The GNU MP Library test suite is distributed in the hope that it will be
      13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
      14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
      15  Public License for more details.
      16  
      17  You should have received a copy of the GNU General Public License along with
      18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
      19  
      20  #include <limits.h>
      21  #include <stdlib.h>
      22  #include <stdio.h>
      23  
      24  #include "testutils.h"
      25  
      26  #define MAXBITS 400
      27  #define COUNT 1000
      28  
      29  void
      30  testmain (int argc, char **argv)
      31  {
      32    unsigned i;
      33    mpz_t b, e, m, res, ref;
      34  
      35    mpz_init (b);
      36    mpz_init (e);
      37    mpz_init (m);
      38    mpz_init (res);
      39    mpz_init (ref);
      40  
      41    for (i = 0; i < COUNT; i++)
      42      {
      43        mini_random_op4 (OP_POWM, MAXBITS, b, e, m, ref);
      44        mpz_powm (res, b, e, m);
      45        if (mpz_cmp (res, ref))
      46  	{
      47  	  fprintf (stderr, "mpz_powm failed:\n");
      48  	  dump ("b", b);
      49  	  dump ("e", e);
      50  	  dump ("m", m);
      51  	  dump ("r", res);
      52  	  dump ("ref", ref);
      53  	  abort ();
      54  	}
      55      }
      56  
      57    /* res >= 0, come from the random choices above, */
      58    if (mpz_cmp_ui (res, 1) <= 0) /* if too small, */
      59      mpz_add_ui (res, res, 9); /* add an arbitrary value. */
      60  
      61    mpz_set_ui (e, 0);
      62    /* Test the case m^0 (mod m), expect 1 (m is greater than 1). */
      63    mpz_powm (res, res, e, res);
      64    if (mpz_cmp_ui (res, 1) != 0)
      65      {
      66        fprintf (stderr, "mpz_powm failed: b=m, e=0; 1 expected,\n");
      67        dump ("m", res);
      68        dump ("r", res);
      69        abort ();
      70      }
      71  
      72    /* Now res is 1. */
      73    /* Test the case 1^0 (mod 1), expect 0. */
      74    mpz_powm (res, res, e, res);
      75    if (mpz_size (res))
      76      {
      77        fprintf (stderr, "mpz_powm failed: b=1, e=0, m=1; 0 expected,\n");
      78        dump ("r", res);
      79        abort ();
      80      }
      81  
      82    mpz_clear (b);
      83    mpz_clear (e);
      84    mpz_clear (m);
      85    mpz_clear (res);
      86    mpz_clear (ref);
      87  }