-
Notifications
You must be signed in to change notification settings - Fork 55
chacha20: get ChaCha8 keystream blocks without applying them to a plaintext #424
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
Comments
If you just want a PRNG you can use the It’s unlikely we’ll add an unsafe API for writing into uninitialized memory. This is a micro-optimization that’s often unnecessary because LLVM can elide zero-initialization if it can prove it will be overwritten by a subsequent write. |
You also could use the block-level trait directly. It still uses mutable references over blocks without |
I found struct PartialYs {
buffer: Vec<GenericArray<u8, U64>>,
len: usize,
}
impl Deref for PartialYs {
type Target = [u8];
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { slice::from_raw_parts(self.buffer.as_ptr().cast::<u8>(), self.len) }
}
}
fn partial_ys<const K: u8>(seed: Seed) -> PartialYs {
let output_len_bits = usize::from(K) * (1 << K);
let output_len = output_len_bits.div_ceil(u8::BITS as usize);
let mut output = vec![
Block::<ChaChaCore<U4>>::default();
output_len.div_ceil(ChaChaCore::<U4>::block_size())
];
let key = Key::from(seed);
let nonce = Nonce::default();
let mut cipher = ChaChaCore::<U4>::new(&key, &nonce);
cipher.write_keystream_blocks(&mut output);
PartialYs {
buffer: output,
len: output_len,
}
}
I'll give |
As @tarcieri wrote, the |
As far as I am aware, See specifically https://docs.rs/chacha20/0.10.0-rc.0/chacha20/struct.ChaCha8Rng.html |
I see, I thought it was the pre-release of rand crates initially.
let mut chacha8 = ChaCha8Rng::from_seed(seed);
for _ in 0..N {
let mut chunk = [0u8; 20];
chacha8.fill_bytes(&mut chunk);
// ...
} And concatenation of |
Chia Proof-of-Space implementation uses ChaCha8 cipher internally as PRNG instead of encrypting anything. This means that with current API it is necessary to apply it to zeroed vector first, which is an extra zeroing, memory copy and pointless XOR.
I'm wondering if it would be possible to somehow access internal keystream directly while doing less work, possibly by accepting
&mut [MaybeUninit<u8>]
as an argument.The text was updated successfully, but these errors were encountered: