(root)/
gmp-6.3.0/
mini-gmp/
tests/
mini-random.c
       1  /*
       2  
       3  Copyright 2011, 2013, 2018 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 <stdio.h>
      21  #include <stdlib.h>
      22  
      23  #include "mini-random.h"
      24  
      25  static void
      26  set_str (mpz_t r, const char *s)
      27  {
      28    if (mpz_set_str (r, s, 16) != 0)
      29      {
      30        fprintf (stderr, "mpz_set_str failed on input %s\n", s);
      31        abort ();
      32      }
      33  }
      34  
      35  void
      36  mini_urandomb (mpz_t r, unsigned long bits)
      37  {
      38    char *s;
      39    s = hex_urandomb (bits);
      40    set_str (r, s);
      41    free (s);
      42  }
      43  
      44  void
      45  mini_rrandomb (mpz_t r, unsigned long bits)
      46  {
      47    char *s;
      48    s = hex_rrandomb (bits);
      49    set_str (r, s);
      50    free (s);
      51  }
      52  
      53  void
      54  mini_rrandomb_export (mpz_t r, void *dst, size_t *countp,
      55  		      int order, size_t size, int endian, unsigned long bits)
      56  {
      57    char *s;
      58    s = hex_rrandomb_export (dst, countp, order, size, endian, bits);
      59    set_str (r, s);
      60    free (s);
      61  }
      62  
      63  void
      64  mini_random_op2 (enum hex_random_op op, unsigned long maxbits,
      65  		 mpz_t a, mpz_t r)
      66  {
      67    char *ap;
      68    char *rp;
      69  
      70    hex_random_op2 (op, maxbits, &ap, &rp);
      71    set_str (a, ap);
      72    set_str (r, rp);
      73  
      74    free (ap);
      75    free (rp);
      76  }
      77  
      78  void
      79  mini_random_op3 (enum hex_random_op op, unsigned long maxbits,
      80  		 mpz_t a, mpz_t b, mpz_t r)
      81  {
      82    char *ap;
      83    char *bp;
      84    char *rp;
      85  
      86    hex_random_op3 (op, maxbits, &ap, &bp, &rp);
      87    set_str (a, ap);
      88    set_str (b, bp);
      89    set_str (r, rp);
      90  
      91    free (ap);
      92    free (bp);
      93    free (rp);
      94  }
      95  
      96  void
      97  mini_random_op4 (enum hex_random_op op, unsigned long maxbits,
      98  		 mpz_t a, mpz_t b, mpz_t c, mpz_t d)
      99  {
     100    char *ap;
     101    char *bp;
     102    char *cp;
     103    char *dp;
     104  
     105    hex_random_op4 (op, maxbits, &ap, &bp, &cp, &dp);
     106    set_str (a, ap);
     107    set_str (b, bp);
     108    set_str (c, cp);
     109    set_str (d, dp);
     110  
     111    free (ap);
     112    free (bp);
     113    free (cp);
     114    free (dp);
     115  }
     116  
     117  void
     118  mini_random_bit_op (enum hex_random_op op, unsigned long maxbits,
     119  			 mpz_t a, mp_bitcnt_t *b, mpz_t r)
     120  {
     121    char *ap;
     122    char *rp;
     123  
     124    hex_random_bit_op (op, maxbits, &ap, b, &rp);
     125    set_str (a, ap);
     126    set_str (r, rp);
     127  
     128    free (ap);
     129    free (rp);
     130  }
     131  
     132  void
     133  mini_random_scan_op (enum hex_random_op op, unsigned long maxbits,
     134  		     mpz_t a, mp_bitcnt_t *b, mp_bitcnt_t *r)
     135  {
     136    char *ap;
     137  
     138    hex_random_scan_op (op, maxbits, &ap, b, r);
     139    set_str (a, ap);
     140  
     141    free (ap);
     142  }
     143  
     144  void
     145  mini_random_lucm_op (unsigned long maxbits, mpz_t v, mpz_t q, mpz_t m,
     146  			  long *Q, unsigned long *b0, int *res)
     147  {
     148    char *vp;
     149    char *qp;
     150    char *mp;
     151  
     152    hex_random_lucm_op (maxbits, &vp, &qp, &mp, Q, b0, res);
     153    set_str (v, vp);
     154    set_str (q, qp);
     155    set_str (m, mp);
     156  
     157    free (vp);
     158    free (qp);
     159    free (mp);
     160  }