Fast common interface for php_gmp and php_bcmath modules

Overview

BigInteger wrapper library for PHP

Information

This library is a common interface for php_gmp and php_bcmath modules. It automatically detects supported modules and uses the best of them (gmp>bcmath). Gmp is a lot faster, but is also missing on many hosting services -- that is why this wrapper has been created. It is used for example in encryption functions of the PrivMX WebMail software.

Installation

You can install this library via Composer:

composer require simplito/bigint-wrapper-php

Documentation

If you want to force using a specific implementation, then define constant S_MATH_BIGINTEGER_MODE - set it to "gmp" or "bcmath". If you do not do this, mode of operation and the constant will be set automatically.

If there are no gmp and bcmath modules, an exception will be thrown. If you want to prevent this, then simply define S_MATH_BIGINTEGER_QUIET constant.

All functions of this library are implemented as members of class BigInteger, which is located under BI namespace. Instances of BigInteger are immutable - member functions usually return new instances of the BigInteger class.

ConvertibleToBi - a placeholder type

To make the below documentation more readable we use the "ConvertibleToBi" type symbol, which in reality can be one of the following types:

  • an instance of the BigInteger class
  • an integer
  • a decimal string
  • a gmp resource or class (only when you are in gmp mode)

If you have a non-decimal string and want to use it -- first you have to convert it to BigInteger class using:

new BigInteger($myNonDecimalString, $baseOfMyNonDecimalString)

BI\BigInteger class members

construct(ConvertibleToBi $value = 0, int $base = 10)

Creates a new instance of BigInteger. If you pass an invalid value, an exception will be thrown. If $base === true then passed $value will be used without any check and conversion. Supported bases: 2, 10, 16, 256.

  • GMP implementation: gmp_init + bin2hex for 256 base
  • Bcmath implementation: custom(bcadd + bcmul)

static BigInteger|false createSafe(ConvertibleToBi $value = 0, int $base = 10)

Creates a new BigInteger instance in the same way as constructor, but if there is an error, false will be returned instead of throwing an exception.

BigInteger add(ConvertibleToBi $x)

Adds numbers

  • GMP implementation: gmp_add
  • Bcmath implementation: bcadd

BigInteger sub(ConvertibleToBi $x)

Subtracts numbers

  • GMP implementation: gmp_sub
  • Bcmath implementation: bcsub

BigInteger mul(ConvertibleToBi $x)

Multiplies numbers

  • GMP implementation: gmp_mul
  • Bcmath implementation: bcmul

BigInteger div(ConvertibleToBi $x)

Divides numbers

  • GMP implementation: gmp_div_q
  • Bcmath implementation: bcdiv

BigInteger divR(ConvertibleToBi $x)

Returns a remainder of the division of numbers. The remainder has the sign of the divided number.

  • GMP implementation: gmp_div_r
  • Bcmath implementation: bcmod

array(BigInteger, BigInteger) divQR(ConvertibleToBi $x)

Divides numbers and returns quotient and remainder. Returns an array(), with the first element being quotient, and the second being remainder.

  • GMP implementation: gmp_div_qr
  • Bcmath implementation: div + divR

BigInteger mod(ConvertibleToBi $x)

The "division modulo" operation. The result is always non-negative, the sign of divider is ignored.

  • GMP implementation: gmp_mod
  • Bcmath implementation: custom (bcmod + bcadd)

BigInteger gcd(ConvertibleToBi $x)

Calculates greatest common divisor

  • GMP implementation: gmp_gcd
  • Bcmath implementation: custom (bccomp + bcdiv + bcsub + bcmul)

BigInteger|false modInverse(ConvertibleToBi $x)

Inverses by modulo, returns false if inversion does not exist.

  • GMP implementation: gmp_invert
  • Bcmath implementation: custom (gcd)

BigInteger pow(ConvertibleToBi $x)

The power function.

  • GMP implementation: gmp_pow
  • Bcmath implementation: bcpow

BigInteger powMod(ConvertibleToBi $x, ConvertibleToBi $n)

The modular power function.

  • GMP implementation: gmp_powm
  • Bcmath implementation: bcpowmod

BigInteger abs()

Returns absolute value.

  • GMP implementation: gmp_abs
  • Bcmath implementation: check first character

BigInteger neg()

Negates the number

  • GMP implementation: gmp_neg
  • Bcmath implementation: check first character

BigInteger binaryAnd(ConvertibleToBi $x)

Bitwise AND.

  • GMP implementation: gmp_and
  • Bcmath implementation: custom (toBytes + php string and)

BigInteger binaryOr(ConvertibleToBi $x)

Bitwise OR

  • GMP implementation: gmp_or
  • Bcmath implementation: custom (toBytes + php string or)

BigInteger binaryXor(ConvertibleToBi $x)

Bitwise XOR

  • GMP implementation: gmp_xor
  • Bcmath implementation: custom (toBytes + php string xor)

BigInteger setbit($index, $bitOn = true)

Sets bit at given index

  • GMP implementation: gmp_setbit
  • Bcmath implementation: custom (toBits)

bool testbit($index)

Tests if a bit at given index is set

  • GMP implementation: gmp_testbit
  • Bcmath implementation: custom (toBits)

int scan0($start)

Scans for 0, and returns index of first found bit

  • GMP implementation: gmp_scan0
  • Bcmath implementation: custom (toBits)

int scan1($start)

Scans for 1, and returns index of first found bit

  • GMP implementation: gmp_scan1
  • Bcmath implementation: custom (toBits)

int cmp(ConvertibleToBi $x)

Compares numbers, returns <0, 0, >0

  • GMP implementation: gmp_cmp
  • Bcmath implementation: bccomp

