File indexing completed on 2025-01-05 04:37:18

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef MSEBIGINT_H
0007 #define MSEBIGINT_H
0008 
0009 #include <cstdio>
0010 #include <gmp.h>
0011 #include <qstring.h>
0012 #include <util/constants.h>
0013 
0014 using bt::Uint16;
0015 using bt::Uint32;
0016 using bt::Uint64;
0017 using bt::Uint8;
0018 
0019 namespace mse
0020 {
0021 /**
0022  * @author Joris Guisson <joris.guisson@gmail.com>
0023  *
0024  * Class which can hold an arbitrary large integer. This will be a very important part of our
0025  * MSE implementation.
0026  */
0027 class BigInt
0028 {
0029 public:
0030     /**
0031      * Create a big integer, with num_bits bits.
0032      * All bits will be set to 0.
0033      * @param num_bits The number of bits
0034      */
0035     BigInt(Uint32 num_bits = 0);
0036 
0037     /**
0038      * Create a big integer of a string. The string must be
0039      * a hexadecimal representation of an integer. For example :
0040      * 12AFFE123488BBBE123
0041      *
0042      * Letters can be upper or lower case. Invalid chars will create an invalid number.
0043      * @param value The hexadecimal representation of the number
0044      */
0045     BigInt(const QString &value);
0046 
0047     /**
0048      * Copy constructor.
0049      * @param bi BigInt to copy
0050      */
0051     BigInt(const BigInt &bi);
0052     ~BigInt();
0053 
0054     /**
0055      * Assignment operator.
0056      * @param bi The BigInt to copy
0057      * @return *this
0058      */
0059     BigInt &operator=(const BigInt &bi);
0060 
0061     /**
0062      * Calculates
0063      * (x ^ e) mod d
0064      * ^ is power
0065      */
0066     static BigInt powerMod(const BigInt &x, const BigInt &e, const BigInt &d);
0067 
0068     /// Make a random BigInt
0069     static BigInt random();
0070 
0071     /// Export the bigint ot a buffer
0072     Uint32 toBuffer(Uint8 *buf, Uint32 max_size) const;
0073 
0074     /// Make a BigInt out of a buffer
0075     static BigInt fromBuffer(const Uint8 *buf, Uint32 size);
0076 
0077 private:
0078     mpz_t val;
0079 };
0080 
0081 }
0082 
0083 #endif