File: pack-test.c

package info (click to toggle)
libzn-poly 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 904 kB
  • sloc: ansic: 10,736; python: 124; makefile: 12; sh: 6
file content (273 lines) | stat: -rw-r--r-- 6,971 bytes parent folder | download | duplicates (3)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
   pack-test.c:  test code for functions in pack.c
   
   Copyright (C) 2007, 2008, David Harvey
   
   This file is part of the zn_poly library (version 0.9).
   
   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"
#include "zn_poly_internal.h"


/*
   Helper function for ref_zn_array_pack().

   Sets x = 2^k * (op[0] + op[1]*2^b + ... + op[n-1]*2^((n-1)*b)).
   
   Running time is soft-linear in output length.
*/
void
ref_zn_array_pack_helper (mpz_t x, const ulong* op, size_t n, unsigned b,
                          unsigned k)
{
   ZNP_ASSERT (n >= 1);

   if (n == 1)
   {
      // base case
      mpz_set_ui (x, op[0]);
      mpz_mul_2exp (x, x, k);
   }
   else
   {
      // recursively split into top and bottom halves
      mpz_t y;
      mpz_init (y);
      ref_zn_array_pack_helper (x, op, n / 2, b, k);
      ref_zn_array_pack_helper (y, op + n / 2, n - n / 2, b, 0);
      mpz_mul_2exp (y, y, (n / 2) * b + k);
      mpz_add (x, x, y);
      mpz_clear (y);
   }
}


/*
   Reference implementation of zn_array_pack().
   
   (doesn't take into account the s or r parameters)
*/
void
ref_zn_array_pack (mp_limb_t* res, const ulong* op, size_t n, unsigned b,
                   unsigned k)
{
   mpz_t x;
   mpz_init (x);
   ref_zn_array_pack_helper (x, op, n, b, k);
   mpz_to_mpn (res, CEIL_DIV (n * b + k, GMP_NUMB_BITS), x);
   mpz_clear (x);
}


/*
   Helper function for ref_zn_array_unpack().

   Inverse operation of ref_zn_array_pack_helper(); each output coefficient
   occupies ceil(b / ULONG_BITS) ulongs.
   
   Running time is soft-linear in output length.
*/
void
ref_zn_array_unpack_helper (ulong* res, const mpz_t op, size_t n, unsigned b,
                            unsigned k)
{
   ZNP_ASSERT (n >= 1);
   ZNP_ASSERT (mpz_sizeinbase (op, 2) <= n * b + k);

   unsigned w = CEIL_DIV (b, ULONG_BITS);

   mpz_t y;
   mpz_init (y);
   
   if (n == 1)
   {
      // base case
      unsigned i;
      mpz_tdiv_q_2exp (y, op, k);
      for (i = 0; i < w; i++)
      {
         res[i] = mpz_get_ui (y);
         mpz_tdiv_q_2exp (y, y, ULONG_BITS);
      }
   }
   else
   {
      // recursively split into top and bottom halves
      mpz_tdiv_q_2exp (y, op, (n / 2) * b + k);
      ref_zn_array_unpack_helper (res + w * (n / 2), y, n - n / 2, b, 0);
      mpz_tdiv_r_2exp (y, op, (n / 2) * b + k);
      ref_zn_array_unpack_helper (res, y, n / 2, b, k);
   }
   
   mpz_clear (y);
}


/*
   Reference implementation of zn_array_unpack().
*/
void
ref_zn_array_unpack (ulong* res, const mp_limb_t* op, size_t n, unsigned b,
                     unsigned k)
{
   mpz_t x;
   mpz_init (x);
   mpn_to_mpz (x, op, CEIL_DIV (n * b + k, GMP_NUMB_BITS));
   ref_zn_array_unpack_helper (res, x, n, b, k);
   mpz_clear (x);
}



