Skip to main content

Exact RNS engine with session/cache API.

Project description

rns_engine

Exact 4-rail integer arithmetic via a Residue Number System (RNS), with AVX2 and optional OpenMP acceleration.

No floating point. No approximation. Exact results modulo the engine's dynamic range.


What it does

Standard Python integers are exact but slow. NumPy arrays are fast, but fixed-width integer arithmetic can overflow and float-based pipelines drift. rns_engine gives you exact modular arithmetic within a fixed dynamic range by decomposing values across four coprime rails:

  • 127
  • 8191
  • 65536
  • 524287

Arithmetic is performed independently on each rail, then reconstructed with Garner-style CRT.

Dynamic range: [0, 35,742,890,181,197,824)

127 × 8191 × 65536 × 524287 = 35,742,890,181,197,824

Install

pip install rns_engine

AVX2 note

HAS_AVX2 is a build-time property of the compiled extension, not runtime CPU detection. On supported x86_64 builds, the core can use AVX2 acceleration.


Quick start

import numpy as np
import rns_engine as rns

a = np.array([123456789, 999999999], dtype=np.uint64)
b = np.array([987654321, 111111111], dtype=np.uint64)

ea = rns.encode(a)
eb = rns.encode(b)

out_add = rns.decode(*rns.add(*ea, *eb))
out_mul = rns.decode(*rns.mul(*ea, *eb))

# Chain multiple exact operations in residue space, decode once at the end
s1 = rns.add(*ea, *eb)
s2 = rns.mul(*s1, *eb)
s3 = rns.sub(*s2, *ea)
out = rns.decode(*s3)

Session quick start

import numpy as np
import rns_engine as rns

s = rns.Session(cache_capacity=32)
x = np.array([1, 2, 3, 4], dtype=np.uint64)

# Cache-aware encode
ex = s.encode(x)

# Chain exact ops without decoding between steps
res = s.mul(s.add(ex, ex), ex)
out = s.decode(res)

# Single exact affine step
one = s.one_shot_affine(x, multiplier=1_000_003, addend=7)

# Repeated exact affine loop: stay in residue space, decode once
hot = s.hot_loop_affine(x, multiplier=1_000_003, addend=7, iterations=1000)

Core API

Encoded rail API

  • rns.encode(x)(r0, r1, r2, r3)
  • rns.decode(r0, r1, r2, r3)uint64[]
  • rns.add(*ea, *eb)
  • rns.sub(*ea, *eb)
  • rns.mul(*ea, *eb)
  • rns.div_(*ea, *eb)
  • rns.fma(*ea, *eb, *ec)
  • rns.op(*ea, *eb, code) where 0=add 1=mul 2=sub 3=div

Scalar-broadcast encoded API

These avoid materializing full constant arrays in Python:

  • rns.mul_u64(*ea, multiplier)
  • rns.fma_u64(*ea, multiplier, addend)
  • rns.affine_repeat_u64(*ea, multiplier, addend, iterations)

Raw fused uint64 API

These perform encode → exact op → decode in one native call:

  • rns.add_u64_io(x, addend)
  • rns.sub_u64_io(x, subtrahend)
  • rns.mul_u64_io(x, multiplier)
  • rns.fma_u64_io(x, multiplier, addend)
  • rns.affine_repeat_u64_io(x, multiplier, addend, iterations)

OpenMP variants:

  • rns.add_u64_io_omp(...)
  • rns.sub_u64_io_omp(...)
  • rns.mul_u64_io_omp(...)
  • rns.fma_u64_io_omp(...)
  • rns.affine_repeat_u64_io_omp(...)

Auto-dispatch variants:

  • rns.add_u64_auto(...)
  • rns.sub_u64_auto(...)
  • rns.mul_u64_auto(...)
  • rns.fma_u64_auto(...)
  • rns.affine_repeat_u64_auto(...)

High-level API

  • rns.Session
  • rns.SessionCache
  • rns.EncodedArray

Division constraint

Division requires the divisor to be invertible on all four rails:

  • b % 127 != 0
  • b % 8191 != 0
  • b must be odd (for mod 65536)
  • b % 524287 != 0

A safe sanitizer looks like this:

import numpy as np
import rns_engine as rns

M = int(rns.M)

