thomwiggers / constant-csidh-c-implementation

Creative Commons Zero v1.0 Universal
0 stars 0 forks source link

Parallel iteration of the Rust wrapper functions freezing #1

Open sjsonucool opened 6 days ago

sjsonucool commented 6 days ago

Background

I am using the Rust wrapper of this C library (available at https://github.com/thomwiggers/csidh-rust) in my personal Rust project to implement a private set intersection protocol based on CSIDH key exchange. Some functions of the protocol calls the keypair() and/or agreement(PK_A,SK_B) functions multiple times and hence are time consuming. So, I am calling them in parallel using the Rayon Parallel iterator. Some examples given below:

example 1 let key_pairs: Vec<_> = (0..private_set.len()).into_par_iter().map(|_| keypair()) .collect();

example 2 let shared_keys: Vec<[u8; 64]> = private_set_y.into_par_iter().zip(secret_keys.into_par_iter()).map(|(_ , secret_key)| { let shared_key_vec = agreement(&sender_public_key, &secret_key); let mut shared_key = [0u8; 64]; shared_key.copy_from_slice(&shared_key_vec); shared_key }).collect();

Problem

When I run the code, "sometimes" the execution gets stuck during the parallel iterations shown above with one core being utilized 100 percent for indefinite amount of time, while other allocated cores are idle. Many times there is no freezing and the execution is successful giving the correct results. I am relatively new in Rust programming language and I am stuck attempting to fix the problem. There are many other code segments in my implementation that are using parallel iterators, but the execution never freezes within those loops because they call pure Rust functions inside. So far from all the research I have done towards solving the issue, my best guess is that the issue lies within the C library functions which may have segments which are not thread friendly and might be causing deadlocks/starvation when called in parallel through the csidh-rust wrapper.

Request

Would it be possible to get some help regarding the problems discussed? Specifically, I need help in understanding which portions of this C library I can modify by forking it, to minimize the deadlocks/starvations that occurs during the parallel calls from csidh-rust wrapper. I would appreciate any hints/help you could provide towards resolving the issue, and I apologize in advance if the questions are too naive.

thomwiggers commented 3 days ago

The underlying C library makes use of shared global variables, and I'm afraid that it is not thread-safe. I'm also afraid that I can't help with fixing that. This library is part of a finished research project and I don't have time to fix these issues.