open-quantum-safe / liboqs-rust

Rust bindings for liboqs
https://openquantumsafe.org/
Apache License 2.0
120 stars 48 forks source link

feat: Allow public access to bytes #250

Closed tbraun96 closed 1 year ago

tbraun96 commented 1 year ago

When building a protocol, we sometimes make use of generic bytes to allow variable use of subprotocols, and don't want to have to add the overhead of serialization. By allowing public access to the inner fields (whether directly or indirectly via a function), everything works.

tbraun96 commented 1 year ago

@thomwiggers let me know what approach you're okay with and I'll amend the PR. Thanks.

thomwiggers commented 1 year ago

Having remembered/looked up what API choices I made, the only supported way of constructing these types is using:

https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.public_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.ciphertext_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.public_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.signature_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.secret_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.secret_key_from_bytes

Any way of constructing them without going through oqs::kem::Kem or oqs::sig::Sig instances is unsafe and should not be supported as we rely on the error checking done in these methods.

Any performance difference in using these methods versus directly contructing these types is either up to error checking (which is not a bug) or a bug in compiler optimizations, which should be reported to the Rust project.

tbraun96 commented 1 year ago

Having remembered/looked up what API choices I made, the only supported way of constructing these types is using:

https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.public_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.ciphertext_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.public_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.signature_from_bytes https://docs.rs/oqs/0.9.0/oqs/sig/struct.Sig.html#method.secret_key_from_bytes https://docs.rs/oqs/0.9.0/oqs/kem/struct.Kem.html#method.secret_key_from_bytes

Any way of constructing them without going through oqs::kem::Kem or oqs::sig::Sig instances is unsafe and should not be supported as we rely on the error checking done in these methods.

Any performance difference in using these methods versus directly contructing these types is either up to error checking (which is not a bug) or a bug in compiler optimizations, which should be reported to the Rust project.

Perfect. I did not see these in the documentation, likely because I was looking at the structs themselves instead of the Kem itself that has metadata on the byte lengths.

thomwiggers commented 1 year ago

I suppose we can improve the docs for the types a bit.