#ifndef BIGINT_HEADER
#define BIGINT_HEADER


//#include "crypto.h"
#include "bigint_impl.h"

bigint *alloc(BI_CTX *ctx, int size);
bigint *trim(bigint *bi);
void bi_initialize(BI_CTX *ctx, char *mem_pool);
void bi_terminate(BI_CTX *ctx);
void bi_permanent(bigint *bi);
void bi_depermanent(bigint *bi);
void bi_clear_cache(BI_CTX *ctx);
void bi_free(BI_CTX *ctx, bigint *bi);
bigint *bi_copy(bigint *bi);
bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
void bi_export(BI_CTX *ctx, bigint *bi, u8 *data, int size);
bigint *bi_import(BI_CTX *ctx, const u8 *data, int len);
bigint *int_to_bi(BI_CTX *ctx, comp i);

/* the functions that actually do something interesting */
bigint *bi_add(BI_CTX *ctx, bigint *bia, bigint *bib);
bigint *bi_subtract(BI_CTX *ctx, bigint *bia,
                    bigint *bib, int *is_negative);
bigint *bi_divide(BI_CTX *ctx, bigint *bia, bigint *bim, int is_mod);
bigint *bi_multiply(BI_CTX *ctx, bigint *bia, bigint *bib);
bigint *bi_mod_power(BI_CTX *ctx, bigint *bi, bigint *biexp);
bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi, bigint *bim, bigint *biexp);
int bi_compare(bigint *bia, bigint *bib);
void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
void bi_free_mod(BI_CTX *ctx, int mod_offset);
bigint *comp_left_shift(bigint *biR, int num_shifts);
int exp_bit_is_one(bigint *biexp, int offset);
int find_max_exp_index(bigint *biexp);
#ifdef CONFIG_SSL_FULL_MODE
void bi_print(const char *label, bigint *bi);
bigint *bi_str_import(BI_CTX *ctx, const char *data);
#endif

//
void bi_wirte_to_byte(bigint *bi, unsigned char *out);
bigint *bi_read_from_byte(BI_CTX *ctx, const unsigned char *buf, int len);
bigint *bi_mod_lshift(BI_CTX *ctx, bigint *x, int shift);
void bi_lshift(BI_CTX *ctx, bigint *x, int shift);
bigint *bi_mod_add(BI_CTX *ctx, bigint *a, bigint *b);
bigint *bi_mod_sub(BI_CTX *ctx, bigint *a, bigint *b);
bigint *bi_mod_sqr(BI_CTX *ctx, bigint *x);
bigint *bi_mod_mul(BI_CTX *ctx, bigint *a, bigint *b);
bigint *bi_mod_inverse(BI_CTX *ctx, bigint *x);
//
//void printf_bigint(bigint *x);

bigint *bi_rshift(bigint *x, int shift);
int bi_is_oneORzero(bigint *x);

/**
 * @def bi_mod
 * Find the residue of B. bi_set_mod() must be called before hand.
 */
#define bi_mod(A, B)      bi_divide(A, B, ctx->bi_mod[ctx->mod_offset], 1)

/**
 * bi_residue() is technically the same as bi_mod(), but it uses the
 * appropriate reduction technique (which is bi_mod() when doing classical
 * reduction).
 */
#if defined(CONFIG_BIGINT_MONTGOMERY)
#define bi_residue(A, B)         bi_mont(A, B)
bigint *bi_mont(BI_CTX *ctx, bigint *bixy);
#elif defined(CONFIG_BIGINT_BARRETT)
#define bi_residue(A, B)         bi_barrett(A, B)
bigint *bi_barrett(BI_CTX *ctx, bigint *bi);
#else /* if defined(CONFIG_BIGINT_CLASSICAL) */
#define bi_residue(A, B)         bi_mod(A, B)
#endif

#ifdef CONFIG_BIGINT_SQUARE
bigint *bi_square(BI_CTX *ctx, bigint *bi);
#else
#define bi_square(A, B)     bi_multiply(A, bi_copy(B), B)
#endif

#ifdef CONFIG_BIGINT_CRT
bigint *bi_crt(BI_CTX *ctx, bigint *bi,
               bigint *dP, bigint *dQ,
               bigint *p, bigint *q,
               bigint *qInv);
#endif

#endif

