-
Notifications
You must be signed in to change notification settings - Fork 50
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
chacha20: returning rand_core feature #333
chacha20: returning rand_core feature #333
Conversation
Alright, I think I've added just about all I can think of. The
There are also some functions in |
I'd suggest less unsafe code and more of a KISS implementation in an initial PR, and then circling back on some of it in a followup so it can be considered separately. |
FYI, #338 bumps the crate to v0.10.0-pre, but also includes some other breaking API changes. Rebasing on it shouldn't be too hard, I hope! |
…rom<[u32; 3]> for StreamId and From<[u8; 4]> for BlockPos
…he WordPosInput struct and adjusting the documentation; added checks in test_set_and_get_equivalence
I've made Some questions I have:
|
I've now got a working version where the backends seem to be safely zeroizing when the |
@nstilt1 can you rebase? This is looking mostly good and I'd like to get it merged. |
Sure thing. I apologize for the delay. I uh... I was mis-using ChaCha in several ways. I should have it rebased by tomorrow. |
…sting (and stopped working), fixed some spelling
…os to Rng instead of RngCore; updated docs to reflect changes
*block_pos. I moved set/get_block_pos to the RNG rather than its core, where it wasn't able to change the RNG's index after trying to change the block pos |
// Tested for N=32; could be bugs in the loop bounds for other N | ||
// returns bytes written, like fwrite: N means no error, 0 means error in all fwrites | ||
size_t LongNumPrint( uint8_t *num, size_t N) | ||
{ | ||
// caller can print a name if it wants | ||
|
||
const int revbufsize = 8192; // 8kiB on the stack should be fine | ||
alignas(32) char revbuf[revbufsize]; | ||
|
||
if (N<32) { | ||
// TODO: maybe use a smaller revbuf for this case to avoid touching new stack pages | ||
ASCIIrev32B(revbuf, num); // the data we want is at the *end* of a 32-byte reverse | ||
return fwrite(revbuf+32-N, 1, N, stdout); | ||
} | ||
|
||
size_t bytes_written = 0; | ||
const uint8_t *inp = num+N; // start with last 32 bytes of num[] | ||
do { | ||
size_t chunksize = (inp - num >= revbufsize) ? revbufsize : inp - num; | ||
|
||
const uint8_t *inp_stop = inp - chunksize + 32; // leave one full vector for the end | ||
uint8_t *outp = revbuf; | ||
while (inp > inp_stop) { // may run 0 times | ||
inp -= 32; | ||
ASCIIrev32B(outp, inp); | ||
outp += 32; | ||
} | ||
// reverse first (lowest address) 32 bytes of this chunk of num | ||
// into last 32 bytes of this chunk of revbuf | ||
// if chunksize%32 != 0 this will overlap, which is fine. | ||
ASCIIrev32B(revbuf + chunksize - 32, inp_stop - 32); | ||
bytes_written += fwrite(revbuf, 1, chunksize, stdout); | ||
inp = inp_stop - 32; | ||
} while ( inp > num ); | ||
|
||
return bytes_written; | ||
// caller can putchar('\n') if it wants | ||
} |
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.
Huh?
This looks very close now, though I'm not sure what's up with |
In the interest of moving this along since it's important, been taking awhile, and rather invasive, I will go ahead and merge and we can address the remaining issues as followups |
I have no idea how that code ended up there, but it is from this post: https://stackoverflow.com/questions/61165307/ I think I was trying to explore palindromic cubes, looking for the fastest way to check if a large number is a palindrome... but I don't know how that code ended up there |
I borrowed some code from version
0.8.1
ofchacha20
, as well asrand_chacha
and was able to get it to compile and pass tests. The main issues in my code that I identified were:[u8; 12]
instead ofu128
for set/get_stream(). It used to have au64
when it had a 64 bit counterset_stream()
takes the lower 96 bits of a u128 input. It now has support for using a[u8; 12]
withset/get_stream_bytes()
ParBlock<LesserBlock>
as a temporary output buffer prior to copying it intoresults
infn generate()
. It will probably leave a copy of itself in memory after use. It might be possible to change theBlockRngResults
type toParBlock<LesserBlock>
AsRef<[u32]> for BlockRngResults
andAsMut<[u32]> for BlockRngResults
set_word_pos()
with an input that wasn't divisible by 16,get_word_pos()
would round the input down to the nearest multiple of 16.rand_chacha
.I've also added
ZeroizeOnDrop
for theBlockRngResults
.rust-random/rand#934