added C and Python bindings

This commit is contained in:
randogoth 2025-01-17 18:50:37 +02:00
parent dcc612fc98
commit f21804ffaf
6 changed files with 211 additions and 2 deletions

109
Cargo.lock generated
View file

@ -77,6 +77,18 @@ dependencies = [
"wasi",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indoc"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]]
name = "kolmogorov_smirnov"
version = "1.1.0"
@ -108,6 +120,15 @@ dependencies = [
"rawpointer",
]
[[package]]
name = "memoffset"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
dependencies = [
"autocfg",
]
[[package]]
name = "miniz_oxide"
version = "0.8.3"
@ -183,12 +204,19 @@ dependencies = [
"libm",
]
[[package]]
name = "once_cell"
version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "onod3000"
version = "0.1.0"
dependencies = [
"flate2",
"kolmogorov_smirnov",
"pyo3",
"statrs",
]
@ -198,6 +226,12 @@ version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
[[package]]
name = "portable-atomic"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6"
[[package]]
name = "ppv-lite86"
version = "0.2.20"
@ -216,6 +250,69 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "pyo3"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57fe09249128b3173d092de9523eaa75136bf7ba85e0d69eca241c7939c933cc"
dependencies = [
"cfg-if",
"indoc",
"libc",
"memoffset",
"once_cell",
"portable-atomic",
"pyo3-build-config",
"pyo3-ffi",
"pyo3-macros",
"unindent",
]
[[package]]
name = "pyo3-build-config"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd3927b5a78757a0d71aa9dff669f903b1eb64b54142a9bd9f757f8fde65fd7"
dependencies = [
"once_cell",
"target-lexicon",
]
[[package]]
name = "pyo3-ffi"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dab6bb2102bd8f991e7749f130a70d05dd557613e39ed2deeee8e9ca0c4d548d"
dependencies = [
"libc",
"pyo3-build-config",
]
[[package]]
name = "pyo3-macros"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91871864b353fd5ffcb3f91f2f703a22a9797c91b9ab497b1acac7b07ae509c7"
dependencies = [
"proc-macro2",
"pyo3-macros-backend",
"quote",
"syn",
]
[[package]]
name = "pyo3-macros-backend"
version = "0.23.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43abc3b80bc20f3facd86cd3c60beed58c3e2aa26213f3cda368de39c60a27e4"
dependencies = [
"heck",
"proc-macro2",
"pyo3-build-config",
"quote",
"syn",
]
[[package]]
name = "quote"
version = "1.0.38"
@ -363,6 +460,12 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "target-lexicon"
version = "0.12.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1"
[[package]]
name = "typenum"
version = "1.17.0"
@ -375,6 +478,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unindent"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7de7d73e1754487cb58364ee906a499937a0dfabd86bcb980fa99ec8c8fa2ce"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"

View file

@ -3,7 +3,15 @@ name = "onod3000"
version = "0.1.0"
edition = "2021"
[lib]
name = "onod3000"
crate-type = ["cdylib", "lib"]
[dependencies]
flate2 = "1.0.35"
kolmogorov_smirnov = "1.1.0"
pyo3 = {version = "0.23.4", optional = true}
statrs = "0.18.0"
[features]
python = ["dep:pyo3"]

View file

@ -62,7 +62,7 @@ fn main() -> io::Result<()> {
("Runs", Onod::runs(&input_data)),
("RunUps", Onod::run_ups(&input_data)),
("Prediction", Onod::prediction(&input_data)),
("UnCorrelation", Onod::un_correlation(&input_data)),
("UnCorrelation", Onod::uncorrelation(&input_data)),
];
for (test_name, (observation, z_score, p_value)) in &tests {

43
src/ffi.rs Normal file
View file

@ -0,0 +1,43 @@
use std::ffi::{c_char, c_double, c_uchar, CStr};
use crate::Onod;
#[no_mangle]
pub extern "C" fn onod_run(
test: *const c_char,
samples: *const c_uchar,
len: usize,
result: *mut c_double,
) -> bool {
// Check for null pointers
if test.is_null() || samples.is_null() || result.is_null() {
eprintln!("Error: Null pointer passed to onod_run.");
return false;
}
// Convert C string to Rust string
let test = unsafe {
match CStr::from_ptr(test).to_str() {
Ok(s) => s,
Err(_) => {
eprintln!("Error: Invalid UTF-8 string passed to onod_run.");
return false;
}
}
};
// Convert samples to a Rust slice
let samples = unsafe { std::slice::from_raw_parts(samples, len as usize) };
// Call the Rust `run` function
let (obs, z, p) = Onod::run(test, samples);
// Write results to the output buffer
unsafe {
*result.offset(0) = obs;
*result.offset(1) = z;
*result.offset(2) = p;
}
true
}

View file

@ -1,3 +1,33 @@
pub struct Onod;
mod uniformity;
pub mod ffi;
#[cfg(feature = "python")]
pub mod python;
impl Onod {
pub fn run(test: &str, samples: &[u8]) -> (f64, f64, f64) {
match test {
"avalanche" => Onod::avalanche(samples),
"chi_bit" => Onod::chi_bit(samples),
"chi_byte" => Onod::chi_byte(samples),
"compression" => Onod::compression(samples),
"gaps" => Onod::gaps(samples),
"ks" => Onod::ks(samples),
"mean_byte" => Onod::mean_byte(samples),
"monobit" => Onod::monobit(samples),
"pi" => Onod::pi(samples),
"prediction" => Onod::prediction(samples),
"runs" => Onod::runs(samples),
"run_ups" => Onod::run_ups(samples),
"shannon" => Onod::shannon(samples),
"shells" => Onod::shells(samples),
"uncorrelation" => Onod::uncorrelation(samples),
_ => {
eprintln!("Error: Unknown test '{}'", test);
(-1.0, 0.0, 0.0) // Default values for unknown test
}
}
}
}

19
src/python.rs Normal file
View file

@ -0,0 +1,19 @@
use pyo3::prelude::*;
use super::Onod as O;
#[pyclass]
struct Onod;
#[pymethods]
impl Onod {
#[staticmethod]
pub fn run(test: &str, samples: Vec<u8>) -> (f64, f64, f64) {
O::run(test, &samples)
}
}
#[pymodule]
fn onod(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Onod>()?;
Ok(())
}