3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2014 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
27 #define BIGINT_WORD_SIZE 8
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;
42 #error "INVALID VALUE FOR BIGINT_WORD_SIZE"
45 typedef uint16_t bigint_length_t;
46 typedef uint8_t bigint_info_t;
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)
56 bigint_length_t length_W;
57 bigint_length_t allocated_W;
61 extern void *(*int_realloc)(void *ptr, size_t size);
62 extern void (*int_free)(void *ptr);
64 int bigint_copy(bigint_t *dest, const bigint_t *a);
66 int bigint_free(bigint_t *a);
69 * \brief dest = |a| + |b|
70 * Adds the bigints a and b and stores the result in dest.
73 int bigint_add_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
76 * \brief dest = |a| - |b|
77 * Subtracts b from a and stores the result in dest.
80 int bigint_sub_u(bigint_t *dest, const bigint_t *a, const bigint_t *b);
85 int bigint_shiftleft_1bit(bigint_t *a);
88 * \brief dest = a << s
90 int bigint_shiftleft(bigint_t *dest, const bigint_t *a, bigint_length_t s);
95 int bigint_shiftright_1bit(bigint_t *a);
98 * \brief dest = a ** 2
100 int bigint_square(bigint_t *dest, const bigint_t *a);
103 * \brief dest = |a * b|
104 * unsigned multiply a bigint (a) by a word (b)
106 int bigint_mul_word(bigint_t *dest, const bigint_t *a, const bigint_word_t b);
109 * \brief dest = a * b
111 int bigint_mul_schoolbook(bigint_t *dest, const bigint_t *a, const bigint_t *b);
117 int8_t bigint_cmp_u(const bigint_t *a, const bigint_t *b);
123 int8_t bigint_cmp_scale(const bigint_t *a, const bigint_t *b, bigint_length_t s);
128 int bigint_adjust_length(bigint_t *a);
130 bigint_length_t bigint_get_first_set_bit(const bigint_t *a);
132 int bigint_divide(bigint_t *q, bigint_t *r, const bigint_t *a, const bigint_t *b);
134 int8_t bigint_cmp_s(const bigint_t *a, const bigint_t *b);
136 int bigint_gcdext(bigint_t *gcd, bigint_t *x, bigint_t *y, const bigint_t *a, const bigint_t *b);
140 #endif /* BIGINT2_H_ */