Skip to main content

cryptotensors/
lib.rs

1// MODIFICATION: This file has been modified from the original safetensors project.
2// Added module exports for CryptoTensors encryption functionality.
3// See NOTICE file for details.
4//
5// TODO(no_std): CryptoTensors encryption modules currently require std due to:
6// - registry.rs: std::sync::RwLock, std::fs::File, libloading for dynamic providers
7// - policy.rs: regorus (Rego engine) may require std
8// - Various modules use std::collections::HashMap, std::sync::Arc
9// Future work: Extract pure crypto functions (encrypt/decrypt/sign/verify) to support no_std,
10// requiring users to pass keys directly instead of using the registry system.
11
12#![deny(missing_docs)]
13#![doc = include_str!("../README.md")]
14#![cfg_attr(not(feature = "std"), no_std)]
15pub mod slice;
16pub mod tensor;
17
18// CryptoTensors: Encryption and signing modules
19/// CryptoTensors module - encryption/decryption manager
20pub mod cryptotensors;
21/// Encryption/decryption functions (AES-GCM, ChaCha20-Poly1305)
22pub mod encryption;
23/// Key management (JWK format)
24pub mod key;
25/// Access policy engine for model loading validation (Rego)
26pub mod policy;
27/// Pluggable KeyProvider registry for external key sources
28pub mod registry;
29/// Signing/verification functions (Ed25519)
30pub mod signing;
31
32/// serialize_to_file and rewrap functions only valid in std
33#[cfg(feature = "std")]
34pub use tensor::{rewrap, rewrap_file, rewrap_header, serialize_to_file};
35pub use tensor::{serialize, Dtype, Metadata, SafeTensorError, SafeTensors, View};
36
37// CryptoTensors: Re-export key types
38pub use cryptotensors::{
39    CryptoTensors, CryptoTensorsError, DeserializeCryptoConfig, SerializeCryptoConfig,
40};
41pub use key::KeyMaterial;
42pub use policy::AccessPolicy;
43pub use registry::{
44    disable_provider, enable_provider, get_master_key, get_signing_key, get_verify_key,
45    load_provider_native, register_provider, register_provider_with_priority, DirectKeyProvider,
46    KeyProvider, PRIORITY_DIRECT, PRIORITY_ENV, PRIORITY_FILE, PRIORITY_NATIVE, PRIORITY_TEMP,
47};
48
49#[cfg(feature = "provider-env")]
50pub use registry::EnvKeyProvider;
51#[cfg(feature = "provider-file")]
52pub use registry::FileKeyProvider;
53
54#[cfg(not(feature = "std"))]
55#[macro_use]
56extern crate alloc;
57
58/// A facade around all the types we need from the `std`, `core`, and `alloc`
59/// crates. This avoids elaborate import wrangling having to happen in every
60/// module.
61mod lib {
62    #[cfg(not(feature = "std"))]
63    mod no_stds {
64        pub use alloc::borrow::Cow;
65        pub use alloc::string::{String, ToString};
66        pub use alloc::vec::Vec;
67        pub use hashbrown::HashMap;
68    }
69    #[cfg(feature = "std")]
70    mod stds {
71        pub use std::borrow::Cow;
72        pub use std::collections::HashMap;
73        pub use std::string::{String, ToString};
74        pub use std::vec::Vec;
75    }
76    /// choose std or no_std to export by feature flag
77    #[cfg(not(feature = "std"))]
78    pub use no_stds::*;
79    #[cfg(feature = "std")]
80    pub use stds::*;
81}