You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
227 lines
6.0 KiB
227 lines
6.0 KiB
14 years ago
|
/*==LICENSE==*
|
||
|
|
||
|
CyanWorlds.com Engine - MMOG client, server and tools
|
||
|
Copyright (C) 2011 Cyan Worlds, Inc.
|
||
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program 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 General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
|
||
13 years ago
|
Additional permissions under GNU GPL version 3 section 7
|
||
|
|
||
|
If you modify this Program, or any covered work, by linking or
|
||
|
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
|
||
|
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
|
||
|
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
|
||
|
(or a modified version of those libraries),
|
||
|
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
|
||
|
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
|
||
|
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
|
||
|
licensors of this Program grant you additional
|
||
|
permission to convey the resulting work. Corresponding Source for a
|
||
|
non-source form of such a combination shall include the source code for
|
||
|
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
|
||
|
work.
|
||
|
|
||
14 years ago
|
You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
||
|
or by snail mail at:
|
||
|
Cyan Worlds, Inc.
|
||
|
14617 N Newport Hwy
|
||
|
Mead, WA 99021
|
||
|
|
||
|
*==LICENSE==*/
|
||
|
|
||
13 years ago
|
#ifndef plBigNum_inc
|
||
|
#define plBigNum_inc
|
||
14 years ago
|
|
||
|
/*****************************************************************************
|
||
|
*
|
||
13 years ago
|
* plBigNum class
|
||
14 years ago
|
*
|
||
|
***/
|
||
|
|
||
13 years ago
|
#include "HeadSpin.h"
|
||
14 years ago
|
#include <openssl/bn.h>
|
||
14 years ago
|
|
||
13 years ago
|
class plBigNum
|
||
|
{
|
||
14 years ago
|
private:
|
||
14 years ago
|
BIGNUM m_number;
|
||
13 years ago
|
mutable BN_CTX* m_context;
|
||
14 years ago
|
|
||
13 years ago
|
BN_CTX* GetContext() const
|
||
14 years ago
|
{
|
||
|
if (!m_context)
|
||
|
m_context = BN_CTX_new();
|
||
|
return m_context;
|
||
|
}
|
||
14 years ago
|
|
||
|
public:
|
||
13 years ago
|
plBigNum();
|
||
|
plBigNum(const plBigNum& a);
|
||
|
plBigNum(uint32_t a);
|
||
|
plBigNum(uint32_t bytess, const void* data, bool le=false);
|
||
|
~plBigNum();
|
||
14 years ago
|
|
||
13 years ago
|
plBigNum& operator=(const plBigNum& a)
|
||
14 years ago
|
{
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
14 years ago
|
// Constant parameters need not be distinct from the destination or from
|
||
|
// each other
|
||
|
|
||
13 years ago
|
void Add(const plBigNum& a, uint32_t b)
|
||
14 years ago
|
{
|
||
|
// this = a + b
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
|
BN_add_word(&m_number, b);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Add(const plBigNum& a, const plBigNum& b)
|
||
14 years ago
|
{
|
||
|
// this = a + b
|
||
|
BN_add(&m_number, &a.m_number, &b.m_number);
|
||
|
}
|
||
|
|
||
13 years ago
|
int Compare(uint32_t a) const;
|
||
|
|
||
|
int Compare(const plBigNum& a) const
|
||
14 years ago
|
{
|
||
|
return BN_cmp(&m_number, &a.m_number);
|
||
|
}
|
||
13 years ago
|
|
||
13 years ago
|
bool isZero() const
|
||
|
{
|
||
|
return BN_is_zero(&m_number);
|
||
|
}
|
||
14 years ago
|
|
||
13 years ago
|
void Div(const plBigNum& a, uint32_t b, uint32_t* remainder)
|
||
14 years ago
|
{
|
||
|
// this = a / b, remainder = a % b
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
13 years ago
|
*remainder = (uint32_t)BN_div_word(&m_number, b);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
void Div(const plBigNum& a, const plBigNum& b, plBigNum* remainder)
|
||
14 years ago
|
{
|
||
|
// this = a / b, remainder = a % b
|
||
|
// either this or remainder may be nil
|
||
|
BN_div(this ? &m_number : nil, remainder ? &remainder->m_number : nil,
|
||
|
&a.m_number, &b.m_number, GetContext());
|
||
|
}
|
||
|
|
||
13 years ago
|
void FromData_BE(uint32_t bytess, const void* data)
|
||
14 years ago
|
{
|
||
13 years ago
|
BN_bin2bn((const uint8_t*)data, bytess, &m_number);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
void FromData_LE(uint32_t bytess, const void* data);
|
||
14 years ago
|
|
||
13 years ago
|
uint8_t* GetData_BE(uint32_t* bytess) const;
|
||
|
uint8_t* GetData_LE(uint32_t* bytess) const;
|
||
14 years ago
|
|
||
13 years ago
|
bool IsPrime() const
|
||
14 years ago
|
{
|
||
|
// Cyan's code uses 3 checks, so we'll follow suit.
|
||
|
// This provides an accurate answer to p < 0.015625
|
||
|
return BN_is_prime_fasttest(&m_number, 3, nil, GetContext(), nil, 1) > 0;
|
||
|
}
|
||
|
|
||
13 years ago
|
void Mod(const plBigNum& a, const plBigNum& b)
|
||
14 years ago
|
{
|
||
|
// this = a % b
|
||
|
BN_div(nil, &m_number, &a.m_number, &b.m_number, GetContext());
|
||
|
}
|
||
|
|
||
13 years ago
|
void Mul(const plBigNum& a, uint32_t b)
|
||
14 years ago
|
{
|
||
|
// this = a * b
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
|
BN_mul_word(&m_number, b);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Mul(const plBigNum& a, const plBigNum& b)
|
||
14 years ago
|
{
|
||
|
// this = a * b
|
||
|
BN_mul(&m_number, &a.m_number, &b.m_number, GetContext());
|
||
|
}
|
||
|
|
||
13 years ago
|
void PowMod(uint32_t a, const plBigNum& b, const plBigNum& c)
|
||
14 years ago
|
{
|
||
|
// this = a ^ b % c
|
||
13 years ago
|
PowMod(plBigNum(a), b, c);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
void PowMod(const plBigNum& a, const plBigNum& b, const plBigNum& c)
|
||
14 years ago
|
{
|
||
|
// this = a ^ b % c
|
||
|
BN_mod_exp(&m_number, &a.m_number, &b.m_number, &c.m_number, GetContext());
|
||
|
}
|
||
|
|
||
13 years ago
|
void Rand(const plBigNum& a, plBigNum* seed)
|
||
14 years ago
|
{
|
||
|
// this = random number less than a
|
||
13 years ago
|
int bits = BN_num_bits(&a.m_number);
|
||
14 years ago
|
do
|
||
|
Rand(bits, seed);
|
||
|
while (Compare(a) >= 0);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Rand(uint32_t bits, plBigNum* seed);
|
||
14 years ago
|
|
||
13 years ago
|
void RandPrime(uint32_t bits, plBigNum* seed)
|
||
14 years ago
|
{
|
||
|
BN_generate_prime(&m_number, bits, 1, nil, nil, nil, nil);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Set(const plBigNum& a)
|
||
14 years ago
|
{
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Set(uint32_t a)
|
||
14 years ago
|
{
|
||
|
BN_set_word(&m_number, a);
|
||
|
}
|
||
|
|
||
13 years ago
|
void SetOne() { Set(1); }
|
||
|
void SetZero() { Set(0); }
|
||
14 years ago
|
|
||
13 years ago
|
void Shl(const plBigNum& a, uint32_t b)
|
||
14 years ago
|
{
|
||
|
// this = a << b
|
||
|
BN_lshift(&m_number, &a.m_number, b);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Shr(const plBigNum& a, uint32_t b)
|
||
14 years ago
|
{
|
||
|
// this = a >> b
|
||
|
BN_rshift(&m_number, &a.m_number, b);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Sub(const plBigNum& a, uint32_t b)
|
||
14 years ago
|
{
|
||
|
// this = a - b
|
||
|
BN_copy(&m_number, &a.m_number);
|
||
|
BN_sub_word(&m_number, b);
|
||
|
}
|
||
|
|
||
13 years ago
|
void Sub(const plBigNum& a, const plBigNum& b)
|
||
14 years ago
|
{
|
||
|
// this = a - b
|
||
|
BN_sub(&m_number, &a.m_number, &b.m_number);
|
||
|
}
|
||
14 years ago
|
};
|
||
13 years ago
|
#endif // plBigNum_inc
|