]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bigint/bigint.h
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / bigint / bigint.h
1 /* bigint.h */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file                bigint.h
21  * \author              Daniel Otte
22  * \date                2010-02-22
23  * 
24  * \license         GPLv3 or later
25  * 
26  */
27
28 #ifndef BIGINT_H_
29 #define BIGINT_H_
30
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <limits.h>
34
35 #define BIGINT_WORD_SIZE 8
36
37 #if BIGINT_WORD_SIZE == 8
38 typedef uint8_t  bigint_word_t;
39 typedef uint16_t bigint_wordplus_t;
40 typedef int16_t  bigint_wordplus_signed_t;
41 #elif BIGINT_WORD_SIZE == 16
42 typedef uint16_t  bigint_word_t;
43 typedef uint32_t bigint_wordplus_t;
44 typedef int32_t  bigint_wordplus_signed_t;
45 #elif BIGINT_WORD_SIZE == 32
46 typedef uint32_t  bigint_word_t;
47 typedef uint64_t bigint_wordplus_t;
48 typedef int64_t  bigint_wordplus_signed_t;
49 #else
50 #error "INVALID VALUE FOR BIGINT_WORD_SIZE"
51 #endif
52
53
54 #define BIGINT_FBS_MASK (BIGINT_WORD_SIZE - 1) /* the last five bits indicate which is the first bit set */
55 #define BIGINT_NEG_MASK 0x80 /* this bit indicates a negative value */
56
57 typedef size_t bigint_length_t;
58 typedef uint_fast8_t bigint_info_t;
59
60 typedef struct{
61         bigint_length_t length_W;
62         bigint_info_t   info;
63         bigint_word_t   *wordv; /* word vector, pointing to the LSB */
64 }bigint_t;
65
66
67
68 /******************************************************************************/
69
70 void   bigint_adjust(bigint_t *a);
71 int32_t bigint_get_first_set_bit(const bigint_t *a);
72 int32_t bigint_get_last_set_bit(const bigint_t *a);
73 bigint_length_t bigint_length_b(const bigint_t *a);
74 bigint_length_t bigint_length_B(const bigint_t *a);
75 void   bigint_copy(bigint_t *dest, const bigint_t *src);
76 void   bigint_add_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
77 void   bigint_add_scale_u(bigint_t *dest, const bigint_t *a, bigint_length_t scale);
78 void   bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
79 int8_t bigint_cmp_u(const bigint_t * a, const bigint_t * b);
80 void   bigint_add_s(bigint_t *dest, const bigint_t *a, const bigint_t *b);
81 void   bigint_sub_s(bigint_t *dest, const bigint_t *a, const bigint_t *b);
82 int8_t bigint_cmp_s(const bigint_t *a, const bigint_t *b);
83 void   bigint_shiftleft_bits(bigint_t *a, uint8_t shift);
84 void   bigint_shiftleft(bigint_t *a, bigint_length_t shift);
85 void   bigint_shiftright_1bit(bigint_t *a);
86 void   bigint_shiftright_1word(bigint_t *a);
87 void   bigint_shiftright(bigint_t *a, bigint_length_t shift);
88 void   bigint_xor(bigint_t *dest, const bigint_t *a);
89 void   bigint_set_zero(bigint_t *a);
90 void   bigint_mul_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
91 void   bigint_mul_s(bigint_t *dest, const bigint_t *a, const bigint_t *b);
92 void   bigint_square(bigint_t *dest, const bigint_t *a);
93 void   bigint_sub_u_bitscale(bigint_t *a, const bigint_t *b, bigint_length_t bitscale);
94 void   bigint_reduce(bigint_t *a, const bigint_t *r);
95 void   bigint_mul_word_u(bigint_t *a, bigint_word_t b);
96 void   bigint_gcdext(bigint_t *gcd, bigint_t *a, bigint_t *b, const bigint_t *x, const bigint_t *y);
97 void   bigint_inverse(bigint_t *dest, const bigint_t *a, const bigint_t *m);
98 void   bigint_changeendianess(bigint_t *a);
99 void   bigint_clip(bigint_t *dest, bigint_length_t s);
100 void   bigint_mont_mul(bigint_t *dest, const bigint_t *a, const bigint_t *b, const bigint_t *m, const bigint_t *m_);
101 void   bigint_mont_red(bigint_t *dest, const bigint_t *a, const bigint_t *m, const bigint_t *m_);
102 void   bigint_mont_gen_m_(bigint_t* dest, const bigint_t* m);
103 void   bigint_mont_trans(bigint_t *dest, const bigint_t *a, const bigint_t *m);
104
105 void   bigint_expmod_u(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r);
106 void   bigint_expmod_u_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r);
107 void   bigint_expmod_u_mont_sam(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r);
108 void   bigint_expmod_u_mont_accel(bigint_t *dest, const bigint_t *a, const bigint_t *exp, const bigint_t *r, const bigint_t *m_);
109
110
111 #endif /*BIGINT_H_*/