def make_invertible_divisor(b):
    b = np.asarray(b, dtype=np.uint64) % np.uint64(M)
    b = np.where((b & np.uint64(1)) == 0, b + np.uint64(1), b)

    bad = (
        (b % np.uint64(127) == 0)
        | (b % np.uint64(8191) == 0)
        | (b % np.uint64(524287) == 0)
        | ((b & np.uint64(1)) == 0)
    )

    while np.any(bad):
        b = np.where(bad, (b + np.uint64(2)) % np.uint64(M), b)
        bad = (
            (b % np.uint64(127) == 0)
            | (b % np.uint64(8191) == 0)
            | (b % np.uint64(524287) == 0)
            | ((b & np.uint64(1)) == 0)
        )

    return b.astype(np.uint64)

Data model

  • input arrays to encode(...) are treated as uint64
  • values outside [0, M) are reduced mod M during encode
  • rails are returned as:
    • r0: uint16
    • r1: uint16
    • r2: uint16
    • r3: uint32
  • high-level EncodedArray objects store four read-only rails

Performance

Verified benchmark

Verified on Google Colab Linux x86_64, with:

  • AVX2 = True
  • omp_num_procs = 2
  • omp_max_threads = 2

Workloads were run against the installed wheel, not just an editable import.

Median throughput over 5 runs on 1,000,000 uint64 values:

Fused single-step affine

Workload: fma_u64_io(x, 1_000_003, 7)

  • fused fma_u64_io: 47.8 million values/sec
  • fma_u64_io_omp (1 thread): 80.7 million values/sec
  • fma_u64_io_omp (2 threads): 84.6 million values/sec

Repeated affine loop

Workload: affine_repeat_u64_io(x, 1_000_003, 7, iterations=1000)

  • affine_repeat_u64_io: 61.19 billion ops/sec
  • affine_repeat_u64_io_omp (1 thread): 82.80 billion ops/sec
  • affine_repeat_u64_io_omp (2 threads): 94.86 billion ops/sec

Verification status

  • correctness sanity checks passed
  • built wheel installed and executed successfully
  • full test suite passed: 49 / 49

Why RNS?

In a Residue Number System, addition and multiplication happen independently on each rail. There is no cross-rail carry propagation. That makes RNS attractive for:

  • exact modular arithmetic within a fixed dynamic range
  • SIMD-friendly kernels
  • repeated arithmetic pipelines where decode can be delayed
  • parallel execution

How it works

Encode

x -> (x mod 127, x mod 8191, x mod 65536, x mod 524287)

Operate

Each rail is processed independently.

Decode

Garner-style CRT reconstruction combines the four residues back into a uint64 value modulo M.

For the Mersenne moduli (127 = 2^7 - 1, 8191 = 2^13 - 1, 524287 = 2^19 - 1), the core uses fold-based reduction instead of general division.


Building from source

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

Requirements:

  • Python 3.10+
  • C++17 compiler
  • NumPy
  • pybind11

Introspection

import rns_engine as rns

rns.info()

rns.M
rns.M0
rns.M1
rns.M2
rns.M3
rns.HAS_AVX2

Current release

v0.4.0rc1

  • 4-rail engine (127 × 8191 × 65536 × 524287)
  • AVX2-accelerated encoded kernels
  • fused fma(...)
  • scalar-broadcast encoded APIs: mul_u64, fma_u64, affine_repeat_u64
  • fused raw uint64 APIs: *_u64_io
  • OpenMP fused raw APIs: *_u64_io_omp
  • auto-dispatch raw APIs: *_u64_auto
  • high-level Session, SessionCache, and EncodedArray

License

