Rizin
unix-like reverse engineering framework and cli tools
rz_big.h
Go to the documentation of this file.
1 #ifndef RZ_BIG_H
2 #define RZ_BIG_H
3 
4 #include "../rz_types.h"
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 #if HAVE_LIB_GMP
11 /* Use GMP's data struct */
12 #define RzNumBig mpz_t
13 #elif HAVE_LIB_SSL
14 #define RzNumBig BIGNUM
15 #else
16 /* Use default impl */
17 #define RZ_BIG_WORD_SIZE 4
18 /* Let's support 4096-bit big number */
19 #define RZ_BIG_ARRAY_SIZE (512 / RZ_BIG_WORD_SIZE)
20 /* RZ_BIG_WORD_SIZE == 4, 32 bits long */
21 #define RZ_BIG_DTYPE ut32
22 #define RZ_BIG_DTYPE_TMP ut64
23 #define RZ_BIG_SPRINTF_FORMAT_STR "%.08x"
24 #define RZ_BIG_FORMAT_STR_LEN 9
25 #define RZ_BIG_SSCANF_FORMAT_STR "%8x"
26 #define RZ_BIG_MAX_VAL (RZ_BIG_DTYPE_TMP) UT32_MAX
27 
28 typedef struct rz_num_big_t {
30  int sign;
32 #endif
33 
38 
39 /* Assignment operations */
42 RZ_API void rz_big_from_hexstr(RzNumBig *b, const char *str);
45 
46 /* Basic arithmetic operations */
47 RZ_API void rz_big_add(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a + b */
48 RZ_API void rz_big_sub(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a - b */
49 RZ_API void rz_big_mul(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a * b */
50 RZ_API void rz_big_div(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a / b */
51 RZ_API void rz_big_mod(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a % b */
52 RZ_API void rz_big_divmod(RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b); /* c = a/b, d = a%b */
53 
54 /* Bitwise operations(for >= 0) */
55 RZ_API void rz_big_and(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a & b */
56 RZ_API void rz_big_or(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a | b */
57 RZ_API void rz_big_xor(RzNumBig *c, RzNumBig *a, RzNumBig *b); /* c = a ^ b */
58 RZ_API void rz_big_lshift(RzNumBig *c, RzNumBig *a, size_t nbits); /* c = a << nbits */
59 RZ_API void rz_big_rshift(RzNumBig *c, RzNumBig *a, size_t nbits); /* c = a >> nbits */
60 
61 /* Special operators and comparison */
62 RZ_API int rz_big_cmp(RzNumBig *a, RzNumBig *b); /* Return 1 if a>b, -1 if a<b, else 0 */
63 RZ_API int rz_big_is_zero(RzNumBig *a); /* For comparison with zero */
64 RZ_API void rz_big_inc(RzNumBig *a); /* Increment: add one to n */
65 RZ_API void rz_big_dec(RzNumBig *a); /* Decrement: subtract one from n */
66 RZ_API void rz_big_powm(RzNumBig *c, RzNumBig *a, RzNumBig *b, RzNumBig *m); /* Calculate a^b -- e.g. 2^10 => 1024 */
67 RZ_API void rz_big_isqrt(RzNumBig *c, RzNumBig *a); /* Integer square root -- e.g. isqrt(5) => 2*/
68 
69 #ifdef __cplusplus
70 }
71 #endif
72 
73 #endif // RZ_BIG_H
lzma_index * src
Definition: index.h:567
#define RZ_API
const char * v
Definition: dsignal.c:12
char * dst
Definition: lz4.h:724
RZ_API char * rz_big_to_hexstr(RzNumBig *b)
Definition: big-gmp.c:47
RZ_API void rz_big_dec(RzNumBig *a)
Definition: big-gmp.c:128
RZ_API int rz_big_cmp(RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:107
RZ_API void rz_big_assign(RzNumBig *dst, RzNumBig *src)
Definition: big-gmp.c:59
RZ_API void rz_big_and(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:87
RZ_API void rz_big_xor(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:95
RZ_API void rz_big_fini(RzNumBig *b)
Definition: big-gmp.c:24
RZ_API void rz_big_add(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:63
RZ_API void rz_big_isqrt(RzNumBig *c, RzNumBig *a)
Definition: big-gmp.c:138
RZ_API void rz_big_from_hexstr(RzNumBig *b, const char *str)
Definition: big-gmp.c:36
#define RZ_BIG_DTYPE
Definition: rz_big.h:21
RZ_API void rz_big_divmod(RzNumBig *c, RzNumBig *d, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:83
RZ_API void rz_big_free(RzNumBig *b)
Definition: big-gmp.c:15
#define RZ_BIG_ARRAY_SIZE
Definition: rz_big.h:19
struct rz_num_big_t RzNumBig
RZ_API void rz_big_rshift(RzNumBig *c, RzNumBig *a, size_t nbits)
Definition: big-gmp.c:103
RZ_API void rz_big_lshift(RzNumBig *c, RzNumBig *a, size_t nbits)
Definition: big-gmp.c:99
RZ_API void rz_big_or(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:91
RZ_API st64 rz_big_to_int(RzNumBig *b)
Definition: big-gmp.c:32
RZ_API RzNumBig * rz_big_new(void)
Definition: big-gmp.c:7
RZ_API void rz_big_powm(RzNumBig *c, RzNumBig *a, RzNumBig *b, RzNumBig *m)
Definition: big-gmp.c:134
RZ_API void rz_big_div(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:75
RZ_API void rz_big_init(RzNumBig *b)
Definition: big-gmp.c:20
RZ_API void rz_big_sub(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:67
RZ_API void rz_big_mod(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:79
RZ_API void rz_big_inc(RzNumBig *a)
Definition: big-gmp.c:122
RZ_API void rz_big_from_int(RzNumBig *b, st64 v)
Definition: big-gmp.c:28
RZ_API void rz_big_mul(RzNumBig *c, RzNumBig *a, RzNumBig *b)
Definition: big-gmp.c:71
RZ_API int rz_big_is_zero(RzNumBig *a)
Definition: big-gmp.c:118
#define st64
Definition: rz_types_base.h:10
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
int sign
Definition: rz_big.h:30
RZ_BIG_DTYPE array[RZ_BIG_ARRAY_SIZE]
Definition: rz_big.h:29