-
-
Notifications
You must be signed in to change notification settings - Fork 434
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
Add Serde #189
Add Serde #189
Conversation
Oh crud, a bunch of |
200 lines for each of the ISAAC variants — painful. I gave up trying to implement Serde support before precisely because of the Can you please remove the rustfmt changes to existing code? (Probably easiest to use |
1c37e96
to
c96c235
Compare
Got it. Also rebased and resolved merge conflicts. |
Hmm, looks like the failing test is the Rust 1.15 version. How important is that? I didn't even remember struct field shorthands being a recent(-ish) feature. It's not a big deal to fix, but if I don't have to I'll leave it. |
Struct field shorthands are certainly recent; I can't answer the question about the importance of old compiler versions. I think there's general agreement that feature-gated Serde support is wanted (it's mentioned in the redesign RFC; unfortunately I can't find any discussion on this). I had a quick look at the code; looks fine and well tested. |
I think I can actually clean this up quite a bit. Firstly, whenever serde-rs/serde#1072 is merged |
Managed to about halve the lines with the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I published @LinearZoetrope's Wrapping impls in Serde 1.0.17.
Yay, now it's down about as low as we can get it without blanket impls for arbitrary constants. |
Very nice. I should point out that there's been some discussion of whether CSPRNGs should implement serialization; see here: dhardy#31. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dhardy#31 still doesn't have many comments, so possibly wait some more, but at least there is no disagreement yet.
After the changes mentioned, I'd like to see this merged.
src/isaac.rs
Outdated
|
||
#[cfg(feature="serde-1")] | ||
mod rand_size_64_serde { | ||
use super::RAND_SIZE_64; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this module identical to the usize
version? I doubt we'll be changing RAND_SIZE_USIZE
or RAND_SIZE_64
— they're fundamental to the algorithms — so it seems silly duplicating the code here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought the same thing, but I did it just to be safe. I can remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RAND_SIZE_USIZE
was just there to avoid a few casts. In https://github.com/dhardy/rand/pull/4/files I removed it.
src/lib.rs
Outdated
@@ -840,6 +844,7 @@ pub struct Closed01<F>(pub F); | |||
/// The standard RNG. This is designed to be efficient on the current | |||
/// platform. | |||
#[derive(Copy, Clone, Debug)] | |||
#[cfg_attr(feature="serde-1",derive(Serialize,Deserialize))] | |||
pub struct StdRng { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally think StdRng
should not be serializable — we wish to be able to replace the algorithm in the future without breaking code, and if someone is serializing the RNG it implies they wish output to remain the same. StdRng
is also architecture dependent (serialize on a 32-bit machine, deserialize on 64-bit — whoops). Users may use IsaacRng
or the 64-bit version directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair point.
I think it would be nice to land this in some form with the other improvements to PRNG's for 0.4.1. I do have some comments/opinions:
@dhardy Does this sound like a way forward to you? |
Mostly I agree with @pitdicker; I'm not convinced on the CSPRNGs but I suspect most demand for serialization will be with the non-crypto PRNGs, so I guess his plan works. Anyway, thanks again @LinearZoetrope for doing the leg-work including getting Serde updated. |
Sorry I've been silent, things have been busy. I'd be more okay with omitting the CSPRNGs if there were better standing options for regular PRNGs implemented. As it stands, limiting yourself to XorShift just because you need serialization makes it almost useless to have the feature. I'd have fewer complaints if we had options like Mersenne Twister or whatever else is being considered available right now. Though I suppose if I can put it on Isaac due to the noted lack of it being known to be CS I can live. That said, I'm also sensitive to the fact that we can't add serialization for CSPRNGs and then remove it once we have better non-CS options (okay, fine, pre-1.0 we CAN but a pre-planned regression before a feature is even added seems like an awful idea). I can address the other issues such as rebase/removing StdRng/etc. |
That's okay. Yes, I think we will merge #209 and possibly #210 first, so if you rebase on top of #209 it would be better. (Unfortunately it's not a simple rebase because ISAAC got moved around a lot.) StdRng shouldn't implement serialisation because it could break just going from a 32-bit machine to a 64-bit one at the moment. We're talking about switching it to HC128 across the board; after that maybe it could; I'm not sure. I still think hiding internal state is a weak argument for not implementing serialization, personally. |
I should also mention that @pitdicker has been working on collecting a number of alternatives to Xorshift: dhardy#60. We aim to make a companion crate available (currently https://github.com/pitdicker/small-rngs) and replace Xorshift: dhardy#52 |
Yes. To be honest I worded it a bit stronger than that I feel about it. Because I am not sure it is a good idea and actually needed, not implementing it feels like a conservative position to start from. You also made the good point in several places that anyone is free to use a cryptographic RNG for applications that do not strictly need one (like many games). Who are we to deny it? I think @LinearZoetrope say's it well that a reason to want serialization for CSPRNG's now is because the quality of the current alternative, Xorshift, is not much. And also that once we add serialisation, we can't just remove it. Personally I would wait to add serialisation to ChaCha until someone opens an issue for it together with his use case. But I won't stand in the way to add it now. B.t.w. @LinearZoetrope. Is there some guideline as to how ChaCha should be serialized (I know next to nothing about that)? In theory it is not necessary to serialize the results buffer, that can easily be recalculated. |
To be fair to your point about hiding the internals, serialisation for I guess we can apply this by making |
So where is this PR is up to (since I have some interest in it)? Does it just need a rebase and stdrng removing? |
I think so, yes. |
Quite some work has already been put into it, so I think that is it. In dhardy#58 (comment) I listed some of the changes planned for the PRNG's. I think we could merge this PR when it is rebased, but everything it adds serialisation for will be different soon(ish). |
3b9b943
to
0a1b4ee
Compare
Okay, that should address all the criticism. I rebased on master since all the changes had been merged into there. Currently just the Isaacs and Xorshift are implemented. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me other than the header block. Thanks @LinearZoetrope!
src/prng/isaac_serde.rs
Outdated
@@ -0,0 +1,68 @@ | |||
#[cfg(feature="serde-1")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file needs a header. Copy the copyright/licence block and add a one-line //!
comment please.
Also I think this cfg
line is redundant since it was also applied to mod isaac_serde;
.
Done |
src/prng/isaac_serde.rs
Outdated
@@ -1,4 +1,15 @@ | |||
#[cfg(feature="serde-1")] | |||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's 2018 now; use that or 2017-2018 (since you started working on it last year)
Yeah, I was wondering but deferred changing it until I was told to. |
This addresses #84 by making
ChaCha
,Isaac
,XorShift
, andStdRng
serializable and deserializable withserde
.This was extremely tedious, and basically comes down entirely to the fact that
serde
doesn't implement its traits forWrapping
, andIsaac
's array length is 256 and thus above the autogenerated trait impls. Fixing both of those from theserde
and Rust side would remove a lot of code and let you just derive them.Luckily, since
IsaacRng
andIsaac64Rng
underlieStdRng
, we get that one viaderive
.Currently this is set up as a feature. In order to use it someone would have to use
rand = { version="1.2.3", features=["serde-1"] }
in their Cargo.toml.This introduces a minor
dev-dependency
onbincode
because I needed something to serialize with for unit tests. Unfortunately there's no way currently to gatedev-dependencies
behind features (See here).I can also push a commit to bump the version if desired, but I figured I should leave that to you instead of scribbling around version numbers myself.