Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mpi.h Source File

mpi.h

Go to the documentation of this file.
00001 /**
00002  * @file mpi.h
00003  * @brief MPI (Multiple Precision Integer Arithmetic)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneCrypto Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _MPI_H
00030 #define _MPI_H
00031 
00032 //Dependencies
00033 #include <stdio.h>
00034 #include "crypto.h"
00035 
00036 //Size of the sub data type
00037 #define MPI_INT_SIZE sizeof(uint_t)
00038 
00039 //Error code checking
00040 #define MPI_CHECK(f) if((error = f) != NO_ERROR) goto end
00041 
00042 //Miscellaneous macros
00043 #define mpiIsEven(a) !mpiGetBitValue(a, 0)
00044 #define mpiIsOdd(a) mpiGetBitValue(a, 0)
00045 
00046 
00047 /**
00048  * @brief Arbitrary precision integer
00049  **/
00050 
00051 typedef struct
00052 {
00053    int_t sign;
00054    uint_t size;
00055    uint_t *data;
00056 } Mpi;
00057 
00058 
00059 //MPI related functions
00060 void mpiInit(Mpi *r);
00061 void mpiFree(Mpi *r);
00062 
00063 error_t mpiGrow(Mpi *r, uint_t size);
00064 
00065 uint_t mpiGetLength(const Mpi *a);
00066 uint_t mpiGetByteLength(const Mpi *a);
00067 uint_t mpiGetBitLength(const Mpi *a);
00068 
00069 error_t mpiSetBitValue(Mpi *r, uint_t index, uint_t value);
00070 uint_t mpiGetBitValue(const Mpi *a, uint_t index);
00071 
00072 int_t mpiComp(const Mpi *a, const Mpi *b);
00073 int_t mpiCompInt(const Mpi *a, int_t b);
00074 int_t mpiCompAbs(const Mpi *a, const Mpi *b);
00075 
00076 error_t mpiCopy(Mpi *r, const Mpi *a);
00077 error_t mpiSetValue(Mpi *a, int_t b);
00078 
00079 error_t mpiRand(Mpi *r, uint_t length, const PrngAlgo *prngAlgo, void *prngContext);
00080 
00081 error_t mpiReadRaw(Mpi *r, const uint8_t *data, uint_t length);
00082 error_t mpiWriteRaw(const Mpi *a, uint8_t *data, uint_t length);
00083 
00084 error_t mpiAdd(Mpi *r, const Mpi *a, const Mpi *b);
00085 error_t mpiAddInt(Mpi *r, const Mpi *a, int_t b);
00086 
00087 error_t mpiSub(Mpi *r, const Mpi *a, const Mpi *b);
00088 error_t mpiSubInt(Mpi *r, const Mpi *a, int_t b);
00089 
00090 error_t mpiAddAbs(Mpi *r, const Mpi *a, const Mpi *b);
00091 error_t mpiSubAbs(Mpi *r, const Mpi *a, const Mpi *b);
00092 
00093 error_t mpiShiftLeft(Mpi *r, uint_t n);
00094 error_t mpiShiftRight(Mpi *r, uint_t n);
00095 
00096 error_t mpiMul(Mpi *r, const Mpi *a, const Mpi *b);
00097 error_t mpiMulInt(Mpi *r, const Mpi *a, int_t b);
00098 
00099 error_t mpiDiv(Mpi *q, Mpi *r, const Mpi *a, const Mpi *b);
00100 error_t mpiDivInt(Mpi *q, Mpi *r, const Mpi *a, int_t b);
00101 
00102 error_t mpiMod(Mpi *r, const Mpi *a, const Mpi *p);
00103 error_t mpiAddMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
00104 error_t mpiSubMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
00105 error_t mpiMulMod(Mpi *r, const Mpi *a, const Mpi *b, const Mpi *p);
00106 error_t mpiInvMod(Mpi *r, const Mpi *a, const Mpi *p);
00107 error_t mpiExpMod(Mpi *r, const Mpi *a, const Mpi *e, const Mpi *p);
00108 
00109 error_t mpiMontgomeryMul(Mpi *r, const Mpi *a, const Mpi *b, uint_t k, const Mpi *p, Mpi *t);
00110 error_t mpiMontgomeryRed(Mpi *r, const Mpi *a, uint_t k, const Mpi *p, Mpi *t);
00111 
00112 void mpiMulAccCore(uint_t *r, const uint_t *a, int_t m, const uint_t b);
00113 
00114 void mpiDump(FILE *stream, const char_t *prepend, const Mpi *a);
00115 
00116 #endif
00117