AGPL-3.0-only
Copyright 2026 Evan Wesley

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.4.0.tar.gz (34.2 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.4.0-cp312-cp312-win_amd64.whl (131.7 kB view details)

Uploaded CPython 3.12Windows x86-64

rns_engine-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

rns_engine-0.4.0-cp312-cp312-macosx_11_0_x86_64.whl (169.1 kB view details)

Uploaded CPython 3.12macOS 11.0+ x86-64

rns_engine-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (173.7 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

rns_engine-0.4.0-cp311-cp311-win_amd64.whl (129.7 kB view details)

Uploaded CPython 3.11Windows x86-64

rns_engine-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

rns_engine-0.4.0-cp311-cp311-macosx_11_0_x86_64.whl (166.4 kB view details)

Uploaded CPython 3.11macOS 11.0+ x86-64

rns_engine-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (172.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

rns_engine-0.4.0-cp310-cp310-win_amd64.whl (130.0 kB view details)

Uploaded CPython 3.10Windows x86-64

rns_engine-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

rns_engine-0.4.0-cp310-cp310-macosx_11_0_x86_64.whl (165.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

rns_engine-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (171.4 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: rns_engine-0.4.0.tar.gz
  • Upload date:
  • Size: 34.2 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.4.0.tar.gz
Algorithm Hash digest
SHA256 8e5a2d2d7f867640db6b0d9a23cf591a6c8914c63c95bf0caf66886e6d9c7d7b
MD5 b2bb02434204c2093bbd0cc8a217e355
BLAKE2b-256 0a86ae100c8b504deeb1b03dddb7a088eb50148c360eb3d8846a8886b7ce0b92

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.4.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 131.7 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.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8bd0a1e9a2f53c11b33d5af94ff1c1487cc006484f2a9012179c0458e231ace8
MD5 197e5733a34a3e1b88bb2f160d5ea5a0
BLAKE2b-256 2267dbb3df3664fb85007d410902758d9a27a2953d363d6a800f1fefbe907f98

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 93feb7bb025fd854609ec994aa2727cc867fb2859894d14c846a8690bf5a7f69
MD5 2f26efa3c7c7c89409daef84053541fa
BLAKE2b-256 5159eab51c97aeed231da707588b709ba0bfa6709264d63942b902d1361d51f3

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp312-cp312-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 27eca03e12c70dad8726424e2ece19fa2513071a734953b97192fc6ee184d2a2
MD5 880c41f7518ee8ada8321b9c230740e4
BLAKE2b-256 045507fa1b499125bb338590fa63a469db6125f4eddc3258d03b8ed956af253c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.0-cp312-cp312-macosx_11_0_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.4.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 61fa4e25db6bf1e3a6f14fd74a525fcde6615e0dc66a4ca19deb805b947d9c8c
MD5 4258214f20f65fe5f11dccc018fa2663
BLAKE2b-256 f87b616d5ab063558fd7ec395f6bb77167d948cb920ac69dc5f5f692a64d48e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 129.7 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.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e4f9e3e5c2263c3cc41efb491f914f757617b1f7e55e39fc2d5a9f1d4f1a429e
MD5 63582e9dabc32c27511e8c82903d0a48
BLAKE2b-256 569d664340ff22845aae506ce624556d80e763e88e85b3b8c758c1e5a86300e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca8a3416fad9352613e556bf44a25f38df9485c95435fb909986f2a0fc20c032
MD5 15f4bc1fbfbcf081a88ab7cf3b84a116
BLAKE2b-256 f944eac00de88aa8a6ab4bde135755aade213196394ba7d54e1aada7e7001bdb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp311-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 f41fd4c79ae21678bd7f8e59049b464e7d2188f53054fa65791806a91bd876a3
MD5 cffe3b47a556b5871ef7166fa7bb6574
BLAKE2b-256 71cbdcc3530a6f4c0521d1d30301c8e96c85d9e5cf8c7a5673037957d1c364ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.0-cp311-cp311-macosx_11_0_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.4.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d584196d4f175440ef1945394e7fd314618ca25e42d7f797174835df4ba4fe04
MD5 11aa03e045165d8b4de82c545f707fe9
BLAKE2b-256 84cd18006a560b7215ca3edb6fe05a2dcc6e070cf9040fb8d8a844078c8a87d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: rns_engine-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 130.0 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.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d50ce89f7905a3d549206122fc2e35ed919131c23530d8636d0a66faaa82df49
MD5 c441546d8bf86f56deb337139966651d
BLAKE2b-256 3d1f6a8f1eb941577547f1dbefa7079c1a1e2b490b92b375fbd7fd13128f2abe

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 28af70bf1bab024513436bf0718bd6a1bfe646e07505fc423748b88c40c7bb20
MD5 f723039eae78e4473ad8a73d0f4c5207
BLAKE2b-256 882ed92a71603db7c79242a4f0448f3ea005c32c68f9ebe4830e1596500a443c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.4.0-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 82d7fe82f063ae723b50b6b8487a029dbb957c46daf906dac69d5d391149f8e9
MD5 f58b48a1e23429c1d4f76168e0590c48
BLAKE2b-256 b60925428748bb160af199b2c2a40aebe7f19c61146604e15b7d6c709e4aff44

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.0-cp310-cp310-macosx_11_0_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.4.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rns_engine-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 170052989522c286138605b7c0b7bfbaff382aa97609b797841e6c5c131a5508
MD5 de3fba5462d191dc4f2da8e2a1a32551
BLAKE2b-256 f5e9c06eb13ec543470ab3bf442a8aff2aa45b244d856ed5f1935c8b24c343ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for rns_engine-0.4.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.

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