/*
   tests zn_array_pack() once for given n, b, k
*/
int
testcase_zn_array_pack (size_t n, unsigned b, unsigned k)
{
   ZNP_ASSERT (b >= 1);
   ZNP_ASSERT (n >= 1);

   int success = 1;
   ulong* in = (ulong*) malloc (sizeof (ulong) * n);

   size_t size = CEIL_DIV (n * b + k, GMP_NUMB_BITS);
   mp_limb_t* res = (mp_limb_t*) malloc (sizeof (mp_limb_t) * (size + 2));
   mp_limb_t* ref = (mp_limb_t*) malloc (sizeof (mp_limb_t) * (size + 2));

   // sentries to check buffer overflow
   res[0] = res[size + 1] = ref[0] = ref[size + 1] = 0x1234;

   // generate random data: at most b bits per input coefficient, possibly less
   unsigned rand_bits = (b >= ULONG_BITS) ? ULONG_BITS : b;
   rand_bits = random_ulong (rand_bits) + 1;
   ulong max = (rand_bits == ULONG_BITS)
                        ? ((ulong)(-1)) : ((1UL << rand_bits) - 1);
   size_t i;
   for (i = 0; i < n; i++)
      in[i] = random_ulong (max);

   // run target and reference implementation
   zn_array_pack (res + 1, in, n, 1, b, k, 0);
   ref_zn_array_pack (ref + 1, in, n, b, k);
   
   // check sentries
   success = success && (res[0] == 0x1234);
   success = success && (ref[0] == 0x1234);
   success = success && (res[size + 1] == 0x1234);
   success = success && (ref[size + 1] == 0x1234);
   // check correct result
   success = success && (mpn_cmp (res + 1, ref + 1, size) == 0);

   free (ref);
   free (res);
   free (in);
   
   return success;
}



/*
   tests zn_array_pack() on a range of input cases
*/
int
test_zn_array_pack (int quick)
{
   int success = 1;
   unsigned b, k;
   size_t n;

   for (b = 1; b < 3 * ULONG_BITS && success; b++)
   for (n = 1; n < (quick ? 100 : 200) && success;
        n += (quick ? (n < 5 ? 1 : 13) : 1))
   for (k = 0; k < 160; k += 20)
      success = success && testcase_zn_array_pack (n, b, k);
   
   return success;
}



/*
   tests zn_array_unpack() once for given n, b, k
*/
int
testcase_zn_array_unpack (size_t n, unsigned b, unsigned k)
{
   size_t buf_size = CEIL_DIV (n * b + k, GMP_NUMB_BITS);
   size_t size = n * CEIL_DIV (b, ULONG_BITS);

   mp_limb_t* buf = (mp_limb_t*) malloc (sizeof (mp_limb_t) * buf_size);
   ulong* res = (ulong*) malloc (sizeof (ulong) * (size + 2));
   ulong* ref = (ulong*) malloc (sizeof (ulong) * (size + 2));

   // sentries to check buffer overflow
   res[0] = res[size + 1] = ref[0] = ref[size + 1] = 0x1234;

   // generate random data
   mpz_t x;
   mpz_init (x);
   mpz_urandomb (x, randstate, n * b);
   mpz_mul_2exp (x, x, k);
   mpz_to_mpn (buf, buf_size, x);
   mpz_clear (x);

   // run target and reference implementation
   zn_array_unpack (res + 1, buf, n, b, k);
   ref_zn_array_unpack (ref + 1, buf, n, b, k);
   
   int success = 1;

   // check sentries
   success = success && (res[0] == 0x1234);
   success = success && (ref[0] == 0x1234);
   success = success && (res[size + 1] == 0x1234);
   success = success && (ref[size + 1] == 0x1234);
   // check correct result
   success = success && (zn_array_cmp (res + 1, ref + 1, size) == 0);
   
   free (ref);
   free (res);
   free (buf);

   return success;
}


/*
   tests zn_array_unpack() on a range of input cases
*/
int
test_zn_array_unpack (int quick)
{
   int success = 1;
   unsigned b, k;
   size_t n;

   for (b = 1; b < 3 * ULONG_BITS && success; b++)
   for (n = 1; n < (quick ? 100 : 200) && success;
        n += (quick ? (n < 5 ? 1 : 13) : 1))
   for (k = 0; k < 160; k += 19)
      success = success && testcase_zn_array_unpack (n, b, k);
   
   return success;
}


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