bool equals(ConvertibleToBi $x)

Checks if numbers are equal

  • GMP implementation: gmp_cmp
  • Bcmath implementation: bccomp

int sign()

Sign of number, returns -1, 0, 1

  • GMP implementation: gmp_sign
  • Bcmath implementation: check first character

int toNumber()

Converts to number (use only with small 32/64bit numbers)

  • GMP implementation: gmp_intval
  • Bcmath implementation: intval

string toDec()

Converts to decimal string

  • GMP implementation: gmp_strval
  • Bcmath implementation: just the value

string toHex()

Converts to hex string

  • GMP implementation: gmp_strval
  • Bcmath implementation: toBytes + bin2hex

string toBytes

Converts to binary string

  • GMP implementation: gmp_strval + hex2bin
  • Bcmath implementation: custom (bcmod + bcdiv + bccomp)

string toBits()

Converts to bits string (0 and 1 characters)

  • GMP implementation: gmp_strval
  • Bcmath implementation: toBytes + decbin

string toString(int $base = 10)

Converts to string using given base (supported bases 2-62, 256)

  • GMP implementation: all above toX functions, and for non standard gmp_strval
  • Bcmath implementation: all above toX functions, and for non standard bcmod + bcdiv + bccomp
You might also like...
A multitool library offering access to recommended security related libraries, standardised implementations of security defences, and secure implementations of commonly performed tasks.

SecurityMultiTool A multitool library offering access to recommended security related libraries, standardised implementations of security defences, an

TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library

About TCrypto is a simple and flexible PHP 5.3+ in-memory key-value storage library. By default, a cookie will be used as a storage backend. TCrypto h

Automatic SQL injection and database takeover tool
Automatic SQL injection and database takeover tool

sqlmap sqlmap is an open source penetration testing tool that automates the process of detecting and exploiting SQL injection flaws and taking over of

SecLists is the security tester's companion. It's a collection of multiple types of lists used during security assessments, collected in one place. List types include usernames, passwords, URLs, sensitive data patterns, fuzzing payloads, web shells, and many more. Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP
Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP

AWare — C# Ransomware Ransomware with automatic Coinbase Commerce integration created in C# (Console) and PHP PD: AWare is just a proof of concept, wi

A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.
A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

A simple php (lumen) app for sharing sensitive text (basically like onetimesecret), but with full end-to-end AES-256-GCM encryption so even the server has no access to the data, and developed with very simple deployment in mind.

A (unofficial) WordPress plugin reporting PHP and JavaScript errors to Sentry.

A (unofficial) WordPress plugin to report PHP and JavaScript errors to Sentry.

Quickly and easily secure HTML text.
Quickly and easily secure HTML text.

Larasane Quickly sanitize text into safe-HTML using fluid methods. Requirements PHP 7.4, 8.0 or later. Laravel 7.x, 8.x or later. Installation Just fi

ChestRandomBP: This plugin generates chests in random places within a specific world. Where you can customize what each one of them contains, the time and the world of spawning.
ChestRandomBP: This plugin generates chests in random places within a specific world. Where you can customize what each one of them contains, the time and the world of spawning.

ChestRandomBP ChestRandomBP: This plugin generates chests, it works PocketMine-MP and random places within a specific world. Where you can customize w

Owner
Simplito
Software R&D
Simplito
Windows and macOS Hardening Interface to make security more accessible.

Welcome to the Hardening Interface Introduction To use HardeningKitty service more easily, we have created an interface which permits better understan

ataumo 24 Dec 5, 2022
High-level cryptography interface powered by libsodium

Halite Halite is a high-level cryptography interface that relies on libsodium for all of its underlying cryptography operations. Halite was created by

Paragon Initiative Enterprises 1.1k Dec 22, 2022
A PHP wrapper around the OpenSSL extension that provides a user-friendly interface for dealing with OpenSSL.

php-openssl-proxy About A PHP wrapper around the OpenSSL extension that provides a user-friendly interface for dealing with OpenSSL. What's up with th

Adão Pedro 4 Mar 5, 2022
PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web application

PHPIDS PHPIDS (PHP-Intrusion Detection System) is a simple to use, well structured, fast and state-of-the-art security layer for your PHP based web ap

null 752 Jan 3, 2023
Fast, general Elliptic Curve Cryptography library. Supports curves used in Bitcoin, Ethereum and other cryptocurrencies (secp256k1, ed25519, ..)

Fast Elliptic Curve Cryptography in PHP Information This library is a PHP port of elliptic, a great JavaScript ECC library. Supported curve types: Sho

Simplito 178 Dec 28, 2022
Automatic Encrypt and Decrypt your database data. Tested and used on Laravel 8

Laravel Encrypt Database Automatic Encrypt and Decrypt your database data. Tested and used on Laravel 8. I'm yet building the tests. Important Note th

Wellington Barbosa 2 Dec 15, 2021
Laravel Security was created by, and is maintained by Graham Campbell, and is a voku/anti-xss wrapper for Laravel, using graham-campbell/security-core

Laravel Security Laravel Security was created by, and is maintained by Graham Campbell, and is a voku/anti-xss wrapper for Laravel, using graham-campb

Graham Campbell 170 Nov 20, 2022
PHP 5.x support for random_bytes() and random_int()

random_compat PHP 5.x polyfill for random_bytes() and random_int() created and maintained by Paragon Initiative Enterprises. Although this library sho

Paragon Initiative Enterprises 8k Jan 5, 2023
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Cossack Labs 1.6k Jan 6, 2023
A library for generating random numbers and strings

RandomLib A library for generating random numbers and strings of various strengths. This library is useful in security contexts. Install Via Composer

Anthony Ferrara 832 Nov 24, 2022