File: support.c

package info (click to toggle)
libzn-poly 0.8-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 700 kB
  • sloc: ansic: 9,295; python: 162; makefile: 7; sh: 6
file content (96 lines) | stat: -rw-r--r-- 2,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
   support.c:  various support routines for test, profiling and tuning code
   
   Copyright (C) 2007, 2008, David Harvey
   
   This file is part of the zn_poly library (version 0.8).
   
   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) version 3 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "support.h"


unsigned test_bitsizes[] = {2, 3, 8,
                            ULONG_BITS/2 - 1, ULONG_BITS/2, ULONG_BITS/2 + 1,
                            ULONG_BITS - 2, ULONG_BITS - 1, ULONG_BITS};
                  
unsigned num_test_bitsizes = sizeof(test_bitsizes) / sizeof(unsigned);


gmp_randstate_t randstate;


void mpz_to_mpn(mp_limb_t* res, size_t len, const mpz_t op)
{
   ZNP_ASSERT(mpz_size(op) <= len);
   size_t count_p;
   
   mpz_export(res, &count_p, -1, sizeof(mp_limb_t), 0, GMP_NAIL_BITS, op);
   // zero-pad remaining buffer
   for (; count_p < len; count_p++)
      res[count_p] = 0;
}


void mpn_to_mpz(mpz_t res, const mp_limb_t* op, size_t len)
{
   ZNP_ASSERT(len >= 1);
   mpz_import(res, len, -1, sizeof(mp_limb_t), 0, GMP_NAIL_BITS, op);
}


ulong random_ulong(ulong max)
{
   return gmp_urandomm_ui(randstate, max);
}


ulong random_ulong_bits(unsigned bits)
{
   return gmp_urandomb_ui(randstate, bits);
}


ulong random_modulus(unsigned bits, int require_odd)
{
   ZNP_ASSERT(bits >= 2 && bits <= ULONG_BITS);

   if (require_odd)
   {
      if (bits == 2)
         return 3;
      return (1UL << (bits - 1)) + 2*random_ulong_bits(bits - 2) + 1;
   }
   else
      return (1UL << (bits - 1)) + random_ulong_bits(bits - 1);
}


void zn_array_print(const ulong* x, size_t len)
{
   size_t i;
   printf("[");
   for (i = 0; i < len; i++)
   {
      if (i)
         printf(", ");
      printf("%lu", x[i]);
   }
   printf("]");
}


// end of file ****************************************************************