triton-csprng
triton-csprng is a small PyTorch/Triton package for counter-based random
streams on CPU and NVIDIA GPUs. It provides ChaCha20-backed tensor generation
and sampling primitives for cryptography-adjacent, simulation, and FHE-style
workloads.
The package exposes low-level stream and sampling building blocks without depending on any downstream library's RNG API.
What is implemented
- ChaCha20 block generation with vectorized PyTorch CPU and Triton CUDA paths.
- Explicit key / nonce / counter stream state.
- Raw
uint32(...)andbytes(...)APIs returning ordinary PyTorch tensors. - Bounded integer sampling with scalar or per-channel bounds.
- Centered integer discrete Gaussian sampling from a 128-bit half-plane CDT.
- Stochastic rounding for CPU and CUDA floating tensors.
RnsRandomStreams, a convenience manager for RNS-like layouts with:- independent streams per device for non-repeated channels;
- repeated channels that reproduce the same values across devices;
- state-dict roundtrip for deterministic continuation.
- No C++/CUDA extension or
torch.opsregistration step.
Installation
For ordinary package use after a PyPI release:
python -m pip install triton-csprng
For development:
git clone git@github.com:visualDust/triton-csprng.git
cd triton-csprng
python -m pip install -e ".[dev]"
Runtime dependencies are PyTorch and mpmath for high-precision CDT
construction. CPU execution does not require Triton. CUDA execution additionally
requires Triton; install triton-csprng[cuda] after installing the PyTorch build
that matches the target CUDA stack.
Quick start
import torch
from triton_csprng import ChaCha20Rng
rng = ChaCha20Rng(
key=list(range(8)), # 8 little-endian uint32 words = 256 bits
nonce=[123, 456], # 2 little-endian uint32 words = 64 bits
counter=0,
device="cuda:0",
)
words = rng.uint32((1024,))
raw = rng.bytes((4096,))
mod_q = rng.randint([17, 257], (2, 1024))
gauss = rng.discrete_gaussian((4, 1024), sigma=3.2)
rounded = rng.stochastic_round(torch.randn(1024, device="cuda:0"))
Every result above is a normal PyTorch tensor. Use device="cpu" to select the
CPU implementation without changing the stream or sampling APIs.
Stream semantics
ChaCha20Rng is a deterministic counter-based stream:
from triton_csprng import ChaCha20Rng
rng1 = ChaCha20Rng(key=list(range(8)), nonce=[1, 2], counter=9)
rng2 = ChaCha20Rng(key=list(range(8)), nonce=[1, 2], counter=9)
assert torch.equal(rng1.uint32((3, 7)), rng2.uint32((3, 7)))
The stream buffers unused bytes internally, so chunked reads are equivalent to a single larger read:
one_shot = ChaCha20Rng(key=list(range(8)), nonce=[5, 6])
chunked = ChaCha20Rng(key=list(range(8)), nonce=[5, 6])
expected = one_shot.uint32(40)
got = torch.cat([chunked.uint32(17), chunked.uint32(23)])
assert torch.equal(got, expected)
Stream state can be checkpointed:
state = rng1.state_dict()
restored = ChaCha20Rng.from_state_dict(state)
Sampling APIs
Bounded integers
x = rng.randint(17, (4, 1024))
y = rng.randint([17, 257, 65537], (3, 1024))
The output dtype is torch.int64; bounds therefore must fit in signed int64.
Multiple bounds are interpreted as leading channels. Internally the sampler
consumes a 128-bit ChaCha word U and returns floor(bound * U / 2**128). This
has no retry or fallback branch. Over the finite 128-bit source domain, output
bucket counts differ by at most one, so the statistical distance from the ideal
uniform distribution is bounded by roughly bound / 2**128.
Discrete Gaussian
e = rng.discrete_gaussian((8, 32768), sigma=3.2)
The sampler builds a 128-bit half-plane cumulative distribution table using
mpmath precision 2 * security_bits, chooses
num_sampling_points = 2**ceil(log2(6*sigma)), reserves one random bit for the
sign, and folds the remaining truncated tail into the last bucket. This keeps the
CUDA path compact and constant-shape while making the finite table construction
explicit and testable.
Stochastic rounding
rounded = rng.stochastic_round(values)
values must be a CUDA floating tensor on the same device as the RNG stream.
The result is torch.int64 with Bernoulli rounding by the fractional part of
abs(values), then sign restoration.
RNS-style stream manager
RnsRandomStreams helps express layouts where each configured CPU or CUDA
device gets independent non-repeated channels, while repeated channels are
generated from matching streams on every device.
from triton_csprng import RnsRandomStreams
streams = RnsRandomStreams(
num_coeffs=32768,
channel_counts=[8, 8],
repeated_channels=2,
devices=["cuda:0", "cuda:1"],
key=list(range(8)),
nonce=[1, 2],
)
u32 = streams.uint32_channels()
gauss = streams.discrete_gaussian_channels(sigma=3.2)
ints = streams.randint_channels([
[17] * 8 + [257] * 2,
[19] * 8 + [257] * 2,
])
For each returned list item:
shape = [non_repeated_channels + repeated_channels, num_coeffs]
The repeated tail channels are reproducible across devices when their bounds and distribution parameters match.
Why there is no torch op wrapper
A Triton kernel can be launched directly with PyTorch CUDA tensors:
out = torch.empty_like(x)
_kernel[grid](x, out, ...)
CUDA paths launch Triton kernels this way; CPU paths use ordinary PyTorch tensor
operations. A torch.ops.* custom op is unnecessary for normal Python/PyTorch
integration and would reintroduce dispatcher/wrapper maintenance. A
torch.library wrapper can still be added later if a downstream project needs
formal fake-tensor or torch.compile dispatcher integration.
Developer checks
Install the optional development tools and pre-commit hooks:
python -m pip install -e ".[dev]"
pre-commit install
Run the same checks manually:
python -m ruff check .
python -m ruff format --check .
python -m pytest tests -q
Validation
Current local validation:
python -m ruff check .
python -m ruff format --check .
python -m pre_commit run --all-files
python -m pytest tests -q
The tests cover:
- ChaCha20 CPU and Triton output against a Python reference implementation;
- non-multiple block counts;
- stream determinism and chunking behavior;
- state-dict restore;
- bounded integer range and multiply-high mapping checks;
- difficult bounds and distribution sanity;
- half-plane CDT table shape/range checks;
- rough discrete Gaussian moments and symmetry;
- stochastic-rounding determinism and integer cases;
- exact seeded CPU/CUDA stream and sampling parity;
- CPU RNS state restore and repeated-channel equality across two GPUs.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file triton_csprng-0.1.4.tar.gz.
File metadata
- Download URL: triton_csprng-0.1.4.tar.gz
- Upload date:
- Size: 21.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d6c4436d7feedef71a826f9df6b4ba7331a1cc02fd927ef95d8309e536768d3
|
|
| MD5 |
0c3d8594ea602d3bbe1584f2b338a991
|
|
| BLAKE2b-256 |
1920dfc4136c57860ec8cf28c94e9777e55a71bbe565892d3cf23a43bc762d04
|
Provenance
The following attestation bundles were made for triton_csprng-0.1.4.tar.gz:
Publisher:
build-and-publish-pypi.yaml on visualDust/triton-csprng
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
triton_csprng-0.1.4.tar.gz -
Subject digest:
2d6c4436d7feedef71a826f9df6b4ba7331a1cc02fd927ef95d8309e536768d3 - Sigstore transparency entry: 2439190248
- Sigstore integration time:
-
Permalink:
visualDust/triton-csprng@6a8b20deae94baf8a19fbff76b92c1928ac6c3d4 -
Branch / Tag:
refs/tags/0.1.4 - Owner: https://github.com/visualDust
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish-pypi.yaml@6a8b20deae94baf8a19fbff76b92c1928ac6c3d4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file triton_csprng-0.1.4-py3-none-any.whl.
File metadata
- Download URL: triton_csprng-0.1.4-py3-none-any.whl
- Upload date:
- Size: 19.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d9f7d84188bb05c861dc76f11f6940b858ef7279b71b25dda9d48eae375a137
|
|
| MD5 |
4737adcfefaadfc5851b1ca58cb9f27a
|
|
| BLAKE2b-256 |
cc5620bb4e6ab5f9a26d9c6e954300721fd87dbace91bfdddc02c0deb1846d24
|
Provenance
The following attestation bundles were made for triton_csprng-0.1.4-py3-none-any.whl:
Publisher:
build-and-publish-pypi.yaml on visualDust/triton-csprng
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
triton_csprng-0.1.4-py3-none-any.whl -
Subject digest:
0d9f7d84188bb05c861dc76f11f6940b858ef7279b71b25dda9d48eae375a137 - Sigstore transparency entry: 2439190280
- Sigstore integration time:
-
Permalink:
visualDust/triton-csprng@6a8b20deae94baf8a19fbff76b92c1928ac6c3d4 -
Branch / Tag:
refs/tags/0.1.4 - Owner: https://github.com/visualDust
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
build-and-publish-pypi.yaml@6a8b20deae94baf8a19fbff76b92c1928ac6c3d4 -
Trigger Event:
release
-
Statement type: