-
Notifications
You must be signed in to change notification settings - Fork 145
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 Rescue Prime Optimized #930
Conversation
…bdaworks into add_rescue_prime
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #930 +/- ##
==========================================
+ Coverage 71.62% 71.97% +0.35%
==========================================
Files 149 152 +3
Lines 32560 33074 +514
==========================================
+ Hits 23321 23805 +484
- Misses 9239 9269 +30 ☔ View full report in Codecov by Sentry. |
crypto/src/hash/rescue_prime/mod.rs
Outdated
Ntt, | ||
Karatsuba, | ||
} | ||
#[warn(dead_code)] |
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.
why do you need this annotation?
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.
Solved, it wasn’t necessary. I added it while trying to fix some Clippy issues, but it turned out to be incorrect
crypto/src/hash/rescue_prime/mod.rs
Outdated
|
||
fn get_mds_vector(m: usize) -> Vec<Fp> { | ||
match m { | ||
12 => vec![7, 23, 8, 26, 13, 10, 9, 7, 6, 22, 21, 8] |
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 can be an array instead of a vec
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.
You're right, the mds_vector is now an array of Fp and is also precomputed
crypto/src/hash/rescue_prime/mod.rs
Outdated
.into_iter() | ||
.map(Fp::from) | ||
.collect(), | ||
_ => panic!("Unsupported state size"), |
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 think this function shouldn't panic. It can return a Result indicating the error case.
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 function isn't used anymore, but I acknowledged your comment about avoiding panics
crypto/src/hash/rescue_prime/mod.rs
Outdated
let m = self.m; | ||
let round_constants = &self.round_constants; | ||
for j in 0..m { | ||
state[j] += round_constants[round * 2 * m + j]; |
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.
instead of indexing, this can be improved by using get_mut and an iterator
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.
updated the implementation as suggested
pub fn bytes_to_field_elements(input: &[u8]) -> Vec<Fp> { | ||
input | ||
.chunks(7) | ||
.map(|chunk| { | ||
let mut buf = [0u8; 8]; | ||
buf[..chunk.len()].copy_from_slice(chunk); | ||
if chunk.len() < 7 { | ||
buf[chunk.len()] = 1; | ||
} | ||
let value = u64::from_le_bytes(buf); | ||
Fp::from(value) | ||
}) | ||
.collect() | ||
} |
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.
Is this function just for rescue_prime or a function that can be used elsewhere?
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.
Currently it 's only use in rescue_prime. Maybe later it can be added to fields
crypto/src/hash/rescue_prime/mod.rs
Outdated
@@ -0,0 +1,816 @@ | |||
use crate::alloc::vec::Vec; |
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 better not to have a giant mod.rs file.
Instead, it's better to have a separate file with each struct or enum.
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.
+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.
Restructured the files as suggested. Now is more maintainable and clear
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.
As a general rule, we won't want to panic anywhere. As this is a library meant for users, we must provide them Result
s of the operations to let them decide what to do with the errors in such cases. Commonly, panics can happen in places where unwraps, expects, as conversions, and slice indexing (and obviously when we explicitly use the panic macro). Unless it is planned for another PR to handle these cases I'd suggest doing it asap.
Unwraps and expects can be avoided returning Results. As conversions can be avoided mostly by using .into()
, and .try_into()
(we will prefer the first one as we'd not have to handle the error). Finally, slice indexing can be avoided using .get()
and handling the Option.
To improve the code's readability we'd want to define descriptive constants for hardcoded structures.
crypto/src/hash/rescue_prime/mod.rs
Outdated
@@ -0,0 +1,816 @@ | |||
use crate::alloc::vec::Vec; |
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.
+1
…bdaworks into add_rescue_prime
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.
The PR is looking good! Left some comments
let (m, capacity) = match security_level { | ||
SecurityLevel::Sec128 => (12, 4), | ||
SecurityLevel::Sec160 => (16, 6), | ||
}; |
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.
Let's do the same for these. get_state_size(security_level: SecurityLevel)
and get_capacity(security_level: SecurityLevel)
.
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.
Great work! LGTM
Add Rescue Prime Optimized
Description
This PR adds the Rescue Prime Optimized hash function.
The implementation offers three types of MDS transformations: Matrix Multiplication, Karatsuba, and NTT
Example:
In this case
128
is the security level, the other available level is160
To use other
MdsMethod
simply changeMatrixMultiplication
forKaratsuba
orNtt
Type of change