Skip to content
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

[Possible bug] sha2 block_type element comparison #188

Open
CblPOK-git opened this issue Feb 2, 2024 · 1 comment
Open

[Possible bug] sha2 block_type element comparison #188

CblPOK-git opened this issue Feb 2, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@CblPOK-git
Copy link
Contributor

in this example

#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>

using namespace nil::crypto3;

[[circuit]] bool sha256_example(typename hashes::sha2<256>::block_type block0,
                                                              typename hashes::sha2<256>::block_type block1) {

    return block0[0] < block1[0];

}

handle_vector_cmp is being called despite that we are comparing fields.

If trying to extract field elements and compare explicitly

#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>

using namespace nil::crypto3;

[[circuit]] bool sha256_example(typename hashes::sha2<256>::block_type block0,
                                                              typename hashes::sha2<256>::block_type block1) {

    typename algebra::curves::pallas::base_field_type::value_type a = block0[0];
    typename algebra::curves::pallas::base_field_type::value_type b = block1[0];
    a = a + b;

    return a < b;

}

then circifier fails:

[build] Unknown type!
[build] UNREACHABLE executed at /home/cblpok/zkllvm_3/zkLLVM/libs/circifier/llvm/lib/CodeGen/ValueTypes.cpp:578!
[build] PLEASE submit a bug report to https://github.com/NilFoundation/zkllvm-circifier/issues/ and include the crash backtrace, preprocessed source, and associated run script.
[build] Stack dump:
[build] 0.	Program arguments: /home/cblpok/zkllvm_3/zkLLVM/build/libs/circifier/llvm/bin/clang-17 -target assigner -Xclang -fpreserve-vec3-type -Werror=unknown-attributes -D_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION -D__ZKLLVM__ -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/algebra/include -I/home/cblpok/zkllvm_3/zkLLVM/build/include -I/usr/local/include -I -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/block/include -I/usr/local/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/blueprint/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/codec/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/containers/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/hash/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/kdf/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/mac/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/marshalling/core/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/marshalling/algebra/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/marshalling/multiprecision/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/marshalling/zk/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/math/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/modes/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/multiprecision/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/passhash/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/pbkdf/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/threshold/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/pkpad/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/pubkey/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/random/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/stream/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/vdf/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/crypto3/libs/zk/include -I/home/cblpok/zkllvm_3/zkLLVM/libs/stdlib/libcpp -I/home/cblpok/zkllvm_3/zkLLVM/libs/circifier/clang/lib/Headers -I/home/cblpok/zkllvm_3/zkLLVM/libs/stdlib/libc/include -emit-llvm -O1 -S -o sha256_cpp_example_no_stdlib_sha2_256.cpp.ll /home/cblpok/zkllvm_3/zkLLVM/examples/cpp/sha2_256.cpp
[build] 1.	<eof> parser at end of file
[build] 2.	Optimizer
[build] Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
[build] 0  libLLVMSupport.so.17         0x00007f3875d94c4d llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 61
[build] 1  libLLVMSupport.so.17         0x00007f3875d950eb
[build] 2  libLLVMSupport.so.17         0x00007f3875d93366 llvm::sys::RunSignalHandlers() + 134
[build] 3  libLLVMSupport.so.17         0x00007f3875d944be llvm::sys::CleanupOnSignal(unsigned long) + 110
[build] 4  libLLVMSupport.so.17         0x00007f3875c44644
[build] 5  libLLVMSupport.so.17         0x00007f3875c44a02
[build] 6  libc.so.6                    0x00007f3875559520
[build] 7  libc.so.6                    0x00007f38755ad9fc pthread_kill + 300
[build] 8  libc.so.6                    0x00007f3875559476 raise + 22
[build] 9  libc.so.6                    0x00007f387553f7f3 abort + 211
[build] 10 libLLVMSupport.so.17         0x00007f3875c67660 llvm::install_out_of_memory_new_handler() + 0
[build] 11 libLLVMCodeGen.so.17         0x00007f387cd66dbc llvm::MVT::getVT(llvm::Type*, bool) + 204
[build] 12 libLLVMCodeGen.so.17         0x00007f387cd66445 llvm::EVT::getEVT(llvm::Type*, bool) + 85
[build] 13 libLLVMAssignerCodeGen.so.17 0x00007f3883e30ffc
[build] 14 libLLVMAssignerCodeGen.so.17 0x00007f3883e4449e
[build] 15 libLLVMAssignerCodeGen.so.17 0x00007f3883e4355b
[build] 16 libLLVMAssignerCodeGen.so.17 0x00007f3883e3d6e1
[build] 17 libLLVMAnalysis.so.17        0x00007f3877f97cf5 llvm::TargetTransformInfo::getArithmeticInstrCost(unsigned int, llvm::Type*, llvm::TargetTransformInfo::TargetCostKind, llvm::TargetTransformInfo::OperandValueInfo, llvm::TargetTransformInfo::OperandValueInfo, llvm::ArrayRef<llvm::Value const*>, llvm::Instruction const*) const + 149
[build] 18 libLLVMVectorize.so.17       0x00007f387af8993f
[build] 19 libLLVMVectorize.so.17       0x00007f387af8176f
[build] 20 libLLVMVectorize.so.17       0x00007f387af7a36a
[build] 21 libLLVMVectorize.so.17       0x00007f387af79e89
[build] 22 libLLVMVectorize.so.17       0x00007f387af79b9b llvm::VectorCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) + 187
[build] 23 libLLVMPasses.so.17          0x00007f387430f967
[build] 24 libLLVMCore.so.17            0x00007f3876dcf59a llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) + 234
[build] 25 libLLVMAMDGPUCodeGen.so.17   0x00007f388540b3a7
[build] 26 libLLVMCore.so.17            0x00007f3876dcdff5 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 293
[build] 27 libLLVMAMDGPUCodeGen.so.17   0x00007f388540af77
[build] 28 libLLVMCore.so.17            0x00007f3876dce54a llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) + 234
[build] 29 libclangCodeGen.so.17        0x00007f387d6d05a2
[build] 30 libclangCodeGen.so.17        0x00007f387d6ca7e5
[build] 31 libclangCodeGen.so.17        0x00007f387d6c9cb7 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream>>) + 1255
[build] 32 libclangCodeGen.so.17        0x00007f387df8c1f4
[build] 33 libclangParse.so.17          0x00007f386f5d9da3 clang::ParseAST(clang::Sema&, bool, bool) + 867
[build] 34 libclangFrontend.so.17       0x00007f387a9102c1 clang::ASTFrontendAction::ExecuteAction() + 305
[build] 35 libclangCodeGen.so.17        0x00007f387df85bd6 clang::CodeGenAction::ExecuteAction() + 70
[build] 36 libclangFrontend.so.17       0x00007f387a90fcbc clang::FrontendAction::Execute() + 124
[build] 37 libclangFrontend.so.17       0x00007f387a81a008 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 952
[build] 38 libclangFrontendTool.so.17   0x00007f387f9fecc7 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 935
[build] 39 clang-17                     0x0000000000432753 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1363
[build] 40 clang-17                     0x00000000004221fa
[build] 41 clang-17                     0x0000000000423b6d
[build] 42 clang-17                     0x0000000000423b3d
[build] 43 libclangDriver.so.17         0x00007f387a29e4a9
[build] 44 libclangDriver.so.17         0x00007f387a297e38
[build] 45 libclangDriver.so.17         0x00007f387a297e05
[build] 46 libLLVMSupport.so.17         0x00007f3875c2fc29
[build] 47 libLLVMSupport.so.17         0x00007f3875c4445a llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) + 234
[build] 48 libclangDriver.so.17         0x00007f387a297757 clang::driver::CC1Command::Execute(llvm::ArrayRef<std::optional<llvm::StringRef>>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, bool*) const + 471
[build] 49 libclangDriver.so.17         0x00007f387a21518f clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&, bool) const + 799
[build] 50 libclangDriver.so.17         0x00007f387a215397 clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&, bool) const + 167
[build] 51 libclangDriver.so.17         0x00007f387a236fc8 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*>>&) + 568
[build] 52 clang-17                     0x0000000000421cb8 clang_main(int, char**, llvm::ToolContext const&) + 4280
[build] 53 clang-17                     0x0000000000456c3d main + 61
[build] 54 libc.so.6                    0x00007f3875540d90
[build] 55 libc.so.6                    0x00007f3875540e40 __libc_start_main + 128
[build] 56 clang-17                     0x0000000000420955 _start + 37
[build] clang-17: error: clang frontend command failed with exit code 134 (use -v to see invocation)
[build] clang version 17.0.4 ([email protected]:NilFoundation/zkllvm-circifier.git 9266caaa758aef44f272c7f814485f4f49496960)
[build] Target: assigner
[build] Thread model: posix
[build] InstalledDir: /home/cblpok/zkllvm_3/zkLLVM/build/libs/circifier/llvm/bin
[build] clang-17: note: diagnostic msg: 
[build] ********************
[build] 
[build] PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
[build] Preprocessed source(s) and associated run script(s) are located at:
[build] clang-17: note: diagnostic msg: /tmp/sha2_256-2b8434.cpp
[build] clang-17: note: diagnostic msg: /tmp/sha2_256-2b8434.sh
[build] clang-17: note: diagnostic msg: 
[build] 
[build] ********************
[build] ninja: build stopped: subcommand failed.
@CblPOK-git CblPOK-git added the bug Something isn't working label Feb 2, 2024
@makxenov
Copy link
Contributor

makxenov commented Feb 5, 2024

The generated code for the first example is correct, but because of an optimization it makes vector comparison first, and then extracts the first element of the result vector. It means that the cost model considers vector comparison and field comparison relatively the same. I think we need to disable this optimization for our target.

; Function Attrs: circuit mustprogress nounwind
define dso_local noundef zeroext i1 @_Z14sha256_exampleDv2_u26__zkllvm_field_pallas_baseS_(<2 x __zkllvm_field_pallas_base> noundef %block0, <2 x __zkllvm_field_pallas_base> noundef %block1) local_unnamed_addr #0 {
entry:
  %0 = icmp ult <2 x __zkllvm_field_pallas_base> %block0, %block1
  %cmp = extractelement <2 x i1> %0, i32 0
  ret i1 %cmp
}

The second example fails inside another optimization, but passes with O0 level.

@akokoshn akokoshn transferred this issue from NilFoundation/zkLLVM Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants