/***************************************************************************
*
Copyright 2013 CertiVox UK Ltd. *
*
This file is part of CertiVox MIRACL Crypto SDK. *
*
The CertiVox MIRACL Crypto SDK provides developers with an *
extensive and efficient set of cryptographic functions. *
For further information about its features and functionalities please *
refer to http://www.certivox.com *
*
* The CertiVox MIRACL Crypto SDK is free software: you can *
redistribute it and/or modify it under the terms of the *
GNU Affero General Public License as published by the *
Free Software Foundation, either version 3 of the License, *
or (at your option) any later version. *
*
* The CertiVox MIRACL Crypto SDK is distributed in the hope *
that it will be useful, but WITHOUT ANY WARRANTY; without even the *
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
See the GNU Affero General Public License for more details. *
*
* You should have received a copy of the GNU Affero General Public *
License along with CertiVox MIRACL Crypto SDK. *
If not, see <http://www.gnu.org/licenses/>. *
*
You can be released from the requirements of the license by purchasing *
a commercial license. Buying such a license is mandatory as soon as you *
develop commercial activities involving the CertiVox MIRACL Crypto SDK *
without disclosing the source code of your own applications, or shipping *
the CertiVox MIRACL Crypto SDK with a closed source product. *
*
***************************************************************************/
#ifndef MIRACL_H
#define MIRACL_H
/*
* main MIRACL header - miracl.h.
*/
#include "mirdef.h"
/* Some modifiable defaults... */
/* Use a smaller buffer if space is limited, don't be so wasteful! */
#ifdef MR_STATIC
#define MR_DEFAULT_BUFFER_SIZE 260
#else
#define MR_DEFAULT_BUFFER_SIZE 1024
#endif
/* see mrgf2m.c */
#ifndef MR_KARATSUBA
#define MR_KARATSUBA 2
#endif
#ifndef MR_DOUBLE_BIG
#ifdef MR_KCM
#ifdef MR_FLASH
#define MR_SPACES 32
#else
#define MR_SPACES 31
#endif
#else
#ifdef MR_FLASH
#define MR_SPACES 28
#else
#define MR_SPACES 27
#endif
#endif
#else
#ifdef MR_KCM
#ifdef MR_FLASH
#define MR_SPACES 44
#else
#define MR_SPACES 43
#endif
#else
#ifdef MR_FLASH
#define MR_SPACES 40
#else
#define MR_SPACES 39
#endif
#endif
#endif
/* To avoid name clashes - undefine this */
/* #define compare mr_compare */
#ifdef MR_AVR
#include <avr/pgmspace.h>
#endif
/* size of bigs and elliptic curve points for memory allocation from stack or heap */
#define MR_ROUNDUP(a,b) ((a)-1)/(b)+1
#define MR_SL sizeof(long)
#ifdef MR_STATIC
#define MR_SIZE (((sizeof(struct bigtype)+(MR_STATIC+2)*sizeof(mr_utype))-1)/MR_SL+1)*MR_SL
#define MR_BIG_RESERVE(n) ((n)*MR_SIZE+MR_SL)
#ifdef MR_AFFINE_ONLY
#define MR_ESIZE (((sizeof(epoint)+MR_BIG_RESERVE(2))-1)/MR_SL+1)*MR_SL
#else
#define MR_ESIZE (((sizeof(epoint)+MR_BIG_RESERVE(3))-1)/MR_SL+1)*MR_SL
#endif
#define MR_ECP_RESERVE(n) ((n)*MR_ESIZE+MR_SL)
#define MR_ESIZE_A (((sizeof(epoint)+MR_BIG_RESERVE(2))-1)/MR_SL+1)*MR_SL
#define MR_ECP_RESERVE_A(n) ((n)*MR_ESIZE_A+MR_SL)
#endif
/* useful macro to convert size of big in words, to size of required structure */
#define mr_size(n) (((sizeof(struct bigtype)+((n)+2)*sizeof(mr_utype))-1)/MR_SL+1)*MR_SL
#define mr_big_reserve(n,m) ((n)*mr_size(m)+MR_SL)
#define mr_esize_a(n) (((sizeof(epoint)+mr_big_reserve(2,(n)))-1)/MR_SL+1)*MR_SL
#define mr_ecp_reserve_a(n,m) ((n)*mr_esize_a(m)+MR_SL)
#ifdef MR_AFFINE_ONLY
#define mr_esize(n) (((sizeof(epoint)+mr_big_reserve(2,(n)))-1)/MR_SL+1)*MR_SL
#else
#define mr_esize(n) (((sizeof(epoint)+mr_big_reserve(3,(n)))-1)/MR_SL+1)*MR_SL
#endif
#define mr_ecp_reserve(n,m) ((n)*mr_esize(m)+MR_SL)
/* if basic library is static, make sure and use static C++ */
#ifdef MR_STATIC
#ifndef BIGS
#define BIGS MR_STATIC
#endif
#ifndef ZZNS
#define ZZNS MR_STATIC
#endif
#ifndef GF2MS
#define GF2MS MR_STATIC
#endif
#endif
#ifdef __ia64__
#if MIRACL==64
#define MR_ITANIUM
#include <ia64intrin.h>
#endif
#endif
#ifdef _M_X64
#ifdef _WIN64
#if MIRACL==64
#define MR_WIN64
#include <intrin.h>
#endif
#endif
#endif
#ifndef MR_NO_FILE_IO
#include <stdio.h>
#endif
/* error returns */
#define MR_ERR_BASE_TOO_BIG 1
#define MR_ERR_DIV_BY_ZERO 2
#define MR_ERR_OVERFLOW 3
#define MR_ERR_NEG_RESULT 4
#define MR_ERR_BAD_FORMAT 5
#define MR_ERR_BAD_BASE 6
#define MR_ERR_BAD_PARAMETERS 7
#define MR_ERR_OUT_OF_MEMORY 8
#define MR_ERR_NEG_ROOT 9
#define MR_ERR_NEG_POWER 10
#define MR_ERR_BAD_ROOT 11
#define MR_ERR_INT_OP 12
#define MR_ERR_FLASH_OVERFLOW 13
#define MR_ERR_TOO_BIG 14
#define MR_ERR_NEG_LOG 15
#define MR_ERR_DOUBLE_FAIL 16
#define MR_ERR_IO_OVERFLOW 17
#define MR_ERR_NO_MIRSYS 18
#define MR_ERR_BAD_MODULUS 19
#define MR_ERR_NO_MODULUS 20
#define MR_ERR_EXP_TOO_BIG 21
#define MR_ERR_NOT_SUPPORTED 22
#define MR_ERR_NOT_DOUBLE_LEN 23
#define MR_ERR_NOT_IRREDUC 24
#define MR_ERR_NO_ROUNDING 25
#define MR_ERR_NOT_BINARY 26
#define MR_ERR_NO_BASIS 27
#define MR_ERR_COMPOSITE_MODULUS 28
#define MR_ERR_DEV_RANDOM 29
/* some useful definitions */
#define forever for(;;)
#define mr_abs(x) ((x)<0? (-(x)) : (x))
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define OFF 0
#define ON 1
#define PLUS 1
#define MINUS (-1)
#define M1 (MIRACL-1)
#define M2 (MIRACL-2)
#define M3 (MIRACL-3)
#define M4 (MIRACL-4)
#define TOPBIT ((mr_small)1<<M1)
#define SECBIT ((mr_small)1<<M2)
#define THDBIT ((mr_small)1<<M3)
#define M8 (MIRACL-8)
#define MR_MAXDEPTH 24
/* max routine stack depth */
/* big and flash variables consist of an encoded length, *
* and an array of mr_smalls containing the digits */
#ifdef MR_COUNT_OPS
extern int fpm2,fpi2,fpc,fpa,fpx;
#endif
typedef int BOOL;
#define MR_BYTE unsigned char
#ifdef MR_BITSINCHAR
#if MR_BITSINCHAR == 8
#define MR_TOBYTE(x) ((MR_BYTE)(x))
#else
#define MR_TOBYTE(x) ((MR_BYTE)((x)&0xFF))
#endif
#else
#define MR_TOBYTE(x) ((MR_BYTE)(x))
#endif
#ifdef MR_FP
typedef mr_utype mr_small;
#ifdef mr_dltype
typedef mr_dltype mr_large;
#endif
#define MR_DIV(a,b) (modf((a)/(b),&dres),dres)
#ifdef MR_FP_ROUNDING
/* slightly dicey - for example the optimizer might remove the MAGIC ! */
#define MR_LROUND(a) ( ( (a) + MR_MAGIC ) - MR_MAGIC )
#else
#define MR_LROUND(a) (modfl((a),&ldres),ldres)
#endif
#define MR_REMAIN(a,b) ((a)-(b)*MR_DIV((a),(b)))
#else
typedef unsigned mr_utype mr_small;
#ifdef mr_dltype
typedef unsigned mr_dltype mr_large;
#endif
#ifdef mr_qltype
typedef unsigned mr_qltype mr_vlarge;
#endif
#define MR_DIV(a,b) ((a)/(b))
#define MR_REMAIN(a,b) ((a)%(b))
#define MR_L