Skip to content

Commit

Permalink
Spell checker
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-shterman committed Jan 30, 2025
1 parent 99abe0f commit 3ec02ae
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 123 deletions.
126 changes: 65 additions & 61 deletions icicle/backend/cpu/src/hash/cpu_poseidon2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,21 @@ namespace icicle {
const unsigned arity = m_use_domain_tag ? m_t - 1 : m_t;
bool is_sponge = false;
int input_size_in_scalars = size / sizeof(S);
if ((config.batch == 1) && (input_size_in_scalars != (m_use_domain_tag ? m_t - 1 : m_t))) { // Check if sponge function.
if ((config.batch == 1) && (input_size_in_scalars != (m_use_domain_tag ? m_t - 1 : m_t))) { // Check if sponge
// function.
is_sponge = true;
if (config.batch != 1) {
ICICLE_LOG_ERROR
<< "The only suppoorted value of config.batch for sponge functions is 1.\n";
ICICLE_LOG_ERROR << "The only suppoorted value of config.batch for sponge functions is 1.\n";

Check failure on line 209 in icicle/backend/cpu/src/hash/cpu_poseidon2.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

suppoorted ==> supported
return eIcicleError::INVALID_ARGUMENT;
}
} // sponge function
else { // Non-sponge function.
} // sponge function
else { // Non-sponge function.
if ((m_use_domain_tag ? input_size_in_scalars : input_size_in_scalars - 1) % (m_t - 1) != 0) {
ICICLE_LOG_ERROR
<< "Padding isn't supported for non-sponge function hash. The following should be true: ((m_use_domain_tag ? size : size-1) % (m_t-1) != 0).\n";
ICICLE_LOG_ERROR << "Padding isn't supported for non-sponge function hash. The following should be true: "
"((m_use_domain_tag ? size : size-1) % (m_t-1) != 0).\n";
return eIcicleError::INVALID_ARGUMENT;
}
} // Non-sponge function.
} // Non-sponge function.

const unsigned int T = m_t;
bool is_unsupported_T_for_this_field = poseidon2_constants[T].nof_upper_full_rounds == 0;
Expand All @@ -234,27 +234,27 @@ namespace icicle {
S* partial_matrix_diagonal_m1 = poseidon2_constants[T].partial_matrix_diagonal_m1;

// Allocate temporary memory for intermediate calcs and in order not to change the input.
// int sponge_nof_hashers = m_use_domain_tag ? (input_size_in_scalars / arity) : ((input_size_in_scalars - 1) / (arity - 1));
// int tmp_fields_nof_scalars = is_sponge ? (T * sponge_nof_hashers) : (T * config.batch);
// S* tmp_fields = new S[tmp_fields_nof_scalars];
// int sponge_nof_hashers = m_use_domain_tag ? (input_size_in_scalars / arity) : ((input_size_in_scalars - 1) /
// (arity - 1)); int tmp_fields_nof_scalars = is_sponge ? (T * sponge_nof_hashers) : (T * config.batch); S*
// tmp_fields = new S[tmp_fields_nof_scalars];
S* tmp_fields;
S* tmp_fields_init_ptr; // This pointer to keep initial tmp_fields value to perform a easy rollback when needed.
S* tmp_fields_init_ptr; // This pointer to keep initial tmp_fields value to perform a easy rollback when needed.
int sponge_nof_hashers;
const S* in_fields = (S*)(input);
int padding_size = 0;
S* padding;
if (is_sponge) {
if (input_size_in_scalars < T) { // Single hasher in the chain.
if (input_size_in_scalars < T) { // Single hasher in the chain.
sponge_nof_hashers = 1;
padding_size = T - (input_size_in_scalars + (m_use_domain_tag == true));
} else if (input_size_in_scalars >= T) { // More than a single hasher in the chain.
sponge_nof_hashers = (input_size_in_scalars - !(m_use_domain_tag == true) + (T - 2)) / (T - 1);
bool is_padding_needed = (input_size_in_scalars - !(m_use_domain_tag == true)) % (T - 1);
if (is_padding_needed ) {
} else if (input_size_in_scalars >= T) { // More than a single hasher in the chain.
sponge_nof_hashers = (input_size_in_scalars - !(m_use_domain_tag == true) + (T - 2)) / (T - 1);
bool is_padding_needed = (input_size_in_scalars - !(m_use_domain_tag == true)) % (T - 1);
if (is_padding_needed) {
padding_size = (T - 1) - ((input_size_in_scalars - !(m_use_domain_tag == true)) % (T - 1));
}
}
if (padding_size > 0) { // Fill padding array with 1,0,0,...
if (padding_size > 0) { // Fill padding array with 1,0,0,...
padding = new S[padding_size];
padding[0] = S::from(1);
for (int i = 1; i < padding_size; i++) {
Expand All @@ -264,105 +264,102 @@ namespace icicle {
tmp_fields = new S[T * sponge_nof_hashers];
tmp_fields_init_ptr = tmp_fields;
// Take care of hasher 0. It's done separately of the rest of the hashers because of the domain tag.
if (m_use_domain_tag) {
if (m_use_domain_tag) {
// Domain tag exists only for the first hasher. For the rest of the hashers this
// input is undefined at this stage and its value will be set later.
// tmp_fields = {{dt, in0}, {undef, in1}, {undef, in2}, etc.}
memcpy(tmp_fields, &m_domain_tag, sizeof(S));
}
else {
} else {
// tmp_fields = {{in0 (T inputs)}, {undef, in1 (T-1 inputs)}, {under, in2 (T-1 inputs)}, etc.}
memcpy(tmp_fields, &in_fields[0], sizeof(S));
in_fields += 1;
in_fields += 1;
}
tmp_fields += 1;
tmp_fields += 1;
// Take care of rest of the hashers (T-1 scalar to each hasher).
for (int hasher_idx = 0; hasher_idx < sponge_nof_hashers; hasher_idx++) {
if (hasher_idx == sponge_nof_hashers-1 && padding_size > 0) {
if (hasher_idx == sponge_nof_hashers - 1 && padding_size > 0) {
// Last hasher in the chain. Take care of padding if needed.
memcpy(tmp_fields, in_fields, (T - padding_size - 1) * sizeof(S));
memcpy(tmp_fields + T - padding_size - 1, padding, padding_size * sizeof(S));
}
else { // Not a last hasher in the chain. There is no padding.
} else { // Not a last hasher in the chain. There is no padding.
memcpy(tmp_fields, in_fields, (T - 1) * sizeof(S));
}
in_fields += (T - 1);
tmp_fields += T;
}
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
} // if (is_sponge) {
else { // Not a sponge function. The is no padding.
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
} // if (is_sponge) {
else { // Not a sponge function. The is no padding.
// Input of each hash should have domain tag at its input.
// tmp_fields = {{dt, in0 (T-1 inputs)}, {dt, in1 (T-1 inputs)}, {dt, in2 (T-1 inputs)}, etc.}
tmp_fields = new S[T * config.batch];
tmp_fields_init_ptr = tmp_fields; // Keep tmp_fields pointer for delete.
if (m_use_domain_tag) {
tmp_fields_init_ptr = tmp_fields; // Keep tmp_fields pointer for delete.
if (m_use_domain_tag) {
for (int batch_idx = 0; batch_idx < config.batch; batch_idx++) {
memcpy(tmp_fields, &m_domain_tag, sizeof(S));
memcpy(tmp_fields + 1, in_fields, (T - 1) * sizeof(S));
in_fields += (T - 1);
tmp_fields += T;
}
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
}
else {
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
} else {
// tmp_fields = {{in0 (T inputs)}, {in1 (T inputs)}, {in2 (T inputs)}, etc.}
memcpy(tmp_fields, in_fields, T * config.batch * sizeof(S));
}
}

// Hashes processing.
if (is_sponge) {
// Call hash_single for hasher[0]
eIcicleError err = hash_single(tmp_fields /* input */, tmp_fields /* output */,
alpha, nof_upper_full_rounds, nof_partial_rounds, nof_bottom_full_rounds,
rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
S* tmp_fields_tmp_ptr = tmp_fields; // Save current pointer in order to access prev output.
// Call hash_single for hasher[0]
eIcicleError err = hash_single(
tmp_fields /* input */, tmp_fields /* output */, alpha, nof_upper_full_rounds, nof_partial_rounds,
nof_bottom_full_rounds, rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
S* tmp_fields_tmp_ptr = tmp_fields; // Save current pointer in order to access prev output.
if (err != eIcicleError::SUCCESS) return err;
if (sponge_nof_hashers != 1) {
tmp_fields[T] = tmp_fields[0]; // Current first output is an input to the next hasher.
tmp_fields[T] = tmp_fields[0]; // Current first output is an input to the next hasher.
}
tmp_fields += T;
// Process rest of the hashers.
for (int hasher_idx = 1; hasher_idx < sponge_nof_hashers; hasher_idx++) {
for (int hasher_idx = 1; hasher_idx < sponge_nof_hashers; hasher_idx++) {
// The first output of the prev hasher is the first input of the current hasher.
// The T-1 new inputs of the current hasher should be added to the T-1 outputs of the
// prev hasher (starting fom index 1).

Check failure on line 327 in icicle/backend/cpu/src/hash/cpu_poseidon2.cpp

View workflow job for this annotation

GitHub Actions / Check Spelling

fom ==> from, form
for (int i = 1; i < T; i++) {
tmp_fields[i] = tmp_fields_tmp_ptr[i] + tmp_fields[i];
}
eIcicleError err = hash_single(tmp_fields /* input */, tmp_fields /* output */,
alpha, nof_upper_full_rounds, nof_partial_rounds, nof_bottom_full_rounds,
rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
tmp_fields_tmp_ptr = tmp_fields; // Save current pointer in order to access prev output.
eIcicleError err = hash_single(
tmp_fields /* input */, tmp_fields /* output */, alpha, nof_upper_full_rounds, nof_partial_rounds,
nof_bottom_full_rounds, rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
tmp_fields_tmp_ptr = tmp_fields; // Save current pointer in order to access prev output.
if (err != eIcicleError::SUCCESS) return err;
if (hasher_idx != sponge_nof_hashers - 1) // Not to do in the last loop to prevent mem leak.
tmp_fields[T] = tmp_fields[0]; // Fill first scalar of the input to the next hasher.
tmp_fields += T; // Now tmp_fields points to input of the next hasher before the addition.
} // for (int hasher_idx = 1; hasher_idx < sponge_nof_hashers; hasher_idx++) {
tmp_fields -= T; // Rollback to the last hasher output.
if (hasher_idx != sponge_nof_hashers - 1) // Not to do in the last loop to prevent mem leak.
tmp_fields[T] = tmp_fields[0]; // Fill first scalar of the input to the next hasher.
tmp_fields += T; // Now tmp_fields points to input of the next hasher before the addition.
} // for (int hasher_idx = 1; hasher_idx < sponge_nof_hashers; hasher_idx++) {
tmp_fields -= T; // Rollback to the last hasher output.
memcpy(output, (std::byte*)(&tmp_fields[1]), sizeof(S));
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
}
else { // Not a sponge function.
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
} else { // Not a sponge function.
for (int batch_hash_idx = 0; batch_hash_idx < config.batch; batch_hash_idx++) {
eIcicleError err = hash_single(tmp_fields /* input */, tmp_fields /* output */,
alpha, nof_upper_full_rounds, nof_partial_rounds, nof_bottom_full_rounds,
rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
eIcicleError err = hash_single(
tmp_fields /* input */, tmp_fields /* output */, alpha, nof_upper_full_rounds, nof_partial_rounds,
nof_bottom_full_rounds, rounds_constants, mds_matrix, partial_matrix_diagonal_m1);
if (err != eIcicleError::SUCCESS) return err;
memcpy(output, (std::byte*)(&tmp_fields[1]), sizeof(S));
tmp_fields += T;
output += sizeof(S);
}
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
tmp_fields = tmp_fields_init_ptr; // Rollback to initial value.
}

delete[] tmp_fields;
if (padding_size != 0) delete[] padding;
tmp_fields = nullptr;

return eIcicleError::SUCCESS;
} // eIcicleError hash(const std::byte* input, uint64_t size, const HashConfig& config, std::byte* output) const override
} // eIcicleError hash(const std::byte* input, uint64_t size, const HashConfig& config, std::byte* output) const
// override

private:
// // DEBUG start. Do not remove!!!
Expand All @@ -383,9 +380,16 @@ namespace icicle {

// This function performs a single hash according to parameters in the poseidon2_constants[] struct.
// eIcicleError hash_single(const std::byte* input, std::byte* output) const
eIcicleError hash_single(S* tmp_fields, S* hasher_output, int alpha, int nof_upper_full_rounds,
int nof_partial_rounds, int nof_bottom_full_rounds,
S* rounds_constants, S* mds_matrix, S* partial_matrix_diagonal_m1) const
eIcicleError hash_single(
S* tmp_fields,
S* hasher_output,
int alpha,
int nof_upper_full_rounds,
int nof_partial_rounds,
int nof_bottom_full_rounds,
S* rounds_constants,
S* mds_matrix,
S* partial_matrix_diagonal_m1) const
{
const unsigned int T = m_t;

Expand Down
2 changes: 1 addition & 1 deletion icicle/include/icicle/fields/quartic_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class QuarticExtensionField
FF::reduce(
(CONFIG::nonresidue_is_negative
? (FF::mul_wide(xs.real, x0) + FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))
: (FF::mul_wide(xs.real, x0))-FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))),
: (FF::mul_wide(xs.real, x0)) - FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im2, x2)))),
FF::reduce(
(CONFIG::nonresidue_is_negative
? FWide::neg(FF::template mul_unsigned<CONFIG::nonresidue>(FF::mul_wide(xs.im3, x2)))
Expand Down
12 changes: 8 additions & 4 deletions icicle/include/icicle/fields/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(1))
#endif
storage<1> {
storage<1>
{
static constexpr unsigned LC = 1;
uint32_t limbs[1];
};
Expand All @@ -27,7 +28,8 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(1))
#endif
storage<3> {
storage<3>
{
static constexpr unsigned LC = 3;
uint32_t limbs[3];
};
Expand All @@ -38,7 +40,8 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(LIMBS_COUNT))
#endif
storage {
storage
{
static_assert(LIMBS_COUNT % 2 == 0, "odd number of limbs is not supported\n");
static constexpr unsigned LC = LIMBS_COUNT;
union { // works only with even LIMBS_COUNT
Expand All @@ -52,6 +55,7 @@ struct
#ifdef __CUDA_ARCH__
__align__(LIMBS_ALIGNMENT(LIMBS_COUNT))
#endif
storage_array {
storage_array
{
storage<LIMBS_COUNT> storages[OMEGAS_COUNT];
};
Loading

0 comments on commit 3ec02ae

Please sign in to comment.