Skip to main content

Exact integer arithmetic via AVX2-accelerated Residue Number System

Project description

rns_engine

Exact integer arithmetic via AVX2-accelerated Residue Number System (RNS).

No floating point. No approximation. Errors are structurally impossible.


What it does

Standard Python integers are exact but slow. NumPy is fast but uses floating point or silently overflows. rns_engine gives you exact integer arithmetic at hundreds of millions of operations per second — the best of both worlds.

It works by decomposing integers into residues across three coprime moduli (127, 8191, 65536), performing all operations in residue space using AVX2 SIMD instructions, and reconstructing exact results via the Chinese Remainder Theorem.

Dynamic range: [0, 68,174,282,752) — about 68 billion.

Install

pip install rns_engine

Requires a CPU with AVX2 (any Intel/AMD since ~2013). Falls back to scalar arithmetic on ARM and older hardware.

Quick start

import rns_engine as rns
import numpy as np

# Works on arrays of uint64
a = np.array([123456789, 999999999], dtype=np.uint64)
b = np.array([987654321, 111111111], dtype=np.uint64)

# Encode once
ea = rns.encode(a)   # returns (r0, r1, r2) residue arrays
eb = rns.encode(b)

# Operate in residue space — no intermediate decode needed
result = rns.decode(*rns.mul(*ea, *eb))   # exact multiplication

# Chain multiple operations — decode once at the end
s1 = rns.add(*ea, *eb)      # a + b
s2 = rns.mul(*s1, *eb)      # (a + b) * b
s3 = rns.sub(*s2, *ea)      # (a + b) * b - a
out = rns.decode(*s3)        # one decode, three operations

Operations

Function Description
rns.encode(x) uint64[](r0, r1, r2) residue arrays
rns.decode(r0, r1, r2) Residues → uint64[] via Garner's algorithm
rns.add(*ea, *eb) Exact addition
rns.sub(*ea, *eb) Exact subtraction
rns.mul(*ea, *eb) Exact multiplication
rns.div_(*ea, *eb) Exact division (b must be coprime to all moduli)
rns.op(*ea, *eb, code) Generic: 0=add 1=mul 2=sub 3=div

Division constraint

Division requires b to be invertible on all three rails:

  • b % 127 != 0
  • b % 8191 != 0
  • b % 65536 is odd (coprime to 2^16)
# Safe way to ensure b is valid for division:
b = np.where(b % 2 == 0, b + 1, b)   # make odd
b = np.where(b % 127  == 0, b + 2, b)
b = np.where(b % 8191 == 0, b + 4, b)
b = b % rns.M

Performance

