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

Document why *ring* does not use traits to model algorithm choice #21

Closed
briansmith opened this issue Sep 24, 2015 · 10 comments
Closed

Comments

@briansmith
Copy link
Owner

For example, why doesn't ring have a DigestAlgorithm trait that is implemented by types named SHA256, SHA384, etc.? Why is everything dynamically dispatched instead of parameterized on types and statically dispatched? How does the current design avoids unnecessary heap usage and minimizes code size. That means we have to verify that there is actually a code size savings relative to the type parameterization and static dispatching approach that people seem to expect in Rust.

@briansmith
Copy link
Owner Author

Reconsider this after the placement box stuff is stable: rust-lang/rfcs#1228

@briansmith
Copy link
Owner Author

It looks like the whole placement new stuff is being tracked in rust-lang/rust#27779. I asked about how to resolve the issues that ring has run into with Traits in rust-lang/rust#27779 (comment).

@briansmith
Copy link
Owner Author

See also https://www.ralfj.de/blog/2015/10/12/formalizing-rust.html: Traits are not in the subset of Rust that they are hoping to define semantics for and/or prove correct.

@briansmith
Copy link
Owner Author

Now in ring::signature we are using traits. And, of course, it creates a problem: We cannot always say &'static signature::Algorithm but instead have to say &'static (signature::Algorithm + Sync) when referencing algorithms from other statics, because of Rust type system weirdness.

@briansmith
Copy link
Owner Author

briansmith commented Jun 19, 2016

Now in ring::signature we are using traits. And, of course, it creates a problem: We cannot always say &'static signature::Algorithm but instead have to say &'static (signature::Algorithm + Sync) when referencing algorithms from other statics, because of Rust type system weirdness.

And, it was so awkward I ended up implementing wrapper types to hide the traits.

Separately, I already had to explain this a bit on IRC:

briansmith> right now, you can use ring with "use ring;" or "use ring::{submodule, submodule, submodule};" and everything works.
20:30 But, as soon as you use traits, that stops working, because you need to import the traits too.
20:31 And also, in ring we usually use polymorphism to select between algorithms.
20:31 And so, usually, every implementation of each trait would only have one implementation, each.
20:32 So, instead, in ring I basically emulate per-object (vs per-trait) polymorphism.
20:34 IMO, it is something that should be fixed in the language: when a type implements a trait, it should be able to say "when I am in scope, the trait should also be in scope."
20:34 but, that's not the case yet.
briansmith

@Diggsey
Copy link

Diggsey commented Jun 4, 2017

It looks like VerificationAlgorithm extends Sync now - doesn't that solve the problem with verbosity? And is the requirement to import traits separately really a show-stopper?

With function pointers, the algorithm structs become much larger than the double-pointer sized &'static Algorithm, and it becomes difficult to add more trait implementations like Debug in future, without introducing even more inefficiencies. It also prevents downstream crates from extending the set of possible algorithms.

From a security perspective, storing multiple function pointers in writable locations on the stack also seems much more dangerous than a reference to a vtable in read-only memory.

@briansmith
Copy link
Owner Author

It looks like VerificationAlgorithm extends Sync now - doesn't that solve the problem with verbosity?

Yes.

And is the requirement to import traits separately really a show-stopper?

It's a real usability problem. But, it's not worse in ring than it is in other APIs.

With function pointers, the algorithm structs become much larger

It doesn't matter, because there is only one copy of every Algorithm, so I don't think it creates any bloat.

it becomes difficult to add more trait implementations like Debug in future

Not really, because the best implementation of Debug for Algorithm types is just to output the name of the algorithm and omit the details anyway.

It also prevents downstream crates from extending the set of possible algorithms.

True. OTOH, we don't have to worry about external crates' implementations when we refactor the code, because of this. People can contribute implementations of new algorithms to ring if they want more algorithms supported.

From a security perspective, storing multiple function pointers in writable locations on the stack also seems much more dangerous than a reference to a vtable in read-only memory.

Very true, but the compiler should be able to put them in read-only memory since they are all static and not static mut, just like trait vtables. I'm not sure if the compiler actually does this, though.

@burdges
Copy link

burdges commented Jun 5, 2017

There is an RFC somewhere to allow types to bring trait methods into scope. I forget where or what the syntax is, but maybe something like struct MyStruct: MyTrait+Sync where ... And no movement recently.

@Diggsey
Copy link

Diggsey commented Jun 6, 2017

It doesn't matter, because there is only one copy of every Algorithm, so I don't think it creates any bloat.

Ah, some of my arguments were based on the mistaken assumption that Algorithms were passed around by value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants