Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Auto-allocate stack in runtime #260

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ jobs:
strategy:
matrix:
os:
- macos-latest
# - macos-latest
- ubuntu-latest
- windows-latest
rust:
- stable
- beta
- nightly
env:
# env:
# 20 MiB stack
RUST_MIN_STACK: 20971520
# RUST_MIN_STACK: 20971520

steps:
- uses: actions/checkout@v4
Expand Down
47 changes: 39 additions & 8 deletions oqs/src/kem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,46 @@ impl Kem {
let mut sk = SecretKey {
bytes: Vec::with_capacity(kem.length_secret_key),
};
let status = unsafe { func(pk.bytes.as_mut_ptr(), sk.bytes.as_mut_ptr()) };
status_to_result(status)?;
// update the lengths of the vecs
// this is safe to do, as we have initialised them now.
unsafe {
pk.bytes.set_len(kem.length_public_key);
sk.bytes.set_len(kem.length_secret_key);

let pklen = kem.length_public_key;
let sklen = kem.length_secret_key;

#[cfg(feature = "std")]
{
// Particularly the classic McEliece kem is very stack-heavy.
// The reason we're using a thread is that it allows
// to increase stack space, on demand, in run time.
// Not only does this eliminate the need to set compiler options for each build;
// it also means we won't be holding on to memory unnecessarily in runtime.
let handle = std::thread::Builder::new()
.stack_size(16_000_000)
.spawn(move || {
let status = unsafe { func(pk.bytes.as_mut_ptr(), sk.bytes.as_mut_ptr()) };
status_to_result(status)?;
// update the lengths of the vecs
// this is safe to do, as we have initialised them now.
unsafe {
pk.bytes.set_len(pklen);
sk.bytes.set_len(sklen);
}
Ok((pk, sk))
})
.unwrap();
handle.join().unwrap()
}
#[cfg(not(feature = "std"))]
{
// For embedded, something different needs to be done to spawn a new task. For now, do it inline.
let status = unsafe { func(pk.bytes.as_mut_ptr(), sk.bytes.as_mut_ptr()) };
status_to_result(status)?;
// update the lengths of the vecs
// this is safe to do, as we have initialised them now.
unsafe {
pk.bytes.set_len(pklen);
sk.bytes.set_len(sklen);
}
Ok((pk, sk))
}
Ok((pk, sk))
}

/// Encapsulate to the provided public key
Expand Down