- Notifications
You must be signed in to change notification settings - Fork1
c# big integer implementation on C++ (portable format desired)
License
NeoResearch/csbiginteger-cpp
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This project is part of thecsBigInteger macro project, a C# BigInteger implementation on C++ (portable format desired)
This project is an ongoing port of csBigInteger.js project:https://github.com/neoresearch/csBigInteger.js
Existing frameworks use high-level languages that may not be suitable for very lightweight architectures,such as microcontrollers with very limited computing capabilities.
C/C++ is interoperable with nearly all existing languages, so the idea is to provide modules that can bereused on other projects (on other languages too).
The project is very flexible, but still very simple to use.
See examples indemo/ folder. GNUmake with build three versions:
- demo_hand: usingHandBigInt.hpp (slow and the simplest to use)
- demo_cshand: usingBigInteger.h +BigIntegerHand.cpp (slow)
- demo_csgmp: usingBigInteger.h +BigIntegerGMP.cpp (fast)
Example from./demo_cshand (print-1,128 and2^128):
demonstration for csBigInteger (Engine: 'HandBigInt')usage: <operation> <enter> <number> <enter> <number <enter>avaliable operations are: + - / * % ^ > < 0x 0b (0x and 0b are conversions to hex big endian and binary)please type the operation: 0xexpects 1 number(s)please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):-1number is (decimal) '-1'conversion: 0xff <----- Negative values have MSb set to oneplease type the operation: 0xexpects 1 number(s)please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):128number is (decimal) '128'conversion: 0x0080 <----- This zero on the left is correct and necessary for csBigIntegerplease type the operation: ^expects 2 number(s)please type first number (in decimal, or unsigned hex with prefix 0x, or binary prefix 0b):2number is (decimal) '2'please type second number (in decimal):128number is '128'result: 340282366920938463463374607431768211456Just include the Single Header solutionsrc/HandBigInt.hpp.
#include<iostream>#include "HandBigInt.hpp"int main() { HandBigInt big(10); big = big + 1; std::cout << big.toString() << std::endl; // output is 11 return 0;}This is VERY innefficient, but it is tested against all BigInteger tests (except Online_Pack test, which takes so long...).The BigInteger layer can be implemented with HandBigInt, GMP or Mono.
If you have a C++ project and want to use csBigInteger, just:
#include"BigInteger.h"usingnamespacecsbiginteger;// ...BigInteger big{"123456789123456789"};big.ToString();// prints '123456789123456789' in base 10//BigInteger big1{1};BigInteger big2{2};BigInteger big3 = big1 + big2;big3.ToString();// prints '3' in base 10
To compile this using GNU MP library (install its libs-lgmp -lgmpxx), just include flagGMP_CSBIG (or link together withBigIntegerGMP.cpp). Example withGCC:g++ -DGMP_CSBIG yourfile.cpp -o output -lgmp -lgmpxx.
Other options is to useMONO_CSBIG (or link againstBigIntegerMono.cpp).With Mono you may have an "equivalent" version of "original" C#.It may be more efficient, but it's harder to build (requiresdotnet and mono dependencies).
To use this library on other languages, you need to build a static (.a) or shared library (.dll,.so). You can find some experimental versions onbuild folder, but it's safer to build by yourself.
The process is similar, using C headercsBigIntegerLib.h and alsocsBigIntegerLib.cpp (seemakefile for an example).
It may look crazy, butcsBigIntegerLib.h is also useful to "get external implementations" from other languages (like javascript, for example). In this case, your C++ code should#include "csBigIntegerLibClass.hpp" andusing namespace csbigintegerlib.The implementation may be provided by C++ itself (viacsBigIntegerLib.cpp) or any other language you like.
There are currently two implementations forBigInteger.h:BigIntegerGMP.cpp orBigIntegerMono.cpp.
Getting submodules:git submodule update --init --recursive andgit pull --recurse-submodules
On debian-based systems (or ubuntu), just typemake vendor (it will installlibgmp-dev package).
Mono implementation will generate acsbiginteger_dotnet.dll and load it throughcsbiginteger_mono.so. It is much more complex process, but it guarantees thatoriginal BigInteger C# library is being used.Right now, tests are passing for both gmp and mono implementations, so gmp C++ native implementation is much preferred (and more lightweight) on practice.
If you want to go this way, you will need mono:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EFsudo apt install apt-transport-https ca-certificatesecho "deb https://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.listsudo apt updatesudo apt install mono-completeIt will also configure test library (as long as you cloned this project with--submodules too).To test, just runmake test.
Currently, C++11 is adopted, in order to keep the best compatibility between conversors and compilers. However, it is recommended to migrate to C++17 as soon as possible, if this does not break compatibility with any existing modules and tools.
Let's please follow theCppCoreGuidelines.
If using vscode IDE, it is recommended to install the following extensions:
- C/C++ (currently 0.23.0-insiders2)
- C++ Intellisense (currently 0.2.2)
- GoogleTest Adapter (currently 1.8.3)
The currently adopted style for C++ isMozilla, with indentation level set to 3.Recommended configuration for vscode:
{"[cpp]": {"editor.tabSize" :3,"editor.detectIndentation":false },"C_Cpp.clang_format_fallbackStyle":"{ BasedOnStyle : Mozilla , ColumnLimit : 0, IndentWidth: 3, AccessModifierOffset: -3}"}The naming style for variables and methods is based onC# language.So, CamelCase is used andpublic variables start with upper-case letters, whileprivate andlocal variables start with lower-case.The idea is to preseve maximum compatibility with reference project (which is on C#).
Anything that is beyond the scope of the reference project can use classic CamelCaseC++ naming (for example, starting variables with lower-case).
Code followsMIT License.
ImplementationBigIntegerGMP.cpp (class implementation of standardBigInteger.h) isLGPLv3. The reason is that this implementation depends on GNU MP Bignum Library (licensed LGPLv3 since version 6), what means that all modifications ofBigIntegerGMP.cpp, or usage of its code (even partially) on other projects should also adoptLGPLv3 (not MIT License).
ImplementationBigIntegerMono.cpp depends on Mono license, which is also MIT License.
The binaries generated by this project (csbiginteger_gmp.so orcsbiginteger_mono.so) can be freely used on other projects, regardless of license.
About
c# big integer implementation on C++ (portable format desired)
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.