8000 Incremental clang-tidy fixes by dtebbs · Pull Request #213 · clearmatics/zeth · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Incremental clang-tidy fixes #213

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

Merged
merged 19 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6DB6
103 changes: 76 additions & 27 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
---

#
# Clang-tidy configuration for Zeth
# This file follows the YAML syntax, see: https://yaml.org/spec/
Expand Down Expand Up @@ -27,7 +25,43 @@
# flexible. We consider this negligible compared to the benefits of
# manipulating variables that are explicitly typed.
#
Checks: >
# -modernize-use-trailing-return-type,
# -modernize-return-braced-init-list,
# These are removed since they conflict with the current code style.
#
# -modernize-pass-by-value
# -modernize-deprecated-headers
# -modernize-use-default-member-init
# -readability-redundant-member-init
# -bugprone-exception-escape
# These conflict with the current code style, or would require larger
# changes. May wish to review in the future.
#
# -readability-magic-numbers
# Disabled for now as it picks up some constants which don't necessarily need
# their own variables (for some trivial multiplication and masking) and some
# test data. May wish to review in the future.
#
# -readability-implicit-bool-conversion
# We use implicit bool conversion for specifying many constants in terms of
# `0` and `1`. (TODO: it may be a good idea to enable this to fix implicit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can enable this check bu we will need to add a lot of NOLINT comments to disable the checks when we use pb_variable_arrays of booleans (like in blake/sha). This is what I did on #212

# conversion in conditions).
#
# -readability-named-parameter,
# We occasionally omit the name of a parameter in a function definition if the
# parameter is unused, and avoid a compiler warning. This check complains
# in these cases.
#
# -readability-identifier-naming
# TODO: Retry this (attempts to customize this to our standards failed under
# clang-tidy-10 - see CheckOptions below).
#
# -misc-non-private-member-variables-in-classes,
# TODO: Public member variables are often used in our code, and in
# dependencies. There is a good argument for not exposing these. Decide on a
# policy and update this comment.

Checks:
-*,
bugprone-*,
llvm-*,
Expand All @@ -38,30 +72,45 @@ Checks: >
readability-*,
-modernize-avoid-c-arrays,
-modernize-use-bool-literals,
-modernize-use-auto
-modernize-use-auto,
-modernize-use-trailing-return-type,
-readability-magic-numbers,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use "magic numbers" in many places? I like this check actually

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general it's pretty good, but picks up a few bits of test data and some numbers in expressions (masking expressions, etc) which don't really warrant a whole separate constant. It would be nice if it was a tiny bit less sensitive, so we may want to revisit in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks

-readability-implicit-bool-conversion,
-modernize-return-braced-init-list,
-readability-named-parameter,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use functions with unnamed arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the case of unused parameters we remove the name to stop the compiler complaining.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes right...

