Initial work. Added some tests for big nums in rust and OpenCL.

This commit is contained in:
Kirill Shakirov
2026-01-11 07:03:09 +01:00
parent 8edbc09493
commit 194f4e734b
16 changed files with 2410 additions and 0 deletions
+260
View File
@@ -0,0 +1,260 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "cl-sys"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4febd824a957638c066180fbf72b2bed5bcee33740773f3dc59fe91f0a3e6595"
dependencies = [
"libc",
]
[[package]]
name = "crossbeam"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1137cd7e7fc0fb5d3c5a8678be38ec56e819125d8d7907411fe24ccb943faca8"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-queue"
version = "0.3.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "enum_primitive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4551092f4d519593039259a9ed8daedf0da12e5109c5280338073eaeb81180"
dependencies = [
"num-traits 0.1.43",
]
[[package]]
name = "futures"
version = "0.1.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a471a38ef8ed83cd6e40aa59c1ffe17db6855c18e3604d9c4ed8c08ebc28678"
[[package]]
name = "libc"
version = "0.2.178"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
[[package]]
name = "nodrop"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "num-complex"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
dependencies = [
"num-traits 0.2.19",
]
[[package]]
name = "num-traits"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
dependencies = [
"num-traits 0.2.19",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "nyash_client"
version = "0.1.0"
dependencies = [
"ocl",
]
[[package]]
name = "ocl"
version = "0.19.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4297afb442d411793e4e24ee5a2977d15b6c95c743418f1c0ce0a2397d7ec8a3"
dependencies = [
"futures",
"nodrop",
"num-traits 0.2.19",
"ocl-core",
"qutex",
"thiserror",
]
[[package]]
name = "ocl-core"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c145dd9f205b86611a5df15eb89517417b03005441cf6cec245c65a4b9248c52"
dependencies = [
"bitflags",
"cl-sys",
"enum_primitive",
"num-complex",
"num-traits 0.2.19",
"ocl-core-vector",
"rustc_version",
"thiserror",
]
[[package]]
name = "ocl-core-vector"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f562279e046ca160aeed5eaf6f7c4eb9fa56cb8fd9d038dbdbf56225caeb8074"
dependencies = [
"num-traits 0.2.19",
]
[[package]]
name = "proc-macro2"
version = "1.0.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
dependencies = [
"proc-macro2",
]
[[package]]
name = "qutex"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11778238e7d8b0e3ca62033fdc69e01ef5cdb08809cdc2398b2ce5ec873a1757"
dependencies = [
"crossbeam",
"futures",
]
[[package]]
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d767eb0aabc880b29956c35734170f26ed551a859dbd361d140cdbeca61ab1e2"
[[package]]
name = "syn"
version = "2.0.111"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "nyash_client"
version = "0.1.0"
edition = "2024"
[dependencies]
ocl = "0.19"
+18
View File
@@ -0,0 +1,18 @@
def main():
start_num = 0x8adb7b7e8a722df091ecea988a4ad2234836636a102ceb688b3985f89bf40002
num1 = start_num+1
num2 = start_num+312
print(start_num.to_bytes(32).hex())
print(num1.to_bytes(32).hex())
print(num2.to_bytes(32).hex())
if __name__ == '__main__':
main()
+255
View File
@@ -0,0 +1,255 @@
extern crate ocl;
use ocl::{
Buffer, Context, Device, DeviceType, Kernel, Platform, Program, Queue, SpatialDims, flags,
};
use std::io;
mod num_utils;
/// Exploded version. Boom!
///
/// The functions above use `ProQue` and other abstractions to greatly reduce
/// the amount of boilerplate and configuration necessary to do basic work.
/// Many tasks, however, will require more configuration and will necessitate
/// doing away with `ProQue` altogether. Enqueuing kernels and reading/writing
/// from buffers and images usually requires a more explicit interface.
///
/// The following function performs the exact same steps that the above
/// functions did, with many of the convenience abstractions peeled away.
///
/// See the function below this to take things a step deeper...
///
// trait FromStr {
// fn from_str(&self);
// }
// // Define a trait with a constructor method
// trait NewFile {
// fn new<P: AsRef<Path>>(path: P) -> std::io::Result<Self> where Self: Sized;
// }
// impl Foo for ocl::flags::DeviceType {
// fn foo(&self) {
// println!("foo");
// }
// }
fn dev_type_from_str(s: &str) -> Result<flags::DeviceType, ()> {
match s {
"CPU" => Ok(flags::DeviceType::CPU),
"GPU" => Ok(flags::DeviceType::GPU),
"ALL" => Ok(flags::DeviceType::ALL),
"CUSTOM" => Ok(flags::DeviceType::CUSTOM),
"ACCELERATOR" => Ok(flags::DeviceType::ACCELERATOR),
"DEFAULT" => Ok(flags::DeviceType::DEFAULT),
_ => Err(()),
}
}
fn str_or_empty(r: ocl::error::Result<String>) -> String {
match r {
Ok(s) => s,
Err(_) => "".to_string(),
}
}
fn print_devices(dev_list: &Vec<(Device, Platform)>) {
let mut i = 0;
for (dev, plt) in dev_list.iter() {
let dev_name = str_or_empty(dev.name());
let plt_name = str_or_empty(plt.name());
println!("({i}) device: \"{dev_name}\" ----- platorm: \"{plt_name}\"");
i += 1;
}
}
fn choose_devices(devices_num: usize) -> Result<Vec<usize>, String> {
println!("Please input desired devices to use as a white space separated list of numbers.");
let mut result: Vec<usize> = Vec::new();
let mut s_devs_nums = String::new();
io::stdin()
.read_line(&mut s_devs_nums)
.expect("Failed to read line");
for s_dev_num in s_devs_nums.split(' ') {
let dev_num: usize = match s_dev_num.trim().parse() {
Ok(num) => num,
Err(_) => return Err("You must input a number from device list.".to_string()),
};
if dev_num >= devices_num {
return Err("You must input a number from device list.".to_string());
};
result.push(dev_num);
}
return Ok(result);
}
fn list_devices(dev_type: DeviceType) -> Vec<(Device, Platform)> {
let platforms = Platform::list();
let mut devices: Vec<(Device, Platform)> = Vec::new();
for plt in platforms.iter() {
//let plat_name = str_or_empty(plt.name());
let list_res = Device::list(plt, Some(dev_type));
match list_res {
Ok(dev_l) => devices.extend(dev_l.iter().map(|dev| (*dev, plt.clone()))),
Err(_) => {}
}
}
return devices;
}
struct CtxBuffers {
start_key: Buffer<u32>,
u_data: Buffer<u32>,
enc_data: Buffer<u32>,
key_found: Buffer<u32>,
}
struct DevConfig {
train_work_size: bool,
global_work_size: SpatialDims,
}
struct ExecContext {
cfg: DevConfig,
ctx: Context,
kernel: Kernel,
prog: Program,
queue: Queue,
buffers: CtxBuffers,
}
fn init_devices(
devices: Vec<(Device, Platform, DevConfig)>,
kern_name: String,
prog_src: String,
inc_dirs: Vec<String>,
) -> Vec<ExecContext> {
let mut contexts: Vec<ExecContext> = Vec::with_capacity(devices.len());
for (dev, plt, dev_cfg) in devices {
let ctx = match Context::builder()
.platform(plt)
.devices(dev.clone())
.build()
{
Ok(c) => c,
Err(_) => continue,
};
let prg = match Program::builder().devices(dev).src(&prog_src).build(&ctx) {
Ok(p) => p,
Err(_) => continue,
};
let queue = match Queue::new(&ctx, dev, None) {
Ok(q) => q,
Err(_) => continue,
};
// Create Buffers:
let start_key_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(8)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let u_data_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(4)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let enc_data_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(4)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
let key_found_b = match Buffer::<u32>::builder()
.queue(queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(1)
.fill_val(0u32)
.build()
{
Ok(buf) => buf,
Err(_) => continue,
};
// (3) Create a kernel with arguments matching those in the source above:
let kernel = match Kernel::builder()
.program(&prg)
.name(&kern_name)
.queue(queue.clone())
.global_work_size(dev_cfg.global_work_size)
.arg(&start_key_b)
.arg(&u_data_b)
.arg(&enc_data_b)
.arg(&key_found_b)
.build() {
Ok(kern) => kern,
Err(_) => continue,
};
contexts.push(ExecContext {
cfg: dev_cfg,
ctx: ctx,
kernel: kernel,
prog: prg,
queue: queue,
buffers: CtxBuffers {
start_key: start_key_b,
u_data: u_data_b,
enc_data: enc_data_b,
key_found: key_found_b,
},
});
}
return contexts;
}
fn main() {
println!("Hello, world nya!");
//use ocl::{Buffer, Context, Device, Kernel, Platform, Program, Queue, flags};
let dev_type = dev_type_from_str("GPU").expect("pur");
// Get devices to be used for key search
let mut all_devices: Vec<(Device, Platform)> = list_devices(dev_type);
let devs_nums = loop {
print_devices(&all_devices);
match choose_devices(all_devices.len()) {
Ok(value) => break value,
Err(exc) => {
println!("Error! {exc}\n")
}
}
};
let devices: Vec<(Device, Platform)> = devs_nums.iter().map(|&i| all_devices[i]).collect();
all_devices.clear();
println!("{:?}", devices);
// let devices: Vec<_> = platforms.iter().flat_map(|p| Device::list(p, Some(dev_type)).iter()).collect();
// let device = Device::first(platform)?;
// let context = Context::builder()
// .platform(platform)
// .devices(device.clone())
// .build()?;
}
+267
View File
@@ -0,0 +1,267 @@
// union U64orU32 {
// l: u64,
// i: [u32; 2],
// }
fn add_u32_to_u256(a: &[u32; 8], b: u32) -> ([u32; 8], bool) {
let mut res: [u32; 8] = [0; 8];
let mut carry = false;
(res[0], carry) = a[0].carrying_add(b, carry);
for idx in 1..8 {
(res[idx], carry) = a[idx].carrying_add(0, carry);
}
return (res, carry);
}
fn add_u32_to_u256_(a: &mut [u32; 8], b: u32) -> bool {
let mut carry = false;
(a[0], carry) = a[0].carrying_add(b, carry);
for idx in 1..8 {
(a[idx], carry) = a[idx].carrying_add(0, carry);
}
return carry;
}
fn bytes_from_chars(chars_chunk: &[char]) -> [u8; 4] {
let mut res: [u8; 4] = [0; 4];
let mut idx: usize = 0;
chars_chunk.chunks_exact(2).for_each(|b_c| {
if idx < 4 {
match u8::from_str_radix(&b_c.iter().collect::<String>(), 16) {
Ok(n) => res[idx] = n,
Err(_) => (),
}
idx += 1;
}
});
return res;
}
fn bignum_from_hex(hex: &str) -> [u32; 8] {
let mut res: [u32; 8] = [0; 8];
let mut idx: usize = 0;
let chars_hex = hex.chars().collect::<Vec<char>>();
chars_hex
.chunks_exact(8)
.rev()
.map(|chunk| bytes_from_chars(chunk))
.for_each(|b_arr| {
if idx < 8 {
res[idx] = u32::from_be_bytes(b_arr);
idx += 1;
}
});
return res;
}
fn hex_fmt_byte(n: u32) -> String {
let res: String = n
.to_be_bytes()
.iter()
.map(|b| format!("{:02x}", b))
.collect();
return res;
}
fn bignum_to_hex(a: &[u32; 8]) -> String {
let res: String = a
.iter()
.rev()
.map(|n| hex_fmt_byte(*n))
.collect::<Vec<String>>()
.join("");
return res;
}
#[cfg(test)]
mod num_utils_tests {
use std::io::Read;
use super::*;
#[test]
fn test_add() {
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
let t0 = bignum_from_hex(test_data_line[1]);
let t1_test = add_u32_to_u256(&t0, 1).0;
let t2_test = add_u32_to_u256(&t0, num_to_add).0;
let res_actual = format!(
"{} {} {} {}",
num_to_add,
bignum_to_hex(&t0),
bignum_to_hex(&t1_test),
bignum_to_hex(&t2_test)
);
assert_eq!(test_line, res_actual);
}
let _ = child.wait().unwrap();
}
#[test]
fn test_cl_add() {
extern crate ocl;
use ocl::{
Buffer, Context, Device, DeviceType, Kernel, Platform, Program, Queue, SpatialDims,
flags,
};
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let cl_test_path = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl/test_num_utils.cl";
let cl_include_opt =
"-I /home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/open_cl";
let mut cl_src = String::new();
// read ocl source
BufReader::new(File::open(cl_test_path).unwrap()).read_to_string(&mut cl_src);
const G_WORK_SIZE: usize = 4096;
let cl_platform = Platform::default();
let cl_device = Device::first(cl_platform).unwrap();
let cl_context = Context::builder()
.platform(cl_platform)
.devices(cl_device.clone())
.build()
.unwrap();
let cl_program = Program::builder()
.devices(cl_device)
.src(cl_src)
.cmplr_opt(cl_include_opt)
.build(&cl_context)
.unwrap();
let cl_queue = Queue::new(&cl_context, cl_device, None).unwrap();
let cl_buffer_num = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(G_WORK_SIZE)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t0 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_READ_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t1 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
let cl_buffer_t2 = Buffer::<u32>::builder()
.queue(cl_queue.clone())
.flags(flags::MEM_WRITE_ONLY)
.len(G_WORK_SIZE * 8)
.fill_val(0u32)
.build()
.unwrap();
// (3) Create a kernel with arguments matching those in the source above:
let kernel = Kernel::builder()
.program(&cl_program)
.name("test_add")
.queue(cl_queue.clone())
.global_work_size(G_WORK_SIZE)
.arg(&cl_buffer_num)
.arg(&cl_buffer_t0)
.arg(&cl_buffer_t1)
.arg(&cl_buffer_t2)
.build()
.unwrap();
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
let mut buffer_num: Vec<u32> = vec![0u32; G_WORK_SIZE];
let mut buffer_t0: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut exp_buffer_t1: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut exp_buffer_t2: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut act_buffer_t1: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut act_buffer_t2: Vec<u32> = vec![0u32; G_WORK_SIZE*8];
let mut w_id: usize = 0;
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
buffer_num[w_id] = num_to_add;
let slise_id = w_id*8;
buffer_t0[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[1]));
exp_buffer_t1[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[2]));
exp_buffer_t2[slise_id..slise_id+8].copy_from_slice(&bignum_from_hex(test_data_line[3]));
w_id += 1;
if w_id >= G_WORK_SIZE {
w_id = 0; // reset counter
cl_buffer_num.cmd().queue(&cl_queue).offset(0).write(&buffer_num).enq().unwrap();
cl_buffer_t0.cmd().queue(&cl_queue).offset(0).write(&buffer_t0).enq().unwrap();
// (4) Run the kernel
unsafe {
kernel
.cmd()
.queue(&cl_queue)
.global_work_size(G_WORK_SIZE)
.enq().unwrap();
}
cl_buffer_t1.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t1).enq().unwrap();
cl_buffer_t2.cmd().queue(&cl_queue).offset(0).read(&mut act_buffer_t2).enq().unwrap();
assert_eq!(exp_buffer_t1, act_buffer_t1);
assert_eq!(exp_buffer_t2, act_buffer_t2);
}
}
let _ = child.wait().unwrap();
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,29 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
inline void aes128_InvertKey (uint *ks);
inline void aes128_ExpandKey (uint *ks, const uint *ukey);
inline void aes128_set_encrypt_key (uint *ks, const uint *ukey);
inline void aes128_set_decrypt_key (uint *ks, const uint *ukey);
inline void aes128_encrypt (const uint *ks, const uint *in, uint *out);
inline void aes128_decrypt (const uint *ks, const uint *in, uint *out);
inline void xts_mul2 (uint *in, uint *out);
inline void aes_xts256_gen_tweak (const uint *ks, const uint *sec_n, const uint block_n, uint *out);
inline void aes_xts256_enc_block (const uint *ks, const uint *T, const uint *in, uint *out);
inline void aes_xts256_dec_block (const uint *ks, const uint *T, const uint *in, uint *out);
+174
View File
@@ -0,0 +1,174 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
typedef struct __attribute__((aligned(32))) {
uint carry;
uint carry4;
} bigintRes;
typedef union {
ulong l;
uint i[2];
} __attribute__((aligned(32))) ul_ui_union;
inline uint add_one_to_bigint4_(uint *_n)
{
ul_ui_union t;
t.l = (ulong)_n[0] + 1ul;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
return t.i[1];
}
inline uint add_uint_to_bigint4_ (uint* _n, const uint b)
{
ul_ui_union t;
t.l = (ulong)_n[0] + (ulong)b;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
return t.i[1];
}
inline bigintRes add_one_to_bigint8(const uint *n, uint *out)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)n[0] + 1ul;
out[0] = t.i[0];
t.l = (ulong)n[1] + (ulong)t.i[1];
out[1] = t.i[0];
t.l = (ulong)n[2] + (ulong)t.i[1];
out[2] = t.i[0];
t.l = (ulong)n[3] + (ulong)t.i[1];
out[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)n[4] + (ulong)t.i[1];
out[4] = t.i[0];
t.l = (ulong)n[5] + (ulong)t.i[1];
out[5] = t.i[0];
t.l = (ulong)n[6] + (ulong)t.i[1];
out[6] = t.i[0];
t.l = (ulong)n[7] + (ulong)t.i[1];
out[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_one_to_bigint8_(uint *_n)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)_n[0] + 1ul;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)_n[4] + (ulong)t.i[1];
_n[4] = t.i[0];
t.l = (ulong)_n[5] + (ulong)t.i[1];
_n[5] = t.i[0];
t.l = (ulong)_n[6] + (ulong)t.i[1];
_n[6] = t.i[0];
t.l = (ulong)_n[7] + (ulong)t.i[1];
_n[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_uint_to_bigint8 (const uint *n, const uint b, uint *out)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)n[0] + (ulong)b;
out[0] = t.i[0];
t.l = (ulong)n[1] + (ulong)t.i[1];
out[1] = t.i[0];
t.l = (ulong)n[2] + (ulong)t.i[1];
out[2] = t.i[0];
t.l = (ulong)n[3] + (ulong)t.i[1];
out[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)n[4] + (ulong)t.i[1];
out[4] = t.i[0];
t.l = (ulong)n[5] + (ulong)t.i[1];
out[5] = t.i[0];
t.l = (ulong)n[6] + (ulong)t.i[1];
out[6] = t.i[0];
t.l = (ulong)n[7] + (ulong)t.i[1];
out[7] = t.i[0];
res.carry = t.i[1];
return res;
}
inline bigintRes add_uint_to_bigint8_ (uint* _n, const uint b)
{
bigintRes res;
ul_ui_union t;
t.l = (ulong)_n[0] + (ulong)b;
_n[0] = t.i[0];
t.l = (ulong)_n[1] + (ulong)t.i[1];
_n[1] = t.i[0];
t.l = (ulong)_n[2] + (ulong)t.i[1];
_n[2] = t.i[0];
t.l = (ulong)_n[3] + (ulong)t.i[1];
_n[3] = t.i[0];
res.carry4 = t.i[1];
t.l = (ulong)_n[4] + (ulong)t.i[1];
_n[4] = t.i[0];
t.l = (ulong)_n[5] + (ulong)t.i[1];
_n[5] = t.i[0];
t.l = (ulong)_n[6] + (ulong)t.i[1];
_n[6] = t.i[0];
t.l = (ulong)_n[7] + (ulong)t.i[1];
_n[7] = t.i[0];
res.carry = t.i[1];
return res;
}
@@ -0,0 +1,101 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "aes256_xts_plain.cl"
#include "num_utils.cl"
// batch_size uint - batch size
// Ti - sector index ulong
// Tj - encryption block number (16 bytes blocks)
// Tk - tweak key uint[4]
// s_Dk - start of data key uint[4]
// t_e_d - target_enc_data uint[4]
// u_d - unencrypted data to be encrypted uint[4]
// g_key_found uint[9] - 0 element - flag that sets to 1 if key found.
// Other 8 elements is found key
__kernel void search_key_test(const uint batch_size, const ulong g_Ti, const uint g_Tj,
__global const uint8* g_start_enc_key,
__global const uint4* g_uenc_data,
__global const uint4* g_target_data,
__global uint* g_key_found)
{
const uint g_id = get_global_id(0);
uint enc_key[8];
uint tweak[4];
uint uenc_data[4];
uint4 target_data = *g_target_data;
uint4 enc_data = (uint4)(0);
uint d_ks[44]; // data expanded key
uint t_ks[44]; // tweak expanded key
uint sec_n[4] = {0};
sec_n[0] = ((uint*)&g_Ti)[0];
sec_n[1] = ((uint*)&g_Ti)[1];
uint Tj = g_Tj;
vstore4(*g_uenc_data, 0, uenc_data);
vstore8(*g_start_enc_key, 0, enc_key);
// Set initial start key for every work thread
uint k_data_carry = add_uint_to_bigint4_ (enc_key, (g_id*batch_size));
uint k_tweak_carry = add_one_to_bigint4_ (&enc_key[4]);
if (k_tweak_carry != 0u) return; // if reached max key value exit thread
// Generate tweak
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
for (uint batch_id = 0u; (batch_id < batch_size); batch_id++)
{
// Data encrypt key always changing because we increment from 0 index to 8
aes128_set_encrypt_key (d_ks, enc_key);
// encrypt data
aes_xts256_enc_block (d_ks, tweak, uenc_data, (uint*)&enc_data);
// check if we found the key!
if (all(enc_data==target_data))
{
g_key_found[0] = 1;
vstore8(vload8(0, enc_key), 0, &g_key_found[1]);
return;
}
// Increment data key part by 1.
k_data_carry = add_one_to_bigint4_ (enc_key);
// Tweak changes only once in 2^128 times
if (k_data_carry != 0u) {
// Increment tweak part
k_tweak_carry = add_one_to_bigint4_ (&enc_key[4]);
if (k_tweak_carry != 0u) return; // if reached max key value exit thread
// Gen new tweak
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
}
}
}
@@ -0,0 +1,98 @@
// aes-xts-pur64 is OpenCL code for aes-xts256-plain64 encryption compatible with LUKS
//
// Copyright (C) 2025 Kirill Shakirov
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "aes256_xts_plain.cl"
__kernel void encrypt_data(__global const ulong* g_Ti, __global const uint* g_Tj,
__global const uint8* g_key,
__global const uint4* g_u_data,
__global uint* g_enc_data)
{
const size_t g_id = get_global_id(0);
uint d_ks[44];
uint t_ks[44];
uint tweak[4];
uint enc_key[8];
uint u_data[4];
uint enc_data[4] = { 0 };
uint sec_n[4];
ulong Ti = g_Ti[g_id];
sec_n[0] = ((uint*)&Ti)[0];
sec_n[1] = ((uint*)&Ti)[1];
sec_n[2] = 0;
sec_n[3] = 0;
uint Tj = g_Tj[g_id];
vstore8(*g_key, 0, enc_key);
vstore4(g_u_data[g_id], 0, u_data);
// printf("Ti: %lu\\n", Ti);
// printf("Tj: %u\\n", Tj);
// printf("enc_key: %v8u\\n", *(uint8*)enc_key);
// printf("uenc_data: %v4u\\n", *(uint4*)uenc_data);
//calculate tweak value
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
// encrypt data
aes128_set_encrypt_key (d_ks, enc_key);
aes_xts256_enc_block (d_ks, tweak, u_data, enc_data);
// printf("enc_data: %v4u\\n", *(uint4*)enc_data);
vstore4(*(uint4*)enc_data, g_id, g_enc_data);
}
__kernel void decrypt_data(__global const ulong* g_Ti, __global const uint* g_Tj,
__global const uint8* g_key,
__global const uint4* g_enc_data,
__global uint* g_u_data)
{
const size_t g_id = get_global_id(0);
uint d_ks[44];
uint t_ks[44];
uint tweak[4];
uint enc_key[8];
uint enc_data[4];
uint u_data[4] = { 0 };
uint sec_n[4];
ulong Ti = g_Ti[g_id];
sec_n[0] = ((uint*)&Ti)[0];
sec_n[1] = ((uint*)&Ti)[1];
sec_n[2] = 0;
sec_n[3] = 0;
uint Tj = g_Tj[g_id];
vstore8(*g_key, 0, enc_key);
vstore4(g_enc_data[g_id], 0, enc_data);
//calculate tweak value
aes128_set_encrypt_key (t_ks, &enc_key[4]);
aes_xts256_gen_tweak (t_ks, sec_n, Tj, tweak);
// decrypt data
aes128_set_decrypt_key (d_ks, enc_key);
aes_xts256_dec_block (d_ks, tweak, enc_data, u_data);
// printf("enc_data: %v4u\\n", *(uint4*)enc_data);
vstore4(*(uint4*)u_data, g_id, g_u_data);
}
@@ -0,0 +1,27 @@
#include "num_utils.cl"
__kernel void test_add(__global const uint* g_num_to_add,
__global const uint8* g_t0,
__global uint* g_t1,
__global uint* g_t2)
{
const size_t g_id = get_global_id(0);
uint t0[8] = {0};
uint t1[8] = {0};
uint t2[8] = {0};
uint num_to_add = g_num_to_add[g_id];
vstore8(g_t0[g_id], 0, t0);
add_one_to_bigint8 (t0, t1);
add_uint_to_bigint8 (t0, num_to_add, t2);
// save results
vstore8(*(uint8*)t1, 0, &g_t1[g_id*8]);
vstore8(*(uint8*)t2, 0, &g_t2[g_id*8]);
}
+50
View File
@@ -0,0 +1,50 @@
#[cfg(test)]
mod cl_num_utils_tests {
use super::*;
#[test]
fn test_add() {
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
let test_gen_cmd = "/home/kira/Development/Rust/nyash-aes-xts256-plain64/nyash_client/src/tests/gen_test_data.py";
let mut child = Command::new(test_gen_cmd)
.stdout(Stdio::piped())
.spawn()
.unwrap();
let gen_stdout = child
.stdout
.take()
.ok_or("Failed to capture stdout")
.unwrap();
let gen_reader = BufReader::new(gen_stdout);
for r_line in gen_reader.lines() {
let test_line: String = r_line.unwrap(); // Handle any I/O errors
let test_data_line = test_line.split(' ').collect::<Vec<&str>>();
let num_to_add = u32::from_str_radix(test_data_line[0], 10).unwrap();
let t0 = bignum_from_hex(test_data_line[1]);
let t1_test = add_u32_to_u256(&t0, 1).0;
let t2_test = add_u32_to_u256(&t0, num_to_add).0;
let res_actual = format!(
"{} {} {} {}",
num_to_add,
bignum_to_hex(&t0),
bignum_to_hex(&t1_test),
bignum_to_hex(&t2_test)
);
assert_eq!(test_line, res_actual);
}
let _ = child.wait().unwrap();
}
}
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/python
import random
def main(iters: int):
for i in range(iters):
rand_u32 = random.randint(1,0xffffffff)
t0 = int.from_bytes(random.randbytes(32))
t1 = t0 + 1
t2 = t0 + rand_u32
print(f"{rand_u32} {t0.to_bytes(32).hex()} {t1.to_bytes(32).hex()} {t2.to_bytes(32).hex()}")
if __name__ == '__main__':
main(1000000)