miasm
Reverse engineering framework
bn.h
Go to the documentation of this file.
1 #ifndef __BIGNUM_H__
2 #define __BIGNUM_H__
3 
4 #if _WIN32
5 #define _MIASM_EXPORT __declspec(dllexport)
6 #else
7 #define _MIASM_EXPORT
8 #endif
9 
10 /*
11 
12 Big number library - arithmetic on multiple-precision unsigned integers.
13 
14 This library is an implementation of arithmetic on arbitrarily large integers.
15 
16 The difference between this and other implementations, is that the data structure
17 has optimal memory utilization (i.e. a 1024 bit integer takes up 128 bytes RAM),
18 and all memory is allocated statically: no dynamic allocation for better or worse.
19 
20 Primary goals are correctness, clarity of code and clean, portable implementation.
21 Secondary goal is a memory footprint small enough to make it suitable for use in
22 embedded applications.
23 
24 
25 The current state is correct functionality and adequate performance.
26 There may well be room for performance-optimizations and improvements.
27 
28 Source: https://github.com/kokke/tiny-bignum-c
29 
30 Code slightly modified to support ast generation calculus style from Expr.
31 
32 */
33 
34 #include <stdint.h>
35 #include <assert.h>
36 
37 
38 /* This macro defines the word size in bytes of the array that constitutes the big-number data structure. */
39 #ifndef WORD_SIZE
40  #define WORD_SIZE 4
41 #endif
42 
43 #define BN_BYTE_SIZE 32
44 
45 #define BN_BIT_SIZE ((BN_BYTE_SIZE) * 8)
46 
47 /* Size of big-numbers in bytes */
48 //#define BN_ARRAY_SIZE (128 / WORD_SIZE)
49 #define BN_ARRAY_SIZE (BN_BYTE_SIZE / WORD_SIZE)
50 
51 
52 /* Here comes the compile-time specialization for how large the underlying array size should be. */
53 /* The choices are 1, 2 and 4 bytes in size with uint32, uint64 for WORD_SIZE==4, as temporary. */
54 #ifndef WORD_SIZE
55  #error Must define WORD_SIZE to be 1, 2, 4
56 #elif (WORD_SIZE == 1)
57  /* Data type of array in structure */
58  #define DTYPE uint8_t
59  #define DTYPE_SIGNED int8_t
60  /* bitmask for getting MSB */
61  #define DTYPE_MSB ((DTYPE_TMP)(0x80))
62  /* Data-type larger than DTYPE, for holding intermediate results of calculations */
63  #define DTYPE_TMP uint32_t
64  /* sprintf format string */
65  #define SPRINTF_FORMAT_STR "%.02x"
66  #define SSCANF_FORMAT_STR "%2hhx"
67  /* Max value of integer type */
68  #define MAX_VAL ((DTYPE_TMP)0xFF)
69 #elif (WORD_SIZE == 2)
70  #define DTYPE uint16_t
71  #define DTYPE_SIGNED int16_t
72  #define DTYPE_TMP uint32_t
73  #define DTYPE_MSB ((DTYPE_TMP)(0x8000))
74  #define SPRINTF_FORMAT_STR "%.04x"
75  #define SSCANF_FORMAT_STR "%4hx"
76  #define MAX_VAL ((DTYPE_TMP)0xFFFF)
77 #elif (WORD_SIZE == 4)
78  #define DTYPE uint32_t
79  #define DTYPE_SIGNED int32_t
80  #define DTYPE_TMP uint64_t
81  #define DTYPE_MSB ((DTYPE_TMP)(0x80000000))
82  #define SPRINTF_FORMAT_STR "%.08x"
83  #define SSCANF_FORMAT_STR "%8x"
84  #define MAX_VAL ((DTYPE_TMP)0xFFFFFFFF)
85 #endif
86 #ifndef DTYPE
87  #error DTYPE must be defined to uint8_t, uint16_t uint32_t or whatever
88 #endif
89 
90 
91 /* Custom assert macro - easy to disable */
92 #define require(p, msg) assert(p && #msg)
93 
94 
95 /* Data-holding structure: array of DTYPEs */
96 typedef struct bn
97 {
99 } bn_t;
100 
101 
102 
103 /* Tokens returned by bignum_cmp() for value comparison */
104 enum { SMALLER = -1, EQUAL = 0, LARGER = 1 };
105 
106 /* Initialization functions: */
112 _MIASM_EXPORT bn_t bignum_from_string(char* str, int nbytes);
113 _MIASM_EXPORT void bignum_to_string(bn_t n, char* str, int maxsize);
114 
115 
116 /* Basic arithmetic operations: */
117 _MIASM_EXPORT bn_t bignum_add(bn_t a, bn_t b); /* c = a + b */
118 _MIASM_EXPORT bn_t bignum_sub(bn_t a, bn_t b); /* c = a - b */
119 _MIASM_EXPORT bn_t bignum_mul(bn_t a, bn_t b); /* c = a * b */
120 _MIASM_EXPORT bn_t bignum_udiv(bn_t a, bn_t b); /* c = a / b */
121 _MIASM_EXPORT bn_t bignum_umod(bn_t a, bn_t b); /* c = a % b */
124 //void bignum_udivmod(struct bn* a, struct bn* b, struct bn* c, struct bn* d); /* c = a/b, d = a%b */
125 
126 
127 
128 /* Bitwise operations: */
129 _MIASM_EXPORT bn_t bignum_and(bn_t a, bn_t b); /* c = a & b */
130 _MIASM_EXPORT bn_t bignum_or(bn_t a, bn_t b); /* c = a | b */
131 _MIASM_EXPORT bn_t bignum_xor(bn_t a, bn_t b); /* c = a ^ b */
132 _MIASM_EXPORT bn_t bignum_lshift(bn_t a, int nbits); /* b = a << nbits */
133 _MIASM_EXPORT bn_t bignum_rshift(bn_t a, int nbits); /* b = a >> nbits */
134 _MIASM_EXPORT bn_t bignum_a_rshift(bn_t a, int size, int nbits); /* b = a a>> nbits */
135 _MIASM_EXPORT bn_t bignum_not(bn_t a); /* c = ~a */
136 
137 /* Special operators and comparison */
138 _MIASM_EXPORT int bignum_cmp(bn_t a, bn_t b); /* Compare: returns LARGER, EQUAL or SMALLER */
139 _MIASM_EXPORT int bignum_is_equal(bn_t a, bn_t b); /* Return 1 if a == b else 0 */
140 _MIASM_EXPORT int bignum_is_inf_unsigned(bn_t a, bn_t b); /* Return 1 if a <u b else 0 */
141 _MIASM_EXPORT int bignum_is_inf_equal_unsigned(bn_t a, bn_t b); /* Return 1 if a <=u b else 0 */
142 _MIASM_EXPORT int bignum_is_inf_signed(bn_t a, bn_t b); /* Return 1 if a <s b else 0 */
143 _MIASM_EXPORT int bignum_is_inf_equal_signed(bn_t a, bn_t b); /* Return 1 if a <=s b else 0 */
144 
145 
146 
147 _MIASM_EXPORT int bignum_is_zero(bn_t n); /* For comparison with zero */
148 _MIASM_EXPORT bn_t bignum_inc(bn_t n); /* Increment: add one to n */
149 _MIASM_EXPORT bn_t bignum_dec(bn_t n); /* Decrement: subtract one from n */
150 //bn_t bignum_pow(bn_t a, bn_t b, bn_t c); /* Calculate a^b -- e.g. 2^10 => 1024 */
151 //bn_t bignum_isqrt(bn_t a, bn_t b); /* Integer square root -- e.g. isqrt(5) => 2*/
154 _MIASM_EXPORT bn_t bignum_assign(bn_t src); /* Copy src into dst -- dst := src */
155 _MIASM_EXPORT bn_t bignum_mask(bn_t src, int bits); /* c = src & ((1<<bits) -1) */
156 
157 _MIASM_EXPORT bn_t bignum_rol(bn_t a, int size, int nbits);
158 _MIASM_EXPORT bn_t bignum_ror(bn_t a, int size, int nbits);
159 _MIASM_EXPORT int bignum_getbit(bn_t a, int pos);
160 
161 #endif /* #ifndef __BIGNUM_H__ */
162 
163 
bignum_init
_MIASM_EXPORT bn_t bignum_init(void)
Definition: bn.c:41
bignum_from_uint64
_MIASM_EXPORT bn_t bignum_from_uint64(uint64_t i)
Definition: bn.c:82
bn.h
bignum_rshift
bn_t bignum_rshift(bn_t a, int nbits)
Definition: bn.c:416
BN_ARRAY_SIZE
#define BN_ARRAY_SIZE
Definition: bn.h:49
bignum_cmp_unsigned
int bignum_cmp_unsigned(bn_t a, bn_t b)
Definition: bn.c:602
bignum_dec
_MIASM_EXPORT bn_t bignum_dec(bn_t n)
Definition: bn.c:218
miasm.arch.msp430.regs.res
res
Definition: regs.py:71
bignum_cmp_signed
int bignum_cmp_signed(bn_t a, bn_t b)
Definition: bn.c:583
bignum_getbit
int bignum_getbit(bn_t a, int pos)
Definition: bn.c:820
bignum_add
bn_t bignum_add(bn_t a, bn_t b)
Definition: bn.c:263
bignum_add
_MIASM_EXPORT bn_t bignum_add(bn_t a, bn_t b)
Definition: bn.c:263
bignum_xor
_MIASM_EXPORT bn_t bignum_xor(bn_t a, bn_t b)
Definition: bn.c:546
require
#define require(p, msg)
Definition: bn.h:92
bignum_cntleadzeros
_MIASM_EXPORT int bignum_cntleadzeros(bn_t n, int size)
Definition: bn.c:842
bn
Definition: bn.h:97
bignum_assign
bn_t bignum_assign(bn_t src)
Definition: bn.c:684
bignum_mask
_MIASM_EXPORT bn_t bignum_mask(bn_t src, int bits)
Definition: bn.c:699
bignum_smod
bn_t bignum_smod(bn_t a, bn_t b, int size)
Definition: bn.c:921
bignum_mul
_MIASM_EXPORT bn_t bignum_mul(bn_t a, bn_t b)
Definition: bn.c:309
bignum_to_int
_MIASM_EXPORT int bignum_to_int(bn_t n)
Definition: bn.c:111
bignum_rol
bn_t bignum_rol(bn_t a, int size, int nbits)
Definition: bn.c:794
SPRINTF_FORMAT_STR
#define SPRINTF_FORMAT_STR
Definition: bn.h:82
bignum_udiv
_MIASM_EXPORT bn_t bignum_udiv(bn_t a, bn_t b)
Definition: bn.c:341
_MIASM_EXPORT
#define _MIASM_EXPORT
Definition: bn.h:7
bignum_smod
_MIASM_EXPORT bn_t bignum_smod(bn_t a, bn_t b, int size)
Definition: bn.c:921
bignum_is_equal
int bignum_is_equal(bn_t a, bn_t b)
Definition: bn.c:609
bignum_or
bn_t bignum_or(bn_t a, bn_t b)
Definition: bn.c:531
bignum_a_rshift
bn_t bignum_a_rshift(bn_t a, int size, int nbits)
Definition: bn.c:446
bignum_mul
bn_t bignum_mul(bn_t a, bn_t b)
Definition: bn.c:309
bignum_cnttrailzeros
int bignum_cnttrailzeros(bn_t n, int size)
Definition: bn.c:867
modint.i
i
Definition: modint.py:70
bignum_cntleadzeros
int bignum_cntleadzeros(bn_t n, int size)
Definition: bn.c:842
bignum_a_rshift
_MIASM_EXPORT bn_t bignum_a_rshift(bn_t a, int size, int nbits)
Definition: bn.c:446
bignum_and
_MIASM_EXPORT bn_t bignum_and(bn_t a, bn_t b)
Definition: bn.c:515
bignum_is_zero
int bignum_is_zero(bn_t n)
Definition: bn.c:668
bignum_assign
_MIASM_EXPORT bn_t bignum_assign(bn_t src)
Definition: bn.c:684
basic_op.d
d
Definition: basic_op.py:30
bignum_is_inf_signed
int bignum_is_inf_signed(bn_t a, bn_t b)
Definition: bn.c:645
bn::array
DTYPE array[BN_ARRAY_SIZE]
Definition: bn.h:98
MAX_VAL
#define MAX_VAL
Definition: bn.h:84
miasm.arch.aarch64.sem.ret
def ret(arg1)
Definition: sem.py:1796
bn_t
struct bn bn_t
bignum_inc
bn_t bignum_inc(bn_t n)
Definition: bn.c:240
bignum_is_zero
_MIASM_EXPORT int bignum_is_zero(bn_t n)
Definition: bn.c:668
bignum_udiv
bn_t bignum_udiv(bn_t a, bn_t b)
Definition: bn.c:341
basic_op.c
c
Definition: basic_op.py:15
BN_BIT_SIZE
#define BN_BIT_SIZE
Definition: bn.h:45
bignum_sub
_MIASM_EXPORT bn_t bignum_sub(bn_t a, bn_t b)
Definition: bn.c:283
WORD_SIZE
#define WORD_SIZE
Definition: bn.h:40
bignum_from_uint64
bn_t bignum_from_uint64(uint64_t i)
Definition: bn.c:82
bignum_umod
bn_t bignum_umod(bn_t a, bn_t b)
Definition: bn.c:493
bignum_umod
_MIASM_EXPORT bn_t bignum_umod(bn_t a, bn_t b)
Definition: bn.c:493
bignum_lshift
_MIASM_EXPORT bn_t bignum_lshift(bn_t a, int nbits)
Definition: bn.c:387
miasm.arch.mips32.sem.j
def j(arg1)
Definition: sem.py:156
bignum_dec
bn_t bignum_dec(bn_t n)
Definition: bn.c:218
bignum_rol
_MIASM_EXPORT bn_t bignum_rol(bn_t a, int size, int nbits)
Definition: bn.c:794
bignum_cnttrailzeros
_MIASM_EXPORT int bignum_cnttrailzeros(bn_t n, int size)
Definition: bn.c:867
SMALLER
@ SMALLER
Definition: bn.h:104
bignum_is_inf_equal_signed
_MIASM_EXPORT int bignum_is_inf_equal_signed(bn_t a, bn_t b)
Definition: bn.c:657
bignum_inc
_MIASM_EXPORT bn_t bignum_inc(bn_t n)
Definition: bn.c:240
bignum_sdiv
_MIASM_EXPORT bn_t bignum_sdiv(bn_t a, bn_t b, int size)
Definition: bn.c:885
EQUAL
@ EQUAL
Definition: bn.h:104
modularintervals.mask
tuple mask
Definition: modularintervals.py:32
bignum_cmp
_MIASM_EXPORT int bignum_cmp(bn_t a, bn_t b)
Definition: bn.c:561
DTYPE
#define DTYPE
Definition: bn.h:78
bignum_ror
bn_t bignum_ror(bn_t a, int size, int nbits)
Definition: bn.c:807
bignum_from_string
_MIASM_EXPORT bn_t bignum_from_string(char *str, int nbytes)
Definition: bn.c:168
bignum_from_int
bn_t bignum_from_int(DTYPE_TMP i)
Definition: bn.c:54
bignum_or
_MIASM_EXPORT bn_t bignum_or(bn_t a, bn_t b)
Definition: bn.c:531
bignum_to_uint64
_MIASM_EXPORT uint64_t bignum_to_uint64(bn_t n)
Definition: bn.c:134
bignum_from_string
bn_t bignum_from_string(char *str, int nbytes)
Definition: bn.c:168
bignum_sub
bn_t bignum_sub(bn_t a, bn_t b)
Definition: bn.c:283
DTYPE_SIGNED
#define DTYPE_SIGNED
Definition: bn.h:79
bignum_is_inf_signed
_MIASM_EXPORT int bignum_is_inf_signed(bn_t a, bn_t b)
Definition: bn.c:645
bignum_rshift
_MIASM_EXPORT bn_t bignum_rshift(bn_t a, int nbits)
Definition: bn.c:416
bignum_not
bn_t bignum_not(bn_t a)
Definition: bn.c:479
bignum_is_inf_equal_signed
int bignum_is_inf_equal_signed(bn_t a, bn_t b)
Definition: bn.c:657
bignum_lshift
bn_t bignum_lshift(bn_t a, int nbits)
Definition: bn.c:387
bignum_is_equal
_MIASM_EXPORT int bignum_is_equal(bn_t a, bn_t b)
Definition: bn.c:609
bignum_from_int
_MIASM_EXPORT bn_t bignum_from_int(DTYPE_TMP i)
Definition: bn.c:54
bignum_getbit
_MIASM_EXPORT int bignum_getbit(bn_t a, int pos)
Definition: bn.c:820
bignum_to_int
int bignum_to_int(bn_t n)
Definition: bn.c:111
bignum_to_uint64
uint64_t bignum_to_uint64(bn_t n)
Definition: bn.c:134
bignum_mask
bn_t bignum_mask(bn_t src, int bits)
Definition: bn.c:699
bignum_not
_MIASM_EXPORT bn_t bignum_not(bn_t a)
Definition: bn.c:479
LARGER
@ LARGER
Definition: bn.h:104
bignum_is_inf_equal_unsigned
_MIASM_EXPORT int bignum_is_inf_equal_unsigned(bn_t a, bn_t b)
Definition: bn.c:633
bignum_is_inf_unsigned
int bignum_is_inf_unsigned(bn_t a, bn_t b)
Definition: bn.c:621
bignum_is_inf_equal_unsigned
int bignum_is_inf_equal_unsigned(bn_t a, bn_t b)
Definition: bn.c:633
bignum_init
bn_t bignum_init(void)
Definition: bn.c:41
bignum_to_string
void bignum_to_string(bn_t n, char *str, int nbytes)
Definition: bn.c:196
bignum_xor
bn_t bignum_xor(bn_t a, bn_t b)
Definition: bn.c:546
bignum_sdiv
bn_t bignum_sdiv(bn_t a, bn_t b, int size)
Definition: bn.c:885
bignum_to_string
_MIASM_EXPORT void bignum_to_string(bn_t n, char *str, int maxsize)
Definition: bn.c:196
basic_op.a
a
Definition: basic_op.py:9
SSCANF_FORMAT_STR
#define SSCANF_FORMAT_STR
Definition: bn.h:83
shellcode.size
size
Definition: shellcode.py:32
basic_op.b
b
Definition: basic_op.py:10
bignum_and
bn_t bignum_and(bn_t a, bn_t b)
Definition: bn.c:515
bignum_is_inf_unsigned
_MIASM_EXPORT int bignum_is_inf_unsigned(bn_t a, bn_t b)
Definition: bn.c:621
bignum_cmp
int bignum_cmp(bn_t a, bn_t b)
Definition: bn.c:561
DTYPE_TMP
#define DTYPE_TMP
Definition: bn.h:80
bignum_ror
_MIASM_EXPORT bn_t bignum_ror(bn_t a, int size, int nbits)
Definition: bn.c:807