-
Notifications
You must be signed in to change notification settings - Fork 64
Bos coster msm #311
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
base: main
Are you sure you want to change the base?
Bos coster msm #311
Conversation
820c108 to
d381d85
Compare
ArtiomTr
left a comment
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.
Please rebase from main branch, and when you finish, squash all your commits into one
kzg/src/msm/bos_coster.rs
Outdated
|
|
||
| impl Scalar256 { | ||
| #[inline(always)] | ||
| fn bitlen_256(&self) -> u32 { |
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 don't think this is a good idea to extend type for your custom usecase - move them to near scalar struct definition, or replace with simple functions
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.
also, don't make up your own functions - it is better to mirror standard library, e.g. instead of bitlen_256, you can easily make leading_zeros function, and then do something like Scalar256::BITS - scalar.leading_zeros() - so it looks the same, like for other types, e.g.: u64::BITS - something.leading_zeros().
| "arkworks3-sppark", | ||
| "arkworks3-sppark-wlc", | ||
| "blst", | ||
| "blst-sppark", |
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.
please keep CUDA backends, don't delete them
fuzz/fuzz_targets/blst_fixed_msm.rs
Outdated
| @@ -0,0 +1,65 @@ | |||
| #![no_main] | |||
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.
all fuzzing is already merged into main branch, so need to be rebased
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
|
|
||
| #[inline(always)] | ||
| fn shl_256(&self, n: u32) -> Scalar256 { |
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 haven't read code thoroughly, but it looks like << implementation for scalar type. For standard operations, please use traits defined in standard library, e.g. here it would be Shl. So you can then use them nicely:
let scalar: Scalar256;
scalar << 8;
kzg/src/msm/bos_coster.rs
Outdated
| if heap.is_empty() { | ||
| return TG1::zero(); | ||
| } | ||
|
|
||
| let pair = heap.pop().unwrap(); | ||
| return pair.point.mul(&TFr::from_u64_arr(&pair.scalar.data)); |
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 written a bit more idiomatically:
if let Some(pair) = heap.pop() {
pair.point.mul(&TFr::from_u64_arr(&pair.scalar.data));
} else {
TG1::zero()
}allows to remove panicking .unwrap() call
kzg/src/msm/bos_coster.rs
Outdated
| for _ in 0..k { | ||
| new_point.dbl_assign(); | ||
| } | ||
| pair2.point.add_assign(&new_point); |
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.
unless you're absolutely sure that pair2 and new_point are not equal, you should use add_or_dbl_assign
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
| let mut d = bitlen_x - bitlen_y; | ||
| let mut shifted_y = y.shl_256(d); | ||
| if shifted_y.cmp(&x) == std::cmp::Ordering::Greater { |
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 not shifted_y > x?
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
| } | ||
|
|
||
| // x >= y |
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 can make this comment as debug_assert, to detect errors early without slowing down runtime:
debug_assert!(x >= y);
kzg/src/msm/bos_coster.rs
Outdated
|
|
||
| // x >= y | ||
| #[inline(always)] | ||
| fn find_k(x: &Scalar256, y: &Scalar256) -> (u32, Scalar256) { |
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.
really confusing function name - rename it
msm-benches/tests/failing_fuzzer.rs
Outdated
| @@ -0,0 +1,101 @@ | |||
| #[cfg(test)] | |||
| mod tests { | |||
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.
msm-benches crate is not for custom tests - keep them in backend crate(s)
bd1fc73 to
d506500
Compare
|
rebased, aligned api with the way arkmsm does it |
4ef9c02 to
fc4a2db
Compare
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
| } | ||
|
|
||
| impl Ord for Scalar256 { |
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.
still not fixed - please keep all trait implementations near struct definition, not in separate file
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
|
|
||
| trait Shr1Assign { | ||
| fn shr_once_assign(&mut self); |
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 don't think this is good idea to create custom method just for >> 1 -- just implement general Shr, and with inline(always) compiler will optimize that usecase for you.
kzg/src/msm/bos_coster.rs
Outdated
|
|
||
| use crate::{Fr, G1Affine, G1Fp, G1Mul, G1ProjAddAffine, Scalar256, G1}; | ||
|
|
||
| pub struct VariableBaseMSM; |
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.
should be named "BosCosterMSM"
kzg/src/msm/bos_coster.rs
Outdated
| break; | ||
| }; | ||
| let pair2_scalar = { | ||
| let Some(pair2) = heap.peek_mut() else { |
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 no longer needs to be peek_mut
kzg/src/msm/bos_coster.rs
Outdated
| let Some(pair1) = heap.pop() else { | ||
| break; | ||
| }; | ||
| let pair2_scalar = { |
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 believe you can remove this block entirely, and just put all its content into loop body
kzg/src/msm/bos_coster.rs
Outdated
| } | ||
| } | ||
|
|
||
| trait LeadingZeros { |
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.
no need to define extension trait, just implement needed method directly on struct
kzg/src/msm/bos_coster.rs
Outdated
| impl Shl<u32> for Scalar256 { | ||
| type Output = Scalar256; | ||
|
|
||
| // This function was generated with ChatGPT |
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.
would be nice to add unit tests for this function
kzg/src/msm/msm_impls.rs
Outdated
| precomputation: Option<&PrecomputationTable<TFr, TG1, TG1Fp, TG1Affine, TProjAddAffine>>, | ||
| ) -> TG1 { | ||
| #[cfg(not(feature = "arkmsm"))] | ||
| #[cfg(not(any(feature = "arkmsm", feature = "bos_coster")))] |
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 believe bos_coster shouldn't conflict with precomputation methods? you can probably just put it instead of pippenger call, if feature is enabled.
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.
what do you mean by conflict? how does arkmsm conflict?
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.
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.
what do you mean by conflict?
For example, you can't use bgmw and wbits at the same time, pippenger and arkmsm, etc. - that is just does not make sense.
how does arkmsm conflict?
arkmsm doesn't, someone who implemented arkmsm just didn't think about this.
you mean change it like this?
exactly! maybe there are more places, where pippenger is being called, but that's fine for now.
| "rust-kzg-blst/bos_coster", | ||
| "rust-kzg-arkworks4/bos_coster", | ||
| "rust-kzg-arkworks5/bos_coster", | ||
| "rust-kzg-constantine/bos_coster", |
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.
mcl missing 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.
added
| "rust-kzg-mcl/parallel", | ||
| ] | ||
| bos_coster = [ | ||
| "rust-kzg-blst/bos_coster", |
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.
other backends are missing 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.
added
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.
after #322 merged, zkcrypto backend also should be working. MCL is also missing here
fc4a2db to
d013787
Compare
pairing heap
d013787 to
b7d180b
Compare
ArtiomTr
left a comment
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, but still few changes needed. Also, don't forget to fix CI - kzg crate needs formatting to be fixed, and zkcrypto crate now has some issues with clippy. Those clippy issues would probably resolve after you remove feature checks between bos_coster and bgmw/arkmsm/wbits ones.
| "rust-kzg-mcl/parallel", | ||
| ] | ||
| bos_coster = [ | ||
| "rust-kzg-blst/bos_coster", |
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.
after #322 merged, zkcrypto backend also should be working. MCL is also missing here
| all(feature = "bgmw", feature = "bos_coster"), | ||
| all(feature = "sppark", feature = "wbits"), | ||
| all(feature = "sppark", feature = "bos_coster"), | ||
| all(feature = "wbits", feature = "bos_coster"), |
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.
no need to check this, bos_coster is compatible with all other features (except arkmsm, but you can ignore this for now)
|
|
||
| // This function was generated with ChatGPT | ||
| #[inline(always)] | ||
| fn shl(self, n: u32) -> Scalar256 { |
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.
still want to see unit tests for this function. you can put them in the same file, at the end, like so:
#[cfg(test)]
mod test {
#[test]
fn shl_must_shift_2_bits() {
let scalar = /* some scalar */;
let received = scalar << 2;
let expected = /* scalar shifted 2 bits by hand */;
assert_eq!(expected, received);
}
}
No description provided.