| Crates.io | cryptotensors |
| lib.rs | cryptotensors |
| version | 0.1.2 |
| created_at | 2026-01-04 11:07:17.924232+00 |
| updated_at | 2026-01-07 01:08:04.034273+00 |
| description | CryptoTensors is an extension of safetensors that adds encryption, signing, and access control (Rego-based policy engine) while maintaining full backward compatibility with the safetensors format. It provides functions to read and write safetensors which aim to be safer than their PyTorch counterpart. |
| homepage | https://github.com/aiyah-meloken/cryptotensors |
| repository | https://github.com/aiyah-meloken/cryptotensors |
| max_upload_size | |
| id | 2021731 |
| size | 262,543 |
This repository implements CryptoTensors, an LLM file format for secure model distribution. This implementation extends safetensors with encryption, signing, and access control capabilities while maintaining full backward compatibility with safetensors.
CryptoTensors provides:
This project is a derivative work based on safetensors by Hugging Face. See NOTICE for details.
This implementation is based on the idea of the following research paper: Zhu, H., Li, S., Li, Q., & Jin, Y. (2025). CryptoTensors: A Light-Weight Large Language Model File Format for Highly-Secure Model Distribution. arXiv:2512.04580.
You can install cryptotensors via the pip manager:
pip install cryptotensors
If you want to load encrypted CryptoTensors models without modifying your code, you can use the compatible package released on GitHub Releases:
# Uninstall the original safetensors package
pip uninstall safetensors
# Install the compatible package directly from GitHub release
# Replace {tag} with the release tag (e.g., v0.1.0)
pip install https://github.com/aiyah-meloken/cryptotensors/releases/download/{tag}/safetensors-0.7.0-py3-none-any.whl
# Example for v0.1.0:
# pip install https://github.com/aiyah-meloken/cryptotensors/releases/download/v0.1.0/safetensors-0.7.0-py3-none-any.whl
After installation, your existing code will transparently support both regular safetensors files and encrypted CryptoTensors files without any code changes. The compatible package uses the safetensors namespace but internally depends on cryptotensors, enabling seamless encryption support.
For the sources, you need Rust
# Install Rust
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Make sure it's up to date and using stable channel
rustup update
git clone https://github.com/aiyah-meloken/cryptotensors
cd cryptotensors/bindings/python
pip install setuptools_rust
pip install -e .
To encrypt tensors during serialization, you need to provide encryption and signing keys in the config parameter.
import torch
from cryptotensors.torch import save_file, load_file
tensors = {
"weight1": torch.zeros((1024, 1024)),
"weight2": torch.zeros((1024, 1024))
}
# Encrypt and save
config = {
"enc_key": enc_key, # JWK format encryption key
"sign_key": sign_key, # JWK format signing key
}
save_file(tensors, "model.cryptotensors", config=config)
# Load encrypted file (keys retrieved from key provider)
tensors = load_file("model.cryptotensors")
See the documentation for detailed guides on encryption, key management, and integration examples.
You can use cryptotensors as a drop-in replacement for safetensors in most cases, where you can save and load unencrypted models as usual.
import torch
from cryptotensors import safe_open
from cryptotensors.torch import save_file
tensors = {
"weight1": torch.zeros((1024, 1024)),
"weight2": torch.zeros((1024, 1024))
}
save_file(tensors, "model.safetensors")
tensors = {}
with safe_open("model.safetensors", framework="pt", device="cpu") as f:
for key in f.keys():
tensors[key] = f.get_tensor(key)
The file format is the same as the safetensors format, with the following additional fields:
N, an unsigned little-endian 64-bit integer, containing the size of the header{ character (0x7B).{"TENSOR_NAME": {"dtype": "F16", "shape": [1, 16, 256], "data_offsets": [BEGIN, END]}, "NEXT_TENSOR_NAME": {...}, ...},
data_offsets point to the tensor data relative to the beginning of the byte buffer (i.e. not an absolute position in the file),
with BEGIN as the starting offset and END as the one-past offset (so total tensor byte size = END - BEGIN).__metadata__ is allowed to contain free form string-to-string map. Arbitrary JSON is not allowed, all values must be strings.__metadata__ section:
__encryption__: JSON string containing per-tensor encryption information (algorithm, nonce, encrytped data encryption key, etc.)__crypto_keys__: JSON string containing key material information in the format {"version": "1", "enc": {...}, "sign": {...}}, where enc and sign are the metadata of the master decryption key and signing key respectively. No secrets are stored in this field, and the metadata is used to retrieve the keys from the key providers.__signature__: Base64-encoded Ed25519 signature of the file header (excluding the signature itself) for integrity verification__policy__: JSON string containing access control policy in Rego formatTwo stages of encryption: the entire header is encrypted using the master decryption key, and the tensor data is encrypted using the per-tensor encryption keys.
Lazy decryption: Even with encryption enabled, tensor data is decrypted on-demand when accessed, maintaining the benefits of lazy loading while ensuring security. This allows loading large encrypted models without decrypting all tensors upfront, preserving memory efficiency and supporting distributed settings where only specific tensors are needed.
Note: Unless otherwise specified, all other notes, features, and benefits of the cryptotensors format are the same as the safetensors format.
License: Apache-2.0