-
Notifications
You must be signed in to change notification settings - Fork 42
Update WHIR version and integrate buffer abstraction #482
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
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,10 @@ use { | |
| ark_ff::{AdditiveGroup, FftField, Field}, | ||
| ntt::ntt_nr, | ||
| tracing::instrument, | ||
| whir::algebra::ntt::ReedSolomon, | ||
| whir::{ | ||
| algebra::ntt::{Messages, ReedSolomon}, | ||
| buffer::{Buffer, BufferOps}, | ||
| }, | ||
| }; | ||
|
|
||
| #[derive(Debug)] | ||
|
|
@@ -42,26 +45,36 @@ impl ReedSolomon<Fr> for RSFr { | |
| } | ||
|
|
||
| #[instrument(skip(self, messages, masks), fields( | ||
| num_messages = messages.len(), | ||
| message_len = messages.first().map(|c| c.len()), | ||
| num_messages = messages.vectors.len() * messages.interleaving_depth, | ||
| message_len = messages.message_length, | ||
| codeword_length = codeword_length, | ||
| mask_len = masks.len().checked_div(messages.len()) | ||
|
|
||
| mask_len = masks.len().checked_div(messages.vectors.len() * messages.interleaving_depth) | ||
| ))] | ||
| fn interleaved_encode( | ||
| &self, | ||
| messages: &[&[Fr]], | ||
| masks: &[Fr], | ||
| messages: Messages<'_, Fr>, | ||
| masks: &Buffer<Fr>, | ||
| codeword_length: usize, | ||
| ) -> Vec<Fr> { | ||
| ) -> Buffer<Fr> { | ||
| let masks = masks.to_slice(); | ||
| let messages = messages | ||
| .vectors | ||
| .iter() | ||
| .flat_map(|message| { | ||
| message | ||
| .to_slice() | ||
| .chunks_exact(messages.message_length) | ||
| .take(messages.interleaving_depth) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| if messages.is_empty() { | ||
| return vec![]; | ||
| return Buffer::from(vec![]); | ||
| } | ||
|
|
||
| let num_messages = messages.len(); | ||
|
|
||
| let message_length = messages[0].len(); | ||
| for message in messages { | ||
| for message in &messages { | ||
| assert_eq!(message_length, message.len()) | ||
| } | ||
|
|
||
|
|
@@ -96,7 +109,7 @@ impl ReedSolomon<Fr> for RSFr { | |
|
|
||
| ntt_nr(&mut result, codeword_length, num_cosets); | ||
|
|
||
| result | ||
| Buffer::from(result) | ||
| } | ||
|
|
||
| fn generator(&self, codeword_length: usize) -> Fr { | ||
|
|
@@ -136,7 +149,8 @@ mod tests { | |
| let mut data = messages_flat; | ||
| data.resize(total, Fr::ZERO); | ||
|
|
||
| let messages: Vec<&[Fr]> = data.chunks(message_length).collect(); | ||
| let messages: Vec<Buffer<Fr>> = data.chunks(message_length).map(|c| Buffer::from(c)).collect(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: cargo clippy warning |
||
| let messages_refs: Vec<&Buffer<Fr>> = messages.iter().collect(); | ||
|
|
||
| // Our masks are interleaved: num_messages x mask_length in row-major order | ||
| // i.e. [m0_c0, m1_c0, m0_c1, m1_c1, ...] | ||
|
|
@@ -154,24 +168,29 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| let messages = Messages::new(&messages_refs, message_length, 1); | ||
| let masks = Buffer::from(masks); | ||
| let masks_transposed = Buffer::from(masks_transposed); | ||
|
|
||
| let indices: Vec<usize> = (0..codeword_length).collect(); | ||
|
|
||
| let reference = NttEngine::<Fr>::new_from_fftfield(); | ||
| let our_codeword = RSFr.interleaved_encode(&messages, &masks, codeword_length); | ||
| let ref_codeword = reference.interleaved_encode(&messages, &masks_transposed, codeword_length); | ||
| let our_codeword = RSFr.interleaved_encode(messages.clone(), &masks, codeword_length); | ||
| let ref_codeword = | ||
| reference.interleaved_encode(messages, &masks_transposed, codeword_length); | ||
|
|
||
| let our_points = RSFr.evaluation_points(message_length, codeword_length, &indices); | ||
| let ref_points = reference.evaluation_points(message_length, codeword_length, &indices); | ||
|
|
||
| // Pair each evaluation point with its num_messages-wide slice, then sort | ||
| // by point so that ordering differences between implementations don't matter. | ||
| let mut our_rows: Vec<_> = our_points.iter().enumerate() | ||
| .map(|(i, pt)| (pt.into_bigint(), &our_codeword[i * num_messages..(i + 1) * num_messages])) | ||
| .map(|(i, pt)| (pt.into_bigint(), &our_codeword.to_slice()[i * num_messages..(i + 1) * num_messages])) | ||
| .collect(); | ||
| our_rows.sort_by_key(|(k, _)| *k); | ||
|
|
||
| let mut ref_rows: Vec<_> = ref_points.iter().enumerate() | ||
| .map(|(i, pt)| (pt.into_bigint(), &ref_codeword[i * num_messages..(i + 1) * num_messages])) | ||
| .map(|(i, pt)| (pt.into_bigint(), &ref_codeword.to_slice()[i * num_messages..(i + 1) * num_messages])) | ||
| .collect(); | ||
| ref_rows.sort_by_key(|(k, _)| *k); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: Could this use
whir::utils::chunks_exact_or_emptyinstead?chunks_exactpanics when the chunk size is 0(defensive).