mirror of
https://github.com/logos-storage/plonky2.git
synced 2026-01-02 13:53:07 +00:00
Goldilocks: better constant propagation through add_with_wraparound (#320)
This commit is contained in:
parent
c406f464b8
commit
db23416b04
@ -13,6 +13,7 @@ use crate::field::extension_field::quartic::QuarticExtension;
|
||||
use crate::field::extension_field::Extendable;
|
||||
use crate::field::field_types::{Field, PrimeField, RichField};
|
||||
use crate::field::inversion::try_inverse_u64;
|
||||
use crate::util::assume;
|
||||
|
||||
const EPSILON: u64 = (1 << 32) - 1;
|
||||
|
||||
@ -280,6 +281,8 @@ unsafe fn add_with_wraparound(x: u64, y: u64) -> u64 {
|
||||
inlateout(reg) y => adjustment,
|
||||
options(pure, nomem, nostack),
|
||||
);
|
||||
assume(x != 0 || (res_wrapped == y && adjustment == 0));
|
||||
assume(y != 0 || (res_wrapped == x && adjustment == 0));
|
||||
res_wrapped.wrapping_add(adjustment) // Add EPSILON == subtract ORDER.
|
||||
}
|
||||
|
||||
@ -307,6 +310,7 @@ unsafe fn sub_with_wraparound(x: u64, y: u64) -> u64 {
|
||||
inlateout(reg) y => adjustment,
|
||||
options(pure, nomem, nostack),
|
||||
);
|
||||
assume(y != 0 || (res_wrapped == x && adjustment == 0));
|
||||
res_wrapped.wrapping_sub(adjustment) // Subtract EPSILON == add ORDER.
|
||||
}
|
||||
|
||||
|
||||
@ -1,3 +1,5 @@
|
||||
use core::hint::unreachable_unchecked;
|
||||
|
||||
use crate::field::field_types::Field;
|
||||
use crate::polynomial::polynomial::PolynomialValues;
|
||||
|
||||
@ -195,6 +197,14 @@ pub(crate) fn reverse_bits(n: usize, num_bits: usize) -> usize {
|
||||
.0
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub(crate) unsafe fn assume(p: bool) {
|
||||
debug_assert!(p);
|
||||
if !p {
|
||||
unreachable_unchecked();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::util::{reverse_bits, reverse_index_bits, reverse_index_bits_in_place};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user