1 /*	$NetBSD: bn_mp_mod_2d.c,v 1.1.1.1 2011/04/13 18:14:54 elric Exp $	*/
2 
3 #include <tommath.h>
4 #ifdef BN_MP_MOD_2D_C
5 /* LibTomMath, multiple-precision integer library -- Tom St Denis
6  *
7  * LibTomMath is a library that provides multiple-precision
8  * integer arithmetic as well as number theoretic functionality.
9  *
10  * The library was designed directly after the MPI library by
11  * Michael Fromberger but has been written from scratch with
12  * additional optimizations in place.
13  *
14  * The library is free for all purposes without any express
15  * guarantee it works.
16  *
17  * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18  */
19 
20 /* calc a value mod 2**b */
21 int
22 mp_mod_2d (mp_int * a, int b, mp_int * c)
23 {
24   int     x, res;
25 
26   /* if b is <= 0 then zero the int */
27   if (b <= 0) {
28     mp_zero (c);
29     return MP_OKAY;
30   }
31 
32   /* if the modulus is larger than the value than return */
33   if (b >= (int) (a->used * DIGIT_BIT)) {
34     res = mp_copy (a, c);
35     return res;
36   }
37 
38   /* copy */
39   if ((res = mp_copy (a, c)) != MP_OKAY) {
40     return res;
41   }
42 
43   /* zero digits above the last digit of the modulus */
44   for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) {
45     c->dp[x] = 0;
46   }
47   /* clear the digit that is not completely outside/inside the modulus */
48   c->dp[b / DIGIT_BIT] &=
49     (mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
50   mp_clamp (c);
51   return MP_OKAY;
52 }
53 #endif
54 
55 /* Source: /cvs/libtom/libtommath/bn_mp_mod_2d.c,v */
56 /* Revision: 1.4 */
57 /* Date: 2006/12/28 01:25:13 */
58