OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gsLargeInt.h
1
3// Large Integer Library
4#ifndef __GSLARGEINT_H__
5#define __GSLARGEINT_H__
6
7#include "gsCommon.h"
8#include "gsXML.h"
9#include "../md5.h"
10
11
12#if defined(__cplusplus)
13extern "C" {
14#endif
15
16
19
20// for simplicity, set the binary size to a value > gsCrypt.h binary size
21#ifndef GS_LARGEINT_BINARY_SIZE
22#define GS_LARGEINT_BINARY_SIZE (2048) // *BIT* size (divide by 8 for byte size)
23#endif
24
25 // !!!!!!WARNING!!!!!! Encryption is fastest when digit type is the default system type, (ex: gsi_u32 on 32bit processor)
26#define GS_LARGEINT_DIGIT_TYPE gsi_u32
27#define GS_LARGEINT_DIGIT_LONG_TYPE gsi_u64
28
29#define GS_LARGEINT_DIGIT_SIZE_BYTES (sizeof(GS_LARGEINT_DIGIT_TYPE))
30#define GS_LARGEINT_DIGIT_SIZE_BITS (GS_LARGEINT_DIGIT_SIZE_BYTES*8)
31
32// short forms for legibility
33#define l_word GS_LARGEINT_DIGIT_TYPE
34#define l_dword GS_LARGEINT_DIGIT_LONG_TYPE
35
36//#define GS_LARGEINT_BYTE_SIZE 32 // binary size of system data type
37//#define GS_LARGEINT_INT_SIZE (GS_LARGEINT_BINARY_SIZE/GS_LARGEINT_BYTE_SIZE) // size in values
38#define GS_LARGEINT_MAX_DIGITS (GS_LARGEINT_BINARY_SIZE / GS_LARGEINT_DIGIT_SIZE_BITS)
39
40#define GS_LARGEINT_KARATSUBA_CUTOFF 32
41
42typedef struct gsLargeInt_s
43{
44 GS_LARGEINT_DIGIT_TYPE mLength;
45 GS_LARGEINT_DIGIT_TYPE mData[GS_LARGEINT_MAX_DIGITS];
46} gsLargeInt_t;
47
48
51// Commonly used functions
52void gsLargeIntAddToMD5(const gsLargeInt_t * lint, MD5_CTX * md5);
53gsi_bool gsLargeIntSetFromHexString(gsLargeInt_t *lint, const char* hexstring);
54gsi_bool gsLargeIntPrint (FILE* logFile, const gsLargeInt_t *lint);
55gsi_u32 gsLargeIntGetByteLength(const gsLargeInt_t *lint);
56
57 // Modular exponentiation (and helpers)
58 // -- uses Montgomery exponentiation, reduction, multiplication
59gsi_bool gsLargeIntPowerMod(const gsLargeInt_t *base, const gsLargeInt_t *power, const gsLargeInt_t *mod, gsLargeInt_t *dest);
60
61
64gsi_bool gsLargeIntSquareMod(const gsLargeInt_t *lint, const gsLargeInt_t *mod, gsLargeInt_t *dest);
65gsi_bool gsLargeIntAdd (const gsLargeInt_t *src1, const gsLargeInt_t *src2, gsLargeInt_t *dest);
66gsi_bool gsLargeIntSub (const gsLargeInt_t *src1, const gsLargeInt_t *fromsrc2, gsLargeInt_t *dest);
67gsi_bool gsLargeIntMult (const gsLargeInt_t *src1, const gsLargeInt_t *src2, gsLargeInt_t *dest);
68gsi_bool gsLargeIntDiv (const gsLargeInt_t *src1, const gsLargeInt_t *divisor, gsLargeInt_t *dest, gsLargeInt_t *remainder);
69
70 //Karatsuba requires that the sizes be equal and a power of two
71gsi_bool gsLargeIntKMult(const gsLargeInt_t *src1, const gsLargeInt_t *src2, gsLargeInt_t *dest);
72
73 //This is useful when packing a BigEndian message directly into a LittleEndian lint buffer.
74gsi_bool gsLargeIntReverseBytes(gsLargeInt_t *lint);
75gsi_bool gsLargeIntSetValue(gsLargeInt_t *lint, l_word value);
76
77 //These are necessary to preserve byte order when copying from array-of-int to char*
78gsi_bool gsLargeIntSetFromMemoryStream(gsLargeInt_t *lint, const gsi_u8* data, gsi_u32 len);
79gsi_bool gsLargeIntWriteToMemoryStream(const gsLargeInt_t *lint, gsi_u8* data);
80
81
84#if defined(__cplusplus)
85}
86#endif
87
88#endif // __GSLARGEINT_H__
Definition md5.h:66
Definition gsLargeInt.h:43