On a machine with AVX2 (tested on Google Colab's CPU):

Operation Throughput
add ~200–400 M ops/sec
sub ~200–400 M ops/sec
mul ~200–400 M ops/sec
div ~1.6 M ops/sec (scalar modinv per element)

Why RNS?

In a Residue Number System, addition and multiplication have no carry propagation between digits. Each residue rail is independent. This makes RNS ideal for:

  • Exact arithmetic — results are always correct within the dynamic range
  • Parallel computation — rails can run simultaneously
  • Error detection — CRT reconstruction fails loudly if any rail is corrupted
  • Cryptography — modular arithmetic is the native language of RSA, ECC, etc.

How it works

Three coprime moduli: m0 = 127, m1 = 8191, m2 = 65536

Dynamic range: M = 127 × 8191 × 65536 = 68,174,282,752

Encode: x → (x mod 127, x mod 8191, x mod 65536)

Operate: each rail independently, e.g. add: (a+b) mod mᵢ per rail

Decode (Garner's algorithm):

t0 = r0
t1 = (r1 - t0) × inv(127, 8191)  mod 8191
t2 = (r2 - t0 - t1×127) × inv(127×8191, 65536)  mod 65536
x  = t0 + t1×127 + t2×127×8191

Mod 127 and mod 8191 reductions use the Mersenne-prime trick: x mod (2^k - 1) = (x & mask) + (x >> k) — no division needed.

Building from source

git clone https://github.com/playfularchitect/rns_engine
cd rns_engine
pip install pybind11 numpy
pip install -e .
pytest tests/ -v

Requires g++ with C++17 support.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rns_engine-0.1.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rns_engine-0.1.0-cp312-cp312-win_amd64.whl (84.6 kB view details)

Uploaded CPython 3.12Windows x86-64

rns_engine-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

rns_engine-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (93.4 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

rns_engine-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl (97.5 kB view details)

Uploaded CPython 3.12macOS 10.13+ x86-64

rns_engine-0.1.0-cp311-cp311-win_amd64.whl (82.9 kB view details)

Uploaded CPython 3.11Windows x86-64

rns_engine-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

rns_engine-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (92.1 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

rns_engine-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl (95.4 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

rns_engine-0.1.0-cp310-cp310-win_amd64.whl (82.4 kB view details)

Uploaded CPython 3.10Windows x86-64

rns_engine-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

rns_engine-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (91.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

rns_engine-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl (94.0 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

rns_engine-0.1.0-cp39-cp39-win_amd64.whl (83.4 kB view details)

Uploaded CPython 3.9Windows x86-64

rns_engine-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

rns_engine-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (91.2 kB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

rns_engine-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (94.1 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

File details

Details for the file rns_engine-0.1.0.tar.gz.

File metadata

  • Download URL: rns_engine-0.1.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rns_engine-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d317bfa0282d4183daa2f8a5a76f36fcdd35bab380174b42e8614cde5ae9251b
MD5 41546cc359c636a80a6be5c979f9824e
BLAKE2b-256 9046e5a97dc02a405c63cb48d7da79c9661a8b6c33db4f6a4c620c821b7607d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0.tar.gz:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 84.6 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rns_engine-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 15451cfb3bba1e5d9084fad981242ad2fdae6f8444ba2bd17b566bd3a33321e0
MD5 cd63ee8e7ef794d979c477ccd3d027db
BLAKE2b-256 9ff9d97f9689c3ad0f94c671910d6ca15ed0e850d68ce82cc729a111ca263903

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e2b90972b5a632e64e2363269cd10d54ef4613e83cdfb488d1fdbf4b79540046
MD5 87407acead1e48d4cc95c529746cd9d5
BLAKE2b-256 429d3c72b9a57daf71b013ebe3af2cfe3168a563e1093c5a6536afbd70d885cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0753b5a0301a4f3799561c5804a8a57c5ea3f7b0d17a32b47292b1eea89dc5df
MD5 f01a2cac4ca98095292a53dedc9594c1
BLAKE2b-256 4e26182c43a477432a6bb8faf00520fe4bd15258d93b2028690be6ba4dbb7178

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 4e4ab45c0a59dc9a7b21dfd2a0801b5acc606bd0f8d9b7b27ad7fd4eef9d7d5f
MD5 888260c637d6e87a73db0cc5d1b2eff8
BLAKE2b-256 9a78a76da275311e78c2b4d936a45283c86bb568a4d896d7c4c5b9c46d3402bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp312-cp312-macosx_10_13_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 82.9 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rns_engine-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 90fe9d576f489f19daf640f6d39b783fbfb2434e6d5eb88553842ee17f79f48e
MD5 da8b4eae4a6226ef52c1a80b04e875b1
BLAKE2b-256 240458605aded5ba23a3e8c92a67382a68d9508ec9f838a3c1352f05dd03afe9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0a27cf07f402bf9b1dacb9341bd17199ba104759c9c62ee51932f99c019886b8
MD5 d8f4ad9a9ec09988f4078b24cd7cd26e
BLAKE2b-256 4669cf1796adbbf85119479ef56da6fc71c4630af7e31814a9d7f73c6868ed36

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 63e51bc9bd5475b3e26df31a93dd93cf9ac7ff956eaa3cf0b4f366754f3a24af
MD5 77032cac93f8bb9edf6d582097133ca6
BLAKE2b-256 534dae49271ca86a5711111cdc12ed630e68190f9a209d8aaea58e5e6c6cc6be

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e0f3e316ab5c1edfd41c741beeb29004dcc9301ed81f70cef23fc55a54e6b98c
MD5 93e7ffec7c27484af56110a43a91e7af
BLAKE2b-256 bbdfbe87572f9357f449e30ead513ed83a7efc9d479832154e4aed58d86f5b6b

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp311-cp311-macosx_10_9_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.1.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 82.4 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rns_engine-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8bdc7078a2da3e2036522b2b8a0213704dcfdb79216ebd40a89f757a23fd5ff2
MD5 c07820f7ac711244070f471856245a5a
BLAKE2b-256 5510428b3bce48a8c2bc0a919071c302686155ec1be17897dabe7c7a83635cd2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp310-cp310-win_amd64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6918e68f02d21486b234e3eed67310aa98df5e87c71c5e0fd44c7b1a7ea29cf2
MD5 bd87ee10ccd9614ad8651751eb869285
BLAKE2b-256 134832b951d09a4713ad67e9688c829a0382c73feb53fbd4a309242dbdd88fd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0a203c8eb7198b33e596f437a6cbb8e8156d2ec4dbe706efa5fe99d936033bab
MD5 2bc67b06eee297fddb6f3fd883ca33b4
BLAKE2b-256 93ee03d04e03d74af7c77deae910f777a66ebba56464fb99174ee2bed40ec63a

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0dc834d0166cad83fa845b618b39ee0e12702b7442e6159c5997fef0959b4689
MD5 f2091c82f0df3bffc10ecac927f27108
BLAKE2b-256 eab49e0ee9c33e9896b66cd5ce50644cd77efcaadb05f2db372b95206ff6e1f0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp310-cp310-macosx_10_9_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.1.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 83.4 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rns_engine-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5647da2cd15dcd181483004f89dfe652e2d901d3ae4c10599c27f8c80efe5b49
MD5 86152191d8dbef08962b799729f5aec9
BLAKE2b-256 6fe579bff88622cd19b3feb1287ac9a16a024846ebbbc915db3bde0c66898b99

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp39-cp39-win_amd64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d5e0ba841e5bd3364d91cfa7a9c7bf7f2c368dbfc0d4cc9bc97298f612629d36
MD5 ce0e9a5e24c6018e415f546549f7e8bd
BLAKE2b-256 f95573f9853dc1b239dc838f5a45eef96e143f1a38cf2072e5cf273e67f9f0d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a60ab87d196a26e4fd2b318bae4ceea05a0db73e8ed5f714eef6a5a414210439
MD5 4c8a52cc04c023b43cd0662599c5bbea
BLAKE2b-256 e47eb757e703cde1d7a3f25bac5d1903647665601e72996b33afe6ba50f86c51

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rns_engine-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ed5bd219ca43abca5858b2a6ac1f272c5a414af8d1a0f16e9ba8557df1964e4f
MD5 17ef62311b30d7524697112c263ad1f4
BLAKE2b-256 84c4b9a58f914c7890da9e67dcad7506bf408849acc48148a3c6f7cbe3ad4779

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl:

Publisher: build.yml on playfularchitect/rns_engine

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page