Skip to content

Cross Platform BigInteger Cpp is a high-performance C++ library designed for seamless big integer arithmetic, inspired by the C# Big Integer class.

License

Notifications You must be signed in to change notification settings

epicchainlabs/CrossPlatformBigIntegerCpp

Repository files navigation

CrossPlatformBigIntegerCpp

Overview

CrossPlatformBigIntegerCpp is a versatile C++ library that provides a portable implementation of big integer arithmetic. This library is designed to seamlessly integrate high-precision integer operations into C++ applications across various platforms, including Windows, macOS, and Linux. Inspired by the csBigInteger project, this library offers robust functionality for handling large numbers with efficiency and precision.

Key Features

  • Cross-Platform Support: Operates consistently across major operating systems—Windows, macOS, and Linux.
  • High Performance: Implements optimized algorithms for arithmetic operations, including addition, subtraction, multiplication, and division.
  • Portable Implementations:
    • csbiginteger_gmp.so: Utilizes GNU GMP for efficient arithmetic operations.
    • csbiginteger_mono.so: Requires Mono runtime and csbiginteger_dotnet.dll, ensuring compatibility with .NET environments.
  • Native Integration: Supports both C++ and .NET environments, with comprehensive build and usage instructions.
  • Extensive Documentation: Provides detailed instructions and usage examples to facilitate easy integration and utilization.

Build Dependencies

C++ Portable

  1. Clone the Repository:

    git clone --recursive https://github.com/yourusername/CrossPlatformBigIntegerCpp.git
  2. Install GNU GMP Library:

    sudo apt install libgmp-dev
  3. Build the Library:

    cd CrossPlatformBigIntegerCpp
    make vendor

C# Dotnet

  1. Install .NET Dependencies:

    python3 -m pip install pip
    pip install msl-loadlib pycparser pythonnet
  2. Install Mono (Linux):

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
    sudo apt install apt-transport-https ca-certificates
    echo "deb https://download.mono-project.com/repo/ubuntu stable-xenial main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list
    sudo apt update
    sudo apt install mono-complete
  3. Build the .NET Library:

    cd csbiginteger/dotnet
    dotnet build -c Release
  4. Publish the .NET Library:

    dotnet publish --configuration Release --self-contained

Usage

To test the library, you can use the provided Python script:

from csbiginteger.BigInteger import BigInteger
# or, use the .NET version
from csbiginteger.BigIntegerNet import BigIntegerNet

def main():
    big = BigInteger()
    print('length = ', len(big))
    print('to_int = ', big.to_int())
    print('to_str: ', big.to_str())
    print('')

    bigM1 = BigInteger(-1)
    print('length = ', len(bigM1))
    print('to_int = ', bigM1.to_int())
    print('to_str: ', bigM1.to_str())
    print('')

    big4293967296 = BigInteger(4293967296)
    print('length = ', len(big4293967296))
    print('to_int = ', big4293967296.to_int())
    print('to_long = ', big4293967296.to_long())
    print('to_str: ', big4293967296.to_str())
    print('')

    bigff = BigInteger(b'\xff')
    print('length = ', len(bigff))
    print('to_int = ', bigff.to_int())
    print('to_long = ', bigff.to_long())
    print('to_str: ', bigff.to_str())
    print('')

    big100 = BigInteger('100')
    print('length = ', len(big100))
    print('to_int = ', big100.to_int())
    print('to_long = ', big100.to_long())
    print('to_str: ', big100.to_str())
    print('')

    big0001 = BigInteger('0x0001', 16)
    print('length = ', len(big0001))
    print('to_int = ', big0001.to_int())
    print('to_long = ', big0001.to_long())
    print('to_str: ', big0001.to_str())
    print('')

    big101 = big100.add(big0001) # big100 + big0001
    print('length = ', len(big101))
    print('to_int = ', big101.to_int())
    print('to_long = ', big101.to_long())
    print('to_str: ', big101.to_str())
    print('')

    big99 = big100.sub(big0001) # big100 - big0001
    print('length = ', len(big99))
    print('to_int = ', big99.to_int())
    print('to_long = ', big99.to_long())
    print('to_str: ', big99.to_str())

    return 0

Expected Output:

length =  1
to_int =  0
to_str:  0x00

length =  1
to_int =  -1
to_str:  0xff

length =  5
to_int =  -1000000
to_long =  4293967296
to_str:  0x00fff0bdc0

length =  1
to_int =  -1
to_long =  -1
to_str:  0xff

length =  1
to_int =  100
to_long =  100
to_str:  0x64

length =  1
to_int =  1
to_long =  1
to_str:  0x01

length =  1
to_int =  101
to_long =  101
to_str:  0x65

length =  1
to_int =  99
to_long =  99
to_str:  0x63

License

This project is licensed under the MIT License. See the LICENSE file for details.

Releases

No releases published

Packages

No packages published