File: array.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 (312 lines) | stat: -rw-r--r-- 8,684 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
   array.c:  simple operations on arrays mod n
   
   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 "zn_poly_internal.h"


int zn_array_cmp(const ulong* op1, const ulong* op2, size_t len)
{
   for (; len > 0; len--)
      if (*op1++ != *op2++)
         return 1;

   return 0;
}


void zn_array_copy(ulong* res, const ulong* op, size_t len)
{
   for (; len > 0; len--)
      *res++ = *op++;
}


void zn_array_neg(ulong* res, const ulong* op, size_t len, const zn_mod_t mod)
{
   for (; len > 0; len--)
      *res++ = zn_mod_neg(*op++, mod);
}


/*
   Same as zn_array_scalar_mul, but:
      * always uses REDC reduction (requires modulus is odd);
      * requires that residues fit into half a word.
*/
#define _zn_array_scalar_mul_redc_v1 \
    ZNP__zn_array_scalar_mul_redc_v1
void _zn_array_scalar_mul_redc_v1(ulong* res, const ulong* op, size_t len,
                                  ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(mod->bits <= ULONG_BITS/2);
   ZNP_ASSERT(mod->n & 1);
   ZNP_ASSERT(x < mod->n);

   for (; len; len--, op++, res++)
      *res = zn_mod_reduce_redc((*op) * x, mod);
}


/*
   Same as zn_array_scalar_mul, but:
      * always uses REDC reduction (requires modulus is odd);
      * requires that modulus is slim.
*/
#define _zn_array_scalar_mul_redc_v2 \
    ZNP__zn_array_scalar_mul_redc_v2
void _zn_array_scalar_mul_redc_v2(ulong* res, const ulong* op, size_t len,
                                  ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(zn_mod_is_slim(mod));
   ZNP_ASSERT(mod->n & 1);
   ZNP_ASSERT(x < mod->n);

   for (; len; len--, op++, res++)
   {
      ulong hi, lo;
      ZNP_MUL_WIDE(hi, lo, *op, x);
      *res = zn_mod_reduce_wide_redc_slim(hi, lo, mod);
   }
}


/*
   Same as zn_array_scalar_mul, but:
      * always uses REDC reduction (requires modulus is odd).
*/
#define _zn_array_scalar_mul_redc_v3 \
    ZNP__zn_array_scalar_mul_redc_v3
void _zn_array_scalar_mul_redc_v3(ulong* res, const ulong* op, size_t len,
                                  ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(mod->n & 1);
   ZNP_ASSERT(x < mod->n);

   for (; len; len--, op++, res++)
   {
      ulong hi, lo;
      ZNP_MUL_WIDE(hi, lo, *op, x);
      *res = zn_mod_reduce_wide_redc(hi, lo, mod);
   }
}


/*
   Same as zn_array_scalar_mul, but always uses REDC reduction (requires that
   modulus is odd).
   
   Dispatches to one of the three versions above, depending on modulus size.
*/
#define _zn_array_scalar_mul_redc \
    ZNP__zn_array_scalar_mul_redc
void _zn_array_scalar_mul_redc(ulong* res, const ulong* op, size_t len,
                               ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(mod->n & 1);
   ZNP_ASSERT(x < mod->n);
   
   if (mod->bits <= ULONG_BITS/2)
      _zn_array_scalar_mul_redc_v1(res, op, len, x, mod);
   else if (zn_mod_is_slim(mod))
      _zn_array_scalar_mul_redc_v2(res, op, len, x, mod);
   else
      _zn_array_scalar_mul_redc_v3(res, op, len, x, mod);
}


/*
   Same as zn_array_scalar_mul, but:
      * always uses plain reduction;
      * requires that residues fit into half a word.
*/
#define _zn_array_scalar_mul_plain_v1 \
    ZNP__zn_array_scalar_mul_plain_v1
void _zn_array_scalar_mul_plain_v1(ulong* res, const ulong* op, size_t len,
                                   ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(mod->bits <= ULONG_BITS/2);
   ZNP_ASSERT(x < mod->n);

   for (; len; len--, op++, res++)
      *res = zn_mod_reduce((*op) * x, mod);
}


