Skip to main content

Puya BigNumber

A library for mathematical operations involving (very) large numbers in Puya for Algorand. It supports addition, subtraction, multiplication, division, comparison, remainder calculation (using Barrett Reduction), and modular exponentiation (using Barrett Reduction) of large big-endian encoded numbers as byte strings. The library is implemented using some of the most efficient algorithms available in the literature, such as Karatsuba multiplication, Algorithm D by Donald Knuth for multi-word division, and Barrett Reduction for remainder calculation.

⚠️ Not officially production ready until documentation updated to indicate otherwise. Use at your own risk.

Features

  • Supports up to 1024 bytes (8192 bits) wide numbers
  • Algorand Puya smart contract HLL compatibility
  • Algorand-python-testing framework

Features the following operators:

  • Addition: O(n) time complexity with 512 bit sized digits
  • Subtraction: O(n) time complexity with 512 bit sized digits
  • Multiplication: O(n**1.58) time complexity (Karatsuba multiplication) with 512 bit sized digits
  • Division: O(n*m) time complexity (Algorithm D by Donald Knuth) with 256 bit sized digits
  • Remainder with Barrett Reduction: O(n**1.58) time complexity with 512 bit sized digits
  • Modular Exponentiation with Barrett Reduction: O(exp.bit_length x n**1.58) time complexity with 512 bit sized digits
  • Less than, greater than and equal comparison: O(max(n,m)) time complexity with 512 bit sized digits

In the above n and m refer to the number of digits in the input.

Install

Puya BigNumber is available on PyPI:

pip install puya-bignumber

Usage

All inputs to math functions are assumed to be big-endian encoded numbers unless explicitly stated otherwise.

from puya_bignumber import (
    add,
    subtract,
    equal,
    multiply,
    divide,
    less_than,
    greater_than,
    mod_barrett_reduce,
    barrett_reducer_factor,
    modexp_barrett_reduce,
)
# ... use the functions as you might expect, e.g. add(big_endian_bytes_a, big_endian_bytes_b)

Develop

This module uses poetry as the package manager and Python environment manager. Please see How to Build and Publish Python Packages With Poetry.

Test

poetry run pytest -v

License & Contribution

Contributions and additions are welcomed. Please respect the terms of the GNU GPL v3 license. Attribution for the author Winton Nathan-Roberts is required. No warranties or liabilities per the license. It is not yet officially production ready, although it is thoroughly tested.

Metadata

Release files for puya-bignumber 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for puya-bignumber 0.4.1
File Size Uploaded
puya_bignumber-0.4.1.tar.gz 17.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for puya-bignumber 0.4.1
File Interpreter ABI Platform
puya_bignumber-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 36.5 kB

Release files / puya_bignumber-0.4.1.tar.gz

Download URL puya_bignumber-0.4.1.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9ec776101b7173aba2eb0927535bb2fe34b6d19b1a7f27d144992051bb53f0a8
BLAKE2b-256 checksum
How to use checksums
479358ffc43bd15c10bcce7a079e712969923e9f994f251b442b3d2f3984c5ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.14 Linux/6.10.9-350.vanilla.fc40.x86_64

Release files / puya_bignumber-0.4.1-py3-none-any.whl

Download URL puya_bignumber-0.4.1-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1fde4a3cb4e81d4d49abefe3ccc3d84cfa7de7fda08394bc168577b0f8eb9f74
BLAKE2b-256 checksum
How to use checksums
9d74e86395dd5f59eca85ee5ad593d28eb243a378432ab68c4d610a5fb83b9cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.14 Linux/6.10.9-350.vanilla.fc40.x86_64

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page