Skip to content

Commit

Permalink
chore: reduce number of gates in stdlib/sha256 hash function (#8905)
Browse files Browse the repository at this point in the history
We can reduce number of gates for round variables a and e in sha256.

At the start of the round variables a and e were converted in maj and ch
form respectively. But after that their .sparse form was replaced in
functions majority and choose with the same values, and this procedure
added some unnecessary gates.

We can fix this by just initializing a and e using default constructors
and put in .normal part values of h_init[0] and h_init[4]. After that
functions majority and choose will add in .sparse values of lookup
automatically

All tests for stdlib/sha256 have passed after this patch. As a result,
number of gates from sha256_nist_vector_five were reduced from 65194 to
65104.

---------

Co-authored-by: Rumata888 <isennovskiy@gmail.com>
  • Loading branch information
DanielKotov and Rumata888 authored Oct 2, 2024
1 parent 78fa676 commit dd3a27e
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,16 @@ std::array<field_t<Builder>, 8> sha256_block(const std::array<field_t<Builder>,
/**
* Initialize round variables with previous block output
**/
auto a = map_into_maj_sparse_form(h_init[0]);
/**
* We can initialize round variables a and c and put value h_init[0] and
* h_init[4] in .normal, and don't do lookup for maj_output, because majority and choose
* functions will do that in the next step
**/
sparse_value<Builder> a = sparse_value<Builder>(h_init[0]);
auto b = map_into_maj_sparse_form(h_init[1]);
auto c = map_into_maj_sparse_form(h_init[2]);
auto d = map_into_maj_sparse_form(h_init[3]);
auto e = map_into_choose_sparse_form(h_init[4]);
sparse_value<Builder> e = sparse_value<Builder>(h_init[4]);
auto f = map_into_choose_sparse_form(h_init[5]);
auto g = map_into_choose_sparse_form(h_init[6]);
auto h = map_into_choose_sparse_form(h_init[7]);
Expand Down

0 comments on commit dd3a27e

Please sign in to comment.