-modernize-pass-by-value,
-modernize-use-default-member-init,
-bugprone-exception-escape,
-misc-non-private-member-variables-in-classes,
-readability-redundant-member-init,
-modernize-deprecated-headers,
-readability-identifier-naming,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see that you have disabled many checks here, which is fine (if we can't do otherwise for now). I think it would be nice to add a few lines about why each check has been disabled (as I started to do above) in order to make it crystal clear that we have a reason to remove the checks.

Eg. If we have a lot of magic numbers (in the tests for eg) we can add a line to say "-readability-magic-numbers is removed because we have a lot of magic numbers in the tests [...]". Likewise, I imagine that you removed readability-implicit-bool-conversion because of the boolean vectors we use in the hash implementation, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments about the disabled checks.


WarningsAsErrors:
'*'

HeaderFilterRegex: 'libzeth/*.(h|hpp|cpp)'

CheckOptions:
- key: readability-identifier-naming.MacroDefinitionCase
value: UPPER_CASE
- key: readability-identifier-naming.StaticConstantCase
value: UPPER_CASE
- key: readability-identifier-naming.ConstantParameterCase
value: lower_case
- key: readability-identifier-naming.FunctionCase
value: lower_case
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.ClassCase
value: lower_case
- key: readability-identifier-naming.ClassMemberCase
value: lower_case
- key: readability-identifier-naming.NamespaceCase
value: lower_case
- key: readability-identifier-naming.TemplateParameterCase
value: CamelCase
- key: readability-identifier-naming.TemplateParameterSuffix
value: T
- key: readability-implicit-bool-conversion.AllowIntegerConditions
value: '1'
# CheckOptions:
# - key: readability-identifier-naming.MacroDefinitionCase
# value: UPPER_CASE
# - key: readability-identifier-naming.StaticConstantCase
# value: UPPER_CASE
# - key: readability-identifier-naming.ConstantParameterCase
# value: lower_case
# - key: readability-identifier-naming.FunctionCase
# value: lower_case
# - key: readability-identifier-naming.VariableCase
# value: lower_case
# - key: readability-identifier-naming.ClassCase
# value: lower_case
# - key: readability-identifier-naming.ClassMemberCase
# value: lower_case
# - key: readability-identifier-naming.NamespaceCase
# value: lower_case
# - key: readability-identifier-naming.TemplateParameterCase
# value: CamelCase
# - key: readability-identifier-naming.TemplateParameterSuffix
# value: T
# - key: readability-implicit-bool-conversion.AllowIntegerConditions
# value: '1'
5 changes: 5 additions & 0 deletions .cppcheck.suppressions
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Errors and Warnings to Suppress
useStlAlgorithm:*

// This raises some false positives, and inline suppressions seem not to work
unusedFunction:*
9 changes: 9 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ jobs:
submodules: recursive
- name: Execute
run: CI_USE_DOCKER=1 CI_CONFIG=Release CI_ZKSNARK=PGHR13 scripts/ci build

check-cpp-linux:
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
with:
submodules: recursive
- name: Execute
run: CI_CHECK_FORMAT=1 CI_USE_DOCKER=1 scripts/ci check_cpp
29 changes: 19 additions & 10 deletions cmake/clang_dev_tools.cmake
3D11
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ option(
if(USE_CLANG_FORMAT)
find_program(CLANG_FORMAT "clang-format")
if(USE_CLANG_FORMAT)
message("clang-format found!")
message("Using clang-format. Creating target... To run, use: make clang-format")
add_custom_target(
clang-format
Expand All @@ -42,15 +41,26 @@ endif()
# The configuration below requires cmake 3.6.3+
# (https://cmake.org/cmake/help/v3.6/variable/CMAKE_LANG_CLANG_TIDY.html)
if(USE_CLANG_TIDY)
find_program(CLANG_TIDY clang-tidy DOC "Path to the clang-tidy tool")
find_program(CLANG_TIDY clang-tidy DOC "Path to clang-tidy tool")
if(CLANG_TIDY)
message("clang-tidy found!")
message("Using clang-tidy. Creating target... To run, use: make clang-tidy (assumes run-clang-tidy.py has been fetched)")
add_custom_target(
clang-tidy
COMMAND python run-clang-tidy.py
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
find_program(RUN_CLANG_TIDY run-clang-tidy.py DOC "Path to run-clang-tidy")
if(RUN_CLANG_TIDY)
message("Using clang-tidy. Creating target... To run, use: make clang-tidy")
add_custom_target(
clang-tidy
COMMAND run-clang-tidy.py
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
)
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/api)
file(WRITE ${PROJECT_BINARY_DIR}/api/.clang-tidy
"Checks: '-*,misc-definitions-in-headers'
CheckOptions:
- { key: HeaderFileExtensions, value: \"x\" }")
else()
message(
FATAL_ERROR
"run-clang-tidy.py not found. (Download and place in PATH). Aborting...")
endif()
else()
message(FATAL_ERROR "clang-tidy not found. Aborting...")
endif()
Expand All @@ -62,7 +72,6 @@ endif()
if(USE_IWYU)
find_program(IWYU include-what-you-use DOC "Path to the include-what-you-use tool")
if(USE_IWYU)
message("IWYU found!")
message("Using include-what-you-use...")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${IWYU};-Xiwyu;")
else()
Expand Down
16 changes: 14 additions & 2 deletions cmake/gcc_dev_tools.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ option(
# Adding clang-format target if executable is found
if(USE_CPP_CHECK)
find_program(CPP_CHECK "cppcheck")
if(USE_CPP_CHECK)
if(CPP_CHECK)
message("cppcheck found!")
message("Using cppcheck. Creating target... To run, use: make cppcheck")
# TODO: gtest includes are not found by cppcheck, because they are marked
# as "SYSTEM" (to prevent compile warnings). We exclude all tests for now,
# to avoid many "syntaxError" reports, but the analysis should be run on
# tests.
add_custom_target(
cppcheck
COMMAND cppcheck
--project=compile_commands.json
--project=${CMAKE_CURRENT_BINARY_DIR}/compile_commands.json
--enable=all
--suppressions-list=${PROJECT_SOURCE_DIR}/.cppcheck.suppressions
--inline-suppr
--quiet
-i${DEPENDS_DIR}
--suppress='*:${DEPENDS_DIR}/*'
-i${PROJECT_BINARY_DIR}/api
--suppress='*:${PROJECT_BINARY_DIR}/api/*'
--suppress='unmatchedSuppression:*'
--error-exitcode=1
--suppress='*:${PROJECT_SOURCE_DIR}/libzeth/tests/*'
)
else()
message(FATAL_ERROR "cppcheck not found. Aborting...")
Expand Down
5 changes: 5 additions & 0 deletions depends/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Disable all checks.
# (Dummy check here prevents error from clang-tidy)
Checks: '-*,misc-definitions-in-headers'
CheckOptions:
- { key: HeaderFileExtensions, value: "x" }
11 changes: 3 additions & 8 deletions libzeth/circuits/binary_operation.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -251,15 +251,10 @@ void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_constraints(
template<typename FieldT>
void double_bit32_sum_eq_gadget<FieldT>::generate_r1cs_witness()
{
bits32 a_bits32;
bits32 b_bits32;
for (size_t i = 0; i < 32; i++) {
a_bits32[i] = a.get_bits(this->pb)[i];
b_bits32[i] = b.get_bits(this->pb)[i];
}

bits32 a_bits32 = bits32::from_vector(a.get_bits(this->pb));
bits32 b_bits32 = bits32::from_vector(b.get_bits(this->pb));
bits32 left_side_acc = bits_add<32>(a_bits32, b_bits32, false);
res.fill_with_bits(this->pb, bits32_to_vector(left_side_acc));
left_side_acc.fill_variable_array(this->pb, res);
}

} // namespace libzeth
Expand Down
29 changes: 15 additions & 14 deletions libzeth/circuits/blake2s/blake2s.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,66 +25,67 @@ template<typename FieldT> class BLAKE2s_256 : public libsnark::gadget<FieldT>
{
private:
// Parameter block, size set to 32 bytes, fanout and depth set to serial
// mode. See: Section 2.8 https://blake2.net/blake2.pdf Table 2
std::array<std::array<bool, BLAKE2s_word_size>, 8> parameter_block = {
// mode. See: Section 2.8 https://blake2.net/blake2.pdf Table 2. Note, byte
// endianness has been swapped to avoid doing this repeatedly at runtime.
Copy link
Contributor

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 "to avoid doing this repeatedly at runtime"?

Copy link
Contributor Author
@dtebbs dtebbs May 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, these constants were endian-swappd before being used (see the .tcc file), and this swap was happening for every invocation of generate_r1cs_witness for every instance of the gadget. Those constants did not seem to be used anywhere else, so the byte swapping seemed unnecessary.

What do you mean by "to avoid doing this repeatedly at runtime"?

It refers to the byte swapping. After this change, it's swapped in the static data, so it doesn't have to be done many times when running the proof gen. (I also put the comment for anyone looking at the reference above, in case they see a discrepency in the order).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I see. The parameter block structure was taken from Table 2. https://blake2.net/blake2.pdf and since blake processed byte strings as little endian the endianness needed to be switched.
Yes that's fine to switch the endianness directly in the parameter block definition. This is what is done in the RFC (see section 2.5 https://tools.ietf.org/html/rfc7693#page-5)

const std::array<const bits<BLAKE2s_word_size>, 8> parameter_block = {
{{
// Digest byte length, Key byte length, Fanout, Depth
0, 0, 1, 0, 0, 0, 0, 0, // 0x20 (32 bytes)
0, 0, 0, 0, 0, 0, 0, 0, // 0x00 (key length)
0, 0, 0, 0, 0, 0, 0, 1, // 0x01 (depth 1)
0, 0, 0, 0, 0, 0, 0, 1, // 0x01 (fanout 1)
0, 0, 0, 0, 0, 0, 0, 1 // 0x01 (depth 1)
0, 0, 0, 0, 0, 0, 0, 0, // 0x00 (key length)
0, 0, 1, 0, 0, 0, 0, 0, // 0x20 (32 bytes)
},
{
// Leaf length
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
},
{
// Node offset
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
},
{
// Node offset (cont.), Node depth, Inner length
0, 0, 0, 0, 0, 0, 0, 0, // 00 (inner length)
0, 0, 0, 0, 0, 0, 0, 0, // 00 (node depth)
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00 (node depth)
0, 0, 0, 0, 0, 0, 0, 0 // 00 (inner length)
},
{
// Salt
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
},
{
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
},
{
// Personalization
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
},
{
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
0, 0, 0, 0, 0, 0, 0, 0 // 00
0, 0, 0, 0, 0, 0, 0, 0, // 00
}}};

// See: Appendix A.2 of https://blake2.net/blake2.pdf for the specification
// of the IV used in BLAKE2s
std::array<std::array<bool, BLAKE2s_word_size>, 8> BLAKE2s_IV = {
std::array<bits<BLAKE2s_word_size>, 8> BLAKE2s_IV = {
{{
0, 1, 1, 0, 1, 0, 1, 0, // 6A
0, 0, 0, 0, 1, 0, 0, 1, // 09
Expand Down
5 changes: 2 additions & 3 deletions libzeth/circuits/blake2s/blake2s.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ template<typename FieldT> void BLAKE2s_256<FieldT>::generate_r1cs_witness()
// See: Appendix A.1 of https://blake2.net/blake2.pdf
std::vector<bool> h_bits;
for (size_t i = 0; i < 8; i++) {
std::array<bool, BLAKE2s_word_size> pb_swapped =
swap_byte_endianness(parameter_block[i]);
bits<BLAKE2s_word_size> pb_swapped = parameter_block[i];
std::vector<bool> h_part =
bits32_to_vector(bits_xor(pb_swapped, BLAKE2s_IV[i]));
bits_xor(pb_swapped, BLAKE2s_IV[i]).to_vector();
h_bits.insert(h_bits.end(), h_part.begin(), h_part.end());
}
h[0].bits.fill_with_bits(this->pb, h_bits);
Expand Down
4 changes: 2 additions & 2 deletions libzeth/circuits/blake2s/blake2s_comp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ class BLAKE2s_256_comp : public libsnark::gadget<FieldT>
private:
// See: Appendix A.2 of https://blake2.net/blake2.pdf for the specification
// of the IV used in BLAKE2s
static const std::array<std::array<bool, BLAKE2s_word_size>, 8> BLAKE2s_IV;
static const std::array<bits<BLAKE2s_word_size>, 8> BLAKE2s_IV;

// Section 2.1 of https://blake2.net/blake2.pdf specifies that BLAKE2s has
// 10 rounds
static const int rounds = 10;

// Low and High words of the offset
std::array<std::array<bool, BLAKE2s_word_size>, 2> t;
std::array<bits<BLAKE2s_word_size>, 2> t;

// Chaining values
libsnark::digest_variable<FieldT> h;
Expand Down
Loading
0