]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bigint2/bigint2.h
4bd942193686551b3024ec499df00757fe7c9819
[avr-crypto-lib.git] / bigint2 / bigint2.h
1 /* bigint2.h */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2014 Daniel Otte (daniel.otte@rub.de)
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 #ifndef BIGINT2_H_
21 #define BIGINT2_H_
22
23 #include <stdint.h>
24 #include <limits.h>
25 #include <stdlib.h>
26
27 #define BIGINT_WORD_SIZE 8
28
29 #if BIGINT_WORD_SIZE == 8
30 typedef uint8_t  bigint_word_t;
31 typedef uint16_t bigint_wordplus_t;
32 typedef int16_t  bigint_wordplus_signed_t;
33 #elif BIGINT_WORD_SIZE == 16
34 typedef uint16_t  bigint_word_t;
35 typedef uint32_t bigint_wordplus_t;
36 typedef int32_t  bigint_wordplus_signed_t;
37 #elif BIGINT_WORD_SIZE == 32
38 typedef uint32_t  bigint_word_t;
39 typedef uint64_t bigint_wordplus_t;
40 typedef int64_t  bigint_wordplus_signed_t;
41 #else
42 #error "INVALID VALUE FOR BIGINT_WORD_SIZE"
43 #endif
44
45 typedef uint16_t bigint_length_t;
46 typedef uint8_t bigint_info_t;
47
48 #define BIGINT_SIGN_MASK 0x80
49 #define BIGINT_GET_SIGN(x) ((x)->info & BIGINT_SIGN_MASK)
50 #define BIGINT_SET_POS(x) ((x)->info &= ~BIGINT_SIGN_MASK)
51 #define BIGINT_SET_NEG(x) ((x)->info |= BIGINT_SIGN_MASK)
52
53
54 typedef struct {
55     bigint_word_t *wordv;
56     bigint_length_t length_W;
57     bigint_length_t allocated_W;
58     bigint_info_t info;
59 } bigint_t;
60
61 extern void *(*int_realloc)(void *ptr, size_t size);
62 extern void (*int_free)(void *ptr);
63
64 int bigint_copy(bigint_t *dest, const bigint_t *a);
65
66 int bigint_free(bigint_t *a);
67
68 /**
69  * \brief dest = |a| + |b|
70  * Adds the bigints a and b and stores the result in dest.
71  * Signs are ignored.
72  */
73 int bigint_add_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
74
75 /**
76  * \brief dest = |a| - |b|
77  * Subtracts b from a and stores the result in dest.
78  * Signs are ignored
79  */
80 int bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
81
82 /**
83  * \brief a <<= 1
84  */
85 int bigint_shiftleft_1bit(bigint_t *a);
86
87 /**
88  * \brief dest = a << s
89  */
90 int bigint_shiftleft(bigint_t *dest, const bigint_t *a, bigint_length_t s);
91
92 /**
93  * \brief a >>= 1
94  */
95 int bigint_shiftright_1bit(bigint_t *a);
96
97 /**
98  * \brief dest = a ** 2
99  */
100 int bigint_square(bigint_t *dest, const bigint_t *a);
101
102 /**
103  * \brief dest = |a * b|
104  * unsigned multiply a bigint (a) by a word (b)
105  */
106 int bigint_mul_word(bigint_t *dest, const bigint_t *a, const bigint_word_t b);
107
108 /**
109  * \brief dest = a * b
110  */
111 int bigint_mul_schoolbook(bigint_t *dest, const bigint_t *a, const bigint_t *b);
112
113 /*
114  * UNSAFE!!!
115  * a <=> b
116  */
117 int8_t bigint_cmp_u(const bigint_t *a, const bigint_t *b);
118
119 /*
120  * UNSAFE!!!
121  * a <=> b * (B ** s)
122  */
123 int8_t bigint_cmp_scale(const bigint_t *a, const bigint_t *b, bigint_length_t s);
124
125 /*
126  * UNSAFE!!!
127  */
128 int bigint_adjust_length(bigint_t *a);
129
130 bigint_length_t bigint_get_first_set_bit(const bigint_t *a);
131
132 int bigint_divide(bigint_t *q, bigint_t *r, const bigint_t *a, const bigint_t *b);
133
134 int8_t bigint_cmp_s(const bigint_t *a, const bigint_t *b);
135
136 int bigint_gcdext(bigint_t *gcd, bigint_t *x, bigint_t *y, const bigint_t *a, const bigint_t *b);
137
138
139
140 #endif /* BIGINT2_H_ */