/*
   Same as zn_array_scalar_mul, but:
      * always uses plain reduction.
*/
#define _zn_array_scalar_mul_plain_v2 \
    ZNP__zn_array_scalar_mul_plain_v2
void _zn_array_scalar_mul_plain_v2(ulong* res, const ulong* op, size_t len,
                                   ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(x < mod->n);

   for (; len; len--, op++, res++)
   {
      ulong hi, lo;
      ZNP_MUL_WIDE(hi, lo, *op, x);
      *res = zn_mod_reduce_wide(hi, lo, mod);
   }
}


/*
   Same as zn_array_scalar_mul, but always uses plain reduction.
   
   Dispatches to one of the versions above, depending on modulus size.
*/
#define _zn_array_scalar_mul_plain \
    ZNP__zn_array_scalar_mul_plain
void _zn_array_scalar_mul_plain(ulong* res, const ulong* op, size_t len,
                                ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(x < mod->n);
   
   if (mod->bits <= ULONG_BITS/2)
      _zn_array_scalar_mul_plain_v1(res, op, len, x, mod);
   else
      _zn_array_scalar_mul_plain_v2(res, op, len, x, mod);
}


void _zn_array_scalar_mul(ulong* res, const ulong* op, size_t len,
                          ulong x, int redc, const zn_mod_t mod)
{
   if (redc)
      _zn_array_scalar_mul_redc(res, op, len, x, mod);
   else
      _zn_array_scalar_mul_plain(res, op, len, x, mod);
}


void zn_array_scalar_mul(ulong* res, const ulong* op, size_t len,
                         ulong x, const zn_mod_t mod)
{
   ZNP_ASSERT(x < mod->n);
   
   // Do plain reduction if the vector is really short, or if the modulus
   // is even (in which case REDC reduction is not available).
   if (len < 5  ||  !(mod->n & 1))
   {
      _zn_array_scalar_mul_plain(res, op, len, x, mod);
   }
   else
   {
      // modulus is odd, and vector is not too short, so we can go faster
      // by adjusting the multiplier and using REDC reduction
      _zn_array_scalar_mul_redc(res, op, len,
                                zn_mod_mul_redc(x, mod->B2, mod), mod);
   }
}



void zn_array_sub(ulong* res, const ulong* op1, const ulong* op2, size_t len,
                  const zn_mod_t mod)
{
   if (zn_mod_is_slim(mod))
      for (; len; len--)
         *res++ = zn_mod_sub_slim(*op1++, *op2++, mod);
   else
      for (; len; len--)
         *res++ = zn_mod_sub(*op1++, *op2++, mod);
}


/*
   Computes

      res = sign1*op1 + sign2*op2,

   where sign1 = -1 if neg1 is set, otherwise +1; ditto for sign2.
   
   op1 and op2 are arrays of length _len_.
   
   res is a staggered array, entries separated by _skip_.
   
   Return value is res + skip*len, i.e. points beyond the written array.
*/
ulong* zn_skip_array_signed_add(ulong* res, ptrdiff_t skip, size_t len,
                                const ulong* op1, int neg1,
                                const ulong* op2, int neg2,
                                const zn_mod_t mod)
{
   if (zn_mod_is_slim(mod))
   {
      // slim version
      if (neg1)
      {
         if (neg2)
            // res = -(op1 + op2)
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_neg(zn_mod_add_slim(*op1, *op2, mod), mod);
         else
            // res = op2 - op1
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_sub_slim(*op2, *op1, mod);
      }
      else
      {
         if (neg2)
            // res = op1 - op2
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_sub_slim(*op1, *op2, mod);
         else
            // res = op1 + op2
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_add_slim(*op1, *op2, mod);
      }
   }
   else
   {
      // non-slim version
      if (neg1)
      {
         if (neg2)
            // res = -(op1 + op2)
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_neg(zn_mod_add(*op1, *op2, mod), mod);
         else
            // res = op2 - op1
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_sub(*op2, *op1, mod);
      }
      else
      {
         if (neg2)
            // res = op1 - op2
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_sub(*op1, *op2, mod);
         else
            // res = op1 + op2
            for (; len > 0; len--, res += skip, op1++, op2++)
               *res = zn_mod_add(*op1, *op2, mod);
      }
   }
   
   return res;
}


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