-
Notifications
You must be signed in to change notification settings - Fork 140
Hadar/reduction from storage #745
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
Changes from 21 commits
d02cc1e
1b58dc4
2e34b68
ba51110
5d04ac8
90d2d7d
c1d532a
5d4fc30
a42e08e
337390e
4527d93
5a97dc0
960c6a7
c928abb
4855b7d
ef57d44
67cdc90
c080d37
47024ce
c47a740
e007b43
a406106
12b2aa0
7ad41f1
e861c2e
d7bfa3c
fab2c90
3d91859
8efd7a7
db2a8cc
2e66dbb
1db8797
c805ca6
8dee5b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,7 @@ class Field | |
static HOST_DEVICE_INLINE Field inv_log_size(uint32_t logn) | ||
{ | ||
if (logn == 0) { return Field{CONFIG::one}; } | ||
base_math::inv_log_size_err(logn, CONFIG::omegas_count); | ||
base_math::index_err(logn, CONFIG::omegas_count); // check if the requested size is within the valid range | ||
storage_array<CONFIG::omegas_count, TLC> const inv = CONFIG::inv; | ||
return Field{inv.storages[logn - 1]}; | ||
} | ||
|
@@ -239,6 +239,13 @@ class Field | |
} | ||
} | ||
|
||
HadarIngonyama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static HOST_DEVICE_INLINE Field get_reduced_digit(int i) | ||
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. I think you should clarify by adding modulus / p to the function name as well to the reduced_digits struct and the mod_sub function + struct |
||
{ | ||
base_math::index_err(i, CONFIG::reduced_digits_count); // check if the requested size is within the valid range | ||
storage_array<CONFIG::reduced_digits_count, TLC> const reduced_digits = CONFIG::reduced_digits; | ||
return Field{reduced_digits.storages[i]}; | ||
} | ||
|
||
template <unsigned NLIMBS, bool CARRY_OUT> | ||
static constexpr HOST_DEVICE_INLINE uint32_t | ||
add_limbs(const storage<NLIMBS>& xs, const storage<NLIMBS>& ys, storage<NLIMBS>& rs) | ||
|
@@ -275,6 +282,16 @@ class Field | |
return rv; | ||
} | ||
|
||
template <unsigned NLIMBS> | ||
static HOST_INLINE storage<NLIMBS> rand_storage(unsigned non_zero_limbs = NLIMBS) | ||
{ | ||
std::uniform_int_distribution<unsigned> distribution; | ||
storage<NLIMBS> value{}; | ||
for (unsigned i = 0; i < non_zero_limbs; i++) | ||
value.limbs[i] = distribution(rand_generator); | ||
return value; | ||
} | ||
|
||
// NOTE this function is used for test and examples - it assumed it is executed on a single-thread (no two threads | ||
// accessing rand_generator at the same time) | ||
static HOST_INLINE Field rand_host() | ||
|
@@ -369,6 +386,25 @@ class Field | |
xs.limbs_storage, get_m(), get_modulus(), get_modulus<2>(), get_neg_modulus())}; | ||
} | ||
|
||
// this function receives a storage object (currently supports up to 544 bits) and reduces it to a field element | ||
// between 0 and p. | ||
template <unsigned NLIMBS> | ||
static constexpr HOST_DEVICE_INLINE Field from(const storage<NLIMBS>& xs) | ||
{ | ||
Wide rs = {}; | ||
int constexpr size = (NLIMBS + TLC - 1) / TLC; | ||
for (int i = 0; i < size; i++) { | ||
Field xi = {}; // split into field size digits | ||
for (int j = 0; j < std::min(TLC, NLIMBS - i * TLC); j++) { | ||
xi.limbs_storage.limbs[j] = xs.limbs[i * TLC + j]; | ||
} | ||
Field pi = get_reduced_digit(i); // use precomputed values - pi = 2^(TLC*32*i) % p | ||
Wide temp = mul_wide(xi, pi); | ||
rs = rs + temp; // wide addition keeps the result under p^2, subtracting p^2 after every addition if needed | ||
} | ||
return reduce(rs); // finally, use barret reduction | ||
} | ||
|
||
HOST_DEVICE Field& operator=(Field const& other) | ||
{ | ||
#pragma unroll | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,13 +227,13 @@ namespace host_math { | |
static HOST_INLINE void multiply_raw_64(const uint64_t* a, const uint64_t* b, uint64_t* r) | ||
{ | ||
#pragma unroll | ||
for (unsigned j = 0; j < NLIMBS_A / 2; j++) { | ||
for (unsigned i = 0; i < NLIMBS_B / 2; i++) { | ||
uint64_t carry = 0; | ||
#pragma unroll | ||
for (unsigned i = 0; i < NLIMBS_B / 2; i++) { | ||
for (unsigned j = 0; j < NLIMBS_A / 2; j++) { | ||
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. Why did you swap the loops? 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. to fix a bug that was inserted in an earlier PR.. |
||
r[j + i] = host_math::madc_cc_64(a[j], b[i], r[j + i], carry); | ||
} | ||
r[NLIMBS_A / 2 + j] = carry; | ||
r[NLIMBS_A / 2 + i] = carry; | ||
} | ||
} | ||
|
||
|
@@ -363,12 +363,13 @@ namespace host_math { | |
{ | ||
return std::memcmp(xs.limbs, ys.limbs, NLIMBS * sizeof(xs.limbs[0])) == 0; | ||
} | ||
static constexpr void inv_log_size_err(uint32_t logn, uint32_t omegas_count) | ||
// this function checks if the given index is within the array range | ||
static constexpr void index_err(uint32_t index, uint32_t max_index) | ||
HadarIngonyama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (logn > omegas_count) | ||
if (index > max_index) | ||
THROW_ICICLE_ERR( | ||
icicle::eIcicleError::INVALID_ARGUMENT, | ||
"Field: Invalid inv index" + std::to_string(logn) + ">" + std::to_string(omegas_count)); | ||
icicle::eIcicleError::INVALID_ARGUMENT, "Field: index out of range: given index -" + std::to_string(index) + | ||
"> max index - " + std::to_string(max_index)); | ||
} | ||
|
||
template <unsigned NLIMBS> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,6 +93,7 @@ namespace params_gen { | |
} | ||
return invs; | ||
} | ||
|
||
} // namespace params_gen | ||
|
||
#define PARAMS(modulus) \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
|
||
# Check if directory is provided as an argument | ||
if [ -z "$1" ]; then | ||
echo "Usage: $0 <directory>" | ||
exit 1 | ||
fi | ||
|
||
DIRECTORY="$1" | ||
|
||
# Find and format all C, C++, header, and other relevant files | ||
find "$DIRECTORY" -name '*.c' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' | while read -r file; do | ||
echo "Formatting $file" | ||
clang-format -i "$file" | ||
done | ||
|
||
echo "All files formatted." |
Uh oh!
There was an error while loading. Please reload this page.