diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/assert.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/assert.hpp index aa8ba838a19..7395e01d903 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/assert.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/assert.hpp @@ -1,5 +1,5 @@ #pragma once -#include "assert.h" +#include #include #include #include diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/c_bind.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/c_bind.cpp index 14c05b7d334..2b2aa0f8115 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/c_bind.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/c_bind.cpp @@ -22,13 +22,13 @@ void thread_test_entry_point(test_threads_data* v) // Do some meaningless work. std::for_each(data.begin(), data.end(), [](auto& i) { i = i & 0x80; }); std::for_each(data.begin(), data.end(), [](auto& i) { i = i | 0x01; }); - std::for_each(data.begin(), data.end(), [](auto& i) { i = (uint8_t)(i << 3); }); + std::for_each(data.begin(), data.end(), [](auto& i) { i = static_cast(i << 3); }); (v->counter)++; } // info("thread end with counter at: ", static_cast(v->counter), " ", t.seconds(), "s"); } -void thread_test_abort_entry_point(void*) +void thread_test_abort_entry_point(void* /*unused*/) { info("thread_test_abort aborting"); std::abort(); @@ -58,7 +58,7 @@ WASM_EXPORT void test_threads(uint32_t const* thread_num, uint32_t const* iterat WASM_EXPORT void test_thread_abort() { - std::thread thread(thread_test_abort_entry_point, (void*)0); + std::thread thread(thread_test_abort_entry_point, (void*)nullptr); thread.join(); } @@ -72,9 +72,12 @@ WASM_EXPORT void test_abort() WASM_EXPORT void test_stdout_stderr() { - fprintf(stdout, "c: hello stdout!"); - fflush(stdout); - fprintf(stderr, "c: hello stderr!"); + // refactoring our file access methods to fix this warning is not worth it! + // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg) + static_cast(fprintf(stdout, "c: hello stdout!")); + static_cast(fflush(stdout)); + static_cast(fprintf(stderr, "c: hello stderr!")); + // NOLINTEND(cppcoreguidelines-pro-type-vararg) std::cout << "c++: hello stdout!" << std::flush; std::cerr << "c++: hello stderr!"; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/constexpr_utils.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/constexpr_utils.hpp index 23079bff708..d25d8a15b88 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/constexpr_utils.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/constexpr_utils.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -165,7 +165,7 @@ constexpr auto concatenate_arrays(const std::array&... arrays) * cannot be constexpr) */ template -constexpr std::array create_array(T value, std::index_sequence) +constexpr std::array create_array(T value, std::index_sequence /*unused*/) { // cast Is to void to remove the warning: unused value std::array result = { { (static_cast(Is), value)... } }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/container.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/container.hpp index 7fb2f26c392..be010ee5d00 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/container.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/container.hpp @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #include @@ -53,7 +53,7 @@ InnerCont flatten(Cont const& in) // Return the first index at which a given item can be found in the vector. // Only safe for vectors with length less than the size_t overflow size. -template long index_of(std::vector const& vec, T const& item) +template int64_t index_of(std::vector const& vec, T const& item) { auto const& begin = vec.begin(); auto const& end = vec.end(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/fuzzer.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/fuzzer.hpp index 3462dc53d84..7839362b643 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/fuzzer.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/fuzzer.hpp @@ -4,6 +4,7 @@ #include "barretenberg/proof_system/circuit_builder/turbo_circuit_builder.hpp" #include +// NOLINTBEGIN(cppcoreguidelines-macro-usage, google-runtime-int) #define PARENS () // Rescan macro tokens 256 times @@ -68,7 +69,9 @@ class FastRandom { FastRandom(uint32_t seed) { reseed(seed); } uint32_t next() { - state = static_cast((uint64_t(state) * uint64_t(363364578) + uint64_t(537)) % uint64_t(3758096939)); + state = static_cast( + (static_cast(state) * static_cast(363364578) + static_cast(537)) % + static_cast(3758096939)); return state; } void reseed(uint32_t seed) @@ -85,80 +88,82 @@ class FastRandom { * * @tparam T */ -template -concept SimpleRng = requires(T a) { - { - a.next() - } -> std::convertible_to; - }; +template concept SimpleRng = requires(T a) +{ + { + a.next() + } + ->std::convertible_to; +}; /** * @brief Concept for forcing ArgumentSizes to be size_t * * @tparam T */ -template -concept InstructionArgumentSizes = requires { - { - std::make_tuple(T::CONSTANT, - T::WITNESS, - T::CONSTANT_WITNESS, - T::ADD, - T::SUBTRACT, - T::MULTIPLY, - T::DIVIDE, - T::ADD_TWO, - T::MADD, - T::MULT_MADD, - T::MSUB_DIV, - T::SQR, - T::SQR_ADD, - T::SUBTRACT_WITH_CONSTRAINT, - T::DIVIDE_WITH_CONSTRAINTS, - T::SLICE, - T::ASSERT_ZERO, - T::ASSERT_NOT_ZERO) - } -> std::same_as>; - }; +template concept InstructionArgumentSizes = requires +{ + { + std::make_tuple(T::CONSTANT, + T::WITNESS, + T::CONSTANT_WITNESS, + T::ADD, + T::SUBTRACT, + T::MULTIPLY, + T::DIVIDE, + T::ADD_TWO, + T::MADD, + T::MULT_MADD, + T::MSUB_DIV, + T::SQR, + T::SQR_ADD, + T::SUBTRACT_WITH_CONSTRAINT, + T::DIVIDE_WITH_CONSTRAINTS, + T::SLICE, + T::ASSERT_ZERO, + T::ASSERT_NOT_ZERO) + } + ->std::same_as>; +}; /** * @brief Concept for Havoc Configurations * * @tparam T */ -template -concept HavocConfigConstraint = - requires { - { - std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY) - } -> std::same_as>; - T::GEN_MUTATION_COUNT_LOG <= 7; - }; +template concept HavocConfigConstraint = requires +{ + { + std::make_tuple(T::GEN_MUTATION_COUNT_LOG, T::GEN_STRUCTURAL_MUTATION_PROBABILITY) + } + ->std::same_as>; + T::GEN_MUTATION_COUNT_LOG <= 7; +}; /** * @brief Concept specifying the class used by the fuzzer * * @tparam T */ -template -concept ArithmeticFuzzHelperConstraint = requires { - typename T::ArgSizes; - typename T::Instruction; - typename T::ExecutionState; - typename T::ExecutionHandler; - InstructionArgumentSizes; - // HavocConfigConstraint; - }; +template concept ArithmeticFuzzHelperConstraint = requires +{ + typename T::ArgSizes; + typename T::Instruction; + typename T::ExecutionState; + typename T::ExecutionHandler; + InstructionArgumentSizes; +}; /** * @brief Fuzzer uses only composers with check_circuit function * * @tparam T */ -template -concept CheckableComposer = requires(T a) { - { - a.check_circuit() - } -> std::same_as; - }; +template concept CheckableComposer = requires(T a) +{ + { + a.check_circuit() + } + ->std::same_as; +}; /** * @brief The fuzzer can use a postprocessing function that is specific to the type being fuzzed @@ -168,11 +173,13 @@ concept CheckableComposer = requires(T a) { * @tparam Context The class containing the full context */ template -concept PostProcessingEnabled = requires(Composer composer, Context context) { - { - T::postProcess(&composer, context) - } -> std::same_as; - }; +concept PostProcessingEnabled = requires(Composer composer, Context context) +{ + { + T::postProcess(&composer, context) + } + ->std::same_as; +}; /** * @brief This concept is used when we want to limit the number of executions of certain instructions (for example, @@ -180,11 +187,11 @@ concept PostProcessingEnabled = requires(Composer composer, Context context) { * * @tparam T */ -template -concept InstructionWeightsEnabled = requires { - typename T::InstructionWeights; - T::InstructionWeights::_LIMIT; - }; +template concept InstructionWeightsEnabled = requires +{ + typename T::InstructionWeights; + T::InstructionWeights::_LIMIT; +}; /** * @brief Mutate the value of a field element @@ -195,9 +202,7 @@ concept InstructionWeightsEnabled = requires { * @param havoc_config Mutation configuration * @return Mutated element */ -template -inline static FF mutateFieldElement(FF e, T& rng) - requires SimpleRng +template inline static FF mutateFieldElement(FF e, T& rng) requires SimpleRng { // With a certain probability, we apply changes to the Montgomery form, rather than the plain form. This // has merit, since the computation is performed in montgomery form and comparisons are often performed @@ -226,7 +231,8 @@ inline static FF mutateFieldElement(FF e, T& rng) if (choice < 2) { // Delegate mutation to libfuzzer (bit/byte mutations, autodictionary, etc) MONT_CONVERSION_LOCAL - LLVMFuzzerMutate((uint8_t*)&value_data, sizeof(uint256_t), sizeof(uint256_t)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + LLVMFuzzerMutate(reinterpret_cast(&value_data), sizeof(uint256_t), sizeof(uint256_t)); INV_MONT_CONVERSION_LOCAL } else if (choice < 3) { // 25% to use small additions @@ -286,9 +292,7 @@ inline static FF mutateFieldElement(FF e, T& rng) * * @tparam T */ -template - requires ArithmeticFuzzHelperConstraint -class ArithmeticFuzzHelper { +template requires ArithmeticFuzzHelperConstraint class ArithmeticFuzzHelper { private: /** * @brief Mutator swapping two instructions together @@ -327,7 +331,7 @@ class ArithmeticFuzzHelper { if (instructions_count == 0) { return; } - if (rng.next() & 1) { + if ((rng.next() & 1) != 0U) { instructions.erase(instructions.begin() + (rng.next() % instructions_count)); } else { // We get the logarithm of number of instructions and subtract 1 to delete at most half @@ -471,7 +475,8 @@ class ArithmeticFuzzHelper { std::vector result; // Choose the size of th resulting vector const size_t final_result_size = rng.next() % (vecA_size + vecB_size) + 1; - size_t indexA = 0, indexB = 0; + size_t indexA = 0; + size_t indexB = 0; size_t* inIndex = &indexA; size_t inSize = vecA_size; auto inIterator = vecA.begin(); @@ -517,7 +522,8 @@ class ArithmeticFuzzHelper { static std::vector parseDataIntoInstructions(const uint8_t* Data, size_t Size) { std::vector fuzzingInstructions; - uint8_t* pData = (uint8_t*)Data; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) + auto* pData = const_cast(Data); size_t size_left = Size; while (size_left != 0) { uint8_t chosen_operation = *pData; @@ -591,12 +597,14 @@ class ArithmeticFuzzHelper { * @param instructions */ template - inline static void executeInstructions(std::vector& instructions) - requires CheckableComposer + // TODO(@Rumata888)(from Zac: maybe try to fix? not comfortable refactoring this myself. Issue #1807) + // NOLINTNEXTLINE(readability-function-size, google-readability-function-size) + inline static void executeInstructions( + std::vector& instructions) requires CheckableComposer { typename T::ExecutionState state; Composer composer = Composer(); - circuit_should_fail = false; + bool circuit_should_fail = false; size_t total_instruction_weight = 0; (void)total_instruction_weight; for (auto& instruction : instructions) { @@ -690,3 +698,5 @@ constexpr void RunWithComposers(const uint8_t* Data, const size_t Size, FastRand RunWithComposer(Data, Size, VarianceRNG); } } + +// NOLINTEND(cppcoreguidelines-macro-usage, google-runtime-int) \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/log.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/log.hpp index 37ea8f10f31..002c225c34d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/log.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/log.hpp @@ -10,7 +10,6 @@ #define BENCHMARK_INFO_SEPARATOR "#" #define BENCHMARK_INFO_SUFFIX "##BENCHMARK_INFO_SUFFIX##" -namespace { template std::string format(Args... args) { std::ostringstream os; @@ -47,7 +46,6 @@ template std::string benchmark_format(Args... args) benchmark_format_chain(os, args...); return os.str(); } -} // namespace #if NDEBUG template inline void debug(Args... args) @@ -55,7 +53,7 @@ template inline void debug(Args... args) logstr(format(args...).c_str()); } #else -template inline void debug(Args...) {} +template inline void debug(Args... /*unused*/) {} #endif template inline void info(Args... args) @@ -83,7 +81,7 @@ inline void benchmark_info(Arg1 composer, Arg2 class_name, Arg3 operation, Arg4 logstr(benchmark_format(composer, class_name, operation, metric, value).c_str()); } #else -template inline void benchmark_info(Args...) {} +template inline void benchmark_info(Args... /*unused*/) {} #endif /** @@ -95,6 +93,12 @@ class BenchmarkInfoCollator { std::vector saved_benchmarks; public: + BenchmarkInfoCollator() = default; + BenchmarkInfoCollator(const BenchmarkInfoCollator& other) = default; + BenchmarkInfoCollator(BenchmarkInfoCollator&& other) = default; + BenchmarkInfoCollator& operator=(const BenchmarkInfoCollator& other) = default; + BenchmarkInfoCollator& operator=(BenchmarkInfoCollator&& other) = default; + /** * @brief Info used to store circuit statistics during CI/CD with concrete structure. Stores string in vector for now * (used to flush all benchmarks at the end of test). @@ -111,7 +115,10 @@ class BenchmarkInfoCollator { saved_benchmarks.push_back(benchmark_format(composer, class_name, operation, metric, value).c_str()); } #else - template inline void benchmark_info_deferred(Args...) {} + explicit BenchmarkInfoCollator(std::vector saved_benchmarks) + : saved_benchmarks(std::move(saved_benchmarks)) + {} + template inline void benchmark_info_deferred(Args... /*unused*/) {} #endif ~BenchmarkInfoCollator() { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/mem.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/mem.hpp index c42aa649a81..d97ccd495ea 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/mem.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/mem.hpp @@ -2,8 +2,8 @@ #include "log.hpp" #include "memory.h" #include "wasm_export.hpp" +#include #include -#include // #include #define pad(size, alignment) (size - (size % alignment) + ((size % alignment) == 0 ? 0 : alignment)) @@ -30,9 +30,10 @@ inline void aligned_free(void* mem) inline void* protected_aligned_alloc(size_t alignment, size_t size) { size += (size % alignment); - void* t = 0; + void* t = nullptr; + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) t = aligned_alloc(alignment, size); - if (t == 0) { + if (t == nullptr) { info("bad alloc of size: ", size); std::abort(); } @@ -43,6 +44,7 @@ inline void* protected_aligned_alloc(size_t alignment, size_t size) inline void aligned_free(void* mem) { + // NOLINTNEXTLINE(cppcoreguidelines-owning-memory, cppcoreguidelines-no-malloc) free(mem); } #endif diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/net.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/net.hpp index 4d738313946..f0daa8fe93b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/net.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/net.hpp @@ -10,5 +10,6 @@ inline bool is_little_endian() { constexpr int num = 42; + // NOLINTNEXTLINE Nope. nope nope nope nope nope. return (*(char*)&num == 42); } \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_atomic_pool.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_atomic_pool.cpp index 4f325ec6586..08da4e27a92 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_atomic_pool.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_atomic_pool.cpp @@ -13,8 +13,13 @@ namespace { class ThreadPool { public: ThreadPool(size_t num_threads); + ThreadPool(const ThreadPool& other) = delete; + ThreadPool(ThreadPool&& other) = delete; ~ThreadPool(); + ThreadPool& operator=(const ThreadPool& other) = delete; + ThreadPool& operator=(ThreadPool&& other) = delete; + void start_tasks(size_t num_iterations, const std::function& func) { { @@ -36,17 +41,17 @@ class ThreadPool { std::vector workers; std::mutex tasks_mutex; std::function task_; - size_t num_iterations_; + size_t num_iterations_ = 0; std::atomic iteration_; std::atomic iterations_completed_; std::condition_variable condition; - bool stop; + bool stop = false; void worker_loop(size_t thread_index); void do_iterations() { - size_t iteration; + size_t iteration = 0; while ((iteration = iteration_.fetch_add(1, std::memory_order_seq_cst)) < num_iterations_) { // info("main thread processing iteration ", iteration); task_(iteration); @@ -56,10 +61,8 @@ class ThreadPool { }; ThreadPool::ThreadPool(size_t num_threads) - : num_iterations_(0) - , iteration_(0) + : iteration_(0) , iterations_completed_(0) - , stop(false) { workers.reserve(num_threads); for (size_t i = 0; i < num_threads; ++i) { @@ -79,7 +82,7 @@ ThreadPool::~ThreadPool() } } -void ThreadPool::worker_loop(size_t) +void ThreadPool::worker_loop(size_t /*unused*/) { // info("created worker ", worker_num); while (true) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_moody.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_moody.cpp index ad0c6818366..3724cd63afe 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_moody.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_moody.cpp @@ -33,9 +33,16 @@ class ThreadPool { } } + ThreadPool(const ThreadPool& other) = delete; + ThreadPool(ThreadPool&& other) = delete; + ThreadPool& operator=(const ThreadPool& other) = delete; + ThreadPool& operator=(ThreadPool&& other) = delete; + void start_tasks(const std::function& task, size_t num_iterations) { std::atomic complete_counter; + // 3rd party library expects c-style array as input. Boo. + // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) std::function funcs[num_iterations]; for (size_t i = 0; i < num_iterations; ++i) { funcs[i] = [&, i]() { @@ -57,7 +64,7 @@ class ThreadPool { } } - bool complete; + bool complete = false; complete_queue_.wait_dequeue(complete); // info("all done!"); } @@ -68,7 +75,7 @@ class ThreadPool { moodycamel::BlockingConcurrentQueue complete_queue_; std::atomic stop = false; - void worker_loop(size_t) + void worker_loop(size_t /*unused*/) { // info("worker started"); while (!stop) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_mutex_pool.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_mutex_pool.cpp index 7dbef9a0b9d..c8fb4ebf5ec 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_mutex_pool.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_mutex_pool.cpp @@ -13,8 +13,13 @@ namespace { class ThreadPool { public: ThreadPool(size_t num_threads); + ThreadPool(const ThreadPool& other) = delete; + ThreadPool(ThreadPool&& other) = delete; ~ThreadPool(); + ThreadPool& operator=(const ThreadPool& other) = delete; + ThreadPool& operator=(ThreadPool&& other) = delete; + void start_tasks(size_t num_iterations, const std::function& func) { { @@ -38,19 +43,19 @@ class ThreadPool { std::vector workers; std::mutex tasks_mutex; std::function task_; - size_t num_iterations_; - size_t iteration_; - size_t complete_; + size_t num_iterations_ = 0; + size_t iteration_ = 0; + size_t complete_ = 0; std::condition_variable condition; std::condition_variable complete_condition_; - bool stop; + bool stop = false; void worker_loop(size_t thread_index); void do_iterations() { while (true) { - size_t iteration; + size_t iteration = 0; { std::unique_lock lock(tasks_mutex); if (iteration_ == num_iterations_) { @@ -71,10 +76,6 @@ class ThreadPool { }; ThreadPool::ThreadPool(size_t num_threads) - : num_iterations_(0) - , iteration_(0) - , complete_(0) - , stop(false) { workers.reserve(num_threads); for (size_t i = 0; i < num_threads; ++i) { @@ -94,7 +95,7 @@ ThreadPool::~ThreadPool() } } -void ThreadPool::worker_loop(size_t) +void ThreadPool::worker_loop(size_t /*unused*/) { // info("created worker ", worker_num); while (true) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_queued.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_queued.cpp index 6fcca9161b9..caac4022d59 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_queued.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_queued.cpp @@ -11,8 +11,13 @@ class ThreadPool { public: ThreadPool(size_t num_threads); + ThreadPool(const ThreadPool& other) = delete; + ThreadPool(ThreadPool&& other) = delete; ~ThreadPool(); + ThreadPool& operator=(const ThreadPool& other) = delete; + ThreadPool& operator=(ThreadPool&& other) = delete; + void enqueue(const std::function& task); void wait(); @@ -23,13 +28,12 @@ class ThreadPool { std::condition_variable condition; std::condition_variable finished_condition; std::atomic tasks_running; - bool stop; + bool stop = false; void worker_loop(size_t thread_index); }; ThreadPool::ThreadPool(size_t num_threads) - : stop(false) { workers.reserve(num_threads); for (size_t i = 0; i < num_threads; ++i) { @@ -64,7 +68,7 @@ void ThreadPool::wait() finished_condition.wait(lock, [this] { return tasks.empty() && tasks_running == 0; }); } -void ThreadPool::worker_loop(size_t) +void ThreadPool::worker_loop(size_t /*unused*/) { // info("created worker ", worker_num); while (true) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_spawning.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_spawning.cpp index 7fec623b169..429f4724ad5 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_spawning.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/parallel_for_spawning.cpp @@ -12,7 +12,7 @@ void parallel_for_spawning(size_t num_iterations, const std::function threads(num_threads); for (size_t i = 0; i < num_threads; ++i) { threads[i] = std::thread(worker, i); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/serialize.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/serialize.hpp index 0e33da1339c..9ed389dd31f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/serialize.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/serialize.hpp @@ -44,6 +44,13 @@ __extension__ using uint128_t = unsigned __int128; #endif +// clang-format off +// disabling the following style guides: +// cert-dcl58-cpp , restricts modifying the standard library. We need to do this for portable serialization methods +// cppcoreguidelines-pro-type-reinterpret-cast, we heavily use reinterpret-cast and would be difficult to refactor this out +// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast, cert-dcl58-cpp) +// clang-format on + template concept IntegralOrEnum = std::integral || std::is_enum_v; namespace serialize { @@ -67,7 +74,7 @@ inline void write(uint8_t*& it, uint8_t value) inline void read(uint8_t const*& it, bool& value) { - value = *it; + value = (*it != 0U); it += 1; } @@ -79,19 +86,19 @@ inline void write(uint8_t*& it, bool value) inline void read(uint8_t const*& it, uint16_t& value) { - value = ntohs(*reinterpret_cast(it)); + value = ntohs(*reinterpret_cast(it)); // NOLINT it += 2; } inline void write(uint8_t*& it, uint16_t value) { - *reinterpret_cast(it) = htons(value); + *reinterpret_cast(it) = htons(value); // NOLINT it += 2; } inline void read(uint8_t const*& it, uint32_t& value) { - value = ntohl(*reinterpret_cast(it)); + value = ntohl(*reinterpret_cast(it)); // NOLINT it += 4; } @@ -116,15 +123,15 @@ inline void write(uint8_t*& it, uint64_t value) #ifndef __i386__ inline void read(uint8_t const*& it, uint128_t& value) { - uint64_t hi, lo; + uint64_t hi, lo; // NOLINT read(it, hi); read(it, lo); value = (static_cast(hi) << 64) | lo; } inline void write(uint8_t*& it, uint128_t value) { - uint64_t hi = static_cast(value >> 64); - uint64_t lo = static_cast(value); + auto hi = static_cast(value >> 64); + auto lo = static_cast(value); write(it, hi); write(it, lo); } @@ -133,7 +140,7 @@ inline void write(uint8_t*& it, uint128_t value) // Reading / writing integer types to / from vectors. void read(std::vector const& buf, std::integral auto& value) { - auto ptr = &buf[0]; + const auto* ptr = &buf[0]; read(ptr, value); } @@ -148,7 +155,7 @@ void write(std::vector& buf, const std::integral auto& value) void read(std::istream& is, std::integral auto& value) { std::array buf; - is.read((char*)buf.data(), sizeof(value)); + is.read(reinterpret_cast(buf.data()), sizeof(value)); uint8_t const* ptr = &buf[0]; read(ptr, value); } @@ -158,7 +165,7 @@ void write(std::ostream& os, const std::integral auto& value) std::array buf; uint8_t* ptr = &buf[0]; write(ptr, value); - os.write((char*)buf.data(), sizeof(value)); + os.write(reinterpret_cast(buf.data()), sizeof(value)); } } // namespace serialize @@ -190,7 +197,7 @@ template inline void write(uint8_t*& buf, std::array cons // Optimised specialisation for reading vectors of bytes from a raw buffer. inline void read(uint8_t const*& it, std::vector& value) { - uint32_t size; + uint32_t size = 0; read(it, size); value.resize(size); std::copy(it, it + size, value.data()); @@ -208,31 +215,31 @@ inline void write(uint8_t*& buf, std::vector const& value) // Optimised specialisation for reading vectors of bytes from an input stream. inline void read(std::istream& is, std::vector& value) { - uint32_t size; + uint32_t size = 0; read(is, size); value.resize(size); - is.read((char*)value.data(), (std::streamsize)size); + is.read(reinterpret_cast(value.data()), static_cast(size)); } // Optimised specialisation for writing vectors of bytes to an output stream. inline void write(std::ostream& os, std::vector const& value) { write(os, static_cast(value.size())); - os.write((char*)value.data(), (std::streamsize)value.size()); + os.write(reinterpret_cast(value.data()), static_cast(value.size())); } // Optimised specialisation for writing arrays of bytes to a vector. template inline void write(std::vector& buf, std::array const& value) { buf.resize(buf.size() + N); - auto ptr = &*buf.end() - N; + auto* ptr = &*buf.end() - N; write(ptr, value); } // Optimised specialisation for writing arrays of bytes to an output stream. template inline void write(std::ostream& os, std::array const& value) { - os.write((char*)value.data(), value.size()); + os.write(reinterpret_cast(value.data()), value.size()); } // Generic read of array of types from supported buffer types. @@ -257,7 +264,7 @@ template inline void write(B& buf, std::array template inline void read(B& it, std::vector& value) { using serialize::read; - uint32_t size; + uint32_t size = 0; read(it, size); value.resize(size); for (size_t i = 0; i < size; ++i) { @@ -328,7 +335,7 @@ template inline void read(B& it, std::map v; @@ -351,7 +358,7 @@ template inline void write(B& buf, std::map template inline void read(B& it, std::optional& opt_value) { using serialize::read; - bool is_nullopt; + bool is_nullopt = false; read(it, is_nullopt); if (is_nullopt) { opt_value = std::nullopt; @@ -383,7 +390,7 @@ template T from_buffer(B const& buffer, size_t offset = { using serialize::read; T result; - auto ptr = (uint8_t const*)&buffer[offset]; + const auto* ptr = reinterpret_cast(&buffer[offset]); read(ptr, result); return result; } @@ -401,7 +408,7 @@ template uint8_t* to_heap_buffer(T const& value) using serialize::write; std::vector buf; write(buf, value); - auto* ptr = (uint8_t*)aligned_alloc(64, buf.size()); + auto* ptr = (uint8_t*)aligned_alloc(64, buf.size()); // NOLINT std::copy(buf.begin(), buf.end(), ptr); return ptr; } @@ -494,3 +501,6 @@ inline void write(auto& buf, const msgpack_concepts::HasMsgPack auto& obj) }); } } // namespace serialize +// clang-format off +// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast, cert-dcl58-cpp) +// clang-format on diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.cpp index ea260389005..7b6d2020caf 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -15,9 +16,11 @@ * (Irony of global slab allocator noted). */ namespace { +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) bool allocator_destroyed = false; // Slabs that are being manually managed by the user. +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) std::unordered_map> manual_slabs; template inline void dbg_info(Args... args) @@ -47,6 +50,11 @@ class SlabAllocator { public: ~SlabAllocator(); + SlabAllocator() = default; + SlabAllocator(const SlabAllocator& other) = delete; + SlabAllocator(SlabAllocator&& other) = delete; + SlabAllocator& operator=(const SlabAllocator& other) = delete; + SlabAllocator& operator=(SlabAllocator&& other) = delete; void init(size_t circuit_size_hint); @@ -136,7 +144,7 @@ std::shared_ptr SlabAllocator::get(size_t req_size) // Can use a preallocated slab that is less than 2 times the requested size. if (it != memory_store.end() && it->first < req_size * 2) { size_t size = it->first; - auto ptr = it->second.back(); + auto* ptr = it->second.back(); it->second.pop_back(); if (it->second.empty()) { @@ -154,23 +162,23 @@ std::shared_ptr SlabAllocator::get(size_t req_size) dbg_info("Reusing memory slab of size: ", size, " for requested ", req_size, " total: ", get_total_size()); } - return std::shared_ptr(ptr, [this, size](void* p) { - if (allocator_destroyed) { - aligned_free(p); - return; - } - this->release(p, size); - }); + return { ptr, [this, size](void* p) { + if (allocator_destroyed) { + aligned_free(p); + return; + } + this->release(p, size); + } }; } - if (req_size > 1024 * 1024) { + if (req_size > static_cast(1024 * 1024)) { dbg_info("WARNING: Allocating unmanaged memory slab of size: ", req_size); } if (req_size % 32 == 0) { - return std::shared_ptr(aligned_alloc(32, req_size), aligned_free); - } else { - return std::shared_ptr(malloc(req_size), free); + return { aligned_alloc(32, req_size), aligned_free }; } + // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) + return { malloc(req_size), free }; } size_t SlabAllocator::get_total_size() @@ -188,7 +196,7 @@ void SlabAllocator::release(void* ptr, size_t size) memory_store[size].push_back(ptr); // dbg_info("Pooled poly memory of size: ", size, " total: ", get_total_size()); } - +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) SlabAllocator allocator; } // namespace diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp index d88d34d529a..050360d4db5 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp @@ -58,14 +58,21 @@ template class ContainerSlabAllocator { pointer allocate(size_type n) { // info("ContainerSlabAllocator allocating: ", n * sizeof(T)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) return reinterpret_cast(get_mem_slab_raw(n * sizeof(T))); } void deallocate(pointer p, size_type /*unused*/) { free_mem_slab_raw(p); } - friend bool operator==(const ContainerSlabAllocator&, const ContainerSlabAllocator&) { return true; } + friend bool operator==(const ContainerSlabAllocator& /*unused*/, const ContainerSlabAllocator& /*unused*/) + { + return true; + } - friend bool operator!=(const ContainerSlabAllocator&, const ContainerSlabAllocator&) { return false; } + friend bool operator!=(const ContainerSlabAllocator& /*unused*/, const ContainerSlabAllocator& /*unused*/) + { + return false; + } }; } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/streams.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/streams.hpp index a7be097603a..12e07d8a174 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/common/streams.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/common/streams.hpp @@ -1,19 +1,24 @@ #pragma once +#include "barretenberg/serialize/msgpack.hpp" +#include "barretenberg/serialize/msgpack_apply.hpp" #include #include #include #include #include #include -#include "barretenberg/serialize/msgpack.hpp" -#include "barretenberg/serialize/msgpack_apply.hpp" + +// clang-format off +// disabling the following style guides: +// cert-dcl58-cpp , restricts modifying the standard library. We need to do this for portable serialization methods +// NOLINTBEGIN(cert-dcl58-cpp) +// clang-format on namespace serialize { /** * @brief Helper method for streaming msgpack values, specialized for shared_ptr */ -template -void _msgpack_stream_write(std::ostream& os, const std::shared_ptr& field) +template void _msgpack_stream_write(std::ostream& os, const std::shared_ptr& field) { using namespace serialize; os << *field; @@ -32,7 +37,7 @@ inline void _msgpack_stream_write(std::ostream& os, const auto& field) inline void _msgpack_stream_write_key_value_pairs(std::ostream& os) { // base case - (void)os; // unused + (void)os; // unused } /** * @brief Recursive helper method for streaming msgpack key value pairs, default arg case @@ -45,7 +50,7 @@ inline void _msgpack_stream_write_key_value_pairs(std::ostream& os, os << key << ": "; _msgpack_stream_write(os, value); os << '\n'; - _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT + _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT } /** * @brief Recursive helper method for streaming msgpack key value pairs, msgpack arg case @@ -60,21 +65,21 @@ inline void _msgpack_stream_write_key_value_pairs(std::ostream& os, os << key << ":\n"; _msgpack_stream_write(os, value); os << '\n'; - _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT + _msgpack_stream_write_key_value_pairs(os, rest...); // NOLINT } } // namespace serialize namespace std { /** - * @brief Automatically derived stream operator for any object that defines .msgpack() (implicitly defined by MSGPACK_FIELDS). - * Note this is duplicated as it must be seen in both std and global namespaces. + * @brief Automatically derived stream operator for any object that defines .msgpack() (implicitly defined by + * MSGPACK_FIELDS). Note this is duplicated as it must be seen in both std and global namespaces. * @param os The stream to write to. * @param obj The object to write. */ -template -std::ostream& operator<<(std::ostream& os, const T& obj) +template std::ostream& operator<<(std::ostream& os, const T& obj) { // We must use const_cast as our method is meant to be polymorphic over const, but there's no such concept in C++ + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) const_cast(obj).msgpack([&](auto&... key_value_pairs) { // apply 'operator<<' to each object field serialize::_msgpack_stream_write_key_value_pairs(os, key_value_pairs...); @@ -87,7 +92,7 @@ inline std::ostream& operator<<(std::ostream& os, std::vector const& ar std::ios_base::fmtflags f(os.flags()); os << "[" << std::hex << std::setfill('0'); for (auto byte : arr) { - os << ' ' << std::setw(2) << +(unsigned char)byte; + os << ' ' << std::setw(2) << +static_cast(byte); } os << " ]"; os.flags(f); @@ -120,7 +125,7 @@ template inline std::ostream& operator<<(std::ostream& os, std::array std::ios_base::fmtflags f(os.flags()); os << "[" << std::hex << std::setfill('0'); for (auto byte : arr) { - os << ' ' << std::setw(2) << +(unsigned char)byte; + os << ' ' << std::setw(2) << +static_cast(byte); } os << " ]"; os.flags(f); @@ -161,3 +166,4 @@ template inline std::ostream& operator<<(std::ostream& } } // namespace std +// NOLINTEND(cert-dcl58-cpp) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/CMakeLists.txt b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/CMakeLists.txt index 2efdfb493aa..1133de8da90 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/CMakeLists.txt +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/CMakeLists.txt @@ -12,7 +12,9 @@ target_precompile_headers( $<$:"barretenberg/numeric/uint256/uint256.hpp"> $<$:"barretenberg/numeric/uint256/uint256_impl.hpp"> $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/asm_macros.hpp"> - $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field.hpp"> + $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field_declarations.hpp"> + $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field_impl.hpp"> $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field_impl_generic.hpp"> $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field_impl_x64.hpp"> + $<$:"${CMAKE_CURRENT_SOURCE_DIR}/fields/field.hpp"> ) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.hpp index 59c5380e66c..21aca46a03c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.hpp @@ -5,6 +5,7 @@ #include "../../fields/field.hpp" +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) namespace barretenberg { class Bn254FqParams { public: @@ -61,6 +62,8 @@ class Bn254FqParams { static constexpr bool has_high_2adicity = false; }; -typedef field fq; +using fq = field; } // namespace barretenberg + +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.test.cpp index f8d79036356..c3368a3e20f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq.test.cpp @@ -1,17 +1,26 @@ #include "barretenberg/serialize/test_helper.hpp" #include "fq.hpp" -#include "pseudorandom.hpp" #include using namespace barretenberg; -TEST(fq, msgpack) +// Used to ensure variables are evaluated at runtime and not compile time. +// If EXPECT_EQ macro params are evaluated at compile-time, compiler can optimise them away. +// This triggers compiler errors due to the gtest suite expecting at least one test statement in a TEST macro +void shallow_copy(const fq& in, fq& out) { - auto [actual, expected] = msgpack_roundtrip(barretenberg::fq{ 1ull, 2ull, 3ull, 4ull }); + out.data[0] = in.data[0]; + out.data[1] = in.data[1]; + out.data[2] = in.data[2]; + out.data[3] = in.data[3]; +}; +TEST(fq, Msgpack) +{ + auto [actual, expected] = msgpack_roundtrip(barretenberg::fq{ 1ULL, 2ULL, 3ULL, 4ULL }); EXPECT_EQ(actual, expected); } -TEST(fq, eq) +TEST(fq, Eq) { constexpr fq a{ 0x01, 0x02, 0x03, 0x04 }; constexpr fq b{ 0x01, 0x02, 0x03, 0x04 }; @@ -31,12 +40,13 @@ TEST(fq, eq) fq d_var; fq e_var; fq f_var; - memcpy((void*)a_var.data, (void*)a.data, 32); - memcpy((void*)b_var.data, (void*)b.data, 32); - memcpy((void*)c_var.data, (void*)c.data, 32); - memcpy((void*)d_var.data, (void*)d.data, 32); - memcpy((void*)e_var.data, (void*)e.data, 32); - memcpy((void*)f_var.data, (void*)f.data, 32); + + shallow_copy(a, a_var); + shallow_copy(b, b_var); + shallow_copy(c, c_var); + shallow_copy(d, d_var); + shallow_copy(e, e_var); + shallow_copy(f, f_var); EXPECT_EQ(a_var == a_var, true); EXPECT_EQ(a_var == b_var, true); @@ -46,7 +56,7 @@ TEST(fq, eq) EXPECT_EQ(a_var == f_var, false); } -TEST(fq, is_zero) +TEST(fq, IsZero) { fq a = fq::zero(); fq b = fq::zero(); @@ -65,7 +75,7 @@ TEST(fq, is_zero) EXPECT_EQ(e.is_zero(), false); } -TEST(fq, random_element) +TEST(fq, RandomElement) { fq a = fq::random_element(); fq b = fq::random_element(); @@ -75,7 +85,7 @@ TEST(fq, random_element) EXPECT_EQ(a.is_zero(), false); } -TEST(fq, mul_check_against_constants) +TEST(fq, MulCheckAgainstConstants) { // test against some randomly generated test data constexpr fq a{ 0x2523b6fa3956f038, 0x158aa08ecdd9ec1d, 0xf48216a4c74738d4, 0x2514cc93d6f0a1bf }; @@ -91,13 +101,13 @@ TEST(fq, mul_check_against_constants) fq c; fq d; - memcpy((void*)c.data, (void*)a.data, 32); - memcpy((void*)d.data, (void*)b.data, 32); + shallow_copy(a, c); + shallow_copy(b, d); EXPECT_EQ(c * d, const_expected); } // validate that zero-value limbs don't cause any problems -TEST(fq, mul_short_integers) +TEST(fq, MulShortIntegers) { constexpr fq a{ 0xa, 0, 0, 0 }; constexpr fq b{ 0xb, 0, 0, 0 }; @@ -107,12 +117,12 @@ TEST(fq, mul_short_integers) fq c; fq d; - memcpy((void*)c.data, (void*)a.data, 32); - memcpy((void*)d.data, (void*)b.data, 32); + shallow_copy(a, c); + shallow_copy(b, d); EXPECT_EQ(c * d, const_expected); } -TEST(fq, mul_sqr_consistency) +TEST(fq, MulSqrConsistency) { fq a = fq::random_element(); fq b = fq::random_element(); @@ -129,7 +139,7 @@ TEST(fq, mul_sqr_consistency) EXPECT_EQ(mul_result, sqr_result); } -TEST(fq, sqr_check_against_constants) +TEST(fq, SqrCheckAgainstConstants) { constexpr fq a{ 0x329596aa978981e8, 0x8542e6e254c2a5d0, 0xc5b687d82eadb178, 0x2d242aaf48f56b8a }; constexpr fq expected{ 0xbf4fb34e120b8b12, 0xf64d70efbf848328, 0xefbb6a533f2e7d89, 0x1de50f941425e4aa }; @@ -137,12 +147,13 @@ TEST(fq, sqr_check_against_constants) static_assert(result == expected); fq b; - memcpy((void*)b.data, (void*)a.data, 32); + shallow_copy(a, b); + fq c = b.sqr(); EXPECT_EQ(result, c); } -TEST(fq, add_check_against_constants) +TEST(fq, AddCheckAgainstConstants) { constexpr fq a{ 0x7d2e20e82f73d3e8, 0x8e50616a7a9d419d, 0xcdc833531508914b, 0xd510253a2ce62c }; constexpr fq b{ 0x2829438b071fd14e, 0xb03ef3f9ff9274e, 0x605b671f6dc7b209, 0x8701f9d971fbc9 }; @@ -152,12 +163,12 @@ TEST(fq, add_check_against_constants) fq c; fq d; - memcpy((void*)c.data, (void*)a.data, 32); - memcpy((void*)d.data, (void*)b.data, 32); + shallow_copy(a, c); + shallow_copy(b, d); EXPECT_EQ(c + d, const_expected); } -TEST(fq, sub_check_against_constants) +TEST(fq, SubCheckAgainstConstants) { constexpr fq a{ 0xd68d01812313fb7c, 0x2965d7ae7c6070a5, 0x08ef9af6d6ba9a48, 0x0cb8fe2108914f53 }; constexpr fq b{ 0x2cd2a2a37e9bf14a, 0xebc86ef589c530f6, 0x75124885b362b8fe, 0x1394324205c7a41d }; @@ -167,15 +178,15 @@ TEST(fq, sub_check_against_constants) fq c; fq d; - memcpy((void*)c.data, (void*)a.data, 32); - memcpy((void*)d.data, (void*)b.data, 32); + shallow_copy(a, c); + shallow_copy(b, d); EXPECT_EQ(c - d, const_expected); } -TEST(fq, coarse_equivalence_checks) +TEST(fq, CoarseEquivalenceChecks) { - fq a = get_pseudorandom_fq(); - fq b = get_pseudorandom_fq(); + fq a = fq::random_element(); + fq b = fq::random_element(); fq c = (a * b) + a - b; @@ -184,14 +195,14 @@ TEST(fq, coarse_equivalence_checks) EXPECT_EQ(c, d); } -TEST(fq, to_montgomery_form) +TEST(fq, toMontgomeryForm) { fq result = fq{ 0x01, 0x00, 0x00, 0x00 }.to_montgomery_form(); fq expected = fq::one(); EXPECT_EQ(result, expected); } -TEST(fq, from_montgomery_form) +TEST(fq, FromMontgomeryForm) { constexpr fq t0 = fq::one(); constexpr fq result = t0.from_montgomery_form(); @@ -199,7 +210,7 @@ TEST(fq, from_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq, montgomery_consistency_check) +TEST(fq, MontgomeryConsistencyCheck) { fq a = fq::random_element(); fq b = fq::random_element(); @@ -231,7 +242,7 @@ TEST(fq, montgomery_consistency_check) EXPECT_EQ((result_a == result_d), true); } -TEST(fq, add_mul_consistency) +TEST(fq, AddMulConsistency) { fq multiplicand = { 0x09, 0, 0, 0 }; multiplicand.self_to_montgomery_form(); @@ -249,7 +260,7 @@ TEST(fq, add_mul_consistency) EXPECT_EQ((result == expected), true); } -TEST(fq, sub_mul_consistency) +TEST(fq, SubMulConsistency) { fq multiplicand = { 0x05, 0, 0, 0 }; multiplicand.self_to_montgomery_form(); @@ -290,7 +301,7 @@ TEST(fq, beta) EXPECT_EQ((x_cubed == beta_x_cubed), true); } -TEST(fq, invert) +TEST(fq, Invert) { fq input = fq::random_element(); fq inverse = input.invert(); @@ -300,14 +311,14 @@ TEST(fq, invert) EXPECT_EQ(result, fq::one()); } -TEST(fq, invert_one_is_one) +TEST(fq, InvertOneIsOne) { fq result = fq::one(); result = result.invert(); EXPECT_EQ((result == fq::one()), true); } -TEST(fq, sqrt) +TEST(fq, Sqrt) { fq input = fq::one(); auto [is_sqr, root] = input.sqrt(); @@ -315,7 +326,7 @@ TEST(fq, sqrt) EXPECT_EQ(result, input); } -TEST(fq, sqrt_random) +TEST(fq, SqrtRandom) { for (size_t i = 0; i < 1; ++i) { fq input = fq::random_element().sqr(); @@ -325,14 +336,14 @@ TEST(fq, sqrt_random) } } -TEST(fq, one_and_zero) +TEST(fq, OneAndZero) { fq result; result = fq::one() - fq::one(); EXPECT_EQ((result == fq::zero()), true); } -TEST(fq, copy) +TEST(fq, Copy) { fq result = fq::random_element(); fq expected; @@ -340,7 +351,7 @@ TEST(fq, copy) EXPECT_EQ((result == expected), true); } -TEST(fq, neg) +TEST(fq, Neg) { fq a = fq::random_element(); fq b; @@ -350,7 +361,7 @@ TEST(fq, neg) EXPECT_EQ((result == fq::zero()), true); } -TEST(fq, split_into_endomorphism_scalars) +TEST(fq, SplitIntoEndomorphismScalars) { fq k = fq::random_element(); fq k1 = 0; @@ -371,7 +382,7 @@ TEST(fq, split_into_endomorphism_scalars) EXPECT_EQ(result, k); } -TEST(fq, split_into_endomorphism_scalars_simple) +TEST(fq, SplitIntoEndomorphismScalarsSimple) { fq input = { 1, 0, 0, 0 }; @@ -396,9 +407,9 @@ TEST(fq, split_into_endomorphism_scalars_simple) } } -TEST(fq, serialize_to_buffer) +TEST(fq, SerializeToBuffer) { - uint8_t buffer[32]; + std::array buffer; fq a = { 0x1234567876543210, 0x2345678987654321, 0x3456789a98765432, 0x006789abcba98765 }; a = a.to_montgomery_form(); @@ -441,9 +452,9 @@ TEST(fq, serialize_to_buffer) EXPECT_EQ(buffer[0], 0x00); } -TEST(fq, serialize_from_buffer) +TEST(fq, SerializeFromBuffer) { - uint8_t buffer[32]; + std::array buffer; fq expected = { 0x1234567876543210, 0x2345678987654321, 0x3456789a98765432, 0x006789abcba98765 }; fq::serialize_to_buffer(expected, &buffer[0]); @@ -453,12 +464,12 @@ TEST(fq, serialize_from_buffer) EXPECT_EQ((result == expected), true); } -TEST(fq, multiplicative_generator) +TEST(fq, MultiplicativeGenerator) { EXPECT_EQ(fq::multiplicative_generator(), fq(3)); } -TEST(fq, r_inv) +TEST(fq, RInv) { uint256_t prime_256{ Bn254FqParams::modulus_0, Bn254FqParams::modulus_1, Bn254FqParams::modulus_2, Bn254FqParams::modulus_3 @@ -473,7 +484,7 @@ TEST(fq, r_inv) } // TEST to check we don't have 0^0=0 -TEST(fq, pow_regression_check) +TEST(fq, PowRegressionCheck) { fq zero = fq::zero(); fq one = fq::one(); @@ -481,22 +492,24 @@ TEST(fq, pow_regression_check) } // 438268ca91d42ad f1e7025a7b654e1f f8d9d72e0438b995 8c422ec208ac8a6e -TEST(fq, sqr_regression) +TEST(fq, SqrRegression) { - uint256_t values[] = { uint256_t(0xbdf876654b0ade1b, 0x2c3a66c64569f338, 0x2cd8bf2ec1fe55a3, 0x11c0ea9ee5693ede), - uint256_t(0x551b14ec34f2151c, 0x62e472ed83a2891e, 0xf208d5e5c9b5b3fb, 0x14315aeaf6027d8c), - uint256_t(0xad39959ae8013750, 0x7f1d2c709ab84cbb, 0x408028b80a60c2f1, 0x1dcd116fc26f856e), - uint256_t(0x95e967d30dcce9ce, 0x56139274241d2ea1, 0x85b19c1c616ec456, 0x1f1780cf9bf045b4), - uint256_t(0xbe841c861d8eb80e, 0xc5980d67a21386c0, 0x5fd1f1afecddeeb5, 0x24dbb8c1baea0250), - uint256_t(0x3ae4b3a27f05d6e3, 0xc5f6785b12df8d29, 0xc3a6c5f095103046, 0xd6b94cb2cc1fd4b), - uint256_t(0xc003c71932a6ced5, 0x6302a413f68e26e9, 0x2ed4a9b64d69fad, 0xfe61ffab1ae227d) }; + std::array values = { + uint256_t(0xbdf876654b0ade1b, 0x2c3a66c64569f338, 0x2cd8bf2ec1fe55a3, 0x11c0ea9ee5693ede), + uint256_t(0x551b14ec34f2151c, 0x62e472ed83a2891e, 0xf208d5e5c9b5b3fb, 0x14315aeaf6027d8c), + uint256_t(0xad39959ae8013750, 0x7f1d2c709ab84cbb, 0x408028b80a60c2f1, 0x1dcd116fc26f856e), + uint256_t(0x95e967d30dcce9ce, 0x56139274241d2ea1, 0x85b19c1c616ec456, 0x1f1780cf9bf045b4), + uint256_t(0xbe841c861d8eb80e, 0xc5980d67a21386c0, 0x5fd1f1afecddeeb5, 0x24dbb8c1baea0250), + uint256_t(0x3ae4b3a27f05d6e3, 0xc5f6785b12df8d29, 0xc3a6c5f095103046, 0xd6b94cb2cc1fd4b), + uint256_t(0xc003c71932a6ced5, 0x6302a413f68e26e9, 0x2ed4a9b64d69fad, 0xfe61ffab1ae227d) + }; for (auto& value : values) { fq element(value); EXPECT_EQ(element.sqr(), element * element); } } -TEST(fq, neg_and_self_neg_0_cmp_regression) +TEST(fq, NegAndSelfNeg0CmpRegression) { fq a = 0; fq a_neg = -a; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.hpp index f772e8946a9..40d513192e8 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.hpp @@ -22,5 +22,5 @@ struct Bn254Fq12Params { }; }; -typedef field12 fq12; +using fq12 = field12; } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.test.cpp index 0e51dd66519..79af5af3a86 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq12.test.cpp @@ -3,7 +3,7 @@ using namespace barretenberg; -TEST(fq12, eq) +TEST(fq12, Eq) { fq12 a = { { { { 0x01, 0x02, 0x03, 0x04 }, { 0x06, 0x07, 0x08, 0x09 } }, { { 0x01, 0x02, 0x03, 0x04 }, { 0x06, 0x07, 0x08, 0x09 } }, @@ -77,7 +77,7 @@ TEST(fq12, eq) EXPECT_EQ((a == j), false); } -TEST(fq12, is_zero) +TEST(fq12, IsZero) { fq12 a = fq12::zero(); fq12 b = fq12::zero(); @@ -89,7 +89,7 @@ TEST(fq12, is_zero) EXPECT_EQ(c.is_zero(), false); } -TEST(fq12, random_element) +TEST(fq12, RandomElement) { fq12 a = fq12::random_element(); fq12 b = fq12::random_element(); @@ -99,7 +99,7 @@ TEST(fq12, random_element) EXPECT_EQ(a.is_zero(), false); } -TEST(fq12, add_check_against_constants) +TEST(fq12, AddCheckAgainstConstants) { fq12 a = { { { { 0xe5090b4f4ae647a8, 0xf5d4801f152fdf6c, 0xcdb69d33dba7f562, 0x228f26abab7d6687 }, { 0xc27a82b14db8404f, 0xcbf9354b3655de9b, 0xa57fd51d8df378ad, 0x2e3fc75bde967502 } }, @@ -141,7 +141,7 @@ TEST(fq12, add_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq12, sub_check_against_constants) +TEST(fq12, SubCheckAgainstConstants) { fq12 a = { { { { 0xf828470cae144505, 0xe05f8f664caae877, 0x27b46814f04c96a3, 0x49d8f97c040a1a2 }, { 0x3651629333fd6d1, 0xf7c08d56035cb892, 0x7fd937c7d75b567f, 0x11aac5d9567d8c7e } }, @@ -183,7 +183,7 @@ TEST(fq12, sub_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq12, mul_check_against_constants) +TEST(fq12, MulCheckAgainstConstants) { fq12 a = { { { { 0xd43e9f8be859502b, 0x26a42a1a95cee1ef, 0x3d63c085c1892b32, 0x2e5beaf431211a76 }, { 0x5f32ad7cee215ff5, 0xce967fda9424120e, 0x10ea4e52628bac33, 0x51b85ee9671b7f3 } }, @@ -225,7 +225,7 @@ TEST(fq12, mul_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq12, sparse_mul_check_against_constants) +TEST(fq12, SparseMulCheckAgainstConstants) { fq12 a = { { { { 0x8860ba0c4eea41a8, 0x71b65207984d47d2, 0x67e55696f8982ba9, 0x18236f03bcec9b00 }, { 0xa69e0f0ce60f64fd, 0x1cf52f3b2335b9b3, 0x45e8ec475fcb1d71, 0x1627ac08d10cebd9 } }, @@ -263,7 +263,7 @@ TEST(fq12, sparse_mul_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq12, sqr_check_against_constants) +TEST(fq12, SqrCheckAgainstConstants) { fq12 a = { { { { 0xef9d68a7df0715fd, 0xfda8aff4030523cf, 0xd09b1482069c0972, 0x252195422f351b07 }, { 0x3192057a31dec453, 0xe1c2dd8879191e47, 0xe90a8a00c9b29c5b, 0x1db75f06dff5dd5e } }, @@ -293,14 +293,14 @@ TEST(fq12, sqr_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq12, inverse) +TEST(fq12, Inverse) { fq12 input = fq12::random_element(); fq12 result = input.invert() * input; EXPECT_EQ(result, fq12::one()); } -TEST(fq12, unitary_inverse) +TEST(fq12, UnitaryInverse) { fq12 input = fq12::random_element(); fq12 result = input.unitary_inverse(); @@ -309,7 +309,7 @@ TEST(fq12, unitary_inverse) EXPECT_EQ(result.c1, fq6::zero()); } -TEST(fq12, frobenius_map_three) +TEST(fq12, FrobeniusMapThree) { fq12 a = { { { { 0x9a56f1e63b1f0db8, 0xd629a6c847f6cedd, 0x4a179c053a91458b, 0xa84c02b0b6d7470 }, { 0xffa3e17eab3609a1, 0x6a97b9cf5c3fe152, 0x8996248da177be9f, 0x113bd2d7f24591d } }, @@ -339,7 +339,7 @@ TEST(fq12, frobenius_map_three) EXPECT_EQ(result, expected); } -TEST(fq12, frobenius_map_two) +TEST(fq12, FrobeniusMapTwo) { fq12 a = { { { { 0x52c2cc6e77bfe9bb, 0xd03d98cc3fd6d95, 0xfaeb6d6577aa9a30, 0x1ea38b81330e34df }, { 0x1f55d493000a14f3, 0x1db7ec50e2f5a356, 0xf3cfcc74b91481ae, 0x256fe76342b33dbb } }, @@ -369,7 +369,7 @@ TEST(fq12, frobenius_map_two) EXPECT_EQ(result, expected); } -TEST(fq12, frobenius_map_one) +TEST(fq12, FrobeniusMapOne) { fq12 a = { { { { 0x6c9edca7f0d6f6e, 0x7bb482de96b01e0, 0xb04fc4b2b2ea7e6, 0x4d9efc00ceb8323 }, { 0xb55c2222935ee583, 0x9c114ab89499b4da, 0x771cb5cabe1f458a, 0x1c3f0ac5303a5935 } }, @@ -399,7 +399,7 @@ TEST(fq12, frobenius_map_one) EXPECT_EQ(result, expected); } -TEST(fq12, to_montgomery_form) +TEST(fq12, ToMontgomeryForm) { fq12 result = fq12::zero(); result.c0.c0.c0.data[0] = 1; @@ -408,7 +408,7 @@ TEST(fq12, to_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq12, from_montgomery_form) +TEST(fq12, FromMontgomeryForm) { fq12 result = fq12::one(); fq12 expected = fq12::zero(); @@ -417,7 +417,7 @@ TEST(fq12, from_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq12, mul_sqr_consistency) +TEST(fq12, MulSqrConsistency) { fq12 a = fq12::random_element(); fq12 b = fq12::random_element(); @@ -428,7 +428,7 @@ TEST(fq12, mul_sqr_consistency) EXPECT_EQ(mul_result, sqr_result); } -TEST(fq12, add_mul_consistency) +TEST(fq12, AddMulConsistency) { fq12 multiplicand = fq12::zero(); multiplicand.c0.c0.c0.data[0] = 9; @@ -445,7 +445,7 @@ TEST(fq12, add_mul_consistency) EXPECT_EQ(result, expected); } -TEST(fq12, sub_mul_consistency) +TEST(fq12, SubMulConsistency) { fq12 multiplicand = fq12::zero(); multiplicand.c0.c0.c0.data[0] = 5; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.hpp index d96f487be8e..d659ee4e766 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.hpp @@ -31,5 +31,5 @@ struct Bn254Fq2Params { }; }; -typedef field2 fq2; +using fq2 = field2; } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.test.cpp index 4bad34d03fb..491f8f54d1b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq2.test.cpp @@ -27,7 +27,7 @@ TEST(fq2, eq) EXPECT_EQ(a == j, false); } -TEST(fq2, is_zero) +TEST(fq2, IsZero) { fq2 a = fq2::zero(); fq2 b = fq2::zero(); @@ -39,7 +39,7 @@ TEST(fq2, is_zero) EXPECT_EQ(c.is_zero(), false); } -TEST(fq2, random_element) +TEST(fq2, RandomElement) { fq2 a = fq2::random_element(); fq2 b = fq2::random_element(); @@ -49,7 +49,7 @@ TEST(fq2, random_element) EXPECT_EQ(b.is_zero(), false); } -TEST(fq2, mul_check_against_constants) +TEST(fq2, MulCheckAgainstConstants) { fq2 a = { { 0xd673ba38b8c4bc86, 0x860cd1cb9e2f0c85, 0x3185f9f9166177b7, 0xd043f963ced2529 }, { 0xd4d2fad9a3de5d98, 0x260f72ca434ef415, 0xca5c20c435accb2d, 0x122a54f828a07ffe } }; @@ -61,7 +61,7 @@ TEST(fq2, mul_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq2, sqr_check_against_constants) +TEST(fq2, SqrCheckAgainstConstants) { fq2 a = { { 0x26402fd760069ee8, 0x17828cf3bf7dd3e3, 0x4e7449f7b1149987, 0x102f6467805d7298 }, { 0xa2a31bf895eaf6f8, 0xf0c88d415c372b16, 0xa65ccca8b7806691, 0x1b51e4526673451f } }; @@ -71,7 +71,7 @@ TEST(fq2, sqr_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq2, add_check_against_constants) +TEST(fq2, AddCheckAgainstConstants) { fq2 a = { { 0x517c157ce1664f30, 0x114ba401b0996437, 0x11b9ae2d856012e8, 0xcc19341ea7cf685 }, { 0x17c6020dde15fdc0, 0x310bc25961b2f002, 0xa766e7e94a865c0d, 0x20176bc8e6b82863 } }; @@ -83,7 +83,7 @@ TEST(fq2, add_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq2, sub_check_against_constants) +TEST(fq2, SubCheckAgainstConstants) { fq2 a = { { 0x3212c3a7d7886da5, 0xcea893f4addae4aa, 0x5c8bfca7a7ed01be, 0x1a8e9dfecd598ef1 }, { 0x4a8d9e6443fda462, 0x93248a3fde6374e7, 0xf4a6c52f75c0fc2e, 0x270aaabb4ae43370 } }; @@ -95,7 +95,7 @@ TEST(fq2, sub_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq2, to_montgomery_form) +TEST(fq2, ToMontgomeryForm) { fq2 result = fq2::zero(); result.c0.data[0] = 1; @@ -104,7 +104,7 @@ TEST(fq2, to_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq2, from_montgomery_form) +TEST(fq2, FromMontgomeryForm) { fq2 result = fq2::one(); fq2 expected = fq2::zero(); @@ -113,7 +113,7 @@ TEST(fq2, from_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq2, mul_sqr_consistency) +TEST(fq2, MulSqrConsistency) { fq2 a = fq2::random_element(); fq2 b = fq2::random_element(); @@ -130,7 +130,7 @@ TEST(fq2, mul_sqr_consistency) EXPECT_EQ(mul_result, sqr_result); } -TEST(fq2, add_mul_consistency) +TEST(fq2, AddMulConsistency) { fq2 multiplicand = { { 0x09, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00 } }; multiplicand = multiplicand.to_montgomery_form(); @@ -146,7 +146,7 @@ TEST(fq2, add_mul_consistency) EXPECT_EQ(result, expected); } -TEST(fq2, sub_mul_consistency) +TEST(fq2, SubMulConsistency) { fq2 multiplicand = { { 0x05, 0, 0, 0 }, { 0x00, 0x00, 0x00, 0x00 } }; multiplicand = multiplicand.to_montgomery_form(); @@ -164,7 +164,7 @@ TEST(fq2, sub_mul_consistency) EXPECT_EQ(result, expected); } -TEST(fq2, invert) +TEST(fq2, Invert) { fq2 input = fq2::random_element(); fq2 inverse = input.invert(); @@ -172,9 +172,9 @@ TEST(fq2, invert) EXPECT_EQ(result, fq2::one()); } -TEST(fq2, serialize) +TEST(fq2, Serialize) { - uint8_t buffer[64]; + std::array buffer; fq expected_c0 = { 0x1234567876543210, 0x2345678987654321, 0x3456789a98765432, 0x006789abcba98765 }; fq expected_c1 = { 0x12a4e67f76b43210, 0x23e56f898a65cc21, 0x005678add98e5432, 0x1f6789a2cba98700 }; fq2 expected{ expected_c0, expected_c1 }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.hpp index a075b1a353a..a9c582db319 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.hpp @@ -57,5 +57,5 @@ struct Bn254Fq6Params { } }; -typedef field6 fq6; +using fq6 = field6; } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.test.cpp index 70d005dfd3b..baf0830af8b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fq6.test.cpp @@ -3,7 +3,7 @@ using namespace barretenberg; -TEST(fq6, eq) +TEST(fq6, Eq) { fq6 a{ { { 0x01, 0x02, 0x03, 0x04 }, { 0x06, 0x07, 0x08, 0x09 } }, { { 0x01, 0x02, 0x03, 0x04 }, { 0x06, 0x07, 0x08, 0x09 } }, @@ -47,7 +47,7 @@ TEST(fq6, eq) EXPECT_EQ((a == j), false); } -TEST(fq6, is_zero) +TEST(fq6, IsZero) { fq6 a = fq6::zero(); fq6 b = fq6::zero(); @@ -62,7 +62,7 @@ TEST(fq6, is_zero) EXPECT_EQ(d.is_zero(), false); } -TEST(fq6, random_element) +TEST(fq6, RandomElement) { fq6 a = fq6::random_element(); fq6 b = fq6::random_element(); @@ -72,7 +72,7 @@ TEST(fq6, random_element) EXPECT_EQ(b.is_zero(), false); } -TEST(fq6, add_check_against_constants) +TEST(fq6, AddCheckAgainstConstants) { fq6 a{ { { 0x68138b3c3e5e820b, 0x9bf71d36786da85f, 0x815831c12e257996, 0x2280b875a27e6d1d }, { 0xff4b05a0ed0df393, 0x94fbe538fe78c1e7, 0xea26586b05301ac2, 0xc90d9fbd6f0360a } }, @@ -96,7 +96,7 @@ TEST(fq6, add_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq6, sub_check_against_constants) +TEST(fq6, SubCheckAgainstConstants) { fq6 a{ { { 0xa1167f5753441035, 0xc7712ba686dd96d, 0x1da0e185b8aa61a3, 0xc875cfdb65ae0b0 }, { 0x69f9322c2f24bd33, 0x322a253d10e59171, 0xa661cfb9aaa595e8, 0x250efd5132c6f2be } }, @@ -120,7 +120,7 @@ TEST(fq6, sub_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq6, mul_check_against_constants) +TEST(fq6, MulCheckAgainstConstants) { fq6 a{ { { 0xa7e3494fc528b8c8, 0xc8c8906c9682e43f, 0xc6e76fc21152721c, 0x12a4c3ee3ff10dbd }, { 0x887ce62a3ae2a578, 0x70caee28e1942bac, 0xc1a58242c34ff94f, 0x0b154d910b492542 } }, @@ -144,7 +144,7 @@ TEST(fq6, mul_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq6, sqr_check_against_constants) +TEST(fq6, SqrCheckAgainstConstants) { fq6 a{ { { 0xe337aaa063afce6, 0xff4b5477485eb20, 0xef6dcf13b3855ef8, 0x14554c38da988ece }, { 0x6a70e65e71431416, 0xd21f95045c45f422, 0x2a17b6c6ff517884, 0x1b01ad6487a3ff16 } }, @@ -162,7 +162,7 @@ TEST(fq6, sqr_check_against_constants) EXPECT_EQ(result, expected); } -TEST(fq6, to_montgomery_form) +TEST(fq6, ToMontgomeryForm) { fq6 result = fq6::zero(); result.c0.c0.data[0] = 1; @@ -171,7 +171,7 @@ TEST(fq6, to_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq6, from_montgomery_form) +TEST(fq6, FromMontgomeryForm) { fq6 result = fq6::one(); fq6 expected = fq6::zero(); @@ -180,7 +180,7 @@ TEST(fq6, from_montgomery_form) EXPECT_EQ(result, expected); } -TEST(fq6, mul_sqr_consistency) +TEST(fq6, MulSqrConsistency) { fq6 a = fq6::random_element(); fq6 b = fq6::random_element(); @@ -192,7 +192,7 @@ TEST(fq6, mul_sqr_consistency) EXPECT_EQ(mul_result, sqr_result); } -TEST(fq6, add_mul_consistency) +TEST(fq6, AddMulConsistency) { fq6 multiplicand = fq6::zero(); multiplicand.c0.c0.data[0] = 9; @@ -208,7 +208,7 @@ TEST(fq6, add_mul_consistency) EXPECT_EQ(result, expected); } -TEST(fq6, sub_mul_consistency) +TEST(fq6, SubMulConsistency) { fq6 multiplicand = fq6::zero(); multiplicand.c0.c0.data[0] = 5; @@ -226,7 +226,7 @@ TEST(fq6, sub_mul_consistency) EXPECT_EQ(result, expected); } -TEST(fq6, invert) +TEST(fq6, Invert) { fq6 input = fq6::random_element(); fq6 result = input.invert(); @@ -235,9 +235,11 @@ TEST(fq6, invert) EXPECT_EQ(result, fq6::one()); } -TEST(fq6, copy) +TEST(fq6, Copy) { fq6 result = fq6::random_element(); + + // NOLINTNEXTLINE(performance-unnecessary-copy-initialization) this is what we want to test! fq6 expected = result; EXPECT_EQ(result, expected); } \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.bench.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.bench.cpp index 89752c24f40..77618655f6b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.bench.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.bench.cpp @@ -115,9 +115,10 @@ uint64_t rdtsc() __asm__ __volatile__("mrs %0, pmccntr_el0" : "=r"(pmccntr)); return pmccntr; #elif __x86_64__ - unsigned int lo, hi; + unsigned int lo = 0; + unsigned int hi = 0; __asm__ __volatile__("rdtsc" : "=a"(lo), "=d"(hi)); - return ((uint64_t)hi << 32) | lo; + return (static_cast(hi) << 32) | lo; #else return 0; #endif @@ -167,7 +168,7 @@ void sqr_assign_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "sqr_assign clocks per operation = " << average << std::endl; } BENCHMARK(sqr_assign_bench); @@ -190,7 +191,7 @@ void sqr_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "sqr clocks per operation = " << average << std::endl; } BENCHMARK(sqr_bench); @@ -213,7 +214,7 @@ void unary_minus_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "unary minus clocks per operation = " << average << std::endl; } BENCHMARK(unary_minus_bench); @@ -236,7 +237,7 @@ void mul_assign_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "mul assign clocks per operation = " << average << std::endl; } BENCHMARK(mul_assign_bench); @@ -260,7 +261,7 @@ void mul_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "mul clocks per operation = " << average << std::endl; } BENCHMARK(mul_bench); @@ -284,7 +285,7 @@ void self_add_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "self add clocks per operation = " << average << std::endl; } BENCHMARK(self_add_bench); @@ -308,7 +309,7 @@ void add_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "add clocks per operation = " << average << std::endl; } BENCHMARK(add_bench); @@ -332,7 +333,7 @@ void sub_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "sub clocks per operation = " << average << std::endl; } BENCHMARK(sub_bench); @@ -358,7 +359,7 @@ void addaddmul_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "field clocks per call = " << average << std::endl; } BENCHMARK(addaddmul_bench); @@ -384,7 +385,7 @@ void subaddmul_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "field clocks per call = " << average << std::endl; } BENCHMARK(subaddmul_bench); @@ -405,7 +406,7 @@ void field_bench(State& state) noexcept clocks += (rdtsc() - before); ++count; } - double average = static_cast(clocks) / (static_cast(count) * double(NUM_POINTS)); + double average = static_cast(clocks) / (static_cast(count) * static_cast(NUM_POINTS)); std::cout << "field clocks per call = " << average << std::endl; } BENCHMARK(field_bench); @@ -435,4 +436,5 @@ void pow_bench(State& state) noexcept } BENCHMARK(pow_bench); +// NOLINTNEXTLINE macro invokation triggers style guideline errors from googletest code BENCHMARK_MAIN(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.hpp index 142804d93dd..81609405385 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.hpp @@ -6,6 +6,8 @@ #include "../../fields/field.hpp" +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) + namespace barretenberg { class Bn254FrParams { public: @@ -66,6 +68,8 @@ class Bn254FrParams { static constexpr bool has_high_2adicity = true; }; -typedef field fr; +using fr = field; + +} // namespace barretenberg -} // namespace barretenberg \ No newline at end of file +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp index 0c45f2301c3..37bbf385995 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/fr.test.cpp @@ -4,13 +4,13 @@ using namespace barretenberg; -TEST(fr, msgpack) +TEST(fr, Msgpack) { - auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{ 1ull, 2ull, 3ull, 4ull }); + auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{ 1ULL, 2ULL, 3ULL, 4ULL }); EXPECT_EQ(actual, expected); } -TEST(fr, eq) +TEST(fr, Eq) { fr a{ 0x01, 0x02, 0x03, 0x04 }; fr b{ 0x01, 0x02, 0x03, 0x04 }; @@ -25,7 +25,7 @@ TEST(fr, eq) EXPECT_EQ((a == f), false); } -TEST(fr, is_zero) +TEST(fr, IsZero) { fr a = fr::zero(); fr b = fr::zero(); @@ -44,7 +44,7 @@ TEST(fr, is_zero) EXPECT_EQ(e.is_zero(), false); } -TEST(fr, random_element) +TEST(fr, RandomElement) { fr a = fr::random_element(); fr b = fr::random_element(); @@ -54,7 +54,7 @@ TEST(fr, random_element) EXPECT_EQ(b.is_zero(), false); } -TEST(fr, mul) +TEST(fr, Mul) { fr a{ 0x192f9ddc938ea63, 0x1db93d61007ec4fe, 0xc89284ec31fa49c0, 0x2478d0ff12b04f0f }; fr b{ 0x7aade4892631231c, 0x8e7515681fe70144, 0x98edb76e689b6fd8, 0x5d0886b15fc835fa }; @@ -64,7 +64,7 @@ TEST(fr, mul) EXPECT_EQ((result == expected), true); } -TEST(fr, sqr) +TEST(fr, Sqr) { fr a{ 0x95f946723a1fc34f, 0x641ec0482fc40bb9, 0xb8d645bc49dd513d, 0x1c1bffd317599dbc }; fr expected{ 0xc787f7d9e2c72714, 0xcf21cf53d8f65f67, 0x8db109903dac0008, 0x26ab4dd65f46be5f }; @@ -73,7 +73,7 @@ TEST(fr, sqr) EXPECT_EQ((result == expected), true); } -TEST(fr, add) +TEST(fr, Add) { fr a{ 0x20565a572c565a66, 0x7bccd0f01f5f7bff, 0x63ec2beaad64711f, 0x624953caaf44a814 }; fr b{ 0xa17307a2108adeea, 0x74629976c14c5e2b, 0x9ce6f072ab1740ee, 0x398c753702b2bef0 }; @@ -83,7 +83,7 @@ TEST(fr, add) EXPECT_EQ(result, expected.reduce_once()); } -TEST(fr, sub) +TEST(fr, Sub) { fr a{ 0xcfbcfcf457cf2d38, 0x7b27af26ce62aa61, 0xf0378e90d48f2b92, 0x4734b22cb21ded }; fr b{ 0x569fdb1db5198770, 0x446ddccef8347d52, 0xef215227182d22a, 0x8281b4fb109306 }; @@ -93,7 +93,7 @@ TEST(fr, sub) EXPECT_EQ((result == expected), true); } -TEST(fr, plus_equals) +TEST(fr, PlusEquals) { fr a{ 0x5def, 0x00, 0x00, 0x00 }; fr a_copy = a; @@ -106,14 +106,14 @@ TEST(fr, plus_equals) EXPECT_EQ((a == expected), true); } -TEST(fr, prefix_increment) +TEST(fr, PrefixIncrement) { fr a{ 0x5def, 0x00, 0x00, 0x00 }; fr b = ++a; EXPECT_EQ(b, a); } -TEST(fr, postfix_increment) +TEST(fr, PostfixIncrement) { fr a{ 0x5def, 0x00, 0x00, 0x00 }; fr a_old = a; @@ -122,7 +122,7 @@ TEST(fr, postfix_increment) EXPECT_EQ(a, a_old + 1); } -TEST(fr, to_montgomery_form) +TEST(fr, ToMontgomeryForm) { fr result{ 0x01, 0x00, 0x00, 0x00 }; fr expected = fr::one(); @@ -130,7 +130,7 @@ TEST(fr, to_montgomery_form) EXPECT_EQ((result == expected), true); } -TEST(fr, from_montgomery_form) +TEST(fr, FromMontgomeryForm) { fr result = fr::one(); fr expected{ 0x01, 0x00, 0x00, 0x00 }; @@ -138,7 +138,7 @@ TEST(fr, from_montgomery_form) EXPECT_EQ((result == expected), true); } -TEST(fr, montgomery_consistency_check) +TEST(fr, MontgomeryConsistencyCheck) { fr a = fr::random_element(); fr b = fr::random_element(); @@ -170,7 +170,7 @@ TEST(fr, montgomery_consistency_check) EXPECT_EQ((result_a == result_d), true); } -TEST(fr, add_mul_consistency) +TEST(fr, AddMulConsistency) { fr multiplicand = { 0x09, 0, 0, 0 }; multiplicand.self_to_montgomery_form(); @@ -188,7 +188,7 @@ TEST(fr, add_mul_consistency) EXPECT_EQ((result == expected), true); } -TEST(fr, sub_mul_consistency) +TEST(fr, SubMulConsistency) { fr multiplicand = { 0x05, 0, 0, 0 }; multiplicand.self_to_montgomery_form(); @@ -208,7 +208,7 @@ TEST(fr, sub_mul_consistency) EXPECT_EQ((result == expected), true); } -TEST(fr, lambda) +TEST(fr, Lambda) { fr x = fr::random_element(); @@ -229,7 +229,7 @@ TEST(fr, lambda) EXPECT_EQ((x_cubed == lambda_x_cubed), true); } -TEST(fr, invert) +TEST(fr, Invert) { fr input = fr::random_element(); fr inverse = input.invert(); @@ -238,14 +238,14 @@ TEST(fr, invert) EXPECT_EQ((result == fr::one()), true); } -TEST(fr, invert_one_is_one) +TEST(fr, InvertOneIsOne) { fr result = fr::one(); result = result.invert(); EXPECT_EQ((result == fr::one()), true); } -TEST(fr, sqrt) +TEST(fr, Sqrt) { fr input = fr::one(); auto [is_sqr, root] = input.sqrt(); @@ -253,7 +253,7 @@ TEST(fr, sqrt) EXPECT_EQ(result, input); } -TEST(fr, sqrt_random) +TEST(fr, SqrtRandom) { size_t n = 1; for (size_t i = 0; i < n; ++i) { @@ -264,14 +264,14 @@ TEST(fr, sqrt_random) } } -TEST(fr, one_and_zero) +TEST(fr, OneAndZero) { fr result; result = fr::one() - fr::one(); EXPECT_EQ((result == fr::zero()), true); } -TEST(fr, copy) +TEST(fr, Copy) { fr result = fr::random_element(); fr expected; @@ -279,7 +279,7 @@ TEST(fr, copy) EXPECT_EQ((result == expected), true); } -TEST(fr, neg) +TEST(fr, Neg) { fr a = fr::random_element(); fr b; @@ -289,7 +289,7 @@ TEST(fr, neg) EXPECT_EQ((result == fr::zero()), true); } -TEST(fr, split_into_endomorphism_scalars) +TEST(fr, SplitIntoEndomorphismScalars) { fr k = fr::random_element(); fr k1 = { 0, 0, 0, 0 }; @@ -310,7 +310,7 @@ TEST(fr, split_into_endomorphism_scalars) EXPECT_EQ(result, k); } -TEST(fr, split_into_endomorphism_scalars_simple) +TEST(fr, SplitIntoEndomorphismScalarsSimple) { fr input = { 1, 0, 0, 0 }; @@ -335,17 +335,17 @@ TEST(fr, split_into_endomorphism_scalars_simple) } } -TEST(fr, batch_invert) +TEST(fr, BatchInvert) { size_t n = 10; - fr coeffs[n]; - fr inverses[n]; + std::vector coeffs(n); + std::vector inverses(n); fr one = fr::one(); for (size_t i = 0; i < n; ++i) { coeffs[i] = fr::random_element(); fr::__copy(coeffs[i], inverses[i]); } - fr::batch_invert(inverses, n); + fr::batch_invert(&inverses[0], n); for (size_t i = 0; i < n; ++i) { coeffs[i] *= inverses[i]; @@ -360,12 +360,12 @@ TEST(fr, batch_invert) } } -TEST(fr, multiplicative_generator) +TEST(fr, MultiplicativeGenerator) { EXPECT_EQ(fr::multiplicative_generator(), fr(5)); } -TEST(fr, uint256_conversions) +TEST(fr, Uint256Conversions) { constexpr uint256_t a{ 0x1111, 0x2222, 0x3333, 0x4444 }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.hpp index 65d0ffaa844..ed16dbdb036 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.hpp @@ -16,13 +16,13 @@ struct Bn254G1Params { static constexpr fq b{ 0x7a17caa950ad28d7UL, 0x1f6ac17ae15521b9UL, 0x334bea4e696bd284UL, 0x2a1f6744ce179d8eUL }; }; -typedef group g1; +using g1 = group; } // namespace barretenberg // specialize the name in msgpack schema generation // consumed by the typescript schema compiler, helps disambiguate templates -inline std::string msgpack_schema_name(barretenberg::g1::affine_element const&) +inline std::string msgpack_schema_name(barretenberg::g1::affine_element const& /*unused*/) { return "G1AffineElement"; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.test.cpp index e636e8f9554..c649456c2ff 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g1.test.cpp @@ -4,19 +4,19 @@ using namespace barretenberg; namespace test_g1 { -TEST(g1, random_element) +TEST(g1, RandomElement) { g1::element result = g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(g1, random_affine_element) +TEST(g1, RandomAffineElement) { - g1::affine_element result = g1::affine_element(g1::element::random_element()); + g1::affine_element result = g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(g1, eq) +TEST(g1, Eq) { g1::element a = g1::element::random_element(); g1::element b = a.normalize(); @@ -36,7 +36,7 @@ TEST(g1, eq) EXPECT_EQ(a == b, true); } -TEST(g1, mixed_add_check_against_constants) +TEST(g1, MixedAddCheckAgainstConstants) { fq a_x{ 0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5 }; fq a_y{ 0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad }; @@ -63,7 +63,7 @@ TEST(g1, mixed_add_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g1, dbl_check_against_constants) +TEST(g1, DblCheckAgainstConstants) { fq a_x{ 0x8d1703aa518d827f, 0xd19cc40779f54f63, 0xabc11ce30d02728c, 0x10938940de3cbeec }; fq a_y{ 0xcf1798994f1258b4, 0x36307a354ad90a25, 0xcd84adb348c63007, 0x6266b85241aff3f }; @@ -88,7 +88,7 @@ TEST(g1, dbl_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g1, add_check_against_constants) +TEST(g1, AddCheckAgainstConstants) { fq a_x{ 0x184b38afc6e2e09a, 0x4965cd1c3687f635, 0x334da8e7539e71c4, 0xf708d16cfe6e14 }; fq a_y{ 0x2a6ff6ffc739b3b6, 0x70761d618b513b9, 0xbf1645401de26ba1, 0x114a1616c164b980 }; @@ -119,7 +119,7 @@ TEST(g1, add_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g1, add_exception_test_infinity) +TEST(g1, AddExceptionTestInfinity) { g1::element lhs = g1::element::random_element(); g1::element rhs; @@ -145,7 +145,7 @@ TEST(g1, add_exception_test_infinity) EXPECT_EQ(rhs == result, true); } -TEST(g1, test_infinity) +TEST(g1, TestInfinity) { g1::affine_element inf_affine = g1::affine_element::infinity(); EXPECT_EQ(inf_affine.is_point_at_infinity(), true); @@ -154,7 +154,7 @@ TEST(g1, test_infinity) EXPECT_EQ(inf_element.is_point_at_infinity(), true); } -TEST(g1, add_exception_test_dbl) +TEST(g1, AddExceptionTestDbl) { g1::element lhs = g1::element::random_element(); g1::element rhs; @@ -169,20 +169,20 @@ TEST(g1, add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(g1, add_affine_test) +TEST(g1, AddAffineTest) { g1::element lhs = g1::element::random_element(); - g1::affine_element lhs_affine = g1::affine_element(lhs); + g1::affine_element lhs_affine(lhs); g1::element rhs = g1::element::random_element(); - g1::affine_element rhs_affine = g1::affine_element(rhs); + g1::affine_element rhs_affine(rhs); g1::element expected = lhs + rhs; g1::affine_element result = lhs_affine + rhs_affine; EXPECT_EQ(g1::element(result) == expected, true); } -TEST(g1, add_dbl_consistency) +TEST(g1, AddDblConsistency) { g1::element a = g1::element::random_element(); g1::element b = g1::element::random_element(); @@ -202,7 +202,7 @@ TEST(g1, add_dbl_consistency) EXPECT_EQ(add_result == dbl_result, true); } -TEST(g1, add_dbl_consistency_repeated) +TEST(g1, AddDblConsistencyRepeated) { g1::element a = g1::element::random_element(); g1::element b; @@ -225,10 +225,10 @@ TEST(g1, add_dbl_consistency_repeated) EXPECT_EQ(result == expected, true); } -TEST(g1, mixed_add_exception_test_infinity) +TEST(g1, MixedAddExceptionTestInfinity) { g1::element lhs = g1::one; - g1::affine_element rhs = g1::affine_element(g1::element::random_element()); + g1::affine_element rhs = g1::element::random_element(); fq::__copy(rhs.x, lhs.x); lhs.y = -rhs.y; @@ -245,9 +245,9 @@ TEST(g1, mixed_add_exception_test_infinity) EXPECT_EQ(rhs_c == result, true); } -TEST(g1, mixed_add_exception_test_dbl) +TEST(g1, MixedAddExceptionTestDbl) { - g1::affine_element rhs = g1::affine_element(g1::element::random_element()); + g1::affine_element rhs = g1::element::random_element(); g1::element lhs; lhs = g1::element(rhs); @@ -260,9 +260,9 @@ TEST(g1, mixed_add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(g1, add_mixed_add_consistency_check) +TEST(g1, AddMixedAddConsistencyCheck) { - g1::affine_element rhs = g1::affine_element(g1::element::random_element()); + g1::affine_element rhs = g1::element::random_element(); g1::element lhs = g1::element::random_element(); g1::element rhs_b; rhs_b = g1::element(rhs); @@ -275,18 +275,18 @@ TEST(g1, add_mixed_add_consistency_check) EXPECT_EQ(add_result == mixed_add_result, true); } -TEST(g1, batch_normalize) +TEST(g1, BatchNormalize) { size_t num_points = 2; - g1::element points[num_points]; - g1::element normalized[num_points]; + std::vector points(num_points); + std::vector normalized(num_points); for (size_t i = 0; i < num_points; ++i) { g1::element a = g1::element::random_element(); g1::element b = g1::element::random_element(); points[i] = a + b; normalized[i] = points[i]; } - g1::element::batch_normalize(normalized, num_points); + g1::element::batch_normalize(&normalized[0], num_points); for (size_t i = 0; i < num_points; ++i) { fq zz; @@ -303,7 +303,7 @@ TEST(g1, batch_normalize) } } -TEST(g1, group_exponentiation_check_against_constants) +TEST(g1, GroupExponentiationCheckAgainstConstants) { fr a{ 0xb67299b792199cf0, 0xc1da7df1e7e12768, 0x692e427911532edf, 0x13dd85e87dc89978 }; a.self_to_montgomery_form(); @@ -320,7 +320,7 @@ TEST(g1, group_exponentiation_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g1, operator_ordering) +TEST(g1, OperatorOrdering) { // fq a_x{ 0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5 }; // fq a_y{ 0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad }; @@ -345,18 +345,18 @@ TEST(g1, operator_ordering) EXPECT_EQ(g, h); } -TEST(g1, group_exponentiation_zero_and_one) +TEST(g1, GroupExponentiationZeroAndOne) { g1::affine_element result(g1::one * fr::zero()); EXPECT_EQ(result.is_point_at_infinity(), true); - result = g1::affine_element(g1::one * fr::one()); + result = g1::one * fr::one(); EXPECT_EQ(result == g1::affine_one, true); } -TEST(g1, group_exponentiation_consistency_check) +TEST(g1, GroupExponentiationConsistencyCheck) { fr a = fr::random_element(); fr b = fr::random_element(); @@ -373,7 +373,7 @@ TEST(g1, group_exponentiation_consistency_check) EXPECT_EQ(result == expected, true); } -TEST(g1, derive_generators) +TEST(g1, DeriveGenerators) { constexpr size_t num_generators = 128; auto result = g1::derive_generators(); @@ -393,28 +393,29 @@ TEST(g1, derive_generators) } } -TEST(g1, serialize) +TEST(g1, Serialize) { - g1::affine_element expected = g1::affine_element(g1::element::random_element()); + g1::affine_element expected = g1::element::random_element(); - uint8_t buffer[sizeof(g1::affine_element)]; + std::vector buffer(sizeof(g1::affine_element)); - g1::affine_element::serialize_to_buffer(expected, buffer); + g1::affine_element::serialize_to_buffer(expected, &buffer[0]); - g1::affine_element result = g1::affine_element::serialize_from_buffer(buffer); + g1::affine_element result = g1::affine_element::serialize_from_buffer(&buffer[0]); EXPECT_EQ(result == expected, true); } template void write(const T t) { FILE* fp = fopen("/dev/null", "wb"); - fwrite(&t, sizeof(t), 1, fp); - fclose(fp); + static_cast(fwrite(&t, sizeof(t), 1, fp)); + static_cast(fclose(fp)); } #if !defined(__wasm__) -TEST(g1, initialization_check) +TEST(g1, InitializationCheck) { + // NOLINTNEXTLINE not our fault googletest uses `goto`! EXPECT_NO_THROW(write({})); } #endif diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.hpp index 37eee4ae379..0be3e32e6f9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.hpp @@ -19,5 +19,5 @@ struct Bn254G2Params { static constexpr fq2 b = fq2::twist_coeff_b(); }; -typedef group g2; +using g2 = group; } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.test.cpp index d2a4a7a0b64..467744ac06c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/g2.test.cpp @@ -3,19 +3,19 @@ using namespace barretenberg; -TEST(g2, random_element) +TEST(g2, RandomElement) { g2::element result = g2::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(g2, random_affine_element) +TEST(g2, RandomAffineElement) { - g2::affine_element result = g2::affine_element(g2::element::random_element()); + g2::affine_element result = g2::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(g2, eq) +TEST(g2, Eq) { g2::element a = g2::element::random_element(); g2::element b = a.normalize(); @@ -35,7 +35,7 @@ TEST(g2, eq) EXPECT_EQ(a == b, true); } -TEST(g2, dbl_check_against_constants) +TEST(g2, DblCheckAgainstConstants) { g2::element lhs = { { { 0x46debd5cd992f6ed, 0x674322d4f75edadd, 0x426a00665e5c4479, 0x1800deef121f1e76 }, { 0x97e485b7aef312c2, 0xf1aa493335a9e712, 0x7260bfb731fb5d25, 0x198e9393920d483a } }, @@ -61,7 +61,7 @@ TEST(g2, dbl_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g2, mixed_add_check_against_constants) +TEST(g2, MixedAddCheckAgainstConstants) { g2::element lhs = { { { 0xfe0ee11d88ef9c7c, 0xa50b3642c93787df, 0x5c4925f0812249a3, 0x13360054113b26e5 }, { 0x85a786ba7563664d, 0xebb6adaab3da2d35, 0x2e5c4b3e8bfae51d, 0x860451c5f3cb08 } }, @@ -97,7 +97,7 @@ TEST(g2, mixed_add_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g2, add_check_against_constants) +TEST(g2, AddCheckAgainstConstants) { g2::element lhs = { { { 0xfe0ee11d88ef9c7c, 0xa50b3642c93787df, 0x5c4925f0812249a3, 0x13360054113b26e5 }, { 0x85a786ba7563664d, 0xebb6adaab3da2d35, 0x2e5c4b3e8bfae51d, 0x860451c5f3cb08 } }, @@ -133,7 +133,7 @@ TEST(g2, add_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g2, add_exception_test_infinity) +TEST(g2, AddExceptionTestInfinity) { g2::element lhs = g2::element::random_element(); g2::element rhs; @@ -159,7 +159,7 @@ TEST(g2, add_exception_test_infinity) EXPECT_EQ(rhs == result, true); } -TEST(g2, add_exception_test_dbl) +TEST(g2, AddExceptionTestDbl) { g2::element lhs = g2::element::random_element(); g2::element rhs; @@ -174,7 +174,7 @@ TEST(g2, add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(g2, add_dbl_consistency) +TEST(g2, AddDblConsistency) { g2::element a = g2::element::random_element(); g2::element b = g2::element::random_element(); @@ -194,7 +194,7 @@ TEST(g2, add_dbl_consistency) EXPECT_EQ(add_result == dbl_result, true); } -TEST(g2, add_dbl_consistency_repeated) +TEST(g2, AddDblConsistencyRepeated) { g2::element a = g2::element::random_element(); g2::element b; @@ -217,10 +217,10 @@ TEST(g2, add_dbl_consistency_repeated) EXPECT_EQ(result == expected, true); } -TEST(g2, mixed_add_exception_test_infinity) +TEST(g2, MixedAddExceptionTestInfinity) { g2::element lhs = g2::one; - g2::affine_element rhs = g2::affine_element(g2::element::random_element()); + g2::affine_element rhs = g2::element::random_element(); lhs = { rhs.x, -rhs.y, fq2::one() }; g2::element result; @@ -236,9 +236,9 @@ TEST(g2, mixed_add_exception_test_infinity) EXPECT_EQ(rhs_c == result, true); } -TEST(g2, mixed_add_exception_test_dbl) +TEST(g2, MixedAddExceptionTestDbl) { - g2::affine_element rhs = g2::affine_element(g2::element::random_element()); + g2::affine_element rhs = g2::element::random_element(); g2::element lhs; lhs = g2::element(rhs); @@ -251,9 +251,9 @@ TEST(g2, mixed_add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(g2, add_mixed_add_consistency_check) +TEST(g2, AddMixedAddConsistencyCheck) { - g2::affine_element rhs = g2::affine_element(g2::element::random_element()); + g2::affine_element rhs = g2::element::random_element(); g2::element lhs = g2::element::random_element(); g2::element rhs_b; rhs_b = g2::element(rhs); @@ -266,18 +266,19 @@ TEST(g2, add_mixed_add_consistency_check) EXPECT_EQ(add_result == mixed_add_result, true); } -TEST(g2, batch_normalize) +TEST(g2, BatchNormalize) { size_t num_points = 2; - g2::element points[num_points]; - g2::element normalized[num_points]; + std::vector points(num_points); + std::vector normalized(num_points); + for (size_t i = 0; i < num_points; ++i) { g2::element a = g2::element::random_element(); g2::element b = g2::element::random_element(); points[i] = a + b; normalized[i] = points[i]; } - g2::element::batch_normalize(normalized, num_points); + g2::element::batch_normalize(&normalized[0], num_points); for (size_t i = 0; i < num_points; ++i) { fq2 zz = points[i].z.sqr(); @@ -290,7 +291,7 @@ TEST(g2, batch_normalize) } } -TEST(g2, group_exponentiation_check_against_constants) +TEST(g2, GroupExponentiationCheckAgainstConstants) { fr scalar = { 0xc4199e4b971f705, 0xc8d89c916a23ab3d, 0x7ea3cd7c05c7af82, 0x2fdafbf994a8d400 }; g2::affine_element lhs = { { { 0x46debd5cd992f6ed, 0x674322d4f75edadd, 0x426a00665e5c4479, 0x1800deef121f1e76 }, @@ -315,17 +316,17 @@ TEST(g2, group_exponentiation_check_against_constants) EXPECT_EQ(result == expected, true); } -TEST(g2, group_exponentiation_zero_and_one) +TEST(g2, GroupExponentiationZeroAndOne) { g2::affine_element result = g2::one * fr::zero(); EXPECT_EQ(result.is_point_at_infinity(), true); - result = g2::affine_element(g2::one * fr::one()); + result = g2::one * fr::one(); EXPECT_EQ(result == g2::affine_one, true); } -TEST(g2, group_exponentiation_consistency_check) +TEST(g2, GroupExponentiationConsistencyCheck) { fr a = fr::random_element(); fr b = fr::random_element(); @@ -342,31 +343,31 @@ TEST(g2, group_exponentiation_consistency_check) EXPECT_EQ(result == expected, true); } -TEST(g2, serialize) +TEST(g2, Serialize) { // test serializing random points size_t num_repetitions(1); for (size_t i = 0; i < num_repetitions; i++) { - g2::affine_element expected = g2::affine_element(g2::element::random_element()); + g2::affine_element expected = g2::element::random_element(); - uint8_t buffer[sizeof(g2::affine_element)]; + std::array buffer; - g2::affine_element::serialize_to_buffer(expected, buffer); + g2::affine_element::serialize_to_buffer(expected, &buffer[0]); - g2::affine_element result = g2::affine_element::serialize_from_buffer(buffer); + g2::affine_element result = g2::affine_element::serialize_from_buffer(&buffer[0]); EXPECT_EQ(result == expected, true); } // test serializing the point at infinity { - g2::affine_element expected = g2::affine_element(g2::element::random_element()); + g2::affine_element expected = g2::element::random_element(); expected.self_set_infinity(); - uint8_t buffer[sizeof(g2::affine_element)]; + std::array buffer; - g2::affine_element::serialize_to_buffer(expected, buffer); + g2::affine_element::serialize_to_buffer(expected, &buffer[0]); - g2::affine_element result = g2::affine_element::serialize_from_buffer(buffer); + g2::affine_element result = g2::affine_element::serialize_from_buffer(&buffer[0]); ASSERT_TRUE(result.is_point_at_infinity()); EXPECT_EQ(result == expected, true); @@ -376,13 +377,14 @@ TEST(g2, serialize) template void write(const T t) { FILE* fp = fopen("/dev/null", "wb"); - fwrite(&t, sizeof(t), 1, fp); - fclose(fp); + static_cast(fwrite(&t, sizeof(t), 1, fp)); + static_cast(fclose(fp)); } #if !defined(__wasm__) -TEST(g2, initialization_check) +TEST(g2, InitializationCheck) { + // NOLINTNEXTLINE not our fault googletest uses `goto`! EXPECT_NO_THROW(write({})); } #endif \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.hpp index ea975c44b54..cffd6e5de5f 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.hpp @@ -8,22 +8,24 @@ #include "./g1.hpp" #include "./g2.hpp" -namespace barretenberg { -namespace pairing { +namespace barretenberg::pairing { constexpr size_t loop_length = 64; constexpr size_t neg_z_loop_length = 62; constexpr size_t precomputed_coefficients_length = 87; -constexpr uint8_t loop_bits[loop_length]{ 1, 0, 1, 0, 0, 0, 3, 0, 3, 0, 0, 0, 3, 0, 1, 0, 3, 0, 0, 3, 0, 0, - 0, 0, 0, 1, 0, 0, 3, 0, 1, 0, 0, 3, 0, 0, 0, 0, 3, 0, 1, 0, 0, 0, - 3, 0, 3, 0, 0, 1, 0, 0, 0, 3, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0 }; +constexpr std::array loop_bits{ 1, 0, 1, 0, 0, 0, 3, 0, 3, 0, 0, 0, 3, 0, 1, 0, 3, 0, 0, 3, 0, 0, + 0, 0, 0, 1, 0, 0, 3, 0, 1, 0, 0, 3, 0, 0, 0, 0, 3, 0, 1, 0, 0, 0, + 3, 0, 3, 0, 0, 1, 0, 0, 0, 3, 0, 0, 3, 0, 1, 0, 1, 0, 0, 0 }; -constexpr bool neg_z_loop_bits[neg_z_loop_length]{ 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, - 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, - 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1 }; +constexpr std::array neg_z_loop_bits{ + false, false, false, true, false, false, true, true, true, false, true, false, false, true, true, false, + false, true, false, false, true, false, true, false, true, true, false, true, false, false, false, true, + false, false, true, false, true, false, false, true, true, false, true, false, false, true, false, false, + false, false, true, false, false, true, true, true, true, true, false, false, false, true +}; struct miller_lines { - fq12::ell_coeffs lines[precomputed_coefficients_length]; + std::array lines; }; constexpr void doubling_step_for_flipped_miller_loop(g2::element& current, fq12::ell_coeffs& ell); @@ -54,7 +56,6 @@ inline fq12 reduced_ate_pairing_batch_precomputed(const g1::affine_element* P_af const miller_lines* lines, size_t num_points); -} // namespace pairing -} // namespace barretenberg +} // namespace barretenberg::pairing #include "./pairing_impl.hpp" \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.test.cpp index 199692b4eef..2d5cf8fe0a8 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing.test.cpp @@ -3,7 +3,7 @@ using namespace barretenberg; -TEST(pairing, reduced_ate_pairing_check_against_constants) +TEST(pairing, ReducedAtePairingCheckAgainstConstants) { constexpr g1::affine_element P = { uint256_t(0x956e256b9db00c13, 0x66d29ac18e1b2bff, 0x5d6f055e34402f6e, 0x5bfcbaaff0feb62), @@ -43,10 +43,10 @@ TEST(pairing, reduced_ate_pairing_check_against_constants) EXPECT_EQ(result, expected); } -TEST(pairing, reduced_ate_pairing_consistency_check) +TEST(pairing, ReducedAtePairingConsistencyCheck) { - g1::affine_element P = g1::affine_element(g1::element::random_element()); - g2::affine_element Q = g2::affine_element(g2::element::random_element()); + g1::affine_element P = g1::element::random_element(); + g2::affine_element Q = g2::element::random_element(); fr scalar = fr::random_element(); @@ -59,22 +59,20 @@ TEST(pairing, reduced_ate_pairing_consistency_check) EXPECT_EQ(result, expected); } -TEST(pairing, reduced_ate_pairing_consistency_check_batch) +TEST(pairing, ReducedAtePairingConsistencyCheckBatch) { size_t num_points = 10; - g1::affine_element P_a[num_points]; - g2::affine_element Q_a[num_points]; - - g1::affine_element P_b[num_points]; - g2::affine_element Q_b[num_points]; - - fr scalars[num_points + num_points]; + std::vector P_a(num_points); + std::vector Q_a(num_points); + std::vector P_b(num_points); + std::vector Q_b(num_points); + std::vector scalars(num_points + num_points); for (size_t i = 0; i < 10; ++i) { scalars[i] = fr::random_element(); scalars[i + num_points] = fr::random_element(); - g1::affine_element P = g1::affine_element(g1::element::random_element()); - g2::affine_element Q = g2::affine_element(g2::element::random_element()); + g1::affine_element P = g1::element::random_element(); + g2::affine_element Q = g2::element::random_element(); P_a[i] = P; Q_a[i] = Q; P_b[i] = P; @@ -94,20 +92,20 @@ TEST(pairing, reduced_ate_pairing_consistency_check_batch) EXPECT_EQ(result, expected); } -TEST(pairing, reduced_ate_pairing_precompute_consistency_check_batch) +TEST(pairing, ReducedAtePairingPrecomputeConsistencyCheckBatch) { size_t num_points = 10; - g1::affine_element P_a[num_points]; - g2::affine_element Q_a[num_points]; - g1::affine_element P_b[num_points]; - g2::affine_element Q_b[num_points]; - pairing::miller_lines precompute_miller_lines[num_points]; - fr scalars[num_points + num_points]; + std::vector P_a(num_points); + std::vector Q_a(num_points); + std::vector P_b(num_points); + std::vector Q_b(num_points); + std::vector precompute_miller_lines(num_points); + std::vector scalars(num_points + num_points); for (size_t i = 0; i < 10; ++i) { scalars[i] = fr::random_element(); scalars[i + num_points] = fr::random_element(); - g1::affine_element P = g1::affine_element(g1::element::random_element()); - g2::affine_element Q = g2::affine_element(g2::element::random_element()); + g1::affine_element P = g1::element::random_element(); + g2::affine_element Q = g2::element::random_element(); P_a[i] = P; Q_a[i] = Q; P_b[i] = P; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing_impl.hpp index f5712d1f4c7..e49174151b5 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pairing_impl.hpp @@ -3,10 +3,9 @@ #include "./fq12.hpp" #include "./g1.hpp" #include "./g2.hpp" +#include "barretenberg/ecc/curves/bn254/pairing.hpp" -namespace barretenberg { -namespace pairing { -namespace { +namespace barretenberg::pairing { constexpr fq two_inv = fq(2).invert(); inline constexpr g2::element mul_by_q(const g2::element& a) { @@ -19,7 +18,6 @@ inline constexpr g2::element mul_by_q(const g2::element& a) a.z.frobenius_map(), }; } -} // namespace constexpr void doubling_step_for_flipped_miller_loop(g2::element& current, fq12::ell_coeffs& ell) { fq2 a = current.x.mul_by_fq(two_inv); @@ -102,13 +100,13 @@ constexpr void precompute_miller_lines(const g2::element& Q, miller_lines& lines g2::element work_point = Q; size_t it = 0; - for (size_t i = 0; i < loop_length; ++i) { + for (unsigned char loop_bit : loop_bits) { doubling_step_for_flipped_miller_loop(work_point, lines.lines[it]); ++it; - if (loop_bits[i] == 1) { + if (loop_bit == 1) { mixed_addition_step_for_flipped_miller_loop(Q, work_point, lines.lines[it]); ++it; - } else if (loop_bits[i] == 3) { + } else if (loop_bit == 3) { mixed_addition_step_for_flipped_miller_loop(Q_neg, work_point, lines.lines[it]); ++it; } @@ -129,7 +127,7 @@ constexpr fq12 miller_loop(g1::element& P, miller_lines& lines) size_t it = 0; fq12::ell_coeffs work_line; - for (size_t i = 0; i < loop_length; ++i) { + for (unsigned char loop_bit : loop_bits) { work_scalar = work_scalar.sqr(); work_line.o = lines.lines[it].o; @@ -138,7 +136,7 @@ constexpr fq12 miller_loop(g1::element& P, miller_lines& lines) work_scalar.self_sparse_mul(work_line); ++it; - if (loop_bits[i] != 0) { + if (loop_bit != 0) { work_line.o = lines.lines[it].o; work_line.vw = lines.lines[it].vw.mul_by_fq(P.y); work_line.vv = lines.lines[it].vv.mul_by_fq(P.x); @@ -167,7 +165,7 @@ constexpr fq12 miller_loop_batch(const g1::element* points, const miller_lines* size_t it = 0; fq12::ell_coeffs work_line; - for (size_t i = 0; i < loop_length; ++i) { + for (unsigned char loop_bit : loop_bits) { work_scalar = work_scalar.sqr(); for (size_t j = 0; j < num_pairs; ++j) { work_line.o = lines[j].lines[it].o; @@ -176,7 +174,7 @@ constexpr fq12 miller_loop_batch(const g1::element* points, const miller_lines* work_scalar.self_sparse_mul(work_line); } ++it; - if (loop_bits[i] != 0) { + if (loop_bit != 0) { for (size_t j = 0; j < num_pairs; ++j) { work_line.o = lines[j].lines[it].o; work_line.vw = lines[j].lines[it].vw.mul_by_fq(points[j].y); @@ -213,13 +211,12 @@ constexpr fq12 final_exponentiation_easy_part(const fq12& elt) constexpr fq12 final_exponentiation_exp_by_neg_z(const fq12& elt) { - fq12 scalar{ elt }; fq12 r = elt; - for (size_t i = 0; i < neg_z_loop_length; ++i) { + for (bool neg_z_loop_bit : neg_z_loop_bits) { r = r.cyclotomic_squared(); - if (neg_z_loop_bits[i]) { - r *= scalar; + if (neg_z_loop_bit) { + r *= elt; } } return r.unitary_inverse(); @@ -270,14 +267,13 @@ fq12 reduced_ate_pairing_batch_precomputed(const g1::affine_element* P_affines, const miller_lines* lines, const size_t num_points) { - g1::element* P = new g1::element[num_points]; + std::vector P(num_points); for (size_t i = 0; i < num_points; ++i) { P[i] = g1::element(P_affines[i]); } fq12 result = miller_loop_batch(&P[0], &lines[0], num_points); result = final_exponentiation_easy_part(result); result = final_exponentiation_tricky_part(result); - delete[] P; return result; } @@ -285,9 +281,10 @@ fq12 reduced_ate_pairing_batch(const g1::affine_element* P_affines, const g2::affine_element* Q_affines, const size_t num_points) { - g1::element* P = new g1::element[num_points]; - g2::element* Q = new g2::element[num_points]; - miller_lines* lines = new miller_lines[num_points]; + std::vector P(num_points); + std::vector Q(num_points); + std::vector lines(num_points); + for (size_t i = 0; i < num_points; ++i) { P[i] = g1::element(P_affines[i]); Q[i] = g2::element(Q_affines[i]); @@ -298,11 +295,7 @@ fq12 reduced_ate_pairing_batch(const g1::affine_element* P_affines, fq12 result = miller_loop_batch(&P[0], &lines[0], num_points); result = final_exponentiation_easy_part(result); result = final_exponentiation_tricky_part(result); - delete[] P; - delete[] Q; - delete[] lines; return result; } -} // namespace pairing -} // namespace barretenberg +} // namespace barretenberg::pairing diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pseudorandom.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pseudorandom.hpp deleted file mode 100644 index e6940305b2d..00000000000 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/bn254/pseudorandom.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "fq.hpp" - -inline barretenberg::fq get_pseudorandom_fq() -{ - static std::seed_seq seq{ 1, 2, 3, 4, 5, 6, 7, 8 }; - static std::mt19937_64 engine = std::mt19937_64(seq); - static std::uniform_int_distribution dist{ 0ULL, UINT64_MAX }; - - barretenberg::fq out{ - (uint64_t)dist(engine), (uint64_t)dist(engine), (uint64_t)dist(engine), (uint64_t)dist(engine) - }; - out.self_reduce_once(); - out.self_reduce_once(); - out.self_reduce_once(); - out.self_reduce_once(); - return out; -} \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/c_bind.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/c_bind.cpp index 0fed15bdbc8..9520e87bff1 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/c_bind.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/c_bind.cpp @@ -1,7 +1,9 @@ // TODO: Delete this cbind once funcs working in root cbind of ecc module. -#include "grumpkin.hpp" #include "barretenberg/common/wasm_export.hpp" +#include "grumpkin.hpp" +// Silencing warnings about reserved identifiers. Fixing would break downstream code that calls our WASM API. +// NOLINTBEGIN(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) WASM_EXPORT void ecc_grumpkin__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result) { using serialize::write; @@ -40,10 +42,12 @@ WASM_EXPORT void ecc_grumpkin__get_random_scalar_mod_circuit_modulus(uint8_t* re WASM_EXPORT void ecc_grumpkin__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result) { - uint512_t bigint_input = from_buffer(input); + auto bigint_input = from_buffer(input); uint512_t barretenberg_modulus(barretenberg::fr::modulus); uint512_t target_output = bigint_input % barretenberg_modulus; write(result, target_output.lo); } + +// NOLINTEND(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.cpp index 17661c709e3..caa7f871fbc 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.cpp @@ -4,7 +4,9 @@ namespace grumpkin { namespace { constexpr size_t max_num_generators = 1 << 10; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static std::array generators; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static bool init_generators = false; } // namespace diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp index 9d654ec5695..0bad58a8d51 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.hpp @@ -8,8 +8,8 @@ namespace grumpkin { constexpr size_t MAX_NO_WRAP_INTEGER_BIT_LENGTH = 252; -typedef barretenberg::fr fq; -typedef barretenberg::fq fr; +using fq = barretenberg::fr; +using fr = barretenberg::fq; struct GrumpkinG1Params { static constexpr bool USE_ENDOMORPHISM = true; @@ -28,9 +28,9 @@ struct GrumpkinG1Params { 0x11b2dff1448c41d8UL, 0x23d3446f21c77dc3UL, 0xaa7b8cf435dfafbbUL, 0x14b34cf69dc25d68UL }; }; -typedef barretenberg::group g1; +using g1 = barretenberg::group; -g1::affine_element get_generator(const size_t generator_index); +g1::affine_element get_generator(size_t generator_index); }; // namespace grumpkin diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.test.cpp index 3719c21f8d2..5f75c2c2c1e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/grumpkin/grumpkin.test.cpp @@ -4,26 +4,26 @@ namespace test_grumpkin { -TEST(grumpkin, check_b) +TEST(grumpkin, CheckB) { auto b = grumpkin::g1::curve_b; barretenberg::fr seventeen = 17; EXPECT_EQ(seventeen, -b); } -TEST(grumpkin, random_element) +TEST(grumpkin, RandomElement) { grumpkin::g1::element result = grumpkin::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(grumpkin, random_affine_element) +TEST(grumpkin, RandomAffineElement) { - grumpkin::g1::affine_element result = grumpkin::g1::affine_element(grumpkin::g1::element::random_element()); + grumpkin::g1::affine_element result = grumpkin::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(grumpkin, eq) +TEST(grumpkin, Eq) { grumpkin::g1::element a = grumpkin::g1::element::random_element(); grumpkin::g1::element b = a.normalize(); @@ -43,7 +43,7 @@ TEST(grumpkin, eq) EXPECT_EQ(a == b, true); } -TEST(grumpkin, check_group_modulus) +TEST(grumpkin, CheckGroupModulus) { // grumpkin::g1::affine_element expected = grumpkin::g1::affine_one; grumpkin::fr exponent = -grumpkin::fr(1); @@ -54,90 +54,7 @@ TEST(grumpkin, check_group_modulus) EXPECT_EQ(result == grumpkin::g1::one, true); } -// TEST(grumpkin, mixed_add_check_against_constants) -// { -// fq a_x = {{0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5}}; -// fq a_y = {{0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad}}; -// fq a_z = {{0xfef593c9ce1df132, 0xe0486f801303c27d, 0x9bbd01ab881dc08e, 0x2a589badf38ec0f9}}; -// fq b_x = {{0xa1ec5d1398660db8, 0x6be3e1f6fd5d8ab1, 0x69173397dd272e11, 0x12575bbfe1198886}}; -// fq b_y = {{0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e}}; -// fq expected_x = {{0x2a9d0201fccca20, 0x36f969b294f31776, 0xee5534422a6f646, 0x911dbc6b02310b6}}; -// fq expected_y = {{0x14c30aaeb4f135ef, 0x9c27c128ea2017a1, 0xf9b7d80c8315eabf, 0x35e628df8add760}}; -// fq expected_z = {{0xa43fe96673d10eb3, 0x88fbe6351753d410, 0x45c21cc9d99cb7d, 0x3018020aa6e9ede5}}; -// grumpkin::g1::element lhs; -// grumpkin::g1::affine_element rhs; -// grumpkin::g1::element result; -// grumpkin::g1::element expected; -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(b_x, rhs.x); -// fq::__to_montgomery_form(b_y, rhs.y); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); -// result = lhs + rhs; - -// EXPECT_EQ(result == expected, true); -// } - -// TEST(grumpkin, dbl_check_against_constants) -// { -// fq a_x = {{0x8d1703aa518d827f, 0xd19cc40779f54f63, 0xabc11ce30d02728c, 0x10938940de3cbeec}}; -// fq a_y = {{0xcf1798994f1258b4, 0x36307a354ad90a25, 0xcd84adb348c63007, 0x6266b85241aff3f}}; -// fq a_z = {{0xe213e18fd2df7044, 0xb2f42355982c5bc8, 0xf65cf5150a3a9da1, 0xc43bde08b03aca2}}; -// fq expected_x = {{0xd5c6473044b2e67c, 0x89b185ea20951f3a, 0x4ac597219cf47467, 0x2d00482f63b12c86}}; -// fq expected_y = {{0x4e7e6c06a87e4314, 0x906a877a71735161, 0xaa7b9893cc370d39, 0x62f206bef795a05}}; -// fq expected_z = {{0x8813bdca7b0b115a, 0x929104dffdfabd22, 0x3fff575136879112, 0x18a299c1f683bdca}}; -// grumpkin::g1::element lhs; -// grumpkin::g1::element result; -// grumpkin::g1::element expected; -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); - -// result = lhs.dbl(); -// result.self_dbl(); -// result.self_dbl(); - -// EXPECT_EQ(result == expected, true); -// } - -// TEST(grumpkin, add_check_against_constants) -// { -// fq a_x = {{0x184b38afc6e2e09a, 0x4965cd1c3687f635, 0x334da8e7539e71c4, 0xf708d16cfe6e14}}; -// fq a_y = {{0x2a6ff6ffc739b3b6, 0x70761d618b513b9, 0xbf1645401de26ba1, 0x114a1616c164b980}}; -// fq a_z = {{0x10143ade26bbd57a, 0x98cf4e1f6c214053, 0x6bfdc534f6b00006, 0x1875e5068ababf2c}}; -// fq b_x = {{0xafdb8a15c98bf74c, 0xac54df622a8d991a, 0xc6e5ae1f3dad4ec8, 0x1bd3fb4a59e19b52}}; -// fq b_y = {{0x21b3bb529bec20c0, 0xaabd496406ffb8c1, 0xcd3526c26ac5bdcb, 0x187ada6b8693c184}}; -// fq b_z = {{0xffcd440a228ed652, 0x8a795c8f234145f1, 0xd5279cdbabb05b95, 0xbdf19ba16fc607a}}; -// fq expected_x = {{0x18764da36aa4cd81, 0xd15388d1fea9f3d3, 0xeb7c437de4bbd748, 0x2f09b712adf6f18f}}; -// fq expected_y = {{0x50c5f3cab191498c, 0xe50aa3ce802ea3b5, 0xd9d6125b82ebeff8, 0x27e91ba0686e54fe}}; -// fq expected_z = {{0xe4b81ef75fedf95, 0xf608edef14913c75, 0xfd9e178143224c96, 0xa8ae44990c8accd}}; -// grumpkin::g1::element lhs; -// grumpkin::g1::element rhs; -// grumpkin::g1::element result; -// grumpkin::g1::element expected; - -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(b_x, rhs.x); -// fq::__to_montgomery_form(b_y, rhs.y); -// fq::__to_montgomery_form(b_z, rhs.z); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); - -// result = lhs + rhs; - -// EXPECT_EQ(result == expected, true); -// } - -TEST(grumpkin, add_exception_test_infinity) +TEST(grumpkin, AddExceptionTestInfinity) { grumpkin::g1::element lhs = grumpkin::g1::element::random_element(); grumpkin::g1::element rhs; @@ -163,7 +80,7 @@ TEST(grumpkin, add_exception_test_infinity) EXPECT_EQ(rhs == result, true); } -TEST(grumpkin, add_exception_test_dbl) +TEST(grumpkin, AddExceptionTestDbl) { grumpkin::g1::element lhs = grumpkin::g1::element::random_element(); grumpkin::g1::element rhs; @@ -178,7 +95,7 @@ TEST(grumpkin, add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(grumpkin, add_dbl_consistency) +TEST(grumpkin, AddDblConsistency) { grumpkin::g1::element a = grumpkin::g1::element::random_element(); grumpkin::g1::element b = grumpkin::g1::element::random_element(); @@ -198,7 +115,7 @@ TEST(grumpkin, add_dbl_consistency) EXPECT_EQ(add_result == dbl_result, true); } -TEST(grumpkin, add_dbl_consistency_repeated) +TEST(grumpkin, AddDblConsistencyRepeated) { grumpkin::g1::element a = grumpkin::g1::element::random_element(); grumpkin::g1::element b; @@ -221,10 +138,10 @@ TEST(grumpkin, add_dbl_consistency_repeated) EXPECT_EQ(result == expected, true); } -TEST(grumpkin, mixed_add_exception_test_infinity) +TEST(grumpkin, MixedAddExceptionTestInfinity) { grumpkin::g1::element lhs = grumpkin::g1::one; - grumpkin::g1::affine_element rhs = grumpkin::g1::affine_element(grumpkin::g1::element::random_element()); + grumpkin::g1::affine_element rhs = grumpkin::g1::element::random_element(); grumpkin::fq::__copy(rhs.x, lhs.x); lhs.y = -rhs.y; @@ -241,9 +158,9 @@ TEST(grumpkin, mixed_add_exception_test_infinity) EXPECT_EQ(rhs_c == result, true); } -TEST(grumpkin, mixed_add_exception_test_dbl) +TEST(grumpkin, MixedAddExceptionTestDbl) { - grumpkin::g1::affine_element rhs = grumpkin::g1::affine_element(grumpkin::g1::element::random_element()); + grumpkin::g1::affine_element rhs = grumpkin::g1::element::random_element(); grumpkin::g1::element lhs; lhs = grumpkin::g1::element(rhs); @@ -256,9 +173,9 @@ TEST(grumpkin, mixed_add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(grumpkin, add_mixed_add_consistency_check) +TEST(grumpkin, AddMixedAddConsistencyCheck) { - grumpkin::g1::affine_element rhs = grumpkin::g1::affine_element(grumpkin::g1::element::random_element()); + grumpkin::g1::affine_element rhs = grumpkin::g1::element::random_element(); grumpkin::g1::element lhs = grumpkin::g1::element::random_element(); grumpkin::g1::element rhs_b; rhs_b = grumpkin::g1::element(rhs); @@ -271,28 +188,27 @@ TEST(grumpkin, add_mixed_add_consistency_check) EXPECT_EQ(add_result == mixed_add_result, true); } -TEST(grumpkin, on_curve) +TEST(grumpkin, OnCurve) { for (size_t i = 0; i < 100; ++i) { grumpkin::g1::element test = grumpkin::g1::element::random_element(); EXPECT_EQ(test.on_curve(), true); - grumpkin::g1::affine_element affine_test = - grumpkin::g1::affine_element(grumpkin::g1::element::random_element()); + grumpkin::g1::affine_element affine_test = grumpkin::g1::element::random_element(); EXPECT_EQ(affine_test.on_curve(), true); } } -TEST(grumpkin, batch_normalize) +TEST(grumpkin, BatchNormalize) { size_t num_points = 2; - grumpkin::g1::element points[num_points]; - grumpkin::g1::element normalized[num_points]; + std::vector points(num_points); + std::vector normalized(num_points); for (size_t i = 0; i < num_points; ++i) { grumpkin::g1::element a = grumpkin::g1::element::random_element(); grumpkin::g1::element b = grumpkin::g1::element::random_element(); points[i] = a + b; normalized[i] = points[i]; } - grumpkin::g1::element::batch_normalize(normalized, num_points); + grumpkin::g1::element::batch_normalize(&normalized[0], num_points); for (size_t i = 0; i < num_points; ++i) { grumpkin::fq zz; @@ -309,7 +225,7 @@ TEST(grumpkin, batch_normalize) } } -TEST(grumpkin, group_exponentiation_zero_and_one) +TEST(grumpkin, GroupExponentiationZeroAndOne) { grumpkin::g1::affine_element result = grumpkin::g1::one * grumpkin::fr::zero(); @@ -320,7 +236,7 @@ TEST(grumpkin, group_exponentiation_zero_and_one) EXPECT_EQ(result == grumpkin::g1::affine_one, true); } -TEST(grumpkin, group_exponentiation_consistency_check) +TEST(grumpkin, GroupExponentiationConsistencyCheck) { grumpkin::fr a = grumpkin::fr::random_element(); grumpkin::fr b = grumpkin::fr::random_element(); @@ -337,7 +253,7 @@ TEST(grumpkin, group_exponentiation_consistency_check) EXPECT_EQ(result == expected, true); } -TEST(grumpkin, derive_generators) +TEST(grumpkin, DeriveGenerators) { constexpr size_t num_generators = 128; auto result = grumpkin::g1::derive_generators(); @@ -357,7 +273,7 @@ TEST(grumpkin, derive_generators) } } -TEST(grumpkin, batch_mul) +TEST(grumpkin, BatchMul) { constexpr size_t num_points = 1024; @@ -376,6 +292,7 @@ TEST(grumpkin, batch_mul) std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); std::vector expected; + expected.reserve(num_points); for (const auto& point : points) { expected.emplace_back((point * exponent).normalize()); } @@ -398,7 +315,7 @@ TEST(grumpkin, batch_mul) } // Checks for "bad points" in terms of sharing a y-coordinate as explained here: // https://github.com/AztecProtocol/aztec2-internal/issues/437 -TEST(grumpkin, bad_points) +TEST(grumpkin, BadPoints) { auto beta = grumpkin::fr::cube_root_of_unity(); auto beta_sqr = beta * beta; @@ -406,8 +323,9 @@ TEST(grumpkin, bad_points) grumpkin::fr c(1); for (size_t i = 0; i < 256; i++) { auto val = c / (grumpkin::fr(1) + c); - if (val == beta || val == beta_sqr) + if (val == beta || val == beta_sqr) { res = false; + } c *= grumpkin::fr(4); } EXPECT_TRUE(res); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.cpp index 8c4e612d847..62eb7c4e953 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.cpp @@ -1,5 +1,7 @@ #include "secp256k1.hpp" +// Silencing warnings about reserved identifiers. Fixing would break downstream code that calls our WASM API. +// NOLINTBEGIN(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result) { using serialize::write; @@ -17,10 +19,11 @@ WASM_EXPORT void ecc_secp256k1__get_random_scalar_mod_circuit_modulus(uint8_t* r WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result) { - uint512_t bigint_input = from_buffer(input); + auto bigint_input = from_buffer(input); uint512_t barretenberg_modulus(secp256k1::fr::modulus); uint512_t target_output = bigint_input % barretenberg_modulus; write(result, target_output.lo); } +// NOLINTEND(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.hpp index 7b5024ab0e8..bd9a377bcb6 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/c_bind.hpp @@ -1,7 +1,10 @@ #include "secp256k1.hpp" +// Silencing warnings about reserved identifiers. Fixing would break downstream code that calls our WASM API. +// NOLINTBEGIN(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result); WASM_EXPORT void ecc_secp256k1__get_random_scalar_mod_circuit_modulus(uint8_t* result); WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result); +// NOLINTEND(cert-dcl37-c, cert-dcl51-cpp, bugprone-reserved-identifier) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.cpp index ed7bd89172f..b199208cec9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.cpp @@ -4,7 +4,9 @@ namespace secp256k1 { namespace { constexpr size_t max_num_generators = 1 << 10; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static std::array generators; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static bool init_generators = false; } // namespace diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.hpp index 9761389b33c..a2de49cd4c9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.hpp @@ -4,6 +4,7 @@ #include "../../groups/group.hpp" #include "../types.hpp" +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) namespace secp256k1 { struct Secp256k1FqParams { @@ -99,8 +100,8 @@ struct Secp256k1FrParams { static constexpr uint64_t primitive_root_3 = 0UL; }; -typedef barretenberg::field fq; -typedef barretenberg::field fr; +using fq = barretenberg::field; +using fr = barretenberg::field; struct Secp256k1G1Params { static constexpr bool USE_ENDOMORPHISM = false; @@ -117,10 +118,9 @@ struct Secp256k1G1Params { fq(0x9C47D08FFB10D4B8UL, 0xFD17B448A6855419UL, 0x5DA4FBFC0E1108A8UL, 0x483ADA7726A3C465UL).to_montgomery_form(); }; -typedef barretenberg:: - group, barretenberg::field, Secp256k1G1Params> - g1; -g1::affine_element get_generator(const size_t generator_index); +using g1 = barretenberg:: + group, barretenberg::field, Secp256k1G1Params>; +g1::affine_element get_generator(size_t generator_index); } // namespace secp256k1 namespace curve { @@ -132,4 +132,6 @@ class SECP256K1 { using Element = typename Group::element; using AffineElement = typename Group::affine_element; }; -} // namespace curve \ No newline at end of file +} // namespace curve + +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.test.cpp index f292e7157f0..03d11da3bb7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1.test.cpp @@ -22,7 +22,7 @@ uint256_t get_fq_element() return res; } -TEST(secp256k1, test_add) +TEST(secp256k1, TestAdd) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -43,7 +43,7 @@ TEST(secp256k1, test_add) } } -TEST(secp256k1, test_sub) +TEST(secp256k1, TestSub) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -64,7 +64,7 @@ TEST(secp256k1, test_sub) } } -TEST(secp256k1, test_to_montgomery_form) +TEST(secp256k1, TestToMontgomeryForm) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -82,7 +82,7 @@ TEST(secp256k1, test_to_montgomery_form) } } -TEST(secp256k1, test_from_montgomery_form) +TEST(secp256k1, TestFromMontgomeryForm) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -93,7 +93,7 @@ TEST(secp256k1, test_from_montgomery_form) } } -TEST(secp256k1, test_mul) +TEST(secp256k1, TestMul) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -104,8 +104,8 @@ TEST(secp256k1, test_mul) secp256k1::fq b(b_raw); secp256k1::fq c = (a * b); - uint1024_t a_1024 = uint1024_t(uint512_t(a_raw)); - uint1024_t b_1024 = uint1024_t(uint512_t(b_raw)); + uint1024_t a_1024((uint512_t(a_raw))); + uint1024_t b_1024((uint512_t(b_raw))); uint1024_t c_1024 = a_1024 * b_1024; uint1024_t cmod = c_1024 % uint1024_t(uint512_t(test_fq_mod)); uint256_t expected = cmod.lo.lo; @@ -114,7 +114,7 @@ TEST(secp256k1, test_mul) } } -TEST(secp256k1, test_sqr) +TEST(secp256k1, TestSqr) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -131,7 +131,7 @@ TEST(secp256k1, test_sqr) } } -TEST(secp256k1, test_arithmetic) +TEST(secp256k1, TestArithmetic) { secp256k1::fq a = secp256k1::fq::random_element(); secp256k1::fq b = secp256k1::fq::random_element(); @@ -141,25 +141,25 @@ TEST(secp256k1, test_arithmetic) EXPECT_EQ(c, d); } -TEST(secp256k1, generator_on_curve) +TEST(secp256k1, GeneratorOnCurve) { secp256k1::g1::element result = secp256k1::g1::one; EXPECT_EQ(result.on_curve(), true); } -TEST(secp256k1, random_element) +TEST(secp256k1, RandomElement) { secp256k1::g1::element result = secp256k1::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(secp256k1, random_affine_element) +TEST(secp256k1, RandomAffineElement) { - secp256k1::g1::affine_element result = secp256k1::g1::affine_element(secp256k1::g1::element::random_element()); + secp256k1::g1::affine_element result = secp256k1::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(secp256k1, eq) +TEST(secp256k1, Eq) { secp256k1::g1::element a = secp256k1::g1::element::random_element(); secp256k1::g1::element b = a.normalize(); @@ -179,7 +179,7 @@ TEST(secp256k1, eq) EXPECT_EQ(a == b, true); } -TEST(secp256k1, check_group_modulus) +TEST(secp256k1, CheckGroupModulus) { // secp256k1::g1::affine_element expected = secp256k1::g1::affine_one; secp256k1::fr exponent = -secp256k1::fr(1); @@ -190,90 +190,7 @@ TEST(secp256k1, check_group_modulus) EXPECT_EQ(result == secp256k1::g1::one, true); } -// TEST(secp256k1, mixed_add_check_against_constants) -// { -// fq a_x = {{0x92716caa6cac6d26, 0x1e6e234136736544, 0x1bb04588cde00af0, 0x9a2ac922d97e6f5}}; -// fq a_y = {{0x9e693aeb52d79d2d, 0xf0c1895a61e5e975, 0x18cd7f5310ced70f, 0xac67920a22939ad}}; -// fq a_z = {{0xfef593c9ce1df132, 0xe0486f801303c27d, 0x9bbd01ab881dc08e, 0x2a589badf38ec0f9}}; -// fq b_x = {{0xa1ec5d1398660db8, 0x6be3e1f6fd5d8ab1, 0x69173397dd272e11, 0x12575bbfe1198886}}; -// fq b_y = {{0xcfbfd4441138823e, 0xb5f817e28a1ef904, 0xefb7c5629dcc1c42, 0x1a9ed3d6f846230e}}; -// fq expected_x = {{0x2a9d0201fccca20, 0x36f969b294f31776, 0xee5534422a6f646, 0x911dbc6b02310b6}}; -// fq expected_y = {{0x14c30aaeb4f135ef, 0x9c27c128ea2017a1, 0xf9b7d80c8315eabf, 0x35e628df8add760}}; -// fq expected_z = {{0xa43fe96673d10eb3, 0x88fbe6351753d410, 0x45c21cc9d99cb7d, 0x3018020aa6e9ede5}}; -// secp256k1::g1::element lhs; -// secp256k1::g1::affine_element rhs; -// secp256k1::g1::element result; -// secp256k1::g1::element expected; -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(b_x, rhs.x); -// fq::__to_montgomery_form(b_y, rhs.y); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); -// result = lhs + rhs; - -// EXPECT_EQ(result == expected, true); -// } - -// TEST(secp256k1, dbl_check_against_constants) -// { -// fq a_x = {{0x8d1703aa518d827f, 0xd19cc40779f54f63, 0xabc11ce30d02728c, 0x10938940de3cbeec}}; -// fq a_y = {{0xcf1798994f1258b4, 0x36307a354ad90a25, 0xcd84adb348c63007, 0x6266b85241aff3f}}; -// fq a_z = {{0xe213e18fd2df7044, 0xb2f42355982c5bc8, 0xf65cf5150a3a9da1, 0xc43bde08b03aca2}}; -// fq expected_x = {{0xd5c6473044b2e67c, 0x89b185ea20951f3a, 0x4ac597219cf47467, 0x2d00482f63b12c86}}; -// fq expected_y = {{0x4e7e6c06a87e4314, 0x906a877a71735161, 0xaa7b9893cc370d39, 0x62f206bef795a05}}; -// fq expected_z = {{0x8813bdca7b0b115a, 0x929104dffdfabd22, 0x3fff575136879112, 0x18a299c1f683bdca}}; -// secp256k1::g1::element lhs; -// secp256k1::g1::element result; -// secp256k1::g1::element expected; -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); - -// result = lhs.dbl(); -// result.self_dbl(); -// result.self_dbl(); - -// EXPECT_EQ(result == expected, true); -// } - -// TEST(secp256k1, add_check_against_constants) -// { -// fq a_x = {{0x184b38afc6e2e09a, 0x4965cd1c3687f635, 0x334da8e7539e71c4, 0xf708d16cfe6e14}}; -// fq a_y = {{0x2a6ff6ffc739b3b6, 0x70761d618b513b9, 0xbf1645401de26ba1, 0x114a1616c164b980}}; -// fq a_z = {{0x10143ade26bbd57a, 0x98cf4e1f6c214053, 0x6bfdc534f6b00006, 0x1875e5068ababf2c}}; -// fq b_x = {{0xafdb8a15c98bf74c, 0xac54df622a8d991a, 0xc6e5ae1f3dad4ec8, 0x1bd3fb4a59e19b52}}; -// fq b_y = {{0x21b3bb529bec20c0, 0xaabd496406ffb8c1, 0xcd3526c26ac5bdcb, 0x187ada6b8693c184}}; -// fq b_z = {{0xffcd440a228ed652, 0x8a795c8f234145f1, 0xd5279cdbabb05b95, 0xbdf19ba16fc607a}}; -// fq expected_x = {{0x18764da36aa4cd81, 0xd15388d1fea9f3d3, 0xeb7c437de4bbd748, 0x2f09b712adf6f18f}}; -// fq expected_y = {{0x50c5f3cab191498c, 0xe50aa3ce802ea3b5, 0xd9d6125b82ebeff8, 0x27e91ba0686e54fe}}; -// fq expected_z = {{0xe4b81ef75fedf95, 0xf608edef14913c75, 0xfd9e178143224c96, 0xa8ae44990c8accd}}; -// secp256k1::g1::element lhs; -// secp256k1::g1::element rhs; -// secp256k1::g1::element result; -// secp256k1::g1::element expected; - -// fq::__to_montgomery_form(a_x, lhs.x); -// fq::__to_montgomery_form(a_y, lhs.y); -// fq::__to_montgomery_form(a_z, lhs.z); -// fq::__to_montgomery_form(b_x, rhs.x); -// fq::__to_montgomery_form(b_y, rhs.y); -// fq::__to_montgomery_form(b_z, rhs.z); -// fq::__to_montgomery_form(expected_x, expected.x); -// fq::__to_montgomery_form(expected_y, expected.y); -// fq::__to_montgomery_form(expected_z, expected.z); - -// result = lhs + rhs; - -// EXPECT_EQ(result == expected, true); -// } - -TEST(secp256k1, add_exception_test_infinity) +TEST(secp256k1, AddExceptionTestInfinity) { secp256k1::g1::element lhs = secp256k1::g1::element::random_element(); secp256k1::g1::element rhs; @@ -299,7 +216,7 @@ TEST(secp256k1, add_exception_test_infinity) EXPECT_EQ(rhs == result, true); } -TEST(secp256k1, add_exception_test_dbl) +TEST(secp256k1, AddExceptionTestDbl) { secp256k1::g1::element lhs = secp256k1::g1::element::random_element(); secp256k1::g1::element rhs; @@ -314,7 +231,7 @@ TEST(secp256k1, add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(secp256k1, add_dbl_consistency) +TEST(secp256k1, AddDblConsistency) { secp256k1::g1::element a = secp256k1::g1::element::random_element(); secp256k1::g1::element b = secp256k1::g1::element::random_element(); @@ -334,7 +251,7 @@ TEST(secp256k1, add_dbl_consistency) EXPECT_EQ(add_result == dbl_result, true); } -TEST(secp256k1, add_dbl_consistency_repeated) +TEST(secp256k1, AddDblConsistencyRepeated) { secp256k1::g1::element a = secp256k1::g1::element::random_element(); secp256k1::g1::element b; @@ -357,10 +274,10 @@ TEST(secp256k1, add_dbl_consistency_repeated) EXPECT_EQ(result == expected, true); } -TEST(secp256k1, mixed_add_exception_test_infinity) +TEST(secp256k1, MixedAddExceptionTestInfinity) { secp256k1::g1::element lhs = secp256k1::g1::one; - secp256k1::g1::affine_element rhs = secp256k1::g1::affine_element(secp256k1::g1::element::random_element()); + secp256k1::g1::affine_element rhs = secp256k1::g1::element::random_element(); secp256k1::fq::__copy(rhs.x, lhs.x); lhs.y = -rhs.y; @@ -377,9 +294,9 @@ TEST(secp256k1, mixed_add_exception_test_infinity) EXPECT_EQ(rhs_c == result, true); } -TEST(secp256k1, mixed_add_exception_test_dbl) +TEST(secp256k1, MixedAddExceptionTestDbl) { - secp256k1::g1::affine_element rhs = secp256k1::g1::affine_element(secp256k1::g1::element::random_element()); + secp256k1::g1::affine_element rhs = secp256k1::g1::element::random_element(); secp256k1::g1::element lhs; lhs = secp256k1::g1::element(rhs); @@ -392,9 +309,9 @@ TEST(secp256k1, mixed_add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(secp256k1, add_mixed_add_consistency_check) +TEST(secp256k1, AddMixedAddConsistencyCheck) { - secp256k1::g1::affine_element rhs = secp256k1::g1::affine_element(secp256k1::g1::element::random_element()); + secp256k1::g1::affine_element rhs = secp256k1::g1::element::random_element(); secp256k1::g1::element lhs = secp256k1::g1::element::random_element(); secp256k1::g1::element rhs_b; rhs_b = secp256k1::g1::element(rhs); @@ -407,28 +324,27 @@ TEST(secp256k1, add_mixed_add_consistency_check) EXPECT_EQ(add_result == mixed_add_result, true); } -TEST(secp256k1, on_curve) +TEST(secp256k1, OnCurve) { for (size_t i = 0; i < 100; ++i) { secp256k1::g1::element test = secp256k1::g1::element::random_element(); EXPECT_EQ(test.on_curve(), true); - secp256k1::g1::affine_element affine_test = - secp256k1::g1::affine_element(secp256k1::g1::element::random_element()); + secp256k1::g1::affine_element affine_test = secp256k1::g1::element::random_element(); EXPECT_EQ(affine_test.on_curve(), true); } } -TEST(secp256k1, batch_normalize) +TEST(secp256k1, BatchNormalize) { size_t num_points = 2; - secp256k1::g1::element points[num_points]; - secp256k1::g1::element normalized[num_points]; + std::vector points(num_points); + std::vector normalized(num_points); for (size_t i = 0; i < num_points; ++i) { secp256k1::g1::element a = secp256k1::g1::element::random_element(); secp256k1::g1::element b = secp256k1::g1::element::random_element(); points[i] = a + b; normalized[i] = points[i]; } - secp256k1::g1::element::batch_normalize(normalized, num_points); + secp256k1::g1::element::batch_normalize(&normalized[0], num_points); for (size_t i = 0; i < num_points; ++i) { secp256k1::fq zz; @@ -445,7 +361,7 @@ TEST(secp256k1, batch_normalize) } } -TEST(secp256k1, group_exponentiation_zero_and_one) +TEST(secp256k1, GroupExponentiationZeroAndOne) { secp256k1::g1::affine_element result = secp256k1::g1::one * secp256k1::fr::zero(); @@ -456,7 +372,7 @@ TEST(secp256k1, group_exponentiation_zero_and_one) EXPECT_EQ(result == secp256k1::g1::affine_one, true); } -TEST(secp256k1, group_exponentiation_consistency_check) +TEST(secp256k1, GroupExponentiationConsistencyCheck) { secp256k1::fr a = secp256k1::fr::random_element(); secp256k1::fr b = secp256k1::fr::random_element(); @@ -473,7 +389,7 @@ TEST(secp256k1, group_exponentiation_consistency_check) EXPECT_EQ(result == expected, true); } -TEST(secp256k1, derive_generators) +TEST(secp256k1, DeriveGenerators) { constexpr size_t num_generators = 128; auto result = secp256k1::g1::derive_generators(); @@ -493,7 +409,7 @@ TEST(secp256k1, derive_generators) } } -TEST(secp256k1, get_endomorphism_scalars) +TEST(secp256k1, GetEndomorphismScalars) { for (size_t i = 0; i < 2048; i++) { secp256k1::fr k = secp256k1::fr::random_element(); @@ -530,7 +446,7 @@ TEST(secp256k1, get_endomorphism_scalars) } } -TEST(secp256k1, test_endomorphism_scalars) +TEST(secp256k1, TestEndomorphismScalars) { secp256k1::fr k = secp256k1::fr::random_element(); secp256k1::fr k1 = 0; @@ -570,7 +486,7 @@ TEST(secp256k1, test_endomorphism_scalars) EXPECT_EQ(k, expected); } -TEST(secp256k1, neg_and_self_neg_0_cmp_regression) +TEST(secp256k1, NegAndSelfNeg0CmpRegression) { secp256k1::fq a = 0; secp256k1::fq a_neg = -a; @@ -581,7 +497,7 @@ TEST(secp256k1, neg_and_self_neg_0_cmp_regression) EXPECT_EQ((a == a_neg), true); } -TEST(secp256k1, montgomery_mul_big_bug) +TEST(secp256k1, MontgomeryMulBigBug) { secp256k1::fq a(uint256_t{ 0xfffffffe630dc02f, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff }); secp256k1::fq a_sqr = a.sqr(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1_endo_notes.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1_endo_notes.hpp index 0e2dcb20567..74bc2adf4d9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1_endo_notes.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256k1/secp256k1_endo_notes.hpp @@ -26,11 +26,11 @@ struct basis_vectors { bool real = false; }; -static basis_vectors get_endomorphism_basis_vectors(const secp256k1::fr& lambda) +[[maybe_unused]] static basis_vectors get_endomorphism_basis_vectors(const secp256k1::fr& lambda) { uint512_t approximate_square_root; uint512_t z = (uint512_t(secp256k1::fr::modulus) + uint512_t(2)) >> 1; - uint512_t y = uint512_t(secp256k1::fr::modulus); + auto y = uint512_t(secp256k1::fr::modulus); while (z < y) { y = z; z = (uint512_t(secp256k1::fr::modulus) / z + z) >> 1; @@ -128,7 +128,7 @@ static basis_vectors get_endomorphism_basis_vectors(const secp256k1::fr& lambda) return result; } -static std::pair get_endomorphism_scalars() +[[maybe_unused]] static std::pair get_endomorphism_scalars() { // find beta \in secp256k1::fq and lambda \in secp256k1::fr such that: diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.cpp index 676ec6c3db1..061bbd2c2fd 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.cpp @@ -4,7 +4,9 @@ namespace secp256r1 { namespace { constexpr size_t max_num_generators = 1 << 10; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static std::array generators; +// NOLINTNEXTLINE TODO(@zac-williamson) #1806 get rid of need for these static variables in Pedersen refactor! static bool init_generators = false; } // namespace diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.hpp index ef6b431ab48..e7bf6422c95 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.hpp @@ -4,7 +4,7 @@ #include "../../groups/group.hpp" namespace secp256r1 { - +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) struct Secp256r1FqParams { static constexpr uint64_t modulus_0 = 0xFFFFFFFFFFFFFFFFULL; static constexpr uint64_t modulus_1 = 0x00000000FFFFFFFFULL; @@ -84,8 +84,8 @@ struct Secp256r1FrParams { static constexpr uint64_t primitive_root_3 = 0UL; }; -typedef barretenberg::field fq; -typedef barretenberg::field fr; +using fq = barretenberg::field; +using fr = barretenberg::field; struct Secp256r1G1Params { static constexpr bool USE_ENDOMORPHISM = false; @@ -104,10 +104,9 @@ struct Secp256r1G1Params { fq(0xCBB6406837BF51F5, 0x2BCE33576B315ECE, 0x8EE7EB4A7C0F9E16, 0x4FE342E2FE1A7F9B).to_montgomery_form(); }; -typedef barretenberg:: - group, barretenberg::field, Secp256r1G1Params> - g1; -g1::affine_element get_generator(const size_t generator_index); +using g1 = barretenberg:: + group, barretenberg::field, Secp256r1G1Params>; +g1::affine_element get_generator(size_t generator_index); } // namespace secp256r1 namespace curve { @@ -119,4 +118,6 @@ class SECP256R1 { using Element = typename Group::element; using AffineElement = typename Group::affine_element; }; -} // namespace curve \ No newline at end of file +} // namespace curve + +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.test.cpp index 7b94a713251..4f945343c7b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/curves/secp256r1/secp256r1.test.cpp @@ -22,7 +22,7 @@ uint256_t get_fq_element() return res; } -TEST(secp256r1, test_add) +TEST(secp256r1, TestAdd) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -43,7 +43,7 @@ TEST(secp256r1, test_add) } } -TEST(secp256r1, test_sub) +TEST(secp256r1, TestSub) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -64,7 +64,7 @@ TEST(secp256r1, test_sub) } } -TEST(secp256r1, test_to_montgomery_form) +TEST(secp256r1, TestToMontgomeryForm) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -82,7 +82,7 @@ TEST(secp256r1, test_to_montgomery_form) } } -TEST(secp256r1, test_from_montgomery_form) +TEST(secp256r1, TestFromMontgomeryForm) { const size_t n = 100; for (size_t i = 0; i < n; ++i) { @@ -93,7 +93,7 @@ TEST(secp256r1, test_from_montgomery_form) } } -TEST(secp256r1, test_mul) +TEST(secp256r1, TestMul) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -104,8 +104,8 @@ TEST(secp256r1, test_mul) secp256r1::fq b(b_raw); secp256r1::fq c = (a * b); - uint1024_t a_1024 = uint1024_t(uint512_t(a_raw)); - uint1024_t b_1024 = uint1024_t(uint512_t(b_raw)); + uint1024_t a_1024((uint512_t(a_raw))); + uint1024_t b_1024((uint512_t(b_raw))); uint1024_t c_1024 = a_1024 * b_1024; uint1024_t cmod = c_1024 % uint1024_t(uint512_t(test_fq_mod)); uint256_t expected = cmod.lo.lo; @@ -114,7 +114,7 @@ TEST(secp256r1, test_mul) } } -TEST(secp256r1, test_sqr) +TEST(secp256r1, TestSqr) { const size_t n = 10; for (size_t i = 0; i < n; ++i) { @@ -131,7 +131,7 @@ TEST(secp256r1, test_sqr) } } -TEST(secp256r1, test_arithmetic) +TEST(secp256r1, TestArithmetic) { secp256r1::fq a = secp256r1::fq::random_element(); secp256r1::fq b = secp256r1::fq::random_element(); @@ -141,25 +141,25 @@ TEST(secp256r1, test_arithmetic) EXPECT_EQ(c, d); } -TEST(secp256r1, generator_on_curve) +TEST(secp256r1, GeneratorOnCurve) { secp256r1::g1::element result = secp256r1::g1::one; EXPECT_EQ(result.on_curve(), true); } -TEST(secp256r1, random_element) +TEST(secp256r1, RandomElement) { secp256r1::g1::element result = secp256r1::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(secp256r1, random_affine_element) +TEST(secp256r1, RandomAffineElement) { - secp256r1::g1::affine_element result = secp256r1::g1::affine_element(secp256r1::g1::element::random_element()); + secp256r1::g1::affine_element result = secp256r1::g1::element::random_element(); EXPECT_EQ(result.on_curve(), true); } -TEST(secp256r1, eq) +TEST(secp256r1, Eq) { secp256r1::g1::element a = secp256r1::g1::element::random_element(); secp256r1::g1::element b = a.normalize(); @@ -179,7 +179,7 @@ TEST(secp256r1, eq) EXPECT_EQ(a == b, true); } -TEST(secp256r1, check_group_modulus) +TEST(secp256r1, CheckGroupModulus) { // secp256r1::g1::affine_element expected = secp256r1::g1::affine_one; secp256r1::fr exponent = -secp256r1::fr(1); @@ -190,7 +190,7 @@ TEST(secp256r1, check_group_modulus) EXPECT_EQ(result == secp256r1::g1::one, true); } -TEST(secp256r1, add_exception_test_infinity) +TEST(secp256r1, AddExceptionTestInfinity) { secp256r1::g1::element lhs = secp256r1::g1::element::random_element(); secp256r1::g1::element rhs; @@ -216,7 +216,7 @@ TEST(secp256r1, add_exception_test_infinity) EXPECT_EQ(rhs == result, true); } -TEST(secp256r1, add_exception_test_dbl) +TEST(secp256r1, AddExceptionTestDbl) { secp256r1::g1::element lhs = secp256r1::g1::element::random_element(); secp256r1::g1::element rhs; @@ -231,7 +231,7 @@ TEST(secp256r1, add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(secp256r1, add_dbl_consistency) +TEST(secp256r1, AddDblConsistency) { secp256r1::g1::element a = secp256r1::g1::one; // P secp256r1::g1::element b = a.dbl(); // 2P @@ -245,25 +245,9 @@ TEST(secp256r1, add_dbl_consistency) d = d + a; // 7P d = d + a; // 8P EXPECT_EQ(c, d); - // secp256r1::g1::element a = secp256r1::g1::element::random_element(); - // secp256r1::g1::element b = secp256r1::g1::element::random_element(); - - // secp256r1::g1::element c; - // secp256r1::g1::element d; - // secp256r1::g1::element add_result; - // secp256r1::g1::element dbl_result; - - // c = a + b; - // b = -b; - // d = a + b; - - // add_result = c + d; - // dbl_result = a.dbl(); - - // EXPECT_EQ(add_result == dbl_result, true); } -TEST(secp256r1, add_dbl_consistency_repeated) +TEST(secp256r1, AddDblConsistencyRepeated) { secp256r1::g1::element a = secp256r1::g1::element::random_element(); secp256r1::g1::element b; @@ -286,10 +270,10 @@ TEST(secp256r1, add_dbl_consistency_repeated) EXPECT_EQ(result == expected, true); } -TEST(secp256r1, mixed_add_exception_test_infinity) +TEST(secp256r1, MixedAddExceptionTestInfinity) { secp256r1::g1::element lhs = secp256r1::g1::one; - secp256r1::g1::affine_element rhs = secp256r1::g1::affine_element(secp256r1::g1::element::random_element()); + secp256r1::g1::affine_element rhs = secp256r1::g1::element::random_element(); secp256r1::fq::__copy(rhs.x, lhs.x); lhs.y = -rhs.y; @@ -306,9 +290,9 @@ TEST(secp256r1, mixed_add_exception_test_infinity) EXPECT_EQ(rhs_c == result, true); } -TEST(secp256r1, mixed_add_exception_test_dbl) +TEST(secp256r1, MixedAddExceptionTestDbl) { - secp256r1::g1::affine_element rhs = secp256r1::g1::affine_element(secp256r1::g1::element::random_element()); + secp256r1::g1::affine_element rhs = secp256r1::g1::element::random_element(); secp256r1::g1::element lhs; lhs = secp256r1::g1::element(rhs); @@ -321,9 +305,9 @@ TEST(secp256r1, mixed_add_exception_test_dbl) EXPECT_EQ(result == expected, true); } -TEST(secp256r1, add_mixed_add_consistency_check) +TEST(secp256r1, AddMixedAddConsistencyCheck) { - secp256r1::g1::affine_element rhs = secp256r1::g1::affine_element(secp256r1::g1::element::random_element()); + secp256r1::g1::affine_element rhs = secp256r1::g1::element::random_element(); secp256r1::g1::element lhs = secp256r1::g1::element::random_element(); secp256r1::g1::element rhs_b; rhs_b = secp256r1::g1::element(rhs); @@ -336,28 +320,27 @@ TEST(secp256r1, add_mixed_add_consistency_check) EXPECT_EQ(add_result == mixed_add_result, true); } -TEST(secp256r1, on_curve) +TEST(secp256r1, OnCurve) { for (size_t i = 0; i < 100; ++i) { secp256r1::g1::element test = secp256r1::g1::element::random_element(); EXPECT_EQ(test.on_curve(), true); - secp256r1::g1::affine_element affine_test = - secp256r1::g1::affine_element(secp256r1::g1::element::random_element()); + secp256r1::g1::affine_element affine_test = secp256r1::g1::element::random_element(); EXPECT_EQ(affine_test.on_curve(), true); } } -TEST(secp256r1, batch_normalize) +TEST(secp256r1, BatchNormalize) { size_t num_points = 2; - secp256r1::g1::element points[num_points]; - secp256r1::g1::element normalized[num_points]; + std::vector points(num_points); + std::vector normalized(num_points); for (size_t i = 0; i < num_points; ++i) { secp256r1::g1::element a = secp256r1::g1::element::random_element(); secp256r1::g1::element b = secp256r1::g1::element::random_element(); points[i] = a + b; normalized[i] = points[i]; } - secp256r1::g1::element::batch_normalize(normalized, num_points); + secp256r1::g1::element::batch_normalize(&normalized[0], num_points); for (size_t i = 0; i < num_points; ++i) { secp256r1::fq zz; @@ -374,7 +357,7 @@ TEST(secp256r1, batch_normalize) } } -TEST(secp256r1, group_exponentiation_zero_and_one) +TEST(secp256r1, GroupExponentiationZeroAndOne) { secp256r1::g1::affine_element result = secp256r1::g1::one * secp256r1::fr::zero(); @@ -389,7 +372,7 @@ TEST(secp256r1, group_exponentiation_zero_and_one) EXPECT_EQ(result == secp256r1::g1::affine_one, true); } -TEST(secp256r1, group_exponentiation_consistency_check) +TEST(secp256r1, GroupExponentiationConsistencyCheck) { secp256r1::fr a = secp256r1::fr::random_element(); secp256r1::fr b = secp256r1::fr::random_element(); @@ -405,29 +388,13 @@ TEST(secp256r1, group_exponentiation_consistency_check) EXPECT_EQ(result == expected, true); } -// TODO: Remove in 2023 -// This test ensures that we haven't regressed to using a buggy implementation of method get_msb (now deleted) from -// field class instead of uint256_t's get_msb in element class's mul_without_endomorphism method. -TEST(secp256r1, msb_bug_regression_check) -{ - uint256_t start = (uint256_t(1) << 64); - uint64_t test_vector_x[4] = { 0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de, 0xfa822bc2811aaa5 }; - uint64_t test_vector_y[4] = { 0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b, 0xbff44ae8f5dba80d }; - secp256r1::g1::affine_element expected_result = secp256r1::g1::affine_element( - secp256r1::fq(uint256_t(test_vector_x[0], test_vector_x[1], test_vector_x[2], test_vector_x[3])), - secp256r1::fq(uint256_t(test_vector_y[0], test_vector_y[1], test_vector_y[2], test_vector_y[3]))); - secp256r1::fr a = secp256r1::fr(start); - secp256r1::g1::affine_element input = secp256r1::g1::affine_one; - secp256r1::g1::affine_element result = input * a; - EXPECT_EQ(result, expected_result); -} /** * @brief We had an issue where we added field elements and subtracted a prime depending on the 2²⁵⁶ overflow. This * was incorrect. Sometimes we need to subtract the prime twice. The same is true for subtractions * */ -TEST(secp256r1, addition_subtraction_regression_check) +TEST(secp256r1, AdditionSubtractionRegressionCheck) { secp256r1::fq fq1(uint256_t{ 0xfffffe0000000200, 0x200fffff9ff, 0xfffffbfffffffe00, 0xfffffbff00000400 }); secp256r1::fq fq2(uint256_t{ 0xfffffe0000000200, 0x200fffff9ff, 0xfffffbfffffffe00, 0xfffffbff00000400 }); @@ -468,7 +435,7 @@ TEST(secp256r1, check_compression_constructor) std::cout << "Affine element: " << el << std::endl; }**/ -TEST(secp256r1, montgomery_mul_big_bug) +TEST(secp256r1, MontgomeryMulBigBug) { secp256r1::fr a; a.data[0] = 0xC5BF4F6AFF993D09; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field.hpp index 914793614e3..ce2de349462 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field.hpp @@ -1,574 +1,10 @@ #pragma once -#include "barretenberg/common/assert.hpp" -#include "barretenberg/common/inline.hpp" -#include "barretenberg/common/serialize.hpp" -#include "barretenberg/numeric/random/engine.hpp" -#include "barretenberg/numeric/uint128/uint128.hpp" -#include "barretenberg/numeric/uint256/uint256.hpp" -#include -#include -#include -#include -#include -#ifndef DISABLE_SHENANIGANS -#ifdef __BMI2__ -#define BBERG_NO_ASM 0 -#else -#define BBERG_NO_ASM 1 -#endif -#else -#define BBERG_NO_ASM 1 -#endif - -namespace barretenberg { -template struct alignas(32) field { - public: - using Params = Params_; - typedef uint8_t const* in_buf; - typedef uint8_t const* vec_in_buf; - typedef uint8_t* out_buf; - typedef uint8_t** vec_out_buf; - - // We don't initialize data in the default constructor since we'd lose a lot of time on huge array initializations. - // Other alternatives have been noted, such as casting to get around constructors where they matter, - // however it is felt that sanitizer tools (e.g. MSAN) can detect garbage well, whereas doing - // hacky casts where needed would require rework to critical algos like MSM, FFT, Sumcheck. - // Instead, the recommended solution is use an explicit {} where initialization is important: - // field f; // not initialized - // field f{}; // zero-initialized - // std::array arr; // not initialized, good for huge N - // std::array arr {}; // zero-initialized, preferable for moderate N - field() = default; - - constexpr field(const uint256_t& input) noexcept - : data{ input.data[0], input.data[1], input.data[2], input.data[3] } - { - self_to_montgomery_form(); - } - - constexpr field(const unsigned long input) noexcept - : data{ input, 0, 0, 0 } - { - self_to_montgomery_form(); - } - - constexpr field(const unsigned int input) noexcept - : data{ input, 0, 0, 0 } - { - self_to_montgomery_form(); - } - - constexpr field(const unsigned long long input) noexcept - : data{ input, 0, 0, 0 } - { - self_to_montgomery_form(); - } - - constexpr field(const int input) noexcept - : data{ 0, 0, 0, 0 } - { - if (input < 0) { - data[0] = static_cast(-input); - data[1] = 0; - data[2] = 0; - data[3] = 0; - self_to_montgomery_form(); - self_neg(); - self_reduce_once(); - } else { - data[0] = static_cast(input); - data[1] = 0; - data[2] = 0; - data[3] = 0; - self_to_montgomery_form(); - } - } - - constexpr field(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept - : data{ a, b, c, d } {}; - - constexpr explicit operator uint32_t() const - { - field out = from_montgomery_form(); - return static_cast(out.data[0]); - } - - constexpr explicit operator uint64_t() const - { - field out = from_montgomery_form(); - return out.data[0]; - } - - constexpr explicit operator uint128_t() const - { - field out = from_montgomery_form(); - uint128_t lo = out.data[0]; - uint128_t hi = out.data[1]; - return (hi << 64) | lo; - } - - constexpr operator uint256_t() const noexcept - { - field out = from_montgomery_form(); - return uint256_t(out.data[0], out.data[1], out.data[2], out.data[3]); - } - - constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept - { - return uint256_t(data[0], data[1], data[2], data[3]); - } - - constexpr field(const field& other) = default; - constexpr field& operator=(const field& other) = default; - - alignas(32) uint64_t data[4]; - - static constexpr uint256_t modulus = - uint256_t{ Params::modulus_0, Params::modulus_1, Params::modulus_2, Params::modulus_3 }; - - static constexpr field cube_root_of_unity() - { - // endomorphism i.e. lambda * [P] = (beta * x, y) - if constexpr (Params::cube_root_0 != 0) { - constexpr field result{ - Params::cube_root_0, Params::cube_root_1, Params::cube_root_2, Params::cube_root_3 - }; - return result; - } else { - constexpr field two_inv = field(2).invert(); - constexpr field numerator = (-field(3)).sqrt() - field(1); - constexpr field result = two_inv * numerator; - return result; - } - } - - static constexpr field zero() { return field(0, 0, 0, 0); } - static constexpr field neg_one() { return -field(1); } - static constexpr field one() { return field(1); } - - static constexpr field external_coset_generator() - { - const field result{ - Params::coset_generators_0[7], - Params::coset_generators_1[7], - Params::coset_generators_2[7], - Params::coset_generators_3[7], - }; - return result; - } - - static constexpr field tag_coset_generator() - { - const field result{ - Params::coset_generators_0[6], - Params::coset_generators_1[6], - Params::coset_generators_2[6], - Params::coset_generators_3[6], - }; - return result; - } - - static constexpr field coset_generator(const size_t idx) - { - ASSERT(idx < 7); - const field result{ - Params::coset_generators_0[idx], - Params::coset_generators_1[idx], - Params::coset_generators_2[idx], - Params::coset_generators_3[idx], - }; - return result; - } - - BBERG_INLINE constexpr field operator*(const field& other) const noexcept; - BBERG_INLINE constexpr field operator+(const field& other) const noexcept; - BBERG_INLINE constexpr field operator-(const field& other) const noexcept; - BBERG_INLINE constexpr field operator-() const noexcept; - constexpr field operator/(const field& other) const noexcept; - - // prefix increment (++x) - BBERG_INLINE constexpr field operator++() noexcept; - // postfix increment (x++) - BBERG_INLINE constexpr field operator++(int) noexcept; - - BBERG_INLINE constexpr field operator*=(const field& other) noexcept; - BBERG_INLINE constexpr field operator+=(const field& other) noexcept; - BBERG_INLINE constexpr field operator-=(const field& other) noexcept; - constexpr field operator/=(const field& other) noexcept; - - BBERG_INLINE constexpr bool operator>(const field& other) const noexcept; - BBERG_INLINE constexpr bool operator<(const field& other) const noexcept; - BBERG_INLINE constexpr bool operator==(const field& other) const noexcept; - BBERG_INLINE constexpr bool operator!=(const field& other) const noexcept; - - BBERG_INLINE constexpr field to_montgomery_form() const noexcept; - BBERG_INLINE constexpr field from_montgomery_form() const noexcept; - - BBERG_INLINE constexpr field sqr() const noexcept; - BBERG_INLINE constexpr void self_sqr() noexcept; - - BBERG_INLINE constexpr field pow(const uint256_t& exponent) const noexcept; - BBERG_INLINE constexpr field pow(const uint64_t exponent) const noexcept; - static constexpr uint256_t modulus_minus_two = - uint256_t(Params::modulus_0 - 2ULL, Params::modulus_1, Params::modulus_2, Params::modulus_3); - constexpr field invert() const noexcept; - static void batch_invert(std::span coeffs) noexcept; - static void batch_invert(field* coeffs, const size_t n) noexcept; - /** - * @brief Compute square root of the field element. - * - * @return if the element is a quadratic remainder, if it's not - */ - constexpr std::pair sqrt() const noexcept; - - BBERG_INLINE constexpr void self_neg() noexcept; - - BBERG_INLINE constexpr void self_to_montgomery_form() noexcept; - BBERG_INLINE constexpr void self_from_montgomery_form() noexcept; - - BBERG_INLINE constexpr void self_conditional_negate(const uint64_t predicate) noexcept; - - BBERG_INLINE constexpr field reduce_once() const noexcept; - BBERG_INLINE constexpr void self_reduce_once() noexcept; - - BBERG_INLINE constexpr void self_set_msb() noexcept; - BBERG_INLINE constexpr bool is_msb_set() const noexcept; - BBERG_INLINE constexpr uint64_t is_msb_set_word() const noexcept; - - BBERG_INLINE constexpr bool is_zero() const noexcept; - - static constexpr field get_root_of_unity(const size_t degree) noexcept; - - static void serialize_to_buffer(const field& value, uint8_t* buffer) { write(buffer, value); } - - static field serialize_from_buffer(const uint8_t* buffer) { return from_buffer(buffer); } - - inline std::vector to_buffer() const { return ::to_buffer(*this); } - - struct wide_array { - uint64_t data[8]; - }; - BBERG_INLINE constexpr wide_array mul_512(const field& other) const noexcept; - BBERG_INLINE constexpr wide_array sqr_512() const noexcept; - - BBERG_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept - { - if (predicate) { - constexpr field p{ - twice_modulus.data[0], twice_modulus.data[1], twice_modulus.data[2], twice_modulus.data[3] - }; - return p - *this; - } else { - return *this; - } - } - - /** - * For short Weierstrass curves y^2 = x^3 + b mod r, if there exists a cube root of unity mod r, - * we can take advantage of an enodmorphism to decompose a 254 bit scalar into 2 128 bit scalars. - * \beta = cube root of 1, mod q (q = order of fq) - * \lambda = cube root of 1, mod r (r = order of fr) - * - * For a point P1 = (X, Y), where Y^2 = X^3 + b, we know that - * the point P2 = (X * \beta, Y) is also a point on the curve - * We can represent P2 as a scalar multiplication of P1, where P2 = \lambda * P1 - * - * For a generic multiplication of P1 by a 254 bit scalar k, we can decompose k - * into 2 127 bit scalars (k1, k2), such that k = k1 - (k2 * \lambda) - * - * We can now represent (k * P1) as (k1 * P1) - (k2 * P2), where P2 = (X * \beta, Y). - * As k1, k2 have half the bit length of k, we have reduced the number of loop iterations of our - * scalar multiplication algorithm in half - * - * To find k1, k2, We use the extended euclidean algorithm to find 4 short scalars [a1, a2], [b1, b2] such that - * modulus = (a1 * b2) - (b1 * a2) - * We then compute scalars c1 = round(b2 * k / r), c2 = round(b1 * k / r), where - * k1 = (c1 * a1) + (c2 * a2), k2 = -((c1 * b1) + (c2 * b2)) - * We pre-compute scalars g1 = (2^256 * b1) / n, g2 = (2^256 * b2) / n, to avoid having to perform long division - * on 512-bit scalars - **/ - static void split_into_endomorphism_scalars(const field& k, field& k1, field& k2) - { - // if the modulus is a 256-bit integer, we need to use a basis where g1, g2 have been shifted by 2^384 - if constexpr (Params::modulus_3 >= 0x4000000000000000ULL) { - split_into_endomorphism_scalars_384(k, k1, k2); - return; - } - field input = k.reduce_once(); - // uint64_t lambda_reduction[4] = { 0 }; - // __to_montgomery_form(lambda, lambda_reduction); - - constexpr field endo_g1 = { Params::endo_g1_lo, Params::endo_g1_mid, Params::endo_g1_hi, 0 }; - - constexpr field endo_g2 = { Params::endo_g2_lo, Params::endo_g2_mid, 0, 0 }; - - constexpr field endo_minus_b1 = { Params::endo_minus_b1_lo, Params::endo_minus_b1_mid, 0, 0 }; - - constexpr field endo_b2 = { Params::endo_b2_lo, Params::endo_b2_mid, 0, 0 }; - - // compute c1 = (g2 * k) >> 256 - wide_array c1 = endo_g2.mul_512(input); - // compute c2 = (g1 * k) >> 256 - wide_array c2 = endo_g1.mul_512(input); - - // (the bit shifts are implicit, as we only utilize the high limbs of c1, c2 - - field c1_hi = { - c1.data[4], c1.data[5], c1.data[6], c1.data[7] - }; // *(field*)((uintptr_t)(&c1) + (4 * sizeof(uint64_t))); - field c2_hi = { - c2.data[4], c2.data[5], c2.data[6], c2.data[7] - }; // *(field*)((uintptr_t)(&c2) + (4 * sizeof(uint64_t))); - - // compute q1 = c1 * -b1 - wide_array q1 = c1_hi.mul_512(endo_minus_b1); - // compute q2 = c2 * b2 - wide_array q2 = c2_hi.mul_512(endo_b2); - - // FIX: Avoid using 512-bit multiplication as its not necessary. - // c1_hi, c2_hi can be uint256_t's and the final result (without montgomery reduction) - // could be casted to a field. - field q1_lo{ q1.data[0], q1.data[1], q1.data[2], q1.data[3] }; - field q2_lo{ q2.data[0], q2.data[1], q2.data[2], q2.data[3] }; - - field t1 = (q2_lo - q1_lo).reduce_once(); - field beta = cube_root_of_unity(); - field t2 = (t1 * beta + input).reduce_once(); - k2.data[0] = t1.data[0]; - k2.data[1] = t1.data[1]; - k1.data[0] = t2.data[0]; - k1.data[1] = t2.data[1]; - } - - static void split_into_endomorphism_scalars_384(const field& input, field& k1_out, field& k2_out) - { - - constexpr field minus_b1f{ - Params::endo_minus_b1_lo, - Params::endo_minus_b1_mid, - 0, - 0, - }; - constexpr field b2f{ - Params::endo_b2_lo, - Params::endo_b2_mid, - 0, - 0, - }; - constexpr uint256_t g1{ - Params::endo_g1_lo, - Params::endo_g1_mid, - Params::endo_g1_hi, - Params::endo_g1_hihi, - }; - constexpr uint256_t g2{ - Params::endo_g2_lo, - Params::endo_g2_mid, - Params::endo_g2_hi, - Params::endo_g2_hihi, - }; - - field kf = input.reduce_once(); - uint256_t k{ kf.data[0], kf.data[1], kf.data[2], kf.data[3] }; - - uint512_t c1 = (uint512_t(k) * uint512_t(g1)) >> 384; - uint512_t c2 = (uint512_t(k) * uint512_t(g2)) >> 384; - - field c1f{ c1.lo.data[0], c1.lo.data[1], c1.lo.data[2], c1.lo.data[3] }; - field c2f{ c2.lo.data[0], c2.lo.data[1], c2.lo.data[2], c2.lo.data[3] }; - - c1f.self_to_montgomery_form(); - c2f.self_to_montgomery_form(); - c1f = c1f * minus_b1f; - c2f = c2f * b2f; - field r2f = c1f - c2f; - field beta = cube_root_of_unity(); - field r1f = input.reduce_once() - r2f * beta; - k1_out = r1f; - k2_out = -r2f; - } - - // static constexpr auto coset_generators = compute_coset_generators(); - // static constexpr std::array coset_generators = compute_coset_generators((1 << 30U)); - - friend std::ostream& operator<<(std::ostream& os, const field& a) - { - field out = a.from_montgomery_form(); - std::ios_base::fmtflags f(os.flags()); - os << std::hex << "0x" << std::setfill('0') << std::setw(16) << out.data[3] << std::setw(16) << out.data[2] - << std::setw(16) << out.data[1] << std::setw(16) << out.data[0]; - os.flags(f); - return os; - } - - BBERG_INLINE static void __copy(const field& a, field& r) noexcept { r = a; } - BBERG_INLINE static void __swap(field& src, field& dest) noexcept - { - field T = dest; - dest = src; - src = T; - } - - static field random_element(numeric::random::Engine* engine = nullptr) noexcept; - - static constexpr field multiplicative_generator() noexcept; - - // BBERG_INLINE sstatic constexpr void butterfly(field& left, field& right) noexcept; - - // For serialization - void msgpack_pack(auto& packer) const; - void msgpack_unpack(auto o); - void msgpack_schema(auto& packer) const { packer.pack_alias(Params::schema_name, "bin32"); } - - private: - static constexpr uint256_t twice_modulus = modulus + modulus; - static constexpr uint256_t not_modulus = -modulus; - static constexpr uint256_t twice_not_modulus = -twice_modulus; - - struct wnaf_table { - uint8_t windows[64]; - - constexpr wnaf_table(const uint256_t& target) - : windows{ (uint8_t)(target.data[0] & 15), (uint8_t)((target.data[0] >> 4) & 15), - (uint8_t)((target.data[0] >> 8) & 15), (uint8_t)((target.data[0] >> 12) & 15), - (uint8_t)((target.data[0] >> 16) & 15), (uint8_t)((target.data[0] >> 20) & 15), - (uint8_t)((target.data[0] >> 24) & 15), (uint8_t)((target.data[0] >> 28) & 15), - (uint8_t)((target.data[0] >> 32) & 15), (uint8_t)((target.data[0] >> 36) & 15), - (uint8_t)((target.data[0] >> 40) & 15), (uint8_t)((target.data[0] >> 44) & 15), - (uint8_t)((target.data[0] >> 48) & 15), (uint8_t)((target.data[0] >> 52) & 15), - (uint8_t)((target.data[0] >> 56) & 15), (uint8_t)((target.data[0] >> 60) & 15), - (uint8_t)(target.data[1] & 15), (uint8_t)((target.data[1] >> 4) & 15), - (uint8_t)((target.data[1] >> 8) & 15), (uint8_t)((target.data[1] >> 12) & 15), - (uint8_t)((target.data[1] >> 16) & 15), (uint8_t)((target.data[1] >> 20) & 15), - (uint8_t)((target.data[1] >> 24) & 15), (uint8_t)((target.data[1] >> 28) & 15), - (uint8_t)((target.data[1] >> 32) & 15), (uint8_t)((target.data[1] >> 36) & 15), - (uint8_t)((target.data[1] >> 40) & 15), (uint8_t)((target.data[1] >> 44) & 15), - (uint8_t)((target.data[1] >> 48) & 15), (uint8_t)((target.data[1] >> 52) & 15), - (uint8_t)((target.data[1] >> 56) & 15), (uint8_t)((target.data[1] >> 60) & 15), - (uint8_t)(target.data[2] & 15), (uint8_t)((target.data[2] >> 4) & 15), - (uint8_t)((target.data[2] >> 8) & 15), (uint8_t)((target.data[2] >> 12) & 15), - (uint8_t)((target.data[2] >> 16) & 15), (uint8_t)((target.data[2] >> 20) & 15), - (uint8_t)((target.data[2] >> 24) & 15), (uint8_t)((target.data[2] >> 28) & 15), - (uint8_t)((target.data[2] >> 32) & 15), (uint8_t)((target.data[2] >> 36) & 15), - (uint8_t)((target.data[2] >> 40) & 15), (uint8_t)((target.data[2] >> 44) & 15), - (uint8_t)((target.data[2] >> 48) & 15), (uint8_t)((target.data[2] >> 52) & 15), - (uint8_t)((target.data[2] >> 56) & 15), (uint8_t)((target.data[2] >> 60) & 15), - (uint8_t)(target.data[3] & 15), (uint8_t)((target.data[3] >> 4) & 15), - (uint8_t)((target.data[3] >> 8) & 15), (uint8_t)((target.data[3] >> 12) & 15), - (uint8_t)((target.data[3] >> 16) & 15), (uint8_t)((target.data[3] >> 20) & 15), - (uint8_t)((target.data[3] >> 24) & 15), (uint8_t)((target.data[3] >> 28) & 15), - (uint8_t)((target.data[3] >> 32) & 15), (uint8_t)((target.data[3] >> 36) & 15), - (uint8_t)((target.data[3] >> 40) & 15), (uint8_t)((target.data[3] >> 44) & 15), - (uint8_t)((target.data[3] >> 48) & 15), (uint8_t)((target.data[3] >> 52) & 15), - (uint8_t)((target.data[3] >> 56) & 15), (uint8_t)((target.data[3] >> 60) & 15) } - {} - }; - - BBERG_INLINE static constexpr std::pair mul_wide(const uint64_t a, const uint64_t b) noexcept; - - BBERG_INLINE static constexpr uint64_t mac( - const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t carry_in, uint64_t& carry_out) noexcept; - - BBERG_INLINE static constexpr void mac(const uint64_t a, - const uint64_t b, - const uint64_t c, - const uint64_t carry_in, - uint64_t& out, - uint64_t& carry_out) noexcept; - - BBERG_INLINE static constexpr uint64_t mac_mini(const uint64_t a, - const uint64_t b, - const uint64_t c, - uint64_t& out) noexcept; - - BBERG_INLINE static constexpr void mac_mini( - const uint64_t a, const uint64_t b, const uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept; - - BBERG_INLINE static constexpr uint64_t mac_discard_lo(const uint64_t a, - const uint64_t b, - const uint64_t c) noexcept; - - BBERG_INLINE static constexpr uint64_t addc(const uint64_t a, - const uint64_t b, - const uint64_t carry_in, - uint64_t& carry_out) noexcept; - - BBERG_INLINE static constexpr uint64_t sbb(const uint64_t a, - const uint64_t b, - const uint64_t borrow_in, - uint64_t& borrow_out) noexcept; - - BBERG_INLINE static constexpr uint64_t square_accumulate(const uint64_t a, - const uint64_t b, - const uint64_t c, - const uint64_t carry_in_lo, - const uint64_t carry_in_hi, - uint64_t& carry_lo, - uint64_t& carry_hi) noexcept; - BBERG_INLINE constexpr field reduce() const noexcept; - BBERG_INLINE constexpr field add(const field& other) const noexcept; - BBERG_INLINE constexpr field subtract(const field& other) const noexcept; - BBERG_INLINE constexpr field subtract_coarse(const field& other) const noexcept; - BBERG_INLINE constexpr field montgomery_mul(const field& other) const noexcept; - BBERG_INLINE constexpr field montgomery_mul_big(const field& other) const noexcept; - BBERG_INLINE constexpr field montgomery_square() const noexcept; - -#if (BBERG_NO_ASM == 0) - BBERG_INLINE static field asm_mul(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_sqr(const field& a) noexcept; - BBERG_INLINE static field asm_add(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_sub(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_sqr_with_coarse_reduction(const field& a) noexcept; - BBERG_INLINE static field asm_add_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_sub_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static field asm_add_without_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_sqr(const field& a) noexcept; - BBERG_INLINE static void asm_self_add(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_sub(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_mul_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_sqr_with_coarse_reduction(const field& a) noexcept; - BBERG_INLINE static void asm_self_add_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_sub_with_coarse_reduction(const field& a, const field& b) noexcept; - BBERG_INLINE static void asm_self_add_without_reduction(const field& a, const field& b) noexcept; - - BBERG_INLINE static void asm_conditional_negate(field& a, const uint64_t predicate) noexcept; - BBERG_INLINE static field asm_reduce_once(const field& a) noexcept; - BBERG_INLINE static void asm_self_reduce_once(const field& a) noexcept; - static constexpr uint64_t zero_reference = 0x00ULL; -#endif - static constexpr size_t COSET_GENERATOR_SIZE = 15; - constexpr field tonelli_shanks_sqrt() const noexcept; - static constexpr size_t primitive_root_log_size() noexcept; - static constexpr std::array compute_coset_generators() noexcept; - -#if defined(__SIZEOF_INT128__) && !defined(__wasm__) - static constexpr uint128_t lo_mask = 0xffffffffffffffffUL; -#endif -}; - -template void read(B& it, field& value) -{ - using serialize::read; - field result{ 0, 0, 0, 0 }; - read(it, result.data[3]); - read(it, result.data[2]); - read(it, result.data[1]); - read(it, result.data[0]); - value = result.to_montgomery_form(); -} -template void write(B& buf, field const& value) -{ - using serialize::write; - const field input = value.from_montgomery_form(); - write(buf, input.data[3]); - write(buf, input.data[2]); - write(buf, input.data[1]); - write(buf, input.data[0]); -} - -} // namespace barretenberg - -#include "./field_impl.hpp" -#include "field_impl_x64.hpp" +/** + * @brief Include order of header-only field class is structured to ensure linter/language server can resolve paths. + * Declarations are defined in "field_declarations.hpp", definitions in "field_impl.hpp" (which includes + * declarations header) Spectialized definitions are in "field_impl_generic.hpp" and "field_impl_x64.hpp" + * (which include "field_impl.hpp") + */ +#include "./field_impl_generic.hpp" +#include "./field_impl_x64.hpp" diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field12.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field12.hpp index d799574fd75..cde920f036c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field12.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field12.hpp @@ -14,25 +14,30 @@ template cl , c1(other.c1) {} - constexpr field12(field12&& other) + constexpr field12(field12&& other) noexcept : c0(other.c0) , c1(other.c1) {} - constexpr field12& operator=(const field12& other) + constexpr field12& operator=(const field12& other) noexcept { + if (this == &other) { + return *this; + } c0 = other.c0; c1 = other.c1; return *this; } - constexpr field12& operator=(field12&& other) + constexpr field12& operator=(field12&& other) noexcept { c0 = other.c0; c1 = other.c1; return *this; } + constexpr ~field12() noexcept = default; + base_field c0; base_field c1; @@ -257,7 +262,7 @@ template cl }; } - constexpr bool is_zero() const { return c0.is_zero() && c1.is_zero(); } + [[nodiscard]] constexpr bool is_zero() const { return c0.is_zero() && c1.is_zero(); } constexpr bool operator==(const field12& other) const { return c0 == other.c0 && c1 == other.c1; } }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2.hpp index 32be96eab9e..cac701076c7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2.hpp @@ -1,147 +1,197 @@ #pragma once -#include "barretenberg/numeric/random/engine.hpp" -namespace barretenberg { -template struct alignas(32) field2 { - public: - constexpr field2(const base_field& a = base_field::zero(), const base_field& b = base_field::zero()) - : c0(a) - , c1(b) - {} - - constexpr field2(const field2& other) - : c0(other.c0) - , c1(other.c1) - {} - constexpr field2(field2&& other) - : c0(other.c0) - , c1(other.c1) - {} - - constexpr field2& operator=(const field2& other) - { - c0 = other.c0; - c1 = other.c1; - return *this; - } - - constexpr field2& operator=(field2&& other) - { - c0 = other.c0; - c1 = other.c1; - return *this; - } - - base_field c0; - base_field c1; - - static constexpr uint256_t modulus = base_field::modulus; - - static constexpr field2 zero() { return field2{ base_field::zero(), base_field::zero() }; } - static constexpr field2 one() { return field2{ base_field::one(), base_field::zero() }; } - static constexpr field2 twist_coeff_b() { return field2{ Params::twist_coeff_b_0, Params::twist_coeff_b_1 }; } - static constexpr field2 twist_mul_by_q_x() - { - return field2{ Params::twist_mul_by_q_x_0, Params::twist_mul_by_q_x_1 }; - } - static constexpr field2 twist_mul_by_q_y() - { - return field2{ Params::twist_mul_by_q_y_0, Params::twist_mul_by_q_y_1 }; - } - static constexpr field2 cube_root_of_unity() - { - return field2{ Params::twist_cube_root_0, Params::twist_cube_root_1 }; - } - - constexpr field2 operator*(const field2& other) const noexcept; - constexpr field2 operator+(const field2& other) const noexcept; - constexpr field2 operator-(const field2& other) const noexcept; - constexpr field2 operator-() const noexcept; - constexpr field2 operator/(const field2& other) const noexcept; - - constexpr field2 operator*=(const field2& other) noexcept; - constexpr field2 operator+=(const field2& other) noexcept; - constexpr field2 operator-=(const field2& other) noexcept; - constexpr field2 operator/=(const field2& other) noexcept; - - constexpr field2 mul_by_fq(const base_field& a) const noexcept - { - field2 r{ a * c0, a * c1 }; - return r; - } - - // constexpr bool operator>(const field& other) const noexcept; - // constexpr bool operator<(const field& other) const noexcept; - constexpr bool operator==(const field2& other) const noexcept; - constexpr bool operator!=(const field2& other) const noexcept { return !(*this == other); } - constexpr field2 sqr() const noexcept; - constexpr void self_sqr() noexcept; +#include "./field2_declarations.hpp" - constexpr field2 pow(const uint256_t& exponent) const noexcept; - constexpr field2 pow(const uint64_t exponent) const noexcept; - - constexpr field2 invert() const noexcept; - - constexpr void self_neg() noexcept; - constexpr field2 to_montgomery_form() const noexcept; - constexpr field2 from_montgomery_form() const noexcept; - - constexpr void self_to_montgomery_form() noexcept; - constexpr void self_from_montgomery_form() noexcept; - - constexpr void self_conditional_negate(const uint64_t predicate) noexcept; - - constexpr field2 reduce_once() const noexcept; - constexpr void self_reduce_once() noexcept; - - constexpr void self_set_msb() noexcept; - constexpr bool is_msb_set() const noexcept; - constexpr uint64_t is_msb_set_word() const noexcept; - - constexpr bool is_zero() const noexcept; - - constexpr field2 frobenius_map() const noexcept; - constexpr void self_frobenius_map() noexcept; - - static field2 random_element(numeric::random::Engine* engine = nullptr); - static void serialize_to_buffer(const field2& value, uint8_t* buffer) - { - base_field::serialize_to_buffer(value.c0, buffer); - base_field::serialize_to_buffer(value.c1, buffer + sizeof(base_field)); - } - - static field2 serialize_from_buffer(uint8_t* buffer) - { - field2 result{ base_field::zero(), base_field::zero() }; - result.c0 = base_field::serialize_from_buffer(buffer); - result.c1 = base_field::serialize_from_buffer(buffer + sizeof(base_field)); - - return result; +/** + * @brief Note, this file contains the definitions. of `field2` class. + * Declarations are in `field2_declarations.hpp`. + * Include ordering ensures linter/language server has knowledge of declarations when parsing definitions + * + */ +namespace barretenberg { +template constexpr field2 field2::operator*(const field2& other) const noexcept +{ + // no funny primes please! we assume -1 is not a quadratic residue + static_assert((base::modulus.data[0] & 0x3UL) == 0x3UL); + base t1 = c0 * other.c0; + base t2 = c1 * other.c1; + base t3 = c0 + c1; + base t4 = other.c0 + other.c1; + + return { t1 - t2, t3 * t4 - (t1 + t2) }; +} + +template constexpr field2 field2::operator+(const field2& other) const noexcept +{ + return { c0 + other.c0, c1 + other.c1 }; +} + +template constexpr field2 field2::operator-(const field2& other) const noexcept +{ + return { c0 - other.c0, c1 - other.c1 }; +} + +template constexpr field2 field2::operator-() const noexcept +{ + return { -c0, -c1 }; +} + +template constexpr field2 field2::operator/(const field2& other) const noexcept +{ + return operator*(other.invert()); +} + +template constexpr field2 field2::operator*=(const field2& other) noexcept +{ + *this = operator*(other); + return *this; +} + +template constexpr field2 field2::operator+=(const field2& other) noexcept +{ + *this = operator+(other); + return *this; +} + +template constexpr field2 field2::operator-=(const field2& other) noexcept +{ + *this = operator-(other); + return *this; +} + +template constexpr field2 field2::operator/=(const field2& other) noexcept +{ + *this = operator/(other); + return *this; +} + +template constexpr field2 field2::sqr() const noexcept +{ + base t1 = (c0 * c1); + return { (c0 + c1) * (c0 - c1), t1 + t1 }; +} + +template constexpr void field2::self_sqr() noexcept +{ + *this = sqr(); +} + +template constexpr field2 field2::to_montgomery_form() const noexcept +{ + return { c0.to_montgomery_form(), c1.to_montgomery_form() }; +} + +template constexpr field2 field2::from_montgomery_form() const noexcept +{ + return { c0.from_montgomery_form(), c1.from_montgomery_form() }; +} + +template constexpr void field2::self_to_montgomery_form() noexcept +{ + c0.self_to_montgomery_form(); + c1.self_to_montgomery_form(); +} + +template constexpr void field2::self_from_montgomery_form() noexcept +{ + c0.self_from_montgomery_form(); + c1.self_from_montgomery_form(); +} + +template constexpr field2 field2::reduce_once() const noexcept +{ + return *this; + // return { c0.reduce_once(), c1.reduce_once() }; +} + +template constexpr void field2::self_reduce_once() noexcept +{ + // c0.self_reduce_once(); + // c1.self_reduce_once(); +} + +template constexpr void field2::self_neg() noexcept +{ + c0.self_neg(); + c1.self_neg(); +} + +template constexpr field2 field2::pow(const uint256_t& exponent) const noexcept +{ + + field2 accumulator = *this; + field2 to_mul = *this; + const uint64_t maximum_set_bit = exponent.get_msb(); + + for (int i = static_cast(maximum_set_bit) - 1; i >= 0; --i) { + accumulator.self_sqr(); + if (exponent.get_bit(static_cast(i))) { + accumulator *= to_mul; + } } - friend std::ostream& operator<<(std::ostream& os, const field2& a) - { - os << a.c0 << " , " << a.c1; - return os; + if (*this == zero()) { + accumulator = zero(); + } else if (exponent == uint256_t(0)) { + accumulator = one(); } -}; - -// template void read(B& it, field2& value) -// { -// using serialize::read; -// field2 result; -// read(it, result.c0); -// read(it, result.c1); -// value = result.to_montgomery_form(); -// } -// template void write(B& buf, field2 const& value) -// { -// using serialize::write; -// const auto input = value.from_montgomery_form(); -// write(buf, input.c0); -// write(buf, input.c1); -// } - -} // namespace barretenberg - -#include "field2_impl.hpp" + return accumulator; +} + +template constexpr field2 field2::pow(const uint64_t exponent) const noexcept +{ + return pow({ exponent, 0, 0, 0 }); +} + +template constexpr field2 field2::invert() const noexcept +{ + base t3 = (c0.sqr() + c1.sqr()).invert(); + return { c0 * t3, -(c1 * t3) }; +} + +template +constexpr void field2::self_conditional_negate(const uint64_t predicate) noexcept +{ + *this = predicate != 0U ? -(*this) : *this; +} + +template constexpr void field2::self_set_msb() noexcept +{ + c0.data[3] = 0ULL | (1ULL << 63ULL); +} + +template constexpr bool field2::is_msb_set() const noexcept +{ + return (c0.data[3] >> 63ULL) == 1ULL; +} + +template constexpr uint64_t field2::is_msb_set_word() const noexcept +{ + return (c0.data[3] >> 63ULL); +} + +template constexpr bool field2::is_zero() const noexcept +{ + return (c0.is_zero() && c1.is_zero()); +} + +template constexpr bool field2::operator==(const field2& other) const noexcept +{ + return (c0 == other.c0) && (c1 == other.c1); +} + +template constexpr field2 field2::frobenius_map() const noexcept +{ + return { c0, -c1 }; +} + +template constexpr void field2::self_frobenius_map() noexcept +{ + c1.self_neg(); +} + +template field2 field2::random_element(numeric::random::Engine* engine) +{ + return { base::random_element(engine), base::random_element(engine) }; +} +} // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_declarations.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_declarations.hpp new file mode 100644 index 00000000000..e99c92aed13 --- /dev/null +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_declarations.hpp @@ -0,0 +1,141 @@ +#pragma once + +#include "barretenberg/numeric/uint256/uint256.hpp" + +// forward declare Engine +namespace numeric::random { +class Engine; +} + +namespace barretenberg { +template struct alignas(32) field2 { + public: + constexpr field2(const base_field& a = base_field::zero(), const base_field& b = base_field::zero()) + : c0(a) + , c1(b) + {} + + constexpr field2(const field2& other) noexcept + : c0(other.c0) + , c1(other.c1) + {} + constexpr field2(field2&& other) noexcept + : c0(other.c0) + , c1(other.c1) + {} + + constexpr field2& operator=(const field2& other) noexcept + { + if (this == &other) { + return *this; + } + c0 = other.c0; + c1 = other.c1; + return *this; + } + + constexpr field2& operator=(field2&& other) noexcept + { + if (this == &other) { + return *this; + } + c0 = other.c0; + c1 = other.c1; + return *this; + } + + constexpr ~field2() noexcept = default; + + base_field c0; + base_field c1; + + static constexpr uint256_t modulus = base_field::modulus; + + static constexpr field2 zero() { return field2{ base_field::zero(), base_field::zero() }; } + static constexpr field2 one() { return field2{ base_field::one(), base_field::zero() }; } + static constexpr field2 twist_coeff_b() { return field2{ Params::twist_coeff_b_0, Params::twist_coeff_b_1 }; } + static constexpr field2 twist_mul_by_q_x() + { + return field2{ Params::twist_mul_by_q_x_0, Params::twist_mul_by_q_x_1 }; + } + static constexpr field2 twist_mul_by_q_y() + { + return field2{ Params::twist_mul_by_q_y_0, Params::twist_mul_by_q_y_1 }; + } + static constexpr field2 cube_root_of_unity() + { + return field2{ Params::twist_cube_root_0, Params::twist_cube_root_1 }; + } + + constexpr field2 operator*(const field2& other) const noexcept; + constexpr field2 operator+(const field2& other) const noexcept; + constexpr field2 operator-(const field2& other) const noexcept; + constexpr field2 operator-() const noexcept; + constexpr field2 operator/(const field2& other) const noexcept; + + constexpr field2 operator*=(const field2& other) noexcept; + constexpr field2 operator+=(const field2& other) noexcept; + constexpr field2 operator-=(const field2& other) noexcept; + constexpr field2 operator/=(const field2& other) noexcept; + + constexpr field2 mul_by_fq(const base_field& a) const noexcept + { + field2 r{ a * c0, a * c1 }; + return r; + } + + constexpr bool operator==(const field2& other) const noexcept; + constexpr bool operator!=(const field2& other) const noexcept { return !(*this == other); } + constexpr field2 sqr() const noexcept; + constexpr void self_sqr() noexcept; + + constexpr field2 pow(const uint256_t& exponent) const noexcept; + constexpr field2 pow(uint64_t exponent) const noexcept; + + constexpr field2 invert() const noexcept; + + constexpr void self_neg() noexcept; + constexpr field2 to_montgomery_form() const noexcept; + constexpr field2 from_montgomery_form() const noexcept; + + constexpr void self_to_montgomery_form() noexcept; + constexpr void self_from_montgomery_form() noexcept; + + constexpr void self_conditional_negate(uint64_t predicate) noexcept; + + constexpr field2 reduce_once() const noexcept; + constexpr void self_reduce_once() noexcept; + + constexpr void self_set_msb() noexcept; + [[nodiscard]] constexpr bool is_msb_set() const noexcept; + [[nodiscard]] constexpr uint64_t is_msb_set_word() const noexcept; + + [[nodiscard]] constexpr bool is_zero() const noexcept; + + constexpr field2 frobenius_map() const noexcept; + constexpr void self_frobenius_map() noexcept; + + static field2 random_element(numeric::random::Engine* engine = nullptr); + static void serialize_to_buffer(const field2& value, uint8_t* buffer) + { + base_field::serialize_to_buffer(value.c0, buffer); + base_field::serialize_to_buffer(value.c1, buffer + sizeof(base_field)); + } + + static field2 serialize_from_buffer(uint8_t* buffer) + { + field2 result{ base_field::zero(), base_field::zero() }; + result.c0 = base_field::serialize_from_buffer(buffer); + result.c1 = base_field::serialize_from_buffer(buffer + sizeof(base_field)); + + return result; + } + + friend std::ostream& operator<<(std::ostream& os, const field2& a) + { + os << a.c0 << " , " << a.c1; + return os; + } +}; + +} // namespace barretenberg diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_impl.hpp deleted file mode 100644 index 391bc7fb7cc..00000000000 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field2_impl.hpp +++ /dev/null @@ -1,191 +0,0 @@ -#pragma once - -#include - -namespace barretenberg { -template constexpr field2 field2::operator*(const field2& other) const noexcept -{ - // no funny primes please! we assume -1 is not a quadratic residue - static_assert((base::modulus.data[0] & 0x3UL) == 0x3UL); - base t1 = c0 * other.c0; - base t2 = c1 * other.c1; - base t3 = c0 + c1; - base t4 = other.c0 + other.c1; - - return { t1 - t2, t3 * t4 - (t1 + t2) }; -} - -template constexpr field2 field2::operator+(const field2& other) const noexcept -{ - return { c0 + other.c0, c1 + other.c1 }; -} - -template constexpr field2 field2::operator-(const field2& other) const noexcept -{ - return { c0 - other.c0, c1 - other.c1 }; -} - -template constexpr field2 field2::operator-() const noexcept -{ - return { -c0, -c1 }; -} - -template constexpr field2 field2::operator/(const field2& other) const noexcept -{ - return operator*(other.invert()); -} - -template constexpr field2 field2::operator*=(const field2& other) noexcept -{ - *this = operator*(other); - return *this; -} - -template constexpr field2 field2::operator+=(const field2& other) noexcept -{ - *this = operator+(other); - return *this; -} - -template constexpr field2 field2::operator-=(const field2& other) noexcept -{ - *this = operator-(other); - return *this; -} - -template constexpr field2 field2::operator/=(const field2& other) noexcept -{ - *this = operator/(other); - return *this; -} - -template constexpr field2 field2::sqr() const noexcept -{ - base t1 = (c0 * c1); - return { (c0 + c1) * (c0 - c1), t1 + t1 }; -} - -template constexpr void field2::self_sqr() noexcept -{ - *this = sqr(); -} - -template constexpr field2 field2::to_montgomery_form() const noexcept -{ - return { c0.to_montgomery_form(), c1.to_montgomery_form() }; -} - -template constexpr field2 field2::from_montgomery_form() const noexcept -{ - return { c0.from_montgomery_form(), c1.from_montgomery_form() }; -} - -template constexpr void field2::self_to_montgomery_form() noexcept -{ - c0.self_to_montgomery_form(); - c1.self_to_montgomery_form(); -} - -template constexpr void field2::self_from_montgomery_form() noexcept -{ - c0.self_from_montgomery_form(); - c1.self_from_montgomery_form(); -} - -template constexpr field2 field2::reduce_once() const noexcept -{ - return *this; - // return { c0.reduce_once(), c1.reduce_once() }; -} - -template constexpr void field2::self_reduce_once() noexcept -{ - // c0.self_reduce_once(); - // c1.self_reduce_once(); -} - -template constexpr void field2::self_neg() noexcept -{ - c0.self_neg(); - c1.self_neg(); -} - -template constexpr field2 field2::pow(const uint256_t& exponent) const noexcept -{ - - field2 accumulator = *this; - field2 to_mul = *this; - const uint64_t maximum_set_bit = exponent.get_msb(); - - for (int i = static_cast(maximum_set_bit) - 1; i >= 0; --i) { - accumulator.self_sqr(); - if (exponent.get_bit(static_cast(i))) { - accumulator *= to_mul; - } - } - - if (*this == zero()) { - accumulator = zero(); - } else if (exponent == uint256_t(0)) { - accumulator = one(); - } - return accumulator; -} - -template constexpr field2 field2::pow(const uint64_t exponent) const noexcept -{ - return pow({ exponent, 0, 0, 0 }); -} - -template constexpr field2 field2::invert() const noexcept -{ - base t3 = (c0.sqr() + c1.sqr()).invert(); - return { c0 * t3, -(c1 * t3) }; -} - -template -constexpr void field2::self_conditional_negate(const uint64_t predicate) noexcept -{ - *this = predicate ? -(*this) : *this; -} - -template constexpr void field2::self_set_msb() noexcept -{ - c0.data[3] = 0ULL | (1ULL << 63ULL); -} - -template constexpr bool field2::is_msb_set() const noexcept -{ - return (c0.data[3] >> 63ULL) == 1ULL; -} - -template constexpr uint64_t field2::is_msb_set_word() const noexcept -{ - return (c0.data[3] >> 63ULL); -} - -template constexpr bool field2::is_zero() const noexcept -{ - return (c0.is_zero() && c1.is_zero()); -} - -template constexpr bool field2::operator==(const field2& other) const noexcept -{ - return (c0 == other.c0) && (c1 == other.c1); -} - -template constexpr field2 field2::frobenius_map() const noexcept -{ - return { c0, -c1 }; -} - -template constexpr void field2::self_frobenius_map() noexcept -{ - c1.self_neg(); -} - -template field2 field2::random_element(numeric::random::Engine* engine) -{ - return { base::random_element(engine), base::random_element(engine) }; -} -} // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field6.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field6.hpp index 3db01c38752..873916060a5 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field6.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field6.hpp @@ -18,21 +18,24 @@ template class field6 { , c2(other.c2) {} - constexpr field6(field6&& other) + constexpr field6(field6&& other) noexcept : c0(other.c0) , c1(other.c1) , c2(other.c2) {} - constexpr field6& operator=(const field6& other) + constexpr field6& operator=(const field6& other) noexcept { + if (this == &other) { + return *this; + } c0 = other.c0; c1 = other.c1; c2 = other.c2; return *this; } - constexpr field6& operator=(field6&& other) + constexpr field6& operator=(field6&& other) noexcept { c0 = other.c0; c1 = other.c1; @@ -40,6 +43,8 @@ template class field6 { return *this; } + constexpr ~field6() noexcept = default; + base_field c0; base_field c1; base_field c2; @@ -212,7 +217,7 @@ template class field6 { }; } - constexpr bool is_zero() const { return c0.is_zero() && c1.is_zero() && c2.is_zero(); } + [[nodiscard]] constexpr bool is_zero() const { return c0.is_zero() && c1.is_zero() && c2.is_zero(); } constexpr bool operator==(const field6& other) const { return c0 == other.c0 && c1 == other.c1 && c2 == other.c2; } }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_declarations.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_declarations.hpp new file mode 100644 index 00000000000..98e47b7f354 --- /dev/null +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_declarations.hpp @@ -0,0 +1,566 @@ +#pragma once +#include "barretenberg/common/assert.hpp" +#include "barretenberg/common/inline.hpp" +#include "barretenberg/numeric/random/engine.hpp" +#include "barretenberg/numeric/uint128/uint128.hpp" +#include "barretenberg/numeric/uint256/uint256.hpp" +#include +#include +#include +#include +#include + +#ifndef DISABLE_SHENANIGANS +#ifdef __BMI2__ +#define BBERG_NO_ASM 0 +#else +#define BBERG_NO_ASM 1 +#endif +#else +#define BBERG_NO_ASM 1 +#endif + +namespace barretenberg { +template struct alignas(32) field { + public: + using Params = Params_; + using in_buf = const uint8_t*; + using vec_in_buf = const uint8_t*; + using out_buf = uint8_t*; + using vec_out_buf = uint8_t**; + + // We don't initialize data in the default constructor since we'd lose a lot of time on huge array initializations. + // Other alternatives have been noted, such as casting to get around constructors where they matter, + // however it is felt that sanitizer tools (e.g. MSAN) can detect garbage well, whereas doing + // hacky casts where needed would require rework to critical algos like MSM, FFT, Sumcheck. + // Instead, the recommended solution is use an explicit {} where initialization is important: + // field f; // not initialized + // field f{}; // zero-initialized + // std::array arr; // not initialized, good for huge N + // std::array arr {}; // zero-initialized, preferable for moderate N + field() = default; + + constexpr field(const numeric::uint256_t& input) noexcept + : data{ input.data[0], input.data[1], input.data[2], input.data[3] } + { + self_to_montgomery_form(); + } + + // NOLINTNEXTLINE (unsigned long is platform dependent, which we want in this case) + constexpr field(const unsigned long input) noexcept + : data{ input, 0, 0, 0 } + { + self_to_montgomery_form(); + } + + constexpr field(const unsigned int input) noexcept + : data{ input, 0, 0, 0 } + { + self_to_montgomery_form(); + } + + // NOLINTNEXTLINE (unsigned long long is platform dependent, which we want in this case) + constexpr field(const unsigned long long input) noexcept + : data{ input, 0, 0, 0 } + { + self_to_montgomery_form(); + } + + constexpr field(const int input) noexcept + : data{ 0, 0, 0, 0 } + { + if (input < 0) { + data[0] = static_cast(-input); + data[1] = 0; + data[2] = 0; + data[3] = 0; + self_to_montgomery_form(); + self_neg(); + self_reduce_once(); + } else { + data[0] = static_cast(input); + data[1] = 0; + data[2] = 0; + data[3] = 0; + self_to_montgomery_form(); + } + } + + constexpr field(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept + : data{ a, b, c, d } {}; + + constexpr explicit operator uint32_t() const + { + field out = from_montgomery_form(); + return static_cast(out.data[0]); + } + + constexpr explicit operator uint64_t() const + { + field out = from_montgomery_form(); + return out.data[0]; + } + + constexpr explicit operator uint128_t() const + { + field out = from_montgomery_form(); + uint128_t lo = out.data[0]; + uint128_t hi = out.data[1]; + return (hi << 64) | lo; + } + + constexpr operator uint256_t() const noexcept + { + field out = from_montgomery_form(); + return uint256_t(out.data[0], out.data[1], out.data[2], out.data[3]); + } + + [[nodiscard]] constexpr uint256_t uint256_t_no_montgomery_conversion() const noexcept + { + return { data[0], data[1], data[2], data[3] }; + } + + constexpr field(const field& other) noexcept = default; + constexpr field(field&& other) noexcept = default; + constexpr field& operator=(const field& other) noexcept = default; + constexpr field& operator=(field&& other) noexcept = default; + constexpr ~field() noexcept = default; + alignas(32) uint64_t data[4]; // NOLINT + + static constexpr uint256_t modulus = + uint256_t{ Params::modulus_0, Params::modulus_1, Params::modulus_2, Params::modulus_3 }; + + static constexpr field cube_root_of_unity() + { + // endomorphism i.e. lambda * [P] = (beta * x, y) + if constexpr (Params::cube_root_0 != 0) { + constexpr field result{ + Params::cube_root_0, Params::cube_root_1, Params::cube_root_2, Params::cube_root_3 + }; + return result; + } else { + constexpr field two_inv = field(2).invert(); + constexpr field numerator = (-field(3)).sqrt() - field(1); + constexpr field result = two_inv * numerator; + return result; + } + } + + static constexpr field zero() { return field(0, 0, 0, 0); } + static constexpr field neg_one() { return -field(1); } + static constexpr field one() { return field(1); } + + static constexpr field external_coset_generator() + { + const field result{ + Params::coset_generators_0[7], + Params::coset_generators_1[7], + Params::coset_generators_2[7], + Params::coset_generators_3[7], + }; + return result; + } + + static constexpr field tag_coset_generator() + { + const field result{ + Params::coset_generators_0[6], + Params::coset_generators_1[6], + Params::coset_generators_2[6], + Params::coset_generators_3[6], + }; + return result; + } + + static constexpr field coset_generator(const size_t idx) + { + ASSERT(idx < 7); + const field result{ + Params::coset_generators_0[idx], + Params::coset_generators_1[idx], + Params::coset_generators_2[idx], + Params::coset_generators_3[idx], + }; + return result; + } + + BBERG_INLINE constexpr field operator*(const field& other) const noexcept; + BBERG_INLINE constexpr field operator+(const field& other) const noexcept; + BBERG_INLINE constexpr field operator-(const field& other) const noexcept; + BBERG_INLINE constexpr field operator-() const noexcept; + constexpr field operator/(const field& other) const noexcept; + + // prefix increment (++x) + BBERG_INLINE constexpr field operator++() noexcept; + // postfix increment (x++) + // NOLINTNEXTLINE + BBERG_INLINE constexpr field operator++(int) noexcept; + + BBERG_INLINE constexpr field& operator*=(const field& other) noexcept; + BBERG_INLINE constexpr field& operator+=(const field& other) noexcept; + BBERG_INLINE constexpr field& operator-=(const field& other) noexcept; + constexpr field& operator/=(const field& other) noexcept; + + // NOTE: comparison operators exist so that `field` is comparible with stl methods that require them. + // (e.g. std::sort) + // Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms. + BBERG_INLINE constexpr bool operator>(const field& other) const noexcept; + BBERG_INLINE constexpr bool operator<(const field& other) const noexcept; + BBERG_INLINE constexpr bool operator==(const field& other) const noexcept; + BBERG_INLINE constexpr bool operator!=(const field& other) const noexcept; + + BBERG_INLINE constexpr field to_montgomery_form() const noexcept; + BBERG_INLINE constexpr field from_montgomery_form() const noexcept; + + BBERG_INLINE constexpr field sqr() const noexcept; + BBERG_INLINE constexpr void self_sqr() noexcept; + + BBERG_INLINE constexpr field pow(const uint256_t& exponent) const noexcept; + BBERG_INLINE constexpr field pow(uint64_t exponent) const noexcept; + static constexpr uint256_t modulus_minus_two = + uint256_t(Params::modulus_0 - 2ULL, Params::modulus_1, Params::modulus_2, Params::modulus_3); + constexpr field invert() const noexcept; + static void batch_invert(std::span coeffs) noexcept; + static void batch_invert(field* coeffs, size_t n) noexcept; + /** + * @brief Compute square root of the field element. + * + * @return if the element is a quadratic remainder, if it's not + */ + constexpr std::pair sqrt() const noexcept; + + BBERG_INLINE constexpr void self_neg() noexcept; + + BBERG_INLINE constexpr void self_to_montgomery_form() noexcept; + BBERG_INLINE constexpr void self_from_montgomery_form() noexcept; + + BBERG_INLINE constexpr void self_conditional_negate(uint64_t predicate) noexcept; + + BBERG_INLINE constexpr field reduce_once() const noexcept; + BBERG_INLINE constexpr void self_reduce_once() noexcept; + + BBERG_INLINE constexpr void self_set_msb() noexcept; + [[nodiscard]] BBERG_INLINE constexpr bool is_msb_set() const noexcept; + [[nodiscard]] BBERG_INLINE constexpr uint64_t is_msb_set_word() const noexcept; + + [[nodiscard]] BBERG_INLINE constexpr bool is_zero() const noexcept; + + static constexpr field get_root_of_unity(size_t subgroup_size) noexcept; + + static void serialize_to_buffer(const field& value, uint8_t* buffer) { write(buffer, value); } + + static field serialize_from_buffer(const uint8_t* buffer) { return from_buffer(buffer); } + + [[nodiscard]] BBERG_INLINE std::vector to_buffer() const { return ::to_buffer(*this); } + + struct wide_array { + uint64_t data[8]; // NOLINT + }; + BBERG_INLINE constexpr wide_array mul_512(const field& other) const noexcept; + BBERG_INLINE constexpr wide_array sqr_512() const noexcept; + + BBERG_INLINE constexpr field conditionally_subtract_from_double_modulus(const uint64_t predicate) const noexcept + { + if (predicate != 0) { + constexpr field p{ + twice_modulus.data[0], twice_modulus.data[1], twice_modulus.data[2], twice_modulus.data[3] + }; + return p - *this; + } + return *this; + } + + /** + * For short Weierstrass curves y^2 = x^3 + b mod r, if there exists a cube root of unity mod r, + * we can take advantage of an enodmorphism to decompose a 254 bit scalar into 2 128 bit scalars. + * \beta = cube root of 1, mod q (q = order of fq) + * \lambda = cube root of 1, mod r (r = order of fr) + * + * For a point P1 = (X, Y), where Y^2 = X^3 + b, we know that + * the point P2 = (X * \beta, Y) is also a point on the curve + * We can represent P2 as a scalar multiplication of P1, where P2 = \lambda * P1 + * + * For a generic multiplication of P1 by a 254 bit scalar k, we can decompose k + * into 2 127 bit scalars (k1, k2), such that k = k1 - (k2 * \lambda) + * + * We can now represent (k * P1) as (k1 * P1) - (k2 * P2), where P2 = (X * \beta, Y). + * As k1, k2 have half the bit length of k, we have reduced the number of loop iterations of our + * scalar multiplication algorithm in half + * + * To find k1, k2, We use the extended euclidean algorithm to find 4 short scalars [a1, a2], [b1, b2] such that + * modulus = (a1 * b2) - (b1 * a2) + * We then compute scalars c1 = round(b2 * k / r), c2 = round(b1 * k / r), where + * k1 = (c1 * a1) + (c2 * a2), k2 = -((c1 * b1) + (c2 * b2)) + * We pre-compute scalars g1 = (2^256 * b1) / n, g2 = (2^256 * b2) / n, to avoid having to perform long division + * on 512-bit scalars + **/ + static void split_into_endomorphism_scalars(const field& k, field& k1, field& k2) + { + // if the modulus is a 256-bit integer, we need to use a basis where g1, g2 have been shifted by 2^384 + if constexpr (Params::modulus_3 >= 0x4000000000000000ULL) { + split_into_endomorphism_scalars_384(k, k1, k2); + return; + } + field input = k.reduce_once(); + + constexpr field endo_g1 = { Params::endo_g1_lo, Params::endo_g1_mid, Params::endo_g1_hi, 0 }; + + constexpr field endo_g2 = { Params::endo_g2_lo, Params::endo_g2_mid, 0, 0 }; + + constexpr field endo_minus_b1 = { Params::endo_minus_b1_lo, Params::endo_minus_b1_mid, 0, 0 }; + + constexpr field endo_b2 = { Params::endo_b2_lo, Params::endo_b2_mid, 0, 0 }; + + // compute c1 = (g2 * k) >> 256 + wide_array c1 = endo_g2.mul_512(input); + // compute c2 = (g1 * k) >> 256 + wide_array c2 = endo_g1.mul_512(input); + + // (the bit shifts are implicit, as we only utilize the high limbs of c1, c2 + + field c1_hi = { + c1.data[4], c1.data[5], c1.data[6], c1.data[7] + }; // *(field*)((uintptr_t)(&c1) + (4 * sizeof(uint64_t))); + field c2_hi = { + c2.data[4], c2.data[5], c2.data[6], c2.data[7] + }; // *(field*)((uintptr_t)(&c2) + (4 * sizeof(uint64_t))); + + // compute q1 = c1 * -b1 + wide_array q1 = c1_hi.mul_512(endo_minus_b1); + // compute q2 = c2 * b2 + wide_array q2 = c2_hi.mul_512(endo_b2); + + // FIX: Avoid using 512-bit multiplication as its not necessary. + // c1_hi, c2_hi can be uint256_t's and the final result (without montgomery reduction) + // could be casted to a field. + field q1_lo{ q1.data[0], q1.data[1], q1.data[2], q1.data[3] }; + field q2_lo{ q2.data[0], q2.data[1], q2.data[2], q2.data[3] }; + + field t1 = (q2_lo - q1_lo).reduce_once(); + field beta = cube_root_of_unity(); + field t2 = (t1 * beta + input).reduce_once(); + k2.data[0] = t1.data[0]; + k2.data[1] = t1.data[1]; + k1.data[0] = t2.data[0]; + k1.data[1] = t2.data[1]; + } + + static void split_into_endomorphism_scalars_384(const field& input, field& k1_out, field& k2_out) + { + + constexpr field minus_b1f{ + Params::endo_minus_b1_lo, + Params::endo_minus_b1_mid, + 0, + 0, + }; + constexpr field b2f{ + Params::endo_b2_lo, + Params::endo_b2_mid, + 0, + 0, + }; + constexpr uint256_t g1{ + Params::endo_g1_lo, + Params::endo_g1_mid, + Params::endo_g1_hi, + Params::endo_g1_hihi, + }; + constexpr uint256_t g2{ + Params::endo_g2_lo, + Params::endo_g2_mid, + Params::endo_g2_hi, + Params::endo_g2_hihi, + }; + + field kf = input.reduce_once(); + uint256_t k{ kf.data[0], kf.data[1], kf.data[2], kf.data[3] }; + + uint512_t c1 = (uint512_t(k) * static_cast(g1)) >> 384; + uint512_t c2 = (uint512_t(k) * static_cast(g2)) >> 384; + + field c1f{ c1.lo.data[0], c1.lo.data[1], c1.lo.data[2], c1.lo.data[3] }; + field c2f{ c2.lo.data[0], c2.lo.data[1], c2.lo.data[2], c2.lo.data[3] }; + + c1f.self_to_montgomery_form(); + c2f.self_to_montgomery_form(); + c1f = c1f * minus_b1f; + c2f = c2f * b2f; + field r2f = c1f - c2f; + field beta = cube_root_of_unity(); + field r1f = input.reduce_once() - r2f * beta; + k1_out = r1f; + k2_out = -r2f; + } + + // static constexpr auto coset_generators = compute_coset_generators(); + // static constexpr std::array coset_generators = compute_coset_generators((1 << 30U)); + + friend std::ostream& operator<<(std::ostream& os, const field& a) + { + field out = a.from_montgomery_form(); + std::ios_base::fmtflags f(os.flags()); + os << std::hex << "0x" << std::setfill('0') << std::setw(16) << out.data[3] << std::setw(16) << out.data[2] + << std::setw(16) << out.data[1] << std::setw(16) << out.data[0]; + os.flags(f); + return os; + } + + BBERG_INLINE static void __copy(const field& a, field& r) noexcept { r = a; } // NOLINT + BBERG_INLINE static void __swap(field& src, field& dest) noexcept // NOLINT + { + field T = dest; + dest = src; + src = T; + } + + static field random_element(numeric::random::Engine* engine = nullptr) noexcept; + + static constexpr field multiplicative_generator() noexcept; + + // For serialization + void msgpack_pack(auto& packer) const; + void msgpack_unpack(auto o); + void msgpack_schema(auto& packer) const { packer.pack_alias(Params::schema_name, "bin32"); } + + private: + static constexpr uint256_t twice_modulus = modulus + modulus; + static constexpr uint256_t not_modulus = -modulus; + static constexpr uint256_t twice_not_modulus = -twice_modulus; + + struct wnaf_table { + uint8_t windows[64]; // NOLINT + + constexpr wnaf_table(const uint256_t& target) + : windows{ + static_cast(target.data[0] & 15), static_cast((target.data[0] >> 4) & 15), + static_cast((target.data[0] >> 8) & 15), static_cast((target.data[0] >> 12) & 15), + static_cast((target.data[0] >> 16) & 15), static_cast((target.data[0] >> 20) & 15), + static_cast((target.data[0] >> 24) & 15), static_cast((target.data[0] >> 28) & 15), + static_cast((target.data[0] >> 32) & 15), static_cast((target.data[0] >> 36) & 15), + static_cast((target.data[0] >> 40) & 15), static_cast((target.data[0] >> 44) & 15), + static_cast((target.data[0] >> 48) & 15), static_cast((target.data[0] >> 52) & 15), + static_cast((target.data[0] >> 56) & 15), static_cast((target.data[0] >> 60) & 15), + static_cast(target.data[1] & 15), static_cast((target.data[1] >> 4) & 15), + static_cast((target.data[1] >> 8) & 15), static_cast((target.data[1] >> 12) & 15), + static_cast((target.data[1] >> 16) & 15), static_cast((target.data[1] >> 20) & 15), + static_cast((target.data[1] >> 24) & 15), static_cast((target.data[1] >> 28) & 15), + static_cast((target.data[1] >> 32) & 15), static_cast((target.data[1] >> 36) & 15), + static_cast((target.data[1] >> 40) & 15), static_cast((target.data[1] >> 44) & 15), + static_cast((target.data[1] >> 48) & 15), static_cast((target.data[1] >> 52) & 15), + static_cast((target.data[1] >> 56) & 15), static_cast((target.data[1] >> 60) & 15), + static_cast(target.data[2] & 15), static_cast((target.data[2] >> 4) & 15), + static_cast((target.data[2] >> 8) & 15), static_cast((target.data[2] >> 12) & 15), + static_cast((target.data[2] >> 16) & 15), static_cast((target.data[2] >> 20) & 15), + static_cast((target.data[2] >> 24) & 15), static_cast((target.data[2] >> 28) & 15), + static_cast((target.data[2] >> 32) & 15), static_cast((target.data[2] >> 36) & 15), + static_cast((target.data[2] >> 40) & 15), static_cast((target.data[2] >> 44) & 15), + static_cast((target.data[2] >> 48) & 15), static_cast((target.data[2] >> 52) & 15), + static_cast((target.data[2] >> 56) & 15), static_cast((target.data[2] >> 60) & 15), + static_cast(target.data[3] & 15), static_cast((target.data[3] >> 4) & 15), + static_cast((target.data[3] >> 8) & 15), static_cast((target.data[3] >> 12) & 15), + static_cast((target.data[3] >> 16) & 15), static_cast((target.data[3] >> 20) & 15), + static_cast((target.data[3] >> 24) & 15), static_cast((target.data[3] >> 28) & 15), + static_cast((target.data[3] >> 32) & 15), static_cast((target.data[3] >> 36) & 15), + static_cast((target.data[3] >> 40) & 15), static_cast((target.data[3] >> 44) & 15), + static_cast((target.data[3] >> 48) & 15), static_cast((target.data[3] >> 52) & 15), + static_cast((target.data[3] >> 56) & 15), static_cast((target.data[3] >> 60) & 15) + } + {} + }; + + BBERG_INLINE static constexpr std::pair mul_wide(uint64_t a, uint64_t b) noexcept; + + BBERG_INLINE static constexpr uint64_t mac( + uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& carry_out) noexcept; + + BBERG_INLINE static constexpr void mac( + uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t& out, uint64_t& carry_out) noexcept; + + BBERG_INLINE static constexpr uint64_t mac_mini(uint64_t a, uint64_t b, uint64_t c, uint64_t& out) noexcept; + + BBERG_INLINE static constexpr void mac_mini( + uint64_t a, uint64_t b, uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept; + + BBERG_INLINE static constexpr uint64_t mac_discard_lo(uint64_t a, uint64_t b, uint64_t c) noexcept; + + BBERG_INLINE static constexpr uint64_t addc(uint64_t a, + uint64_t b, + uint64_t carry_in, + uint64_t& carry_out) noexcept; + + BBERG_INLINE static constexpr uint64_t sbb(uint64_t a, + uint64_t b, + uint64_t borrow_in, + uint64_t& borrow_out) noexcept; + + BBERG_INLINE static constexpr uint64_t square_accumulate(uint64_t a, + uint64_t b, + uint64_t c, + uint64_t carry_in_lo, + uint64_t carry_in_hi, + uint64_t& carry_lo, + uint64_t& carry_hi) noexcept; + BBERG_INLINE constexpr field reduce() const noexcept; + BBERG_INLINE constexpr field add(const field& other) const noexcept; + BBERG_INLINE constexpr field subtract(const field& other) const noexcept; + BBERG_INLINE constexpr field subtract_coarse(const field& other) const noexcept; + BBERG_INLINE constexpr field montgomery_mul(const field& other) const noexcept; + BBERG_INLINE constexpr field montgomery_mul_big(const field& other) const noexcept; + BBERG_INLINE constexpr field montgomery_square() const noexcept; + +#if (BBERG_NO_ASM == 0) + BBERG_INLINE static field asm_mul(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_sqr(const field& a) noexcept; + BBERG_INLINE static field asm_add(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_sub(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_sqr_with_coarse_reduction(const field& a) noexcept; + BBERG_INLINE static field asm_add_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_sub_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static field asm_add_without_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_sqr(const field& a) noexcept; + BBERG_INLINE static void asm_self_add(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_sub(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_mul_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_sqr_with_coarse_reduction(const field& a) noexcept; + BBERG_INLINE static void asm_self_add_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_sub_with_coarse_reduction(const field& a, const field& b) noexcept; + BBERG_INLINE static void asm_self_add_without_reduction(const field& a, const field& b) noexcept; + + BBERG_INLINE static void asm_conditional_negate(field& r, uint64_t predicate) noexcept; + BBERG_INLINE static field asm_reduce_once(const field& a) noexcept; + BBERG_INLINE static void asm_self_reduce_once(const field& a) noexcept; + static constexpr uint64_t zero_reference = 0x00ULL; +#endif + static constexpr size_t COSET_GENERATOR_SIZE = 15; + constexpr field tonelli_shanks_sqrt() const noexcept; + static constexpr size_t primitive_root_log_size() noexcept; + static constexpr std::array compute_coset_generators() noexcept; + +#if defined(__SIZEOF_INT128__) && !defined(__wasm__) + static constexpr uint128_t lo_mask = 0xffffffffffffffffUL; +#endif +}; + +template void read(B& it, field& value) +{ + using serialize::read; + field result{ 0, 0, 0, 0 }; + read(it, result.data[3]); + read(it, result.data[2]); + read(it, result.data[1]); + read(it, result.data[0]); + value = result.to_montgomery_form(); +} +template void write(B& buf, field const& value) +{ + using serialize::write; + const field input = value.from_montgomery_form(); + write(buf, input.data[3]); + write(buf, input.data[2]); + write(buf, input.data[1]); + write(buf, input.data[0]); +} + +} // namespace barretenberg diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl.hpp index 7f31f20d127..1bb8756ad25 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl.hpp @@ -3,31 +3,21 @@ #include "barretenberg/common/throw_or_abort.hpp" #include "barretenberg/numeric/bitop/get_msb.hpp" #include "barretenberg/numeric/random/engine.hpp" -#include "field_impl_generic.hpp" #include #include #include #include -#if (BBERG_NO_ASM == 0) -#include "field_impl_x64.hpp" -#endif +#include "./field_declarations.hpp" -#include "field_impl_generic.hpp" namespace barretenberg { -// template constexpr void field::butterfly(field& left, field& right) noexcept -// { -// if constexpr(BBERG_NO_ASM || (T::modulus_3 >= 0x4000000000000000ULL) || (T::modulus_1 == 0 && T::modulus_2 == 0 && -// T::modulus_3 == 0)) { - -// } else { -// if (std::is_constant_evaluated()) { -// } -// return asm_butterfly(left, right); -// } -// } - +// clang-format off +// disable the following style guides: +// cppcoreguidelines-avoid-c-arrays : we make heavy use of c-style arrays here to prevent default-initialization of memory when constructing `field` objects. +// The intention is for field to act like a primitive numeric type with the performance/complexity trade-offs expected from this. +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) +// clang-format on /** * * Mutiplication @@ -47,7 +37,7 @@ template constexpr field field::operator*(const field& other) co } } -template constexpr field field::operator*=(const field& other) noexcept +template constexpr field& field::operator*=(const field& other) noexcept { if constexpr (BBERG_NO_ASM || (T::modulus_3 >= 0x4000000000000000ULL) || (T::modulus_1 == 0 && T::modulus_2 == 0 && T::modulus_3 == 0)) { @@ -76,9 +66,8 @@ template constexpr field field::sqr() const noexcept } else { if (std::is_constant_evaluated()) { return montgomery_square(); - } else { - return asm_sqr_with_coarse_reduction(*this); // asm_sqr(*this); } + return asm_sqr_with_coarse_reduction(*this); // asm_sqr(*this); } } @@ -109,13 +98,12 @@ template constexpr field field::operator+(const field& other) co } else { if (std::is_constant_evaluated()) { return add(other); - } else { - return asm_add_with_coarse_reduction(*this, other); // asm_add_without_reduction(*this, other); } + return asm_add_with_coarse_reduction(*this, other); // asm_add_without_reduction(*this, other); } } -template constexpr field field::operator+=(const field& other) noexcept +template constexpr field& field::operator+=(const field& other) noexcept { if constexpr (BBERG_NO_ASM || (T::modulus_3 >= 0x4000000000000000ULL) || (T::modulus_1 == 0 && T::modulus_2 == 0 && T::modulus_3 == 0)) { @@ -135,6 +123,7 @@ template constexpr field field::operator++() noexcept return *this += 1; } +// NOLINTNEXTLINE(cert-dcl21-cpp) circular linting errors. If const is added, linter suggests removing template constexpr field field::operator++(int) noexcept { field value_before_incrementing = *this; @@ -155,9 +144,8 @@ template constexpr field field::operator-(const field& other) co } else { if (std::is_constant_evaluated()) { return subtract_coarse(other); // subtract(other); - } else { - return asm_sub_with_coarse_reduction(*this, other); // asm_sub(*this, other); } + return asm_sub_with_coarse_reduction(*this, other); // asm_sub(*this, other); } } @@ -169,7 +157,7 @@ template constexpr field field::operator-() const noexcept return p - *this; // modulus - *this; } - // TODO: there are 3 ways we can make this more efficient + // TODO(@zac-williamson): there are 3 ways we can make this more efficient // 1: we subtract `p` from `*this` instead of `2p` // 2: instead of `p - *this`, we use an asm block that does `p - *this` without the assembly reduction step // 3: we replace `(p - *this).reduce_once()` with an assembly block that is equivalent to `p - *this`, @@ -189,7 +177,7 @@ template constexpr field field::operator-() const noexcept return (p - *this).reduce_once(); // modulus - *this; } -template constexpr field field::operator-=(const field& other) noexcept +template constexpr field& field::operator-=(const field& other) noexcept { if constexpr (BBERG_NO_ASM || (T::modulus_3 >= 0x4000000000000000ULL) || (T::modulus_1 == 0 && T::modulus_2 == 0 && T::modulus_3 == 0)) { @@ -220,10 +208,10 @@ template constexpr void field::self_conditional_negate(const uint64 { if constexpr (BBERG_NO_ASM || (T::modulus_3 >= 0x4000000000000000ULL) || (T::modulus_1 == 0 && T::modulus_2 == 0 && T::modulus_3 == 0)) { - *this = predicate ? -(*this) : *this; + *this = predicate ? -(*this) : *this; // NOLINT } else { if (std::is_constant_evaluated()) { - *this = predicate ? -(*this) : *this; + *this = predicate ? -(*this) : *this; // NOLINT } else { asm_conditional_negate(*this, predicate); } @@ -231,8 +219,16 @@ template constexpr void field::self_conditional_negate(const uint64 } /** - * Comparison operators - **/ + * @brief Greater-than operator + * @details comparison operators exist so that `field` is comparible with stl methods that require them. + * (e.g. std::sort) + * Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms. + * + * @tparam T + * @param other + * @return true + * @return false + */ template constexpr bool field::operator>(const field& other) const noexcept { const field left = reduce_once(); @@ -246,6 +242,17 @@ template constexpr bool field::operator>(const field& other) const return (t0 || t1 || t2 || t3); } +/** + * @brief Less-than operator + * @details comparison operators exist so that `field` is comparible with stl methods that require them. + * (e.g. std::sort) + * Finite fields do not have an explicit ordering, these should *NEVER* be used in algebraic algorithms. + * + * @tparam T + * @param other + * @return true + * @return false + */ template constexpr bool field::operator<(const field& other) const noexcept { return (other > *this); @@ -269,7 +276,7 @@ template constexpr field field::to_montgomery_form() const noexc constexpr field r_squared{ T::r_squared_0, T::r_squared_1, T::r_squared_2, T::r_squared_3 }; field result = *this; - // TODO: are these reductions needed? + // TODO(@zac-williamson): are these reductions needed? // Rationale: We want to take any 256-bit input and be able to convert into montgomery form. // A basic heuristic we use is that any input into the `*` operator must be between [0, 2p - 1] // to prevent overflows in the asm algorithm. @@ -312,9 +319,8 @@ template constexpr field field::reduce_once() const noexcept } else { if (std::is_constant_evaluated()) { return reduce(); - } else { - return asm_reduce_once(*this); } + return asm_reduce_once(*this); } } @@ -378,7 +384,7 @@ template void field::batch_invert(std::span coeffs) noexcept auto temporaries_ptr = std::static_pointer_cast(get_mem_slab(n * sizeof(field))); auto skipped_ptr = std::static_pointer_cast(get_mem_slab(n)); auto temporaries = temporaries_ptr.get(); - auto skipped = skipped_ptr.get(); + auto* skipped = skipped_ptr.get(); field accumulator = one(); for (size_t i = 0; i < n; ++i) { @@ -509,9 +515,8 @@ template constexpr std::pair> field::sqrt() const no } if ((root * root) == (*this)) { return std::pair(true, root); - } else { - return std::pair(false, field::zero()); } + return std::pair(false, field::zero()); } // namespace barretenberg @@ -520,7 +525,7 @@ template constexpr field field::operator/(const field& other) co return operator*(other.invert()); } -template constexpr field field::operator/=(const field& other) noexcept +template constexpr field& field::operator/=(const field& other) noexcept { *this = operator/(other); return *this; @@ -547,7 +552,7 @@ template constexpr bool field::is_zero() const noexcept (data[0] == T::modulus_0 && data[1] == T::modulus_1 && data[2] == T::modulus_2 && data[3] == T::modulus_3); } -template constexpr field field::get_root_of_unity(const size_t subgroup_size) noexcept +template constexpr field field::get_root_of_unity(size_t subgroup_size) noexcept { field r{ T::primitive_root_0, T::primitive_root_1, T::primitive_root_2, T::primitive_root_3 }; for (size_t i = primitive_root_log_size(); i > subgroup_size; --i) { @@ -572,7 +577,7 @@ template constexpr size_t field::primitive_root_log_size() noexcept { uint256_t target = modulus - 1; size_t result = 0; - while (target.get_bit(result) == 0) { + while (!target.get_bit(result)) { ++result; } return result; @@ -640,7 +645,7 @@ template void field::msgpack_pack(auto& packer) const // The packer is then used to write the binary data to the buffer, just like in the old format. packer.pack_bin(sizeof(bin_data)); - packer.pack_bin_body((const char*)bin_data, sizeof(bin_data)); + packer.pack_bin_body((const char*)bin_data, sizeof(bin_data)); // NOLINT } // This function is used to deserialize a field. It also matches the old deserialization format by @@ -653,7 +658,7 @@ template void field::msgpack_unpack(auto o) // The binary data is then read as big endian uint64_t's. This is done by casting the raw data to uint64_t* and then // using ntohll ("network to host long long") to correct the endianness to the host's endianness. - uint64_t* cast_data = (uint64_t*)&raw_data[0]; + uint64_t* cast_data = (uint64_t*)&raw_data[0]; // NOLINT uint64_t reversed[] = { ntohll(cast_data[3]), ntohll(cast_data[2]), ntohll(cast_data[1]), ntohll(cast_data[0]) }; // The corrected data is then copied back into the field's data array. @@ -666,3 +671,7 @@ template void field::msgpack_unpack(auto o) } } // namespace barretenberg + +// clang-format off +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) +// clang-format on diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hpp index 064dc7d71cd..25fb238ec6d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_generic.hpp @@ -1,13 +1,17 @@ #pragma once +#include +#include + +#include "./field_impl.hpp" namespace barretenberg { -template -constexpr std::pair field::mul_wide(const uint64_t a, const uint64_t b) noexcept +// NOLINTBEGIN(readability-implicit-bool-conversion) +template constexpr std::pair field::mul_wide(uint64_t a, uint64_t b) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = ((uint128_t)a * (uint128_t)b); - return { (uint64_t)(res), (uint64_t)(res >> 64) }; + const uint128_t res = (static_cast(a) * static_cast(b)); + return { static_cast(res), static_cast(res >> 64) }; #else const uint64_t product = a * b; return { product & 0xffffffffULL, product >> 32 }; @@ -19,9 +23,10 @@ constexpr uint64_t field::mac( const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t carry_in, uint64_t& carry_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = (uint128_t)a + ((uint128_t)b * (uint128_t)c) + (uint128_t)carry_in; - carry_out = (uint64_t)(res >> 64); - return (uint64_t)res; + const uint128_t res = static_cast(a) + (static_cast(b) * static_cast(c)) + + static_cast(carry_in); + carry_out = static_cast(res >> 64); + return static_cast(res); #else const uint64_t product = b * c + a + carry_in; carry_out = product >> 32; @@ -38,9 +43,10 @@ constexpr void field::mac(const uint64_t a, uint64_t& carry_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = (uint128_t)a + ((uint128_t)b * (uint128_t)c) + (uint128_t)carry_in; - out = (uint64_t)(res); - carry_out = (uint64_t)(res >> 64); + const uint128_t res = static_cast(a) + (static_cast(b) * static_cast(c)) + + static_cast(carry_in); + out = static_cast(res); + carry_out = static_cast(res >> 64); #else const uint64_t product = b * c + a + carry_in; carry_out = product >> 32; @@ -55,9 +61,9 @@ constexpr uint64_t field::mac_mini(const uint64_t a, uint64_t& carry_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = (uint128_t)a + ((uint128_t)b * (uint128_t)c); - carry_out = (uint64_t)(res >> 64); - return (uint64_t)(res); + const uint128_t res = static_cast(a) + (static_cast(b) * static_cast(c)); + carry_out = static_cast(res >> 64); + return static_cast(res); #else const uint64_t product = b * c + a; carry_out = product >> 32; @@ -70,9 +76,9 @@ constexpr void field::mac_mini( const uint64_t a, const uint64_t b, const uint64_t c, uint64_t& out, uint64_t& carry_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = (uint128_t)a + ((uint128_t)b * (uint128_t)c); - out = (uint64_t)(res); - carry_out = (uint64_t)(res >> 64); + const uint128_t res = static_cast(a) + (static_cast(b) * static_cast(c)); + out = static_cast(res); + carry_out = static_cast(res >> 64); #else const uint64_t result = b * c + a; carry_out = result >> 32; @@ -84,8 +90,8 @@ template constexpr uint64_t field::mac_discard_lo(const uint64_t a, const uint64_t b, const uint64_t c) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t res = (uint128_t)a + ((uint128_t)b * (uint128_t)c); - return (uint64_t)(res >> 64); + const uint128_t res = static_cast(a) + (static_cast(b) * static_cast(c)); + return static_cast(res >> 64); #else return (b * c + a) >> 32; #endif @@ -98,9 +104,9 @@ constexpr uint64_t field::addc(const uint64_t a, uint64_t& carry_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - uint128_t res = (uint128_t)a + (uint128_t)b + (uint128_t)carry_in; - carry_out = (uint64_t)(res >> 64); - return (uint64_t)res; + uint128_t res = static_cast(a) + static_cast(b) + static_cast(carry_in); + carry_out = static_cast(res >> 64); + return static_cast(res); #else uint64_t r = a + b; const uint64_t carry_temp = r < a; @@ -117,9 +123,9 @@ constexpr uint64_t field::sbb(const uint64_t a, uint64_t& borrow_out) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - uint128_t res = (uint128_t)a - ((uint128_t)b + (uint128_t)(borrow_in >> 63)); - borrow_out = (uint64_t)(res >> 64); - return (uint64_t)res; + uint128_t res = static_cast(a) - (static_cast(b) + static_cast(borrow_in >> 63)); + borrow_out = static_cast(res >> 64); + return static_cast(res); #else uint64_t t_1 = a - (borrow_in >> 63ULL); uint64_t borrow_temp_1 = t_1 > a; @@ -140,9 +146,9 @@ constexpr uint64_t field::square_accumulate(const uint64_t a, uint64_t& carry_hi) noexcept { #if defined(__SIZEOF_INT128__) && !defined(__wasm__) - const uint128_t product = (uint128_t)b * (uint128_t)c; - const uint64_t r0 = (uint64_t)product; - const uint64_t r1 = (uint64_t)(product >> 64); + const uint128_t product = static_cast(b) * static_cast(c); + const auto r0 = static_cast(product); + const auto r1 = static_cast(product >> 64); uint64_t out = r0 + r0; carry_lo = (out < r0); out += a; @@ -297,12 +303,12 @@ template constexpr field field::montgomery_mul_big(const field& uint64_t t4 = 0; uint64_t t5 = 0; uint64_t k = 0; - for (size_t i = 0; i < 4; ++i) { + for (const auto& element : data) { c = 0; - mac(t0, data[i], other.data[0], c, t0, c); - mac(t1, data[i], other.data[1], c, t1, c); - mac(t2, data[i], other.data[2], c, t2, c); - mac(t3, data[i], other.data[3], c, t3, c); + mac(t0, element, other.data[0], c, t0, c); + mac(t1, element, other.data[1], c, t1, c); + mac(t2, element, other.data[2], c, t2, c); + mac(t3, element, other.data[3], c, t3, c); t4 = addc(t4, c, 0, t5); c = 0; @@ -807,4 +813,5 @@ template constexpr struct field::wide_array field::mul_512(const #endif } +// NOLINTEND(readability-implicit-bool-conversion) } // namespace barretenberg \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_x64.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_x64.hpp index 2664f123451..10e48440264 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_x64.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/fields/field_impl_x64.hpp @@ -1,67 +1,10 @@ #pragma once #if (BBERG_NO_ASM == 0) +#include "./field_impl.hpp" #include "asm_macros.hpp" - namespace barretenberg { -/* -asm_butterfly(field& left, field& root) noexcept -{ - __asm__(MUL("32(%0)", "40(%0)", "48(%0)", "56(%0)", "%1") - // r12, r13, r14, r15 contains b*omega - // we want a + b*omega - // and a - b*omega - "xorq %%rdx, %%rdx \n\t" - "movq %%r12, %%r8 \n\t" - "movq %%r13, %%r9 \n\t" - "movq %%r14, %%r10 \n\t" - "movq %%r15, %%r11 \n\t" - // "adcxq (%0), %%r8 \n\t" - // "adcxq 8(%0), %%r9 \n\t" - // "adcxq 16(%0), %%r10 \n\t" - // "adcxq 24(%0), %%r11 \n\t" - "subq 32(%0), %%r8 \n\t" - "sbbq 40(%0), %%r9 \n\t" - "sbbq 48(%0), %%r10 \n\t" - "sbbq 56(%0), %%r11 \n\t" - "sbbq [%zero_reference], %%rdx \n\t" - "xorq %%rdi, %%rdi\n\t" - "movq %%rdx, %%rdi \n\t" - "andq $not_modulus_0, %%rdi \n\t" - "adoxq %%rdi, %%r8 \n\t" - "adcxq 0(%0), %%r12 \n\t" - "movq %%r8, 32(%0) \n\t" - "movq %%rdx, %%rdi \n\t" - "andq $not_modulus_1, %%rdi \n\t" - "adoxq %%rdi, %%r9 \n\t" - "adcxq 8(%0), %%r13 \n\t" - "movq %%r9, 32(%0) \n\t" - "movq %%rdx, %%rdi \n\t" - "andq $not_modulus_2, %%rdi \n\t" - "adoxq %%rdi, %%r10 \n\t" - "adcxq 16(%0), %%r14 \n\t" - "movq %%r10, 32(%0) \n\t" - "movq %%rdx, %%rdi \n\t" - "andq $not_modulus_3, %%rdi \n\t" - "adoxq %%rdi, %%r11 \n\t" - "adcxq 24(%0), %%r1 \n\t" - "movq %%r8, 32(%0) \n\t" - - : - : "%r"(&a), - "%r"(&b), - "r"(&r), - [modulus_0] "m"(modulus_0), - [modulus_1] "m"(modulus_1), - [modulus_2] "m"(modulus_2), - [modulus_3] "m"(modulus_3), - [r_inv] "m"(r_inv), - [zero_reference] "m"(zero_reference) - : "%rdx", "%rdi", "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "cc", "memory"); - return r; -} -*/ template field field::asm_mul_with_coarse_reduction(const field& a, const field& b) noexcept { field r; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.hpp index f88a0c19f26..b7d0a43712e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.hpp @@ -5,16 +5,16 @@ #include #include -namespace barretenberg { -namespace group_elements { +namespace barretenberg::group_elements { template class alignas(64) affine_element { public: - typedef uint8_t const* in_buf; - typedef uint8_t const* vec_in_buf; - typedef uint8_t* out_buf; - typedef uint8_t** vec_out_buf; + using in_buf = const uint8_t*; + using vec_in_buf = const uint8_t*; + using out_buf = uint8_t*; + using vec_out_buf = uint8_t**; - affine_element() noexcept {} + affine_element() noexcept = default; + ~affine_element() noexcept = default; constexpr affine_element(const Fq& a, const Fq& b) noexcept; @@ -58,15 +58,15 @@ template class alignas(64) affine_el template > 255) == uint256_t(0), void>> - constexpr uint256_t compress() const noexcept; + [[nodiscard]] constexpr uint256_t compress() const noexcept; static affine_element infinity(); constexpr affine_element set_infinity() const noexcept; constexpr void self_set_infinity() noexcept; - constexpr bool is_point_at_infinity() const noexcept; + [[nodiscard]] constexpr bool is_point_at_infinity() const noexcept; - constexpr bool on_curve() const noexcept; + [[nodiscard]] constexpr bool on_curve() const noexcept; /** * @brief Samples a random point on the curve. @@ -75,12 +75,18 @@ template class alignas(64) affine_el */ static affine_element random_element(numeric::random::Engine* engine = nullptr) noexcept; + static std::optional derive_from_x_coordinate(const Fq& x, bool sign_bit) noexcept; + /** * @brief Hash a seed value to curve. * * @return A point on the curve corresponding to the given seed */ - static affine_element hash_to_curve(const uint64_t seed) noexcept; + template > + static affine_element hash_to_curve(uint64_t seed) noexcept; + + template > + static affine_element hash_to_curve(const std::vector& seed) noexcept; constexpr bool operator==(const affine_element& other) const noexcept; @@ -131,7 +137,7 @@ template class alignas(64) affine_el affine_element result; // need to read a raw uint256_t to avoid reductions so we can check whether the point is the point at infinity - uint256_t raw_x = from_buffer(buffer + sizeof(Fq)); + auto raw_x = from_buffer(buffer + sizeof(Fq)); if constexpr (Fq::modulus.get_msb() == 255) { if (raw_x == Fq::modulus) { @@ -168,7 +174,7 @@ template class alignas(64) affine_el * * @return Vector with serialized representation of the point */ - inline std::vector to_buffer() const + [[nodiscard]] inline std::vector to_buffer() const { std::vector buffer(sizeof(affine_element)); affine_element::serialize_to_buffer(*this, &buffer[0]); @@ -185,7 +191,6 @@ template class alignas(64) affine_el // for serialization: update with new fields MSGPACK_FIELDS(x, y); }; -} // namespace group_elements -} // namespace barretenberg +} // namespace barretenberg::group_elements #include "./affine_element_impl.hpp" diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp index 16ed91083c5..dfb4e8e4b85 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element.test.cpp @@ -7,8 +7,8 @@ #include "barretenberg/serialize/test_helper.hpp" #include -namespace test_affine_element { -template class test_affine_element : public testing::Test { +namespace TestAffineElement { +template class TestAffineElement : public testing::Test { using element = typename G1::element; using affine_element = typename G1::affine_element; @@ -77,7 +77,7 @@ template class test_affine_element : public testing::Test { // Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie // on the curve, depending on the y-coordinate. - // TODO: add corresponding typed test class + // TODO(@Rumata888): add corresponding typed test class static void test_infinity_regression() { affine_element P; @@ -87,16 +87,16 @@ template class test_affine_element : public testing::Test { } }; -typedef testing::Types TestTypes; +using TestTypes = testing::Types; -TYPED_TEST_SUITE(test_affine_element, TestTypes); +TYPED_TEST_SUITE(TestAffineElement, TestTypes); -TYPED_TEST(test_affine_element, read_write_buffer) +TYPED_TEST(TestAffineElement, ReadWriteBuffer) { TestFixture::test_read_write_buffer(); } -TYPED_TEST(test_affine_element, point_compression) +TYPED_TEST(TestAffineElement, PointCompression) { if constexpr (TypeParam::Fq::modulus.data[3] >= 0x4000000000000000ULL) { GTEST_SKIP(); @@ -105,7 +105,7 @@ TYPED_TEST(test_affine_element, point_compression) } } -TYPED_TEST(test_affine_element, point_compression_unsafe) +TYPED_TEST(TestAffineElement, PointCompressionUnsafe) { if constexpr (TypeParam::Fq::modulus.data[3] >= 0x4000000000000000ULL) { TestFixture::test_point_compression_unsafe(); @@ -116,17 +116,18 @@ TYPED_TEST(test_affine_element, point_compression_unsafe) // Regression test to ensure that the point at infinity is not equal to its coordinate-wise reduction, which may lie // on the curve, depending on the y-coordinate. -TEST(affine_element, infinity_ordering_regression) +TEST(AffineElement, InfinityOrderingRegression) { - secp256k1::g1::affine_element P(0, 1), Q(0, 1); + secp256k1::g1::affine_element P(0, 1); + secp256k1::g1::affine_element Q(0, 1); P.self_set_infinity(); EXPECT_NE(P < Q, Q < P); } -TEST(affine_element, msgpack) +TEST(AffineElement, Msgpack) { auto [actual, expected] = msgpack_roundtrip(secp256k1::g1::affine_element{ 1, 1 }); EXPECT_EQ(actual, expected); } -} // namespace test_affine_element +} // namespace TestAffineElement diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element_impl.hpp index a23d5a37751..debf4a41d74 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/affine_element_impl.hpp @@ -1,9 +1,9 @@ #pragma once #include "./element.hpp" #include "barretenberg/crypto/keccak/keccak.hpp" +#include "barretenberg/crypto/sha256/sha256.hpp" -namespace barretenberg { -namespace group_elements { +namespace barretenberg::group_elements { template constexpr affine_element::affine_element(const Fq& a, const Fq& b) noexcept : x(a) @@ -83,6 +83,9 @@ constexpr affine_element affine_element::operator+( template constexpr affine_element& affine_element::operator=(const affine_element& other) noexcept { + if (this == &other) { + return *this; + } x = other.x; y = other.y; return *this; @@ -183,25 +186,28 @@ constexpr bool affine_element::operator>(const affine_element& other) // We are setting point at infinity to always be the lowest element if (is_point_at_infinity()) { return false; - } else if (other.is_point_at_infinity()) { + } + if (other.is_point_at_infinity()) { return true; } if (x > other.x) { return true; - } else if (x == other.x && y > other.y) { + } + if (x == other.x && y > other.y) { return true; } return false; } template -affine_element affine_element::hash_to_curve(const uint64_t seed) noexcept +template +affine_element affine_element::hash_to_curve(uint64_t seed) noexcept { - static_assert(T::can_hash_to_curve == true); + static_assert(static_cast(T::can_hash_to_curve)); Fq input(seed, 0, 0, 0); - keccak256 c = hash_field_element((uint64_t*)&input.data[0]); + keccak256 c = hash_field_element(&input.data[0]); uint256_t hash{ c.word64s[0], c.word64s[1], c.word64s[2], c.word64s[3] }; uint256_t x_coordinate = hash; @@ -252,7 +258,7 @@ affine_element affine_element::random_element(numeric::ran y = y1; // Negate the y-coordinate based on a randomly sampled bit. - bool random_bit = (engine->get_random_uint8() & 1); + bool random_bit = (engine->get_random_uint8() & 1) != 0; if (random_bit) { y = -y; } @@ -262,5 +268,4 @@ affine_element affine_element::random_element(numeric::ran return affine_element(x, y); } -} // namespace group_elements -} // namespace barretenberg +} // namespace barretenberg::group_elements diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element.hpp index c04bb0fce36..3eba4298afb 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element.hpp @@ -10,8 +10,7 @@ #include #include -namespace barretenberg { -namespace group_elements { +namespace barretenberg::group_elements { /** * @brief element class. Implements ecc group arithmetic using Jacobian coordinates @@ -29,12 +28,13 @@ template class alignas(32) element { public: static constexpr Fq curve_b = Params::b; - element() noexcept {} + element() noexcept = default; constexpr element(const Fq& a, const Fq& b, const Fq& c) noexcept; constexpr element(const element& other) noexcept; constexpr element(element&& other) noexcept; constexpr element(const affine_element& other) noexcept; + constexpr ~element() noexcept = default; static constexpr element one() noexcept { return { Params::one_x, Params::one_y, Fq::one() }; }; static constexpr element zero() noexcept @@ -53,8 +53,7 @@ template class alignas(32) element { constexpr element dbl() const noexcept; constexpr void self_dbl() noexcept; - constexpr void self_mixed_add_or_sub(const affine_element& other, - const uint64_t predicate) noexcept; + constexpr void self_mixed_add_or_sub(const affine_element& other, uint64_t predicate) noexcept; constexpr element operator+(const element& other) const noexcept; constexpr element operator+(const affine_element& other) const noexcept; @@ -76,8 +75,8 @@ template class alignas(32) element { return -right + left; } - element operator*(const Fr& other) const noexcept; - element operator*=(const Fr& other) noexcept; + element operator*(const Fr& exponent) const noexcept; + element operator*=(const Fr& exponent) noexcept; // If you end up implementing this, congrats, you've solved the DL problem! // P.S. This is a joke, don't even attempt! 😂 @@ -87,11 +86,11 @@ template class alignas(32) element { static element infinity(); BBERG_INLINE constexpr element set_infinity() const noexcept; BBERG_INLINE constexpr void self_set_infinity() noexcept; - BBERG_INLINE constexpr bool is_point_at_infinity() const noexcept; - BBERG_INLINE constexpr bool on_curve() const noexcept; + [[nodiscard]] BBERG_INLINE constexpr bool is_point_at_infinity() const noexcept; + [[nodiscard]] BBERG_INLINE constexpr bool on_curve() const noexcept; BBERG_INLINE constexpr bool operator==(const element& other) const noexcept; - static void batch_normalize(element* elements, const size_t num_elements) noexcept; + static void batch_normalize(element* elements, size_t num_elements) noexcept; static std::vector> batch_mul_with_endomorphism( const std::vector>& points, const Fr& exponent) noexcept; @@ -128,7 +127,7 @@ template class alignas(32) element { static void conditional_negate_affine(const affine_element& in, affine_element& out, - const uint64_t predicate) noexcept; + uint64_t predicate) noexcept; friend std::ostream& operator<<(std::ostream& os, const element& a) { @@ -145,8 +144,7 @@ template std::ostream& operator<<(std::ostrea // constexpr element::one = element{ Params::one_x, Params::one_y, Fq::one() }; // constexpr element::point_at_infinity = one.set_infinity(); // constexpr element::curve_b = Params::b; -} // namespace group_elements -} // namespace barretenberg +} // namespace barretenberg::group_elements #include "./element_impl.hpp" diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element_impl.hpp index 60f2faef284..b6483aa5776 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/element_impl.hpp @@ -1,7 +1,8 @@ #pragma once +#include "barretenberg/ecc/groups/element.hpp" -namespace barretenberg { -namespace group_elements { +// NOLINTBEGIN(readability-implicit-bool-conversion, cppcoreguidelines-avoid-c-arrays) +namespace barretenberg::group_elements { template constexpr element::element(const Fq& a, const Fq& b, const Fq& c) noexcept : x(a) @@ -33,6 +34,9 @@ constexpr element::element(const affine_element& other) no template constexpr element& element::operator=(const element& other) noexcept { + if (this == &other) { + return *this; + } x = other.x; y = other.y; z = other.z; @@ -149,7 +153,7 @@ constexpr void element::self_mixed_add_or_sub(const affine_element= 0x4000000000000000ULL) { if (is_point_at_infinity()) { - conditional_negate_affine(other, *(affine_element*)this, predicate); + conditional_negate_affine(other, *(affine_element*)this, predicate); // NOLINT z = Fq::one(); return; } @@ -157,7 +161,7 @@ constexpr void element::self_mixed_add_or_sub(const affine_element*)this, predicate); + conditional_negate_affine(other, *(affine_element*)this, predicate); // NOLINT z = Fq::one(); } return; @@ -183,10 +187,9 @@ constexpr void element::self_mixed_add_or_sub(const affine_element element::operator+=(const affine_element if (T2.is_zero()) { self_dbl(); return *this; - } else { - self_set_infinity(); - return *this; } + self_set_infinity(); + return *this; } // T2 = 2T2 = 2(y2.z1.z1.z1 - y1) = R @@ -397,10 +399,9 @@ constexpr element element::operator+=(const element& other if (F.is_zero()) { self_dbl(); return *this; - } else { - self_set_infinity(); - return *this; } + self_set_infinity(); + return *this; } F += F; @@ -636,7 +637,7 @@ element element::mul_with_endomorphism(const Fr& exponent) uint64_t wnaf_table[num_rounds * 2]; Fr endo_scalar; - Fr::split_into_endomorphism_scalars(converted_scalar, endo_scalar, *(Fr*)&endo_scalar.data[2]); + Fr::split_into_endomorphism_scalars(converted_scalar, endo_scalar, *(Fr*)&endo_scalar.data[2]); // NOLINT bool skew = false; bool endo_skew = false; @@ -647,9 +648,9 @@ element element::mul_with_endomorphism(const Fr& exponent) element work_element{ T::one_x, T::one_y, Fq::one() }; work_element.self_set_infinity(); - uint64_t wnaf_entry; - uint64_t index; - bool sign; + uint64_t wnaf_entry = 0; + uint64_t index = 0; + bool sign = false; Fq beta = Fq::cube_root_of_unity(); for (size_t i = 0; i < num_rounds * 2; ++i) { @@ -782,7 +783,7 @@ std::vector> element::batch_mul_with_endomo uint64_t wnaf_table[num_rounds * 2]; Fr endo_scalar; - Fr::split_into_endomorphism_scalars(converted_scalar, endo_scalar, *(Fr*)&endo_scalar.data[2]); + Fr::split_into_endomorphism_scalars(converted_scalar, endo_scalar, *(Fr*)&endo_scalar.data[2]); // NOLINT bool skew = false; bool endo_skew = false; @@ -792,9 +793,9 @@ std::vector> element::batch_mul_with_endomo std::vector work_elements(num_points); - uint64_t wnaf_entry; - uint64_t index; - bool sign; + uint64_t wnaf_entry = 0; + uint64_t index = 0; + bool sign = 0; Fq beta = Fq::cube_root_of_unity(); for (size_t i = 0; i < 2; ++i) { @@ -857,11 +858,11 @@ std::vector> element::batch_mul_with_endomo } template -void element::conditional_negate_affine(const affine_element& src, - affine_element& dest, +void element::conditional_negate_affine(const affine_element& in, + affine_element& out, const uint64_t predicate) noexcept { - dest = { src.x, predicate ? -src.y : src.y }; + out = { in.x, predicate ? -in.y : in.y }; } template @@ -938,5 +939,5 @@ element element::random_coordinates_on_curve(numeric::rand return { x, y, Fq::one() }; } -} // namespace group_elements -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg::group_elements +// NOLINTEND(readability-implicit-bool-conversion, cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group.hpp index 3f59987d8e2..6d4fdbc550e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group.hpp @@ -30,10 +30,10 @@ template element; - typedef group_elements::affine_element affine_element; - typedef coordinate_field Fq; - typedef subgroup_field Fr; + using element = group_elements::element; + using affine_element = group_elements::affine_element; + using Fq = coordinate_field; + using Fr = subgroup_field; static constexpr bool USE_ENDOMORPHISM = GroupParams::USE_ENDOMORPHISM; static constexpr bool has_a = GroupParams::has_a; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_asm.tcc b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_asm.tcc index 73a6374b950..48b690caf3c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_asm.tcc +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_asm.tcc @@ -1,5 +1,8 @@ #pragma once +#ifndef DISABLE_SHENANIGANS + +#include "barretenberg/ecc/groups/group.hpp" #include namespace barretenberg { @@ -145,7 +148,7 @@ inline void group::conditional_ne : "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", "memory", "cc"); #endif } else { - if (predicate) { + if (predicate) { // NOLINT coordinate_field::__copy(src->x, dest->x); dest->y = -src->y; } else { @@ -154,4 +157,6 @@ inline void group::conditional_ne } } -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg + +#endif \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_int128.tcc b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_int128.tcc index 8010b3df916..49453e640f9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_int128.tcc +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/group_impl_int128.tcc @@ -1,5 +1,8 @@ #pragma once +#ifdef DISABLE_SHENANIGANS + +#include "barretenberg/ecc/groups/group.hpp" #include namespace barretenberg { @@ -26,4 +29,6 @@ inline void group::conditional_ne { *dest = predicate ? -(*src) : (*src); } -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg + +#endif \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.hpp index a7b3bacb908..c6dbee10ead 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.hpp @@ -3,11 +3,11 @@ #include #include -namespace barretenberg { -namespace wnaf { +// NOLINTBEGIN(readability-implicit-bool-conversion) +namespace barretenberg::wnaf { constexpr size_t SCALAR_BITS = 127; -#define WNAF_SIZE(x) ((barretenberg::wnaf::SCALAR_BITS + x - 1) / (x)) +#define WNAF_SIZE(x) ((barretenberg::wnaf::SCALAR_BITS + (x)-1) / (x)) // NOLINT(cppcoreguidelines-macro-usage) constexpr size_t get_optimal_bucket_width(const size_t num_points) { @@ -119,8 +119,8 @@ inline uint64_t get_wnaf_bits(const uint64_t* scalar, const uint64_t bits, const * get high limb and shift left by (64 - (bit_position & 63)) * */ - const size_t lo_limb_idx = static_cast(bit_position >> 6); - const size_t hi_limb_idx = static_cast((bit_position + bits - 1) >> 6); + const auto lo_limb_idx = static_cast(bit_position >> 6); + const auto hi_limb_idx = static_cast((bit_position + bits - 1) >> 6); const uint64_t lo_shift = bit_position & 63UL; const uint64_t bit_mask = (1UL << static_cast(bits)) - 1UL; @@ -136,7 +136,7 @@ inline void fixed_wnaf_packed( const uint64_t* scalar, uint64_t* wnaf, bool& skew_map, const uint64_t point_index, const size_t wnaf_bits) noexcept { skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + (uint64_t)skew_map; + uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + static_cast(skew_map); const size_t wnaf_entries = (SCALAR_BITS + wnaf_bits - 1) / wnaf_bits; for (size_t round_i = 1; round_i < wnaf_entries - 1; ++round_i) { @@ -164,7 +164,7 @@ inline void fixed_wnaf(const uint64_t* scalar, const size_t wnaf_bits) noexcept { skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + (uint64_t)skew_map; + uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + static_cast(skew_map); const size_t wnaf_entries = (SCALAR_BITS + wnaf_bits - 1) / wnaf_bits; for (size_t round_i = 1; round_i < wnaf_entries - 1; ++round_i) { @@ -268,10 +268,10 @@ inline void fixed_wnaf_with_counts(const uint64_t* scalar, } return; } - const size_t current_scalar_bits = static_cast(get_num_scalar_bits(scalar) + 1); + const auto current_scalar_bits = static_cast(get_num_scalar_bits(scalar) + 1); skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + (uint64_t)skew_map; - const size_t wnaf_entries = static_cast((current_scalar_bits + wnaf_bits - 1) / wnaf_bits); + uint64_t previous = get_wnaf_bits(scalar, wnaf_bits, 0) + static_cast(skew_map); + const auto wnaf_entries = static_cast((current_scalar_bits + wnaf_bits - 1) / wnaf_bits); if (wnaf_entries == 1) { wnaf[(max_wnaf_entries - 1) * num_points] = (previous >> 1UL) | (point_index); @@ -306,7 +306,7 @@ inline void fixed_wnaf_with_counts(const uint64_t* scalar, previous = slice + predicate; } // The final iteration for top bits - size_t final_bits = static_cast(current_scalar_bits - (wnaf_bits * (wnaf_entries - 1))); + auto final_bits = static_cast(current_scalar_bits - (wnaf_bits * (wnaf_entries - 1))); uint64_t slice = get_wnaf_bits(scalar, final_bits, (wnaf_entries - 1) * wnaf_bits); uint64_t predicate = ((slice & 1UL) == 0UL); @@ -329,7 +329,7 @@ template inline void wnaf_round(uint64_t* scalar, uint64_t* wnaf, const uint64_t point_index, const uint64_t previous) noexcept { constexpr size_t wnaf_entries = (SCALAR_BITS + wnaf_bits - 1) / wnaf_bits; - constexpr size_t log2_num_points = static_cast(numeric::get_msb(static_cast(num_points))); + constexpr auto log2_num_points = static_cast(numeric::get_msb(static_cast(num_points))); if constexpr (round_i < wnaf_entries - 1) { uint64_t slice = get_wnaf_bits(scalar, wnaf_bits, round_i * wnaf_bits); @@ -354,7 +354,7 @@ template (numeric::get_msb(static_cast(num_points))); + constexpr auto log2_num_points = static_cast(numeric::get_msb(static_cast(num_points))); if constexpr (round_i < wnaf_entries - 1) { uint64_t slice = get_wnaf_bits_const(scalar); @@ -409,7 +409,7 @@ template inline void fixed_wnaf(uint64_t* scalar, uint64_t* wnaf, bool& skew_map, const size_t point_index) noexcept { skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits_const(scalar) + (uint64_t)skew_map; + uint64_t previous = get_wnaf_bits_const(scalar) + static_cast(skew_map); wnaf_round(scalar, wnaf, point_index, previous); } @@ -417,7 +417,7 @@ template inline void fixed_wnaf(uint64_t* scalar, uint64_t* wnaf, bool& skew_map, const size_t point_index) noexcept { skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits_const(scalar) + (uint64_t)skew_map; + uint64_t previous = get_wnaf_bits_const(scalar) + static_cast(skew_map); wnaf_round(scalar, wnaf, point_index, previous); } @@ -428,7 +428,7 @@ inline void wnaf_round_with_restricted_first_slice(uint64_t* scalar, const uint64_t previous) noexcept { constexpr size_t wnaf_entries = (scalar_bits + wnaf_bits - 1) / wnaf_bits; - constexpr size_t log2_num_points = static_cast(numeric::get_msb(static_cast(num_points))); + constexpr auto log2_num_points = static_cast(numeric::get_msb(static_cast(num_points))); constexpr size_t bits_in_first_slice = scalar_bits % wnaf_bits; if constexpr (round_i == 1) { uint64_t slice = get_wnaf_bits_const(scalar); @@ -472,7 +472,7 @@ inline void fixed_wnaf_with_restricted_first_slice(uint64_t* scalar, constexpr size_t bits_in_first_slice = num_bits % wnaf_bits; std::cerr << "bits in first slice = " << bits_in_first_slice << std::endl; skew_map = ((scalar[0] & 1) == 0); - uint64_t previous = get_wnaf_bits_const(scalar) + (uint64_t)skew_map; + uint64_t previous = get_wnaf_bits_const(scalar) + static_cast(skew_map); std::cerr << "previous = " << previous << std::endl; wnaf_round_with_restricted_first_slice(scalar, wnaf, point_index, previous); } @@ -495,5 +495,6 @@ inline void fixed_wnaf_with_restricted_first_slice(uint64_t* scalar, // uint64_t previous = get_wnaf_bits_const(scalar) + (uint64_t)skew_map; // std::array result; // } -} // namespace wnaf -} // namespace barretenberg +} // namespace barretenberg::wnaf + +// NOLINTEND(readability-implicit-bool-conversion) \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.test.cpp index ec9cc198d36..3011920ecce 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/groups/wnaf.test.cpp @@ -9,36 +9,39 @@ namespace { auto& engine = numeric::random::get_debug_engine(); } +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays) namespace { -void recover_fixed_wnaf(uint64_t* wnaf, bool skew, uint64_t& hi, uint64_t& lo, size_t wnaf_bits) +void recover_fixed_wnaf(const uint64_t* wnaf, bool skew, uint64_t& hi, uint64_t& lo, size_t wnaf_bits) { size_t wnaf_entries = (127 + wnaf_bits - 1) / wnaf_bits; uint128_t scalar = 0; // (uint128_t)(skew); - for (int i = (int)0; i < (int)wnaf_entries; ++i) { - uint64_t entry_formatted = wnaf[(size_t)i]; - bool negative = entry_formatted >> 31; + for (int i = 0; i < static_cast(wnaf_entries); ++i) { + uint64_t entry_formatted = wnaf[static_cast(i)]; + bool negative = (entry_formatted >> 31) != 0U; uint64_t entry = ((entry_formatted & 0x0fffffffU) << 1) + 1; if (negative) { - scalar -= (uint128_t)((uint128_t)entry) << (uint128_t)(wnaf_bits * (wnaf_entries - 1 - (size_t)i)); + scalar -= (static_cast(entry)) + << static_cast(wnaf_bits * (wnaf_entries - 1 - static_cast(i))); } else { - scalar += (uint128_t)((uint128_t)entry) << (uint128_t)(wnaf_bits * (wnaf_entries - 1 - (size_t)i)); + scalar += (static_cast(entry)) + << static_cast(wnaf_bits * (wnaf_entries - 1 - static_cast(i))); } } - scalar -= (uint128_t)(skew); - hi = (uint64_t)(uint128_t)(scalar >> (uint128_t)(64)); - lo = (uint64_t)(uint128_t)(scalar & (uint128_t)0xffff'ffff'ffff'ffff); + scalar -= static_cast(skew); + hi = static_cast(scalar >> static_cast(64)); + lo = static_cast(static_cast(scalar & static_cast(0xffff'ffff'ffff'ffff))); } } // namespace -TEST(wnaf, wnaf_zero) +TEST(wnaf, WnafZero) { uint64_t buffer[2]{ 0, 0 }; uint64_t wnaf[WNAF_SIZE(5)] = { 0 }; bool skew = false; wnaf::fixed_wnaf<1, 5>(buffer, wnaf, skew, 0); - uint64_t recovered_hi; - uint64_t recovered_lo; + uint64_t recovered_hi = 0; + uint64_t recovered_lo = 0; recover_fixed_wnaf(wnaf, skew, recovered_hi, recovered_lo, 5); EXPECT_EQ(recovered_lo, 0UL); EXPECT_EQ(recovered_hi, 0UL); @@ -46,7 +49,7 @@ TEST(wnaf, wnaf_zero) EXPECT_EQ(buffer[1], recovered_hi); } -TEST(wnaf, wnaf_two_bit_window) +TEST(wnaf, WnafTwoBitWindow) { /** * We compute the 2-bit windowed NAF form of `input`. @@ -85,62 +88,62 @@ TEST(wnaf, wnaf_two_bit_window) */ uint256_t recovered = 0; uint256_t four_power = (uint256_t(1) << num_bits); - for (size_t i = 0; i < num_quads; i++) { - int extracted = 2 * (int(wnaf[i]) & 1) + 1; - bool sign = (wnaf[i] >> 31) == 0; + for (uint64_t i : wnaf) { + int extracted = 2 * (static_cast(i) & 1) + 1; + bool sign = (i >> 31) == 0; if (sign) { - recovered += uint256_t(uint64_t(extracted)) * four_power; + recovered += uint256_t(static_cast(extracted)) * four_power; } else { - recovered -= uint256_t(uint64_t(extracted)) * four_power; + recovered -= uint256_t(static_cast(extracted)) * four_power; } four_power >>= 2; } - recovered -= skew; + recovered -= static_cast(skew); EXPECT_EQ(recovered, input); } -TEST(wnaf, wnaf_fixed) +TEST(wnaf, WnafFixed) { uint256_t buffer = engine.get_random_uint256(); buffer.data[1] &= 0x7fffffffffffffffUL; uint64_t wnaf[WNAF_SIZE(5)] = { 0 }; bool skew = false; wnaf::fixed_wnaf<1, 5>(&buffer.data[0], wnaf, skew, 0); - uint64_t recovered_hi; - uint64_t recovered_lo; + uint64_t recovered_hi = 0; + uint64_t recovered_lo = 0; recover_fixed_wnaf(wnaf, skew, recovered_hi, recovered_lo, 5); EXPECT_EQ(buffer.data[0], recovered_lo); EXPECT_EQ(buffer.data[1], recovered_hi); } -TEST(wnaf, wnaf_fixed_simple_lo) +TEST(wnaf, WnafFixedSimpleLo) { uint64_t rand_buffer[2]{ 1, 0 }; uint64_t wnaf[WNAF_SIZE(5)]{ 0 }; bool skew = false; wnaf::fixed_wnaf<1, 5>(rand_buffer, wnaf, skew, 0); - uint64_t recovered_hi; - uint64_t recovered_lo; + uint64_t recovered_hi = 0; + uint64_t recovered_lo = 0; recover_fixed_wnaf(wnaf, skew, recovered_hi, recovered_lo, 5); EXPECT_EQ(rand_buffer[0], recovered_lo); EXPECT_EQ(rand_buffer[1], recovered_hi); } -TEST(wnaf, wnaf_fixed_simple_hi) +TEST(wnaf, WnafFixedSimpleHi) { uint64_t rand_buffer[2] = { 0, 1 }; uint64_t wnaf[WNAF_SIZE(5)] = { 0 }; bool skew = false; wnaf::fixed_wnaf<1, 5>(rand_buffer, wnaf, skew, 0); - uint64_t recovered_hi; - uint64_t recovered_lo; + uint64_t recovered_hi = 0; + uint64_t recovered_lo = 0; recover_fixed_wnaf(wnaf, skew, recovered_hi, recovered_lo, 5); EXPECT_EQ(rand_buffer[0], recovered_lo); EXPECT_EQ(rand_buffer[1], recovered_hi); } -TEST(wnaf, wnaf_fixed_with_endo_split) +TEST(wnaf, WnafFixedWithEndoSplit) { fr k = engine.get_random_uint256(); k.data[3] &= 0x0fffffffffffffffUL; @@ -169,3 +172,5 @@ TEST(wnaf, wnaf_fixed_with_endo_split) EXPECT_EQ(result, k); } + +// NOLINTEND(cppcoreguidelines-avoid-c-arrays) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/point_table.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/point_table.hpp index 0e51261d3bf..2dc1fb76add 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/point_table.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/point_table.hpp @@ -4,8 +4,7 @@ #include "barretenberg/ecc/curves/bn254/g1.hpp" #include -namespace barretenberg { -namespace scalar_multiplication { +namespace barretenberg::scalar_multiplication { inline size_t point_table_size(size_t num_points) { @@ -26,5 +25,4 @@ template inline std::shared_ptr point_table_alloc(size_t num_p return std::static_pointer_cast(get_mem_slab(point_table_buf_size(num_points))); } -} // namespace scalar_multiplication -} // namespace barretenberg +} // namespace barretenberg::scalar_multiplication diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.cpp index 01f92b8673d..e96132b2946 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.cpp @@ -2,8 +2,9 @@ #include -namespace barretenberg { -namespace scalar_multiplication { +namespace barretenberg::scalar_multiplication { + +// NOLINTNEXTLINE(misc-no-recursion) recursion is fine here, max recursion depth is 8 (64 bit int / 8 bits per call) void radix_sort(uint64_t* keys, const size_t num_entries, const uint32_t shift) noexcept { constexpr size_t num_bits = 8; @@ -60,5 +61,4 @@ void process_buckets(uint64_t* wnaf_entries, const size_t num_entries, const uin radix_sort(wnaf_entries, num_entries, shift); } -} // namespace scalar_multiplication -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg::scalar_multiplication diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.hpp index bde5916663b..f2bbd415569 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/process_buckets.hpp @@ -3,10 +3,8 @@ #include #include -namespace barretenberg { -namespace scalar_multiplication { -void radix_sort(uint64_t* keys, const size_t num_entries, const uint32_t shift) noexcept; +namespace barretenberg::scalar_multiplication { +void radix_sort(uint64_t* keys, size_t num_entries, uint32_t shift) noexcept; -void process_buckets(uint64_t* wnaf_entries, const size_t num_entries, const uint32_t num_bits) noexcept; -} // namespace scalar_multiplication -} // namespace barretenberg \ No newline at end of file +void process_buckets(uint64_t* wnaf_entries, size_t num_entries, uint32_t num_bits) noexcept; +} // namespace barretenberg::scalar_multiplication \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.cpp index cb7cde0bfe4..e9ab38acf08 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.cpp @@ -5,78 +5,97 @@ #include "barretenberg/common/thread.hpp" #include "barretenberg/numeric/bitop/get_msb.hpp" -namespace barretenberg { -namespace scalar_multiplication { +// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) +namespace barretenberg::scalar_multiplication { -template pippenger_runtime_state::pippenger_runtime_state(const size_t num_initial_points) +size_t get_num_pippenger_rounds(const size_t num_points) +{ + const auto num_points_floor = static_cast(1ULL << (numeric::get_msb(num_points))); + const auto num_rounds = + static_cast(barretenberg::scalar_multiplication::get_num_rounds(static_cast(num_points_floor))); + return num_rounds; +} +template +pippenger_runtime_state::pippenger_runtime_state(const size_t num_initial_points) noexcept + : num_points(num_initial_points * 2) + , num_buckets(static_cast(1ULL << barretenberg::scalar_multiplication::get_optimal_bucket_width( + static_cast(num_initial_points)))) + , num_rounds(get_num_pippenger_rounds(static_cast(num_points))) + , num_threads(get_num_cpus_pow2()) + , prefetch_overflow(num_threads * 16) + , point_schedule_ptr( + get_mem_slab((static_cast(num_points) * num_rounds + prefetch_overflow) * sizeof(uint64_t))) + , point_pairs_1_ptr( + get_mem_slab((static_cast(num_points) * 2 + (num_threads * 16)) * sizeof(AffineElement))) + , point_pairs_2_ptr( + get_mem_slab((static_cast(num_points) * 2 + (num_threads * 16)) * sizeof(AffineElement))) + , scratch_space_ptr(get_mem_slab(static_cast(num_points) * sizeof(AffineElement))) + , point_schedule(reinterpret_cast(point_schedule_ptr.get())) + , point_pairs_1(reinterpret_cast(point_pairs_1_ptr.get())) + , point_pairs_2(reinterpret_cast(point_pairs_2_ptr.get())) + , scratch_space(reinterpret_cast(scratch_space_ptr.get())) + , skew_table(reinterpret_cast(aligned_alloc(64, pad(static_cast(num_points) * sizeof(bool), 64)))) + , bucket_counts(reinterpret_cast(aligned_alloc(64, num_threads * num_buckets * sizeof(uint32_t)))) + , bit_counts(reinterpret_cast(aligned_alloc(64, num_threads * num_buckets * sizeof(uint32_t)))) + , bucket_empty_status(reinterpret_cast(aligned_alloc(64, num_threads * num_buckets * sizeof(bool)))) + , round_counts(reinterpret_cast(aligned_alloc(32, MAX_NUM_ROUNDS * sizeof(uint64_t)))) { using Fq = typename Curve::BaseField; using AffineElement = typename Curve::AffineElement; - constexpr size_t MAX_NUM_ROUNDS = 256; - num_points = num_initial_points * 2; - const size_t num_points_floor = static_cast(1ULL << (numeric::get_msb(num_points))); - const size_t num_buckets = static_cast( - 1U << barretenberg::scalar_multiplication::get_optimal_bucket_width(static_cast(num_initial_points))); - const size_t num_threads = get_num_cpus_pow2(); - const size_t prefetch_overflow = 16 * num_threads; - const size_t num_rounds = + const auto num_points_floor = static_cast(1ULL << (numeric::get_msb(num_points))); + const auto num_buckets = static_cast( + 1ULL << barretenberg::scalar_multiplication::get_optimal_bucket_width(static_cast(num_initial_points))); + const auto num_rounds = static_cast(barretenberg::scalar_multiplication::get_num_rounds(static_cast(num_points_floor))); - point_schedule_ptr = - get_mem_slab((static_cast(num_points) * num_rounds + prefetch_overflow) * sizeof(uint64_t)); - point_pairs_1_ptr = - get_mem_slab((static_cast(num_points) * 2 + (num_threads * 16)) * sizeof(AffineElement)); - point_pairs_2_ptr = - get_mem_slab((static_cast(num_points) * 2 + (num_threads * 16)) * sizeof(AffineElement)); - scratch_space_ptr = get_mem_slab(static_cast(num_points) * sizeof(AffineElement)); - point_schedule = (uint64_t*)point_schedule_ptr.get(); - point_pairs_1 = (AffineElement*)point_pairs_1_ptr.get(); - point_pairs_2 = (AffineElement*)point_pairs_2_ptr.get(); - scratch_space = (Fq*)scratch_space_ptr.get(); - - skew_table = (bool*)(aligned_alloc(64, pad(static_cast(num_points) * sizeof(bool), 64))); - bucket_counts = (uint32_t*)(aligned_alloc(64, num_threads * num_buckets * sizeof(uint32_t))); - bit_counts = (uint32_t*)(aligned_alloc(64, num_threads * num_buckets * sizeof(uint32_t))); - bucket_empty_status = (bool*)(aligned_alloc(64, num_threads * num_buckets * sizeof(bool))); - round_counts = (uint64_t*)(aligned_alloc(32, MAX_NUM_ROUNDS * sizeof(uint64_t))); - const size_t points_per_thread = static_cast(num_points) / num_threads; parallel_for(num_threads, [&](size_t i) { const size_t thread_offset = i * points_per_thread; - memset((void*)(point_pairs_1 + thread_offset + (i * 16)), 0, (points_per_thread + 16) * sizeof(AffineElement)); - memset((void*)(point_pairs_2 + thread_offset + (i * 16)), 0, (points_per_thread + 16) * sizeof(AffineElement)); - memset((void*)(scratch_space + thread_offset), 0, (points_per_thread) * sizeof(Fq)); + memset(reinterpret_cast(point_pairs_1 + thread_offset + (i * 16)), + 0, + (points_per_thread + 16) * sizeof(AffineElement)); + memset(reinterpret_cast(point_pairs_2 + thread_offset + (i * 16)), + 0, + (points_per_thread + 16) * sizeof(AffineElement)); + memset(reinterpret_cast(scratch_space + thread_offset), 0, (points_per_thread) * sizeof(Fq)); for (size_t j = 0; j < num_rounds; ++j) { const size_t round_offset = (j * static_cast(num_points)); - memset((void*)(point_schedule + round_offset + thread_offset), 0, points_per_thread * sizeof(uint64_t)); + memset(reinterpret_cast(point_schedule + round_offset + thread_offset), + 0, + points_per_thread * sizeof(uint64_t)); } - memset((void*)(skew_table + thread_offset), 0, points_per_thread * sizeof(bool)); + memset(reinterpret_cast(skew_table + thread_offset), 0, points_per_thread * sizeof(bool)); }); - memset((void*)bucket_counts, 0, num_threads * num_buckets * sizeof(uint32_t)); - memset((void*)bit_counts, 0, num_threads * num_buckets * sizeof(uint32_t)); - memset((void*)bucket_empty_status, 0, num_threads * num_buckets * sizeof(bool)); - memset((void*)round_counts, 0, MAX_NUM_ROUNDS * sizeof(uint64_t)); + memset(reinterpret_cast(bucket_counts), 0, num_threads * num_buckets * sizeof(uint32_t)); + memset(reinterpret_cast(bit_counts), 0, num_threads * num_buckets * sizeof(uint32_t)); + memset(reinterpret_cast(bucket_empty_status), 0, num_threads * num_buckets * sizeof(bool)); + memset(reinterpret_cast(round_counts), 0, MAX_NUM_ROUNDS * sizeof(uint64_t)); } -template pippenger_runtime_state::pippenger_runtime_state(pippenger_runtime_state&& other) -{ - point_schedule_ptr = std::move(other.point_schedule_ptr); - point_pairs_1_ptr = std::move(other.point_pairs_1_ptr); - point_pairs_2_ptr = std::move(other.point_pairs_2_ptr); - scratch_space_ptr = std::move(other.scratch_space_ptr); - - point_schedule = other.point_schedule; - skew_table = other.skew_table; - point_pairs_1 = other.point_pairs_1; - point_pairs_2 = other.point_pairs_2; - scratch_space = other.scratch_space; - bit_counts = other.bit_counts; - bucket_counts = other.bucket_counts; - bucket_empty_status = other.bucket_empty_status; - round_counts = other.round_counts; +template +pippenger_runtime_state::pippenger_runtime_state(pippenger_runtime_state&& other) noexcept + : num_points(other.num_points) + , num_buckets(other.num_buckets) + , num_rounds(other.num_rounds) + , num_threads(other.num_threads) + , prefetch_overflow(other.prefetch_overflow) + , point_schedule_ptr(std::move(other.point_schedule_ptr)) + , point_pairs_1_ptr(std::move(other.point_pairs_1_ptr)) + , point_pairs_2_ptr(std::move(other.point_pairs_2_ptr)) + , scratch_space_ptr(std::move(other.scratch_space_ptr)) + , point_schedule(other.point_schedule) + , point_pairs_1(other.point_pairs_1) + , point_pairs_2(other.point_pairs_2) + , scratch_space(other.scratch_space) + , skew_table(other.skew_table) + , bucket_counts(other.bucket_counts) + , bit_counts(other.bit_counts) + , bucket_empty_status(other.bucket_empty_status) + , round_counts(other.round_counts) +{ other.point_schedule = nullptr; other.skew_table = nullptr; other.point_pairs_1 = nullptr; @@ -86,30 +105,29 @@ template pippenger_runtime_state::pippenger_runtime_stat other.bucket_counts = nullptr; other.bucket_empty_status = nullptr; other.round_counts = nullptr; - - num_points = other.num_points; } template -pippenger_runtime_state& pippenger_runtime_state::operator=(pippenger_runtime_state&& other) +pippenger_runtime_state& pippenger_runtime_state::operator=( + pippenger_runtime_state&& other) noexcept { - if (skew_table) { + if (skew_table != nullptr) { aligned_free(skew_table); } - if (bit_counts) { + if (bit_counts != nullptr) { aligned_free(bit_counts); } - if (bucket_counts) { + if (bucket_counts != nullptr) { aligned_free(bucket_counts); } - if (bucket_empty_status) { + if (bucket_empty_status != nullptr) { aligned_free(bucket_empty_status); } - if (round_counts) { + if (round_counts != nullptr) { aligned_free(round_counts); } @@ -146,8 +164,8 @@ template affine_product_runtime_state pippenger_runtime_state::get_affine_product_runtime_state( const size_t num_threads, const size_t thread_index) { - const size_t points_per_thread = static_cast(num_points / num_threads); - const size_t num_buckets = + const auto points_per_thread = static_cast(num_points / num_threads); + const auto num_buckets = static_cast(1U << scalar_multiplication::get_optimal_bucket_width(static_cast(num_points) / 2)); scalar_multiplication::affine_product_runtime_state product_state; @@ -161,25 +179,25 @@ affine_product_runtime_state pippenger_runtime_state::get_affine_p return product_state; } -template pippenger_runtime_state::~pippenger_runtime_state() +template pippenger_runtime_state::~pippenger_runtime_state() noexcept { - if (skew_table) { + if (skew_table != nullptr) { aligned_free(skew_table); } - if (bit_counts) { + if (bit_counts != nullptr) { aligned_free(bit_counts); } - if (bucket_counts) { + if (bucket_counts != nullptr) { aligned_free(bucket_counts); } - if (bucket_empty_status) { + if (bucket_empty_status != nullptr) { aligned_free(bucket_empty_status); } - if (round_counts) { + if (round_counts != nullptr) { aligned_free(round_counts); } } @@ -188,5 +206,6 @@ template struct affine_product_runtime_state; template struct affine_product_runtime_state; template struct pippenger_runtime_state; template struct pippenger_runtime_state; -} // namespace scalar_multiplication -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg::scalar_multiplication + +// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.hpp index a7e6fdd01e0..6935846ed0b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/runtime_states.hpp @@ -4,10 +4,9 @@ #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" #include "barretenberg/ecc/groups/wnaf.hpp" -namespace barretenberg { +namespace barretenberg::scalar_multiplication { // simple helper functions to retrieve pointers to pre-allocated memory for the scalar multiplication algorithm. // This is to eliminate page faults when allocating (and writing) to large tranches of memory. -namespace scalar_multiplication { constexpr size_t get_optimal_bucket_width(const size_t num_points) { if (num_points >= 14617149) { @@ -78,6 +77,15 @@ template struct affine_product_runtime_state { }; template struct pippenger_runtime_state { + using Fq = typename Curve::BaseField; + using AffineElement = typename Curve::AffineElement; + + static constexpr size_t MAX_NUM_ROUNDS = 256; + uint64_t num_points; + size_t num_buckets; + size_t num_rounds; + size_t num_threads; + size_t prefetch_overflow; std::shared_ptr point_schedule_ptr; std::shared_ptr point_pairs_1_ptr; std::shared_ptr point_pairs_2_ptr; @@ -92,20 +100,22 @@ template struct pippenger_runtime_state { uint32_t* bit_counts; bool* bucket_empty_status; uint64_t* round_counts; - uint64_t num_points; - pippenger_runtime_state(const size_t num_initial_points); - pippenger_runtime_state(pippenger_runtime_state&& other); - pippenger_runtime_state& operator=(pippenger_runtime_state&& other); - ~pippenger_runtime_state(); + pippenger_runtime_state(size_t num_initial_points) noexcept; + pippenger_runtime_state(pippenger_runtime_state&& other) noexcept; + pippenger_runtime_state& operator=(pippenger_runtime_state&& other) noexcept; + ~pippenger_runtime_state() noexcept; + + // explicitly delete copy constructor and copy assignment operator. + // This is an expensive, large data structure. No copy! Bad! + pippenger_runtime_state& operator=(pippenger_runtime_state& other) = delete; + pippenger_runtime_state(pippenger_runtime_state& other) = delete; - affine_product_runtime_state get_affine_product_runtime_state(const size_t num_threads, - const size_t thread_index); + affine_product_runtime_state get_affine_product_runtime_state(size_t num_threads, size_t thread_index); }; extern template struct affine_product_runtime_state; extern template struct affine_product_runtime_state; extern template struct pippenger_runtime_state; extern template struct pippenger_runtime_state; -} // namespace scalar_multiplication -} // namespace barretenberg \ No newline at end of file +} // namespace barretenberg::scalar_multiplication diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.cpp index 1440889e7bf..84be0133581 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.cpp @@ -14,6 +14,8 @@ #include "barretenberg/ecc/groups/wnaf.hpp" #include "barretenberg/numeric/bitop/get_msb.hpp" +// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays, google-readability-casting) + #define BBERG_SCALAR_MULTIPLICATION_FETCH_BLOCK \ __builtin_prefetch(state.points + (state.point_schedule[schedule_it + 16] >> 32ULL)); \ __builtin_prefetch(state.points + (state.point_schedule[schedule_it + 17] >> 32ULL)); \ @@ -91,8 +93,7 @@ current_offset += 16; \ schedule_it += 16; -namespace barretenberg { -namespace scalar_multiplication { +namespace barretenberg::scalar_multiplication { /** * The pippppenger point table computes for each point P = (x,y), a point P' = (\beta * x, -y) which enables us @@ -490,7 +491,7 @@ typename Curve::AffineElement* reduce_buckets(affine_product_runtime_state(state.num_points); + const auto end = static_cast(state.num_points); // The output of `evaluate_addition_chains` has a bit of an odd structure, should probably refactor. // Effectively, we used to have one big 1d array, and the act of computing these pair-wise point additions // has chopped it up into sequences of smaller 1d arrays, with gaps in between @@ -545,9 +546,9 @@ uint32_t construct_addition_chains(affine_product_runtime_state& state, b // if this is the first call to `construct_addition_chains`, we need to count up our buckets if (empty_bucket_counts) { memset((void*)state.bucket_counts, 0x00, sizeof(uint32_t) * state.num_buckets); - const uint32_t first_bucket = static_cast(state.point_schedule[0] & 0x7fffffffUL); + const auto first_bucket = static_cast(state.point_schedule[0] & 0x7fffffffUL); for (size_t i = 0; i < state.num_points; ++i) { - size_t bucket_index = static_cast(state.point_schedule[i] & 0x7fffffffUL); + const auto bucket_index = static_cast(state.point_schedule[i] & 0x7fffffffUL); ++state.bucket_counts[bucket_index - first_bucket]; } for (size_t i = 0; i < state.num_buckets; ++i) { @@ -596,7 +597,7 @@ uint32_t construct_addition_chains(affine_product_runtime_state& state, b // In the absence of a more elegant solution, we use ugly macro hacks to try and // unroll loops, and prefetch memory a few cycles before we need it switch (k_end) { - case 64: { + case 64: { // NOLINT(bugprone-branch-clone) [[fallthrough]]; } case 32: { @@ -803,7 +804,7 @@ typename Curve::Element evaluate_pippenger_rounds(pippenger_runtime_state // e.g. if first bucket is 0, no scaling // if first bucket is 1, we need to add (2 * running_sum) if (first_bucket > 0) { - uint32_t multiplier = static_cast(first_bucket << 1UL); + auto multiplier = static_cast(first_bucket << 1UL); size_t shift = numeric::get_msb(multiplier); Element rolling_accumulator = Curve::Group::point_at_infinity; bool init = false; @@ -903,8 +904,8 @@ typename Curve::Element pippenger(typename Curve::ScalarField* scalars, return exponentiation_results[0]; } - const size_t slice_bits = static_cast(numeric::get_msb(static_cast(num_initial_points))); - const size_t num_slice_points = static_cast(1ULL << slice_bits); + const auto slice_bits = static_cast(numeric::get_msb(static_cast(num_initial_points))); + const auto num_slice_points = static_cast(1ULL << slice_bits); Element result = pippenger_internal(points, scalars, num_slice_points, state, handle_edge_cases); @@ -915,9 +916,8 @@ typename Curve::Element pippenger(typename Curve::ScalarField* scalars, static_cast(leftover_points), state, handle_edge_cases); - } else { - return result; } + return result; } /** @@ -1058,5 +1058,6 @@ template curve::Grumpkin::Element pippenger_without_endomorphism_basis_points& state); -} // namespace scalar_multiplication -} // namespace barretenberg +} // namespace barretenberg::scalar_multiplication + +// NOLINTEND(cppcoreguidelines-avoid-c-arrays, google-readability-casting) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp index bb308ad12c5..dfba7226f8e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/scalar_multiplication/scalar_multiplication.hpp @@ -3,11 +3,10 @@ #include "./runtime_states.hpp" #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" -#include -#include +#include +#include -namespace barretenberg { -namespace scalar_multiplication { +namespace barretenberg::scalar_multiplication { constexpr size_t get_num_buckets(const size_t num_points) { @@ -91,16 +90,16 @@ void compute_wnaf_states(uint64_t* point_schedule, bool* input_skew_table, uint64_t* round_counts, const typename Curve::ScalarField* scalars, - const size_t num_initial_points); + size_t num_initial_points); template void generate_pippenger_point_table(typename Curve::AffineElement* points, typename Curve::AffineElement* table, size_t num_points); -void organize_buckets(uint64_t* point_schedule, const size_t num_points); +void organize_buckets(uint64_t* point_schedule, size_t num_points); -inline void count_bits(uint32_t* bucket_counts, +inline void count_bits(const uint32_t* bucket_counts, uint32_t* bit_offsets, const uint32_t num_buckets, const size_t num_bits) @@ -122,29 +121,29 @@ uint32_t construct_addition_chains(affine_product_runtime_state& state, b template void add_affine_points(typename Curve::AffineElement* points, - const size_t num_points, + size_t num_points, typename Curve::BaseField* scratch_space); template void add_affine_points_with_edge_cases(typename Curve::AffineElement* points, - const size_t num_points, + size_t num_points, typename Curve::BaseField* scratch_space); template void evaluate_addition_chains(affine_product_runtime_state& state, - const size_t max_bucket_bits, + size_t max_bucket_bits, bool handle_edge_cases); template typename Curve::Element pippenger_internal(typename Curve::AffineElement* points, typename Curve::ScalarField* scalars, - const size_t num_initial_points, + size_t num_initial_points, pippenger_runtime_state& state, bool handle_edge_cases); template typename Curve::Element evaluate_pippenger_rounds(pippenger_runtime_state& state, typename Curve::AffineElement* points, - const size_t num_points, + size_t num_points, bool handle_edge_cases = false); template @@ -155,20 +154,20 @@ typename Curve::AffineElement* reduce_buckets(affine_product_runtime_state typename Curve::Element pippenger(typename Curve::ScalarField* scalars, typename Curve::AffineElement* points, - const size_t num_points, + size_t num_initial_points, pippenger_runtime_state& state, bool handle_edge_cases = true); template typename Curve::Element pippenger_unsafe(typename Curve::ScalarField* scalars, typename Curve::AffineElement* points, - const size_t num_initial_points, + size_t num_initial_points, pippenger_runtime_state& state); template typename Curve::Element pippenger_without_endomorphism_basis_points(typename Curve::ScalarField* scalars, typename Curve::AffineElement* points, - const size_t num_initial_points, + size_t num_initial_points, pippenger_runtime_state& state); // Explicit instantiation @@ -278,5 +277,4 @@ extern template curve::Grumpkin::Element pippenger_without_endomorphism_basis_po const size_t num_initial_points, pippenger_runtime_state& state); -} // namespace scalar_multiplication -} // namespace barretenberg +} // namespace barretenberg::scalar_multiplication diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/serialize.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/serialize.test.cpp index 16250c97e17..603525720e4 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/serialize.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/ecc/serialize.test.cpp @@ -3,8 +3,8 @@ #include "barretenberg/serialize/test_helper.hpp" #include -TEST(msgpack_tests, msgpack_field) +TEST(MsgpackTests, MsgpackField) { - auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{ 1ull, 2ull, 3ull, 4ull }); + auto [actual, expected] = msgpack_roundtrip(barretenberg::fr{ 1ULL, 2ULL, 3ULL, 4ULL }); EXPECT_EQ(actual, expected); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/bitop.bench.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/bitop.bench.cpp index c94252b4905..aa2e6bcaaec 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/bitop.bench.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/bitop.bench.cpp @@ -13,4 +13,5 @@ void count_leading_zeros(State& state) noexcept } BENCHMARK(count_leading_zeros); +// NOLINTNEXTLINE macro invokation triggers style errors from googletest code BENCHMARK_MAIN(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.hpp index fa7bb93200b..72aa82a60a9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.hpp @@ -14,42 +14,36 @@ template constexpr inline size_t count_leading_zeros(T const& u); template <> constexpr inline size_t count_leading_zeros(uint32_t const& u) { - return (size_t)__builtin_clz(u); + return static_cast(__builtin_clz(u)); } -template <> constexpr inline size_t count_leading_zeros(unsigned long const& u) +template <> constexpr inline size_t count_leading_zeros(uint64_t const& u) { - return (size_t)__builtin_clzl(u); -} - -template <> constexpr inline size_t count_leading_zeros(unsigned long long const& u) -{ - return (size_t)__builtin_clzll(u); + return static_cast(__builtin_clzll(u)); } template <> constexpr inline size_t count_leading_zeros(uint128_t const& u) { - uint64_t hi = static_cast(u >> 64); - if (hi) { - return (size_t)__builtin_clzll(hi); - } else { - uint64_t lo = static_cast(u); - return (size_t)__builtin_clzll(lo) + 64; + auto hi = static_cast(u >> 64); + if (hi != 0U) { + return static_cast(__builtin_clzll(hi)); } + auto lo = static_cast(u); + return static_cast(__builtin_clzll(lo)) + 64; } template <> constexpr inline size_t count_leading_zeros(uint256_t const& u) { - if (u.data[3]) { + if (u.data[3] != 0U) { return count_leading_zeros(u.data[3]); } - if (u.data[2]) { + if (u.data[2] != 0U) { return count_leading_zeros(u.data[2]) + 64; } - if (u.data[1]) { + if (u.data[1] != 0U) { return count_leading_zeros(u.data[1]) + 128; } - if (u.data[0]) { + if (u.data[0] != 0U) { return count_leading_zeros(u.data[0]) + 192; } return 256; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.test.cpp index cf0248bc60c..f7c93d3368e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/count_leading_zeros.test.cpp @@ -1,25 +1,25 @@ #include "count_leading_zeros.hpp" #include -TEST(bitop, clz_uint32_31) +TEST(bitop, ClzUint3231) { uint32_t a = 0b00000000000000000000000000000001; EXPECT_EQ(numeric::count_leading_zeros(a), 31U); } -TEST(bitop, clz_uint32_0) +TEST(bitop, ClzUint320) { uint32_t a = 0b10000000000000000000000000000001; EXPECT_EQ(numeric::count_leading_zeros(a), 0U); } -TEST(bitop, clz_uint64_0) +TEST(bitop, ClzUint640) { uint64_t a = 0b1000000000000000000000000000000100000000000000000000000000000000; EXPECT_EQ(numeric::count_leading_zeros(a), 0U); } -TEST(bitop, clz_size_t) +TEST(bitop, ClzSizeT) { size_t a = 0x80; auto r = numeric::count_leading_zeros(a); @@ -30,14 +30,14 @@ TEST(bitop, clz_size_t) } } -TEST(bitop, clz_uint256_255) +TEST(bitop, ClzUint256255) { uint256_t a = 0x1; auto r = numeric::count_leading_zeros(a); EXPECT_EQ(r, 255U); } -TEST(bitop, clz_uint256_248) +TEST(bitop, ClzUint256248) { uint256_t a = 0x80; auto r = numeric::count_leading_zeros(a); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.hpp index d83f136f521..d3df553401b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.hpp @@ -1,16 +1,15 @@ #pragma once -#include -#include - +#include +#include +#include namespace numeric { // from http://supertech.csail.mit.edu/papers/debruijn.pdf constexpr inline uint32_t get_msb32(const uint32_t in) { - constexpr uint8_t MultiplyDeBruijnBitPosition[32] = { - 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, - 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 - }; + constexpr std::array MultiplyDeBruijnBitPosition{ 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, + 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, + 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; uint32_t v = in | (in >> 1); v |= v >> 2; @@ -24,10 +23,11 @@ constexpr inline uint32_t get_msb32(const uint32_t in) constexpr inline uint64_t get_msb64(const uint64_t in) { - constexpr uint8_t de_bruijn_sequence[64]{ 0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, 16, 3, 61, - 54, 58, 35, 52, 50, 42, 21, 44, 38, 32, 29, 23, 17, 11, 4, 62, - 46, 55, 26, 59, 40, 36, 15, 53, 34, 51, 20, 43, 31, 22, 10, 45, - 25, 39, 14, 33, 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63 }; + constexpr std::array de_bruijn_sequence{ 0, 47, 1, 56, 48, 27, 2, 60, 57, 49, 41, 37, 28, + 16, 3, 61, 54, 58, 35, 52, 50, 42, 21, 44, 38, 32, + 29, 23, 17, 11, 4, 62, 46, 55, 26, 59, 40, 36, 15, + 53, 34, 51, 20, 43, 31, 22, 10, 45, 25, 39, 14, 33, + 19, 30, 9, 24, 13, 18, 8, 12, 7, 6, 5, 63 }; uint64_t t = in | (in >> 1); t |= t >> 2; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.test.cpp index a6ba2061340..044fb3bbfd9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/get_msb.test.cpp @@ -1,31 +1,31 @@ #include "get_msb.hpp" #include -TEST(bitop, get_msb_uint64_0_value) +TEST(bitop, GetMsbUint640Value) { uint64_t a = 0b00000000000000000000000000000000; EXPECT_EQ(numeric::get_msb(a), 0U); } -TEST(bitop, get_msb_uint32_0) +TEST(bitop, GetMsbUint320) { uint32_t a = 0b00000000000000000000000000000001; EXPECT_EQ(numeric::get_msb(a), 0U); } -TEST(bitop, get_msb_uint32_31) +TEST(bitop, GetMsbUint3231) { uint32_t a = 0b10000000000000000000000000000001; EXPECT_EQ(numeric::get_msb(a), 31U); } -TEST(bitop, get_msb_uint64_63) +TEST(bitop, GetMsbUint6463) { uint64_t a = 0b1000000000000000000000000000000100000000000000000000000000000000; EXPECT_EQ(numeric::get_msb(a), 63U); } -TEST(bitop, get_msb_size_t_7) +TEST(bitop, GetMsbSizeT7) { size_t a = 0x80; auto r = numeric::get_msb(a); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/keep_n_lsb.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/keep_n_lsb.hpp index 438e68f9fb3..ee9859eecb6 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/keep_n_lsb.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/keep_n_lsb.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include namespace numeric { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/pow.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/pow.hpp index 7d2b391f8c9..295a5e5b4c7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/pow.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/pow.hpp @@ -8,7 +8,8 @@ constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent) { if (input == 0) { return 0; - } else if (exponent == 0) { + } + if (exponent == 0) { return 1; } @@ -18,7 +19,7 @@ constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent) for (int i = static_cast(maximum_set_bit) - 1; i >= 0; --i) { accumulator *= accumulator; - if ((exponent >> i) & 1) { + if (((exponent >> i) & 1) != 0U) { accumulator *= to_mul; } } @@ -27,7 +28,7 @@ constexpr uint64_t pow64(const uint64_t input, const uint64_t exponent) constexpr bool is_power_of_two(uint64_t x) { - return x && !(x & (x - 1)); + return (x != 0U) && ((x & (x - 1)) == 0U); } } // namespace numeric \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/rotate.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/rotate.hpp index da5402804fb..1fc0cd1cfdd 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/rotate.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/rotate.hpp @@ -1,16 +1,16 @@ #pragma once -#include -#include +#include +#include namespace numeric { constexpr inline uint64_t rotate64(const uint64_t value, const uint64_t rotation) { - return rotation ? (value >> rotation) + (value << (64 - rotation)) : value; + return rotation != 0U ? (value >> rotation) + (value << (64 - rotation)) : value; } constexpr inline uint32_t rotate32(const uint32_t value, const uint32_t rotation) { - return rotation ? (value >> rotation) + (value << (32 - rotation)) : value; + return rotation != 0U ? (value >> rotation) + (value << (32 - rotation)) : value; } } // namespace numeric \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/sparse_form.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/sparse_form.hpp index 651ca694d54..dcc4a13315d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/sparse_form.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/bitop/sparse_form.hpp @@ -1,15 +1,15 @@ #pragma once #include "barretenberg/common/throw_or_abort.hpp" +#include +#include #include -#include -#include #include #include "../uint256/uint256.hpp" namespace numeric { -inline std::vector slice_input(const uint256_t input, const uint64_t base, const size_t num_slices) +inline std::vector slice_input(const uint256_t& input, const uint64_t base, const size_t num_slices) { uint256_t target = input; std::vector slices; @@ -27,7 +27,8 @@ inline std::vector slice_input(const uint256_t input, const uint64_t b return slices; } -inline std::vector slice_input_using_variable_bases(const uint256_t input, const std::vector bases) +inline std::vector slice_input_using_variable_bases(const uint256_t& input, + const std::vector& bases) { uint256_t target = input; std::vector slices; @@ -54,7 +55,7 @@ template constexpr std::array constexpr uint256_t map_into_sparse_form(const uint64_t input) { uint256_t out = 0UL; - uint64_t converted = (uint64_t)input; + auto converted = input; constexpr auto base_powers = get_base_powers(); for (size_t i = 0; i < 32; ++i) { @@ -66,7 +67,7 @@ template constexpr uint256_t map_into_sparse_form(const uint64_t return out; } -template constexpr uint64_t map_from_sparse_form(const uint256_t input) +template constexpr uint64_t map_from_sparse_form(const uint256_t& input) { uint256_t target = input; uint64_t output = 0; @@ -105,11 +106,11 @@ template class sparse_int { limbs[i] = bit; } } - sparse_int(const sparse_int& other) = default; - sparse_int(sparse_int&& other) = default; - - sparse_int& operator=(const sparse_int& other) = default; - sparse_int& operator=(sparse_int&& other) = default; + sparse_int(const sparse_int& other) noexcept = default; + sparse_int(sparse_int&& other) noexcept = default; + sparse_int& operator=(const sparse_int& other) noexcept = default; + sparse_int& operator=(sparse_int&& other) noexcept = default; + ~sparse_int() noexcept = default; sparse_int operator+(const sparse_int& other) const { @@ -133,9 +134,9 @@ template class sparse_int { return *this; } - uint64_t get_value() const { return value; } + [[nodiscard]] uint64_t get_value() const { return value; } - uint64_t get_sparse_value() const + [[nodiscard]] uint64_t get_sparse_value() const { uint64_t result = 0; for (size_t i = num_bits - 1; i < num_bits; --i) { diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp index fe8ac086cd7..200e73530be 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.cpp @@ -4,8 +4,7 @@ #include #include -namespace numeric { -namespace random { +namespace numeric::random { namespace { auto generate_random_data() @@ -19,48 +18,48 @@ auto generate_random_data() class RandomEngine : public Engine { public: - uint8_t get_random_uint8() + uint8_t get_random_uint8() override { auto buf = generate_random_data(); uint32_t out = buf[0]; return static_cast(out); } - uint16_t get_random_uint16() + uint16_t get_random_uint16() override { auto buf = generate_random_data(); uint32_t out = buf[0]; return static_cast(out); } - uint32_t get_random_uint32() + uint32_t get_random_uint32() override { auto buf = generate_random_data(); uint32_t out = buf[0]; return static_cast(out); } - uint64_t get_random_uint64() + uint64_t get_random_uint64() override { auto buf = generate_random_data(); - uint64_t lo = static_cast(buf[0]); - uint64_t hi = static_cast(buf[1]); + auto lo = static_cast(buf[0]); + auto hi = static_cast(buf[1]); return (lo + (hi << 32ULL)); } - uint128_t get_random_uint128() + uint128_t get_random_uint128() override { auto big = get_random_uint256(); - uint128_t lo = static_cast(big.data[0]); - uint128_t hi = static_cast(big.data[1]); - return (lo + (hi << (uint128_t)(64ULL))); + auto lo = static_cast(big.data[0]); + auto hi = static_cast(big.data[1]); + return (lo + (hi << static_cast(64ULL))); } - uint256_t get_random_uint256() + uint256_t get_random_uint256() override { const auto get64 = [](const std::array& buffer, const size_t offset) { - uint64_t lo = static_cast(buffer[0 + offset]); - uint64_t hi = static_cast(buffer[1 + offset]); + auto lo = static_cast(buffer[0 + offset]); + auto hi = static_cast(buffer[1 + offset]); return (lo + (hi << 32ULL)); }; auto buf = generate_random_data(); @@ -68,13 +67,15 @@ class RandomEngine : public Engine { uint64_t lohi = get64(buf, 2); uint64_t hilo = get64(buf, 4); uint64_t hihi = get64(buf, 6); - return uint256_t(lolo, lohi, hilo, hihi); + return { lolo, lohi, hilo, hihi }; } }; class DebugEngine : public Engine { public: DebugEngine() + // disable linting for this line: we want the DEBUG engine to produce predictable pseudorandom numbers! + // NOLINTNEXTLINE(cert-msc32-c, cert-msc51-cpp) : engine(std::mt19937_64(12345)) {} @@ -82,35 +83,31 @@ class DebugEngine : public Engine { : engine(std::mt19937_64(seed)) {} - uint8_t get_random_uint8() { return static_cast(dist(engine)); } + uint8_t get_random_uint8() override { return static_cast(dist(engine)); } - uint16_t get_random_uint16() { return static_cast(dist(engine)); } + uint16_t get_random_uint16() override { return static_cast(dist(engine)); } - uint32_t get_random_uint32() { return static_cast(dist(engine)); } + uint32_t get_random_uint32() override { return static_cast(dist(engine)); } - uint64_t get_random_uint64() { return dist(engine); } + uint64_t get_random_uint64() override { return dist(engine); } - uint128_t get_random_uint128() + uint128_t get_random_uint128() override { uint128_t hi = dist(engine); uint128_t lo = dist(engine); return (hi << 64) | lo; } - uint256_t get_random_uint256() + uint256_t get_random_uint256() override { // Do not inline in constructor call. Evaluation order is important for cross-compiler consistency. auto a = dist(engine); auto b = dist(engine); auto c = dist(engine); auto d = dist(engine); - return uint256_t(a, b, c, d); + return { a, b, c, d }; } - uint512_t get_random_uint512(); - - uint1024_t get_random_uint1024(); - private: std::mt19937_64 engine; std::uniform_int_distribution dist = std::uniform_int_distribution{ 0ULL, UINT64_MAX }; @@ -139,5 +136,4 @@ Engine& get_engine() return engine; } -} // namespace random -} // namespace numeric +} // namespace numeric::random diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.hpp index 2506a733e23..9721f69886e 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.hpp @@ -2,11 +2,10 @@ #include "../uint128/uint128.hpp" #include "../uint256/uint256.hpp" #include "../uintx/uintx.hpp" -#include "stdint.h" #include "unistd.h" +#include -namespace numeric { -namespace random { +namespace numeric::random { class Engine { public: @@ -22,12 +21,19 @@ class Engine { virtual uint256_t get_random_uint256() = 0; + virtual ~Engine() = default; + Engine() noexcept = default; + Engine(const Engine& other) = default; + Engine(Engine&& other) = default; + Engine& operator=(const Engine& other) = default; + Engine& operator=(Engine&& other) = default; + uint512_t get_random_uint512() { // Do not inline in constructor call. Evaluation order is important for cross-compiler consistency. auto lo = get_random_uint256(); auto hi = get_random_uint256(); - return uint512_t(lo, hi); + return { lo, hi }; } uint1024_t get_random_uint1024() @@ -35,12 +41,11 @@ class Engine { // Do not inline in constructor call. Evaluation order is important for cross-compiler consistency. auto lo = get_random_uint512(); auto hi = get_random_uint512(); - return uint1024_t(lo, hi); + return { lo, hi }; } }; Engine& get_debug_engine(bool reset = false); Engine& get_engine(); -} // namespace random -} // namespace numeric \ No newline at end of file +} // namespace numeric::random diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.test.cpp index 69fa536fae6..d55b57d8ca0 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/random/engine.test.cpp @@ -3,7 +3,7 @@ #include "engine.hpp" #include -TEST(engine, get_random_uint64) +TEST(engine, GetRandomUint64) { auto& engine = numeric::random::get_engine(); auto a = engine.get_random_uint64(); @@ -13,7 +13,7 @@ TEST(engine, get_random_uint64) EXPECT_NE(a, b); } -TEST(engine, reset_debug_engine) +TEST(engine, ResetDebugEngine) { auto& debug_engine = numeric::random::get_debug_engine(); @@ -31,7 +31,7 @@ TEST(engine, reset_debug_engine) EXPECT_NE(a, e); } -TEST(engine, get_expected_debug_value) +TEST(engine, GetExpectedDebugValue) { auto& debug_engine = numeric::random::get_debug_engine(true); auto a = debug_engine.get_random_uint1024(); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.hpp index e0ec587400d..fc0410b647d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.hpp @@ -10,7 +10,7 @@ namespace numeric { class alignas(32) uint128_t { public: - uint32_t data[4]; + uint32_t data[4]; // NOLINT constexpr uint128_t(const uint64_t a = 0) : data{ static_cast(a), static_cast(a >> 32), 0, 0 } @@ -23,25 +23,27 @@ class alignas(32) uint128_t { constexpr uint128_t(const uint128_t& other) : data{ other.data[0], other.data[1], other.data[2], other.data[3] } {} + constexpr uint128_t(uint128_t&& other) = default; static constexpr uint128_t from_uint64(const uint64_t a) { - return uint128_t(static_cast(a), static_cast(a >> 32), 0, 0); + return { static_cast(a), static_cast(a >> 32), 0, 0 }; } - constexpr explicit operator uint64_t() { return (uint64_t(data[1]) << 32) + data[0]; } + constexpr explicit operator uint64_t() { return (static_cast(data[1]) << 32) + data[0]; } constexpr uint128_t& operator=(const uint128_t& other) = default; - + constexpr uint128_t& operator=(uint128_t&& other) = default; + constexpr ~uint128_t() = default; explicit constexpr operator bool() const { return static_cast(data[0]); }; template explicit constexpr operator T() const { return static_cast(data[0]); }; - constexpr bool get_bit(const uint64_t bit_index) const; - constexpr uint64_t get_msb() const; + [[nodiscard]] constexpr bool get_bit(uint64_t bit_index) const; + [[nodiscard]] constexpr uint64_t get_msb() const; - constexpr uint128_t slice(const uint64_t start, const uint64_t end) const; - constexpr uint128_t pow(const uint128_t& exponent) const; + [[nodiscard]] constexpr uint128_t slice(uint64_t start, uint64_t end) const; + [[nodiscard]] constexpr uint128_t pow(const uint128_t& exponent) const; constexpr uint128_t operator+(const uint128_t& other) const; constexpr uint128_t operator-(const uint128_t& other) const; @@ -134,25 +136,22 @@ class alignas(32) uint128_t { return *this; }; - constexpr std::pair mul_extended(const uint128_t& other) const; + [[nodiscard]] constexpr std::pair mul_extended(const uint128_t& other) const; - constexpr std::pair divmod(const uint128_t& b) const; + [[nodiscard]] constexpr std::pair divmod(const uint128_t& b) const; private: - constexpr std::pair mul_wide(const uint32_t a, const uint32_t b) const; - constexpr std::pair addc(const uint32_t a, const uint32_t b, const uint32_t carry_in) const; - constexpr uint32_t addc_discard_hi(const uint32_t a, const uint32_t b, const uint32_t carry_in) const; - constexpr uint32_t sbb_discard_hi(const uint32_t a, const uint32_t b, const uint32_t borrow_in) const; - - constexpr std::pair sbb(const uint32_t a, const uint32_t b, const uint32_t borrow_in) const; - constexpr uint32_t mac_discard_hi(const uint32_t a, - const uint32_t b, - const uint32_t c, - const uint32_t carry_in) const; - constexpr std::pair mac(const uint32_t a, - const uint32_t b, - const uint32_t c, - const uint32_t carry_in) const; + [[nodiscard]] static constexpr std::pair mul_wide(uint32_t a, uint32_t b); + [[nodiscard]] static constexpr std::pair addc(uint32_t a, uint32_t b, uint32_t carry_in); + [[nodiscard]] static constexpr uint32_t addc_discard_hi(uint32_t a, uint32_t b, uint32_t carry_in); + [[nodiscard]] static constexpr uint32_t sbb_discard_hi(uint32_t a, uint32_t b, uint32_t borrow_in); + + [[nodiscard]] static constexpr std::pair sbb(uint32_t a, uint32_t b, uint32_t borrow_in); + [[nodiscard]] static constexpr uint32_t mac_discard_hi(uint32_t a, uint32_t b, uint32_t c, uint32_t carry_in); + [[nodiscard]] static constexpr std::pair mac(uint32_t a, + uint32_t b, + uint32_t c, + uint32_t carry_in); }; inline std::ostream& operator<<(std::ostream& os, uint128_t const& a) @@ -167,7 +166,10 @@ inline std::ostream& operator<<(std::ostream& os, uint128_t const& a) template inline void read(B& it, uint128_t& value) { using serialize::read; - uint32_t a, b, c, d; + uint32_t a = 0; + uint32_t b = 0; + uint32_t c = 0; + uint32_t d = 0; read(it, d); read(it, c); read(it, b); @@ -188,15 +190,20 @@ template inline void write(B& it, uint128_t const& value) #include "./uint128_impl.hpp" +// disable linter errors; we want to expose a global uint128_t type to mimic uint64_t, uint32_t etc +// NOLINTNEXTLINE(tidymisc-unused-using-decls, google-global-names-in-headers, misc-unused-using-decls) using numeric::uint128_t; #else __extension__ using uint128_t = unsigned __int128; namespace std { +// can ignore linter error for streaming operations, we need to add to std namespace to support printing this type! +// NOLINTNEXTLINE(cert-dcl58-cpp) inline std::ostream& operator<<(std::ostream& os, uint128_t const& a) { std::ios_base::fmtflags f(os.flags()); - os << std::hex << "0x" << std::setfill('0') << std::setw(16) << (uint64_t)(a >> 64) << std::setw(16) << (uint64_t)a; + os << std::hex << "0x" << std::setfill('0') << std::setw(16) << static_cast(a >> 64) << std::setw(16) + << static_cast(a); os.flags(f); return os; } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.test.cpp index f6af85e7bed..46283e7185d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128.test.cpp @@ -8,7 +8,7 @@ auto& engine = numeric::random::get_debug_engine(); using namespace numeric; -TEST(uint128, get_bit) +TEST(uint128, GetBit) { constexpr uint128_t a{ 0b0110011001110010011001100, 0b1001011101101010101010100, @@ -23,7 +23,7 @@ TEST(uint128, get_bit) EXPECT_EQ(a, res); } -TEST(uint128, add) +TEST(uint128, Add) { constexpr uint128_t a{ 1, 2, 3, 4 }; constexpr uint128_t b{ 5, 6, 7, 8 }; @@ -41,7 +41,7 @@ TEST(uint128, add) EXPECT_EQ(d.data[3], 12ULL); } -TEST(uint128, get_msb) +TEST(uint128, GetMsb) { uint128_t a{ 0, 0, 1, 1 }; uint128_t b{ 1, 0, 1, 0 }; @@ -54,7 +54,7 @@ TEST(uint128, get_msb) EXPECT_EQ(d.get_msb(), 0ULL); } -TEST(uint128, mul) +TEST(uint128, Mul) { uint128_t a = engine.get_random_uint128(); uint128_t b = engine.get_random_uint128(); @@ -67,7 +67,7 @@ TEST(uint128, mul) EXPECT_EQ(c.data[3], d.data[3]); } -TEST(uint128, div_and_mod) +TEST(uint128, DivAndMod) { for (size_t i = 0; i < 128; ++i) { uint128_t a = engine.get_random_uint128(); @@ -103,7 +103,7 @@ TEST(uint128, div_and_mod) EXPECT_EQ(r, uint128_t(0)); } -TEST(uint128, sub) +TEST(uint128, Sub) { uint128_t a = engine.get_random_uint128(); uint128_t b = engine.get_random_uint128(); @@ -125,7 +125,7 @@ TEST(uint128, sub) EXPECT_EQ(e.data[3], UINT32_MAX); } -TEST(uint128, right_shift) +TEST(uint128, RightShift) { constexpr uint128_t a{ 0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd }; @@ -143,7 +143,7 @@ TEST(uint128, right_shift) EXPECT_EQ(f, uint128_t(0, 0xb8000000, 0xcccccccc, 0xdddddddd)); } -TEST(uint128, left_shift) +TEST(uint128, LeftShift) { uint128_t a{ 0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd }; @@ -165,7 +165,7 @@ TEST(uint128, left_shift) EXPECT_EQ(f, uint128_t(0)); } -TEST(uint128, and) +TEST(uint128, And) { uint128_t a = engine.get_random_uint128(); uint128_t b = engine.get_random_uint128(); @@ -178,7 +178,7 @@ TEST(uint128, and) EXPECT_EQ(c.data[3], a.data[3] & b.data[3]); } -TEST(uint128, or) +TEST(uint128, Or) { uint128_t a = engine.get_random_uint128(); uint128_t b = engine.get_random_uint128(); @@ -191,7 +191,7 @@ TEST(uint128, or) EXPECT_EQ(c.data[3], a.data[3] | b.data[3]); } -TEST(uint128, xor) +TEST(uint128, Xor) { uint128_t a = engine.get_random_uint128(); uint128_t b = engine.get_random_uint128(); @@ -204,7 +204,7 @@ TEST(uint128, xor) EXPECT_EQ(c.data[3], a.data[3] ^ b.data[3]); } -TEST(uint128, bit_not) +TEST(uint128, BitNot) { uint128_t a = engine.get_random_uint128(); @@ -216,7 +216,7 @@ TEST(uint128, bit_not) EXPECT_EQ(c.data[3], ~a.data[3]); } -TEST(uint128, logic_not) +TEST(uint128, LogicNot) { uint128_t a{ 1, 0, 0, 0 }; @@ -229,7 +229,7 @@ TEST(uint128, logic_not) EXPECT_EQ(!c, true); } -TEST(uint128, equality) +TEST(uint128, Equality) { uint128_t a{ 1, 0, 0, 0 }; uint128_t b{ 1, 0, 0, 0 }; @@ -249,7 +249,7 @@ TEST(uint128, equality) EXPECT_EQ(a == b, false); } -TEST(uint128, not_equal) +TEST(uint128, NotEqual) { uint128_t a{ 1, 0, 0, 0 }; uint128_t b{ 1, 0, 0, 0 }; @@ -269,7 +269,7 @@ TEST(uint128, not_equal) EXPECT_EQ(a != b, true); } -TEST(uint128, greater_than) +TEST(uint128, GreaterThan) { constexpr uint128_t a{ UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX }; constexpr uint128_t b{ UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX }; @@ -288,7 +288,7 @@ TEST(uint128, greater_than) EXPECT_EQ(a > f, true); } -TEST(uint128, greater_than_or_equal) +TEST(uint128, GeaterThanOrEqual) { uint128_t a{ UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX - 1 }; uint128_t b{ UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX }; @@ -308,11 +308,11 @@ TEST(uint128, greater_than_or_equal) EXPECT_EQ(a >= b, false); } -TEST(uint128, to_from_buffer) +TEST(uint128, ToFromBuffer) { uint128_t a{ 1, 2, 3, 4 }; auto buf = to_buffer(a); - uint128_t b = from_buffer(buf); + auto b = from_buffer(buf); EXPECT_EQ(a, b); } #endif \ No newline at end of file diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128_impl.hpp index eb6ca3295ca..8fa503c95e3 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint128/uint128_impl.hpp @@ -1,11 +1,11 @@ #ifdef __i386__ #pragma once #include "../bitop/get_msb.hpp" +#include "./uint128.hpp" #include "barretenberg/common/assert.hpp" - namespace numeric { -constexpr std::pair uint128_t::mul_wide(const uint32_t a, const uint32_t b) const +constexpr std::pair uint128_t::mul_wide(const uint32_t a, const uint32_t b) { const uint32_t a_lo = a & 0xffffULL; const uint32_t a_hi = a >> 16ULL; @@ -23,35 +23,31 @@ constexpr std::pair uint128_t::mul_wide(const uint32_t a, co } // compute a + b + carry, returning the carry -constexpr std::pair uint128_t::addc(const uint32_t a, - const uint32_t b, - const uint32_t carry_in) const +constexpr std::pair uint128_t::addc(const uint32_t a, const uint32_t b, const uint32_t carry_in) { const uint32_t sum = a + b; - const uint32_t carry_temp = sum < a; + const auto carry_temp = static_cast(sum < a); const uint32_t r = sum + carry_in; - const uint32_t carry_out = carry_temp + (r < carry_in); + const uint32_t carry_out = carry_temp + static_cast(r < carry_in); return { r, carry_out }; } -constexpr uint32_t uint128_t::addc_discard_hi(const uint32_t a, const uint32_t b, const uint32_t carry_in) const +constexpr uint32_t uint128_t::addc_discard_hi(const uint32_t a, const uint32_t b, const uint32_t carry_in) { return a + b + carry_in; } -constexpr std::pair uint128_t::sbb(const uint32_t a, - const uint32_t b, - const uint32_t borrow_in) const +constexpr std::pair uint128_t::sbb(const uint32_t a, const uint32_t b, const uint32_t borrow_in) { const uint32_t t_1 = a - (borrow_in >> 31ULL); - const uint32_t borrow_temp_1 = t_1 > a; + const auto borrow_temp_1 = static_cast(t_1 > a); const uint32_t t_2 = t_1 - b; - const uint32_t borrow_temp_2 = t_2 > t_1; + const auto borrow_temp_2 = static_cast(t_2 > t_1); return { t_2, 0ULL - (borrow_temp_1 | borrow_temp_2) }; } -constexpr uint32_t uint128_t::sbb_discard_hi(const uint32_t a, const uint32_t b, const uint32_t borrow_in) const +constexpr uint32_t uint128_t::sbb_discard_hi(const uint32_t a, const uint32_t b, const uint32_t borrow_in) { return a - b - (borrow_in >> 31ULL); } @@ -60,13 +56,13 @@ constexpr uint32_t uint128_t::sbb_discard_hi(const uint32_t a, const uint32_t b, constexpr std::pair uint128_t::mac(const uint32_t a, const uint32_t b, const uint32_t c, - const uint32_t carry_in) const + const uint32_t carry_in) { std::pair result = mul_wide(b, c); result.first += a; - const uint32_t overflow_c = (result.first < a); + const auto overflow_c = static_cast(result.first < a); result.first += carry_in; - const uint32_t overflow_carry = (result.first < carry_in); + const auto overflow_carry = static_cast(result.first < carry_in); result.second += (overflow_c + overflow_carry); return result; } @@ -74,7 +70,7 @@ constexpr std::pair uint128_t::mac(const uint32_t a, constexpr uint32_t uint128_t::mac_discard_hi(const uint32_t a, const uint32_t b, const uint32_t c, - const uint32_t carry_in) const + const uint32_t carry_in) { return (b * c + a + carry_in); } @@ -83,11 +79,14 @@ constexpr std::pair uint128_t::divmod(const uint128_t& b) { if (*this == 0 || b == 0) { return { 0, 0 }; - } else if (b == 1) { + } + if (b == 1) { return { *this, 0 }; - } else if (*this == b) { + } + if (*this == b) { return { 1, 0 }; - } else if (b > *this) { + } + if (b > *this) { return { 0, *this }; } @@ -187,19 +186,19 @@ constexpr bool uint128_t::get_bit(const uint64_t bit_index) const { ASSERT(bit_index < 128); if (bit_index > 127) { - return bool(0); + return false; } - const size_t idx = static_cast(bit_index >> 5); + const auto idx = static_cast(bit_index >> 5); const size_t shift = bit_index & 31; - return bool((data[idx] >> shift) & 1); + return static_cast((data[idx] >> shift) & 1); } constexpr uint64_t uint128_t::get_msb() const { - uint64_t idx = numeric::get_msb(data[3]); - idx = (idx == 0 && data[3] == 0) ? numeric::get_msb(data[2]) : idx + 32; - idx = (idx == 0 && data[2] == 0) ? numeric::get_msb(data[1]) : idx + 32; - idx = (idx == 0 && data[1] == 0) ? numeric::get_msb(data[0]) : idx + 32; + uint64_t idx = numeric::get_msb64(data[3]); + idx = (idx == 0 && data[3] == 0) ? numeric::get_msb64(data[2]) : idx + 32; + idx = (idx == 0 && data[2] == 0) ? numeric::get_msb64(data[1]) : idx + 32; + idx = (idx == 0 && data[1] == 0) ? numeric::get_msb64(data[0]) : idx + 32; return idx; } @@ -320,7 +319,7 @@ constexpr uint128_t uint128_t::operator>>(const uint128_t& other) const { uint32_t total_shift = other.data[0]; - if (total_shift >= 128 || other.data[1] || other.data[2] || other.data[3]) { + if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) { return 0; } @@ -331,7 +330,7 @@ constexpr uint128_t uint128_t::operator>>(const uint128_t& other) const uint32_t num_shifted_limbs = total_shift >> 5ULL; uint32_t limb_shift = total_shift & 31ULL; - uint32_t shifted_limbs[4] = { 0 }; + std::array shifted_limbs = { 0, 0, 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = data[0]; @@ -357,8 +356,8 @@ constexpr uint128_t uint128_t::operator>>(const uint128_t& other) const } uint128_t result(0); - for (uint32_t i = 0; i < 4 - num_shifted_limbs; ++i) { - result.data[i] = shifted_limbs[i + num_shifted_limbs]; + for (size_t i = 0; i < 4 - num_shifted_limbs; ++i) { + result.data[i] = shifted_limbs[static_cast(i + num_shifted_limbs)]; } return result; @@ -368,7 +367,7 @@ constexpr uint128_t uint128_t::operator<<(const uint128_t& other) const { uint32_t total_shift = other.data[0]; - if (total_shift >= 128 || other.data[1] || other.data[2] || other.data[3]) { + if (total_shift >= 128 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) { return 0; } @@ -378,7 +377,7 @@ constexpr uint128_t uint128_t::operator<<(const uint128_t& other) const uint32_t num_shifted_limbs = total_shift >> 5ULL; uint32_t limb_shift = total_shift & 31ULL; - uint32_t shifted_limbs[4]{ 0 }; + std::array shifted_limbs{ 0, 0, 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = data[0]; @@ -404,8 +403,8 @@ constexpr uint128_t uint128_t::operator<<(const uint128_t& other) const } uint128_t result(0); - for (uint32_t i = 0; i < 4 - num_shifted_limbs; ++i) { - result.data[i + num_shifted_limbs] = shifted_limbs[i]; + for (size_t i = 0; i < 4 - num_shifted_limbs; ++i) { + result.data[static_cast(i + num_shifted_limbs)] = shifted_limbs[i]; } return result; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.hpp index 22aa382ded2..70c80c811a9 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.hpp @@ -22,45 +22,48 @@ namespace numeric { class alignas(32) uint256_t { public: - constexpr uint256_t(const uint64_t a = 0) + constexpr uint256_t(const uint64_t a = 0) noexcept : data{ a, 0, 0, 0 } {} - constexpr uint256_t(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) + constexpr uint256_t(const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept : data{ a, b, c, d } {} - constexpr uint256_t(const uint256_t& other) + constexpr uint256_t(const uint256_t& other) noexcept : data{ other.data[0], other.data[1], other.data[2], other.data[3] } {} + constexpr uint256_t(uint256_t&& other) noexcept = default; - explicit uint256_t(std::string const& str) + explicit uint256_t(std::string const& str) noexcept { for (int i = 0; i < 4; ++i) { std::stringstream ss; - ss << std::hex << str.substr(size_t(i) * 16, 16); + ss << std::hex << str.substr(static_cast(i) * 16, 16); ss >> data[3 - i]; } } - static constexpr uint256_t from_uint128(const uint128_t a) + static constexpr uint256_t from_uint128(const uint128_t a) noexcept { - return uint256_t(static_cast(a), static_cast(a >> 64), 0, 0); + return { static_cast(a), static_cast(a >> 64), 0, 0 }; } - constexpr explicit operator uint128_t() { return (uint128_t(data[1]) << 64) + data[0]; } + constexpr explicit operator uint128_t() { return (static_cast(data[1]) << 64) + data[0]; } - constexpr uint256_t& operator=(const uint256_t& other) = default; + constexpr uint256_t& operator=(const uint256_t& other) noexcept = default; + constexpr uint256_t& operator=(uint256_t&& other) noexcept = default; + constexpr ~uint256_t() noexcept = default; explicit constexpr operator bool() const { return static_cast(data[0]); }; template explicit constexpr operator T() const { return static_cast(data[0]); }; - constexpr bool get_bit(const uint64_t bit_index) const; - constexpr uint64_t get_msb() const; + [[nodiscard]] constexpr bool get_bit(uint64_t bit_index) const; + [[nodiscard]] constexpr uint64_t get_msb() const; - constexpr uint256_t slice(const uint64_t start, const uint64_t end) const; - constexpr uint256_t pow(const uint256_t& exponent) const; + [[nodiscard]] constexpr uint256_t slice(uint64_t start, uint64_t end) const; + [[nodiscard]] constexpr uint256_t pow(const uint256_t& exponent) const; constexpr uint256_t operator+(const uint256_t& other) const; constexpr uint256_t operator-(const uint256_t& other) const; @@ -153,27 +156,23 @@ class alignas(32) uint256_t { return *this; }; - constexpr std::pair mul_extended(const uint256_t& other) const; + [[nodiscard]] constexpr std::pair mul_extended(const uint256_t& other) const; - uint64_t data[4]; + uint64_t data[4]; // NOLINT - constexpr std::pair divmod(const uint256_t& b) const; + [[nodiscard]] constexpr std::pair divmod(const uint256_t& b) const; private: - constexpr std::pair mul_wide(const uint64_t a, const uint64_t b) const; - constexpr std::pair addc(const uint64_t a, const uint64_t b, const uint64_t carry_in) const; - constexpr uint64_t addc_discard_hi(const uint64_t a, const uint64_t b, const uint64_t carry_in) const; - constexpr uint64_t sbb_discard_hi(const uint64_t a, const uint64_t b, const uint64_t borrow_in) const; - - constexpr std::pair sbb(const uint64_t a, const uint64_t b, const uint64_t borrow_in) const; - constexpr uint64_t mac_discard_hi(const uint64_t a, - const uint64_t b, - const uint64_t c, - const uint64_t carry_in) const; - constexpr std::pair mac(const uint64_t a, - const uint64_t b, - const uint64_t c, - const uint64_t carry_in) const; + [[nodiscard]] static constexpr std::pair mul_wide(uint64_t a, uint64_t b); + [[nodiscard]] static constexpr std::pair addc(uint64_t a, uint64_t b, uint64_t carry_in); + [[nodiscard]] static constexpr uint64_t addc_discard_hi(uint64_t a, uint64_t b, uint64_t carry_in); + [[nodiscard]] static constexpr uint64_t sbb_discard_hi(uint64_t a, uint64_t b, uint64_t borrow_in); + [[nodiscard]] static constexpr std::pair sbb(uint64_t a, uint64_t b, uint64_t borrow_in); + [[nodiscard]] static constexpr uint64_t mac_discard_hi(uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in); + [[nodiscard]] static constexpr std::pair mac(uint64_t a, + uint64_t b, + uint64_t c, + uint64_t carry_in); }; inline std::ostream& operator<<(std::ostream& os, uint256_t const& a) @@ -188,7 +187,10 @@ inline std::ostream& operator<<(std::ostream& os, uint256_t const& a) template inline void read(B& it, uint256_t& value) { using serialize::read; - uint64_t a, b, c, d; + uint64_t a = 0; + uint64_t b = 0; + uint64_t c = 0; + uint64_t d = 0; read(it, d); read(it, c); read(it, b); @@ -209,4 +211,6 @@ template inline void write(B& it, uint256_t const& value) #include "./uint256_impl.hpp" +// disable linter errors; we want to expose a global uint256_t type to mimic uint64_t, uint32_t etc +// NOLINTNEXTLINE(tidymisc-unused-using-decls, google-global-names-in-headers, misc-unused-using-decls) using numeric::uint256_t; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.test.cpp index ba12aad275b..ecec6636c6b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256.test.cpp @@ -8,7 +8,7 @@ auto& engine = numeric::random::get_debug_engine(); using namespace numeric; -TEST(uint256, get_bit) +TEST(uint256, GetBit) { constexpr uint256_t a{ 0b0110011001110010011001100111001001100110011100100110011001110011, 0b1001011101101010101010100100101101101001001010010101110101010111, @@ -23,7 +23,7 @@ TEST(uint256, get_bit) EXPECT_EQ(a, res); } -TEST(uint256, add) +TEST(uint256, Add) { constexpr uint256_t a{ 1, 2, 3, 4 }; constexpr uint256_t b{ 5, 6, 7, 8 }; @@ -41,7 +41,7 @@ TEST(uint256, add) EXPECT_EQ(d.data[3], 12ULL); } -TEST(uint256, get_msb) +TEST(uint256, GetMsb) { uint256_t a{ 0, 0, 1, 1 }; uint256_t b{ 1, 0, 1, 0 }; @@ -54,7 +54,7 @@ TEST(uint256, get_msb) EXPECT_EQ(d.get_msb(), 0ULL); } -TEST(uint256, mul) +TEST(uint256, Mul) { uint256_t a = engine.get_random_uint256(); uint256_t b = engine.get_random_uint256(); @@ -67,7 +67,7 @@ TEST(uint256, mul) EXPECT_EQ(c.data[3], d.data[3]); } -TEST(uint256, div_and_mod) +TEST(uint256, DivAndMod) { for (size_t i = 0; i < 256; ++i) { uint256_t a = engine.get_random_uint256(); @@ -103,7 +103,7 @@ TEST(uint256, div_and_mod) EXPECT_EQ(r, uint256_t(0)); } -TEST(uint256, sub) +TEST(uint256, Sub) { uint256_t a = engine.get_random_uint256(); uint256_t b = engine.get_random_uint256(); @@ -125,7 +125,7 @@ TEST(uint256, sub) EXPECT_EQ(e.data[3], UINT64_MAX); } -TEST(uint256, right_shift) +TEST(uint256, RightShift) { constexpr uint256_t a{ 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc, 0xdddddddddddddddd }; @@ -143,7 +143,7 @@ TEST(uint256, right_shift) EXPECT_EQ(f, uint256_t(0, 0xb800000000000000, 0xcccccccccccccccc, 0xdddddddddddddddd)); } -TEST(uint256, left_shift) +TEST(uint256, LeftShift) { uint256_t a{ 0xaaaaaaaaaaaaaaaa, 0xbbbbbbbbbbbbbbbb, 0xcccccccccccccccc, 0xdddddddddddddddd }; @@ -165,7 +165,7 @@ TEST(uint256, left_shift) EXPECT_EQ(f, uint256_t(0)); } -TEST(uint256, and) +TEST(uint256, And) { uint256_t a = engine.get_random_uint256(); uint256_t b = engine.get_random_uint256(); @@ -178,7 +178,7 @@ TEST(uint256, and) EXPECT_EQ(c.data[3], a.data[3] & b.data[3]); } -TEST(uint256, or) +TEST(uint256, Or) { uint256_t a = engine.get_random_uint256(); uint256_t b = engine.get_random_uint256(); @@ -191,7 +191,7 @@ TEST(uint256, or) EXPECT_EQ(c.data[3], a.data[3] | b.data[3]); } -TEST(uint256, xor) +TEST(uint256, Xor) { uint256_t a = engine.get_random_uint256(); uint256_t b = engine.get_random_uint256(); @@ -204,7 +204,7 @@ TEST(uint256, xor) EXPECT_EQ(c.data[3], a.data[3] ^ b.data[3]); } -TEST(uint256, bit_not) +TEST(uint256, BitNot) { uint256_t a = engine.get_random_uint256(); @@ -216,7 +216,7 @@ TEST(uint256, bit_not) EXPECT_EQ(c.data[3], ~a.data[3]); } -TEST(uint256, logic_not) +TEST(uint256, LogicNot) { uint256_t a{ 1, 0, 0, 0 }; @@ -229,7 +229,7 @@ TEST(uint256, logic_not) EXPECT_EQ(!c, true); } -TEST(uint256, equality) +TEST(uint256, Equality) { uint256_t a{ 1, 0, 0, 0 }; uint256_t b{ 1, 0, 0, 0 }; @@ -249,7 +249,7 @@ TEST(uint256, equality) EXPECT_EQ(a == b, false); } -TEST(uint256, not_equal) +TEST(uint256, NotEqual) { uint256_t a{ 1, 0, 0, 0 }; uint256_t b{ 1, 0, 0, 0 }; @@ -269,7 +269,7 @@ TEST(uint256, not_equal) EXPECT_EQ(a != b, true); } -TEST(uint256, greater_than) +TEST(uint256, GreaterThan) { constexpr uint256_t a{ UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX }; constexpr uint256_t b{ UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX }; @@ -288,7 +288,7 @@ TEST(uint256, greater_than) EXPECT_EQ(a > f, true); } -TEST(uint256, greater_than_or_equal) +TEST(uint256, GreaterThanOrEqual) { uint256_t a{ UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX - 1 }; uint256_t b{ UINT64_MAX, UINT64_MAX, UINT64_MAX, UINT64_MAX }; @@ -308,10 +308,10 @@ TEST(uint256, greater_than_or_equal) EXPECT_EQ(a >= b, false); } -TEST(uint256, to_from_buffer) +TEST(uint256, ToFromBuffer) { uint256_t a{ 1, 2, 3, 4 }; auto buf = to_buffer(a); - uint256_t b = from_buffer(buf); + auto b = from_buffer(buf); EXPECT_EQ(a, b); } diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256_impl.hpp index 659f8e06f8d..9f4d4daa33d 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uint256/uint256_impl.hpp @@ -1,10 +1,10 @@ #pragma once #include "../bitop/get_msb.hpp" +#include "./uint256.hpp" #include "barretenberg/common/assert.hpp" - namespace numeric { -constexpr std::pair uint256_t::mul_wide(const uint64_t a, const uint64_t b) const +constexpr std::pair uint256_t::mul_wide(const uint64_t a, const uint64_t b) { const uint64_t a_lo = a & 0xffffffffULL; const uint64_t a_hi = a >> 32ULL; @@ -22,35 +22,31 @@ constexpr std::pair uint256_t::mul_wide(const uint64_t a, co } // compute a + b + carry, returning the carry -constexpr std::pair uint256_t::addc(const uint64_t a, - const uint64_t b, - const uint64_t carry_in) const +constexpr std::pair uint256_t::addc(const uint64_t a, const uint64_t b, const uint64_t carry_in) { const uint64_t sum = a + b; - const uint64_t carry_temp = sum < a; + const auto carry_temp = static_cast(sum < a); const uint64_t r = sum + carry_in; - const uint64_t carry_out = carry_temp + (r < carry_in); + const uint64_t carry_out = carry_temp + static_cast(r < carry_in); return { r, carry_out }; } -constexpr uint64_t uint256_t::addc_discard_hi(const uint64_t a, const uint64_t b, const uint64_t carry_in) const +constexpr uint64_t uint256_t::addc_discard_hi(const uint64_t a, const uint64_t b, const uint64_t carry_in) { return a + b + carry_in; } -constexpr std::pair uint256_t::sbb(const uint64_t a, - const uint64_t b, - const uint64_t borrow_in) const +constexpr std::pair uint256_t::sbb(const uint64_t a, const uint64_t b, const uint64_t borrow_in) { const uint64_t t_1 = a - (borrow_in >> 63ULL); - const uint64_t borrow_temp_1 = t_1 > a; + const auto borrow_temp_1 = static_cast(t_1 > a); const uint64_t t_2 = t_1 - b; - const uint64_t borrow_temp_2 = t_2 > t_1; + const auto borrow_temp_2 = static_cast(t_2 > t_1); return { t_2, 0ULL - (borrow_temp_1 | borrow_temp_2) }; } -constexpr uint64_t uint256_t::sbb_discard_hi(const uint64_t a, const uint64_t b, const uint64_t borrow_in) const +constexpr uint64_t uint256_t::sbb_discard_hi(const uint64_t a, const uint64_t b, const uint64_t borrow_in) { return a - b - (borrow_in >> 63ULL); } @@ -59,13 +55,13 @@ constexpr uint64_t uint256_t::sbb_discard_hi(const uint64_t a, const uint64_t b, constexpr std::pair uint256_t::mac(const uint64_t a, const uint64_t b, const uint64_t c, - const uint64_t carry_in) const + const uint64_t carry_in) { std::pair result = mul_wide(b, c); result.first += a; - const uint64_t overflow_c = (result.first < a); + const auto overflow_c = static_cast(result.first < a); result.first += carry_in; - const uint64_t overflow_carry = (result.first < carry_in); + const auto overflow_carry = static_cast(result.first < carry_in); result.second += (overflow_c + overflow_carry); return result; } @@ -73,7 +69,7 @@ constexpr std::pair uint256_t::mac(const uint64_t a, constexpr uint64_t uint256_t::mac_discard_hi(const uint64_t a, const uint64_t b, const uint64_t c, - const uint64_t carry_in) const + const uint64_t carry_in) { return (b * c + a + carry_in); } @@ -82,11 +78,14 @@ constexpr std::pair uint256_t::divmod(const uint256_t& b) { if (*this == 0 || b == 0) { return { 0, 0 }; - } else if (b == 1) { + } + if (b == 1) { return { *this, 0 }; - } else if (*this == b) { + } + if (*this == b) { return { 1, 0 }; - } else if (b > *this) { + } + if (b > *this) { return { 0, *this }; } @@ -186,11 +185,11 @@ constexpr bool uint256_t::get_bit(const uint64_t bit_index) const { ASSERT(bit_index < 256); if (bit_index > 255) { - return bool(0); + return static_cast(0); } - const size_t idx = static_cast(bit_index >> 6); + const auto idx = static_cast(bit_index >> 6); const size_t shift = bit_index & 63; - return bool((data[idx] >> shift) & 1); + return static_cast((data[idx] >> shift) & 1); } constexpr uint64_t uint256_t::get_msb() const @@ -319,7 +318,7 @@ constexpr uint256_t uint256_t::operator>>(const uint256_t& other) const { uint64_t total_shift = other.data[0]; - if (total_shift >= 256 || other.data[1] || other.data[2] || other.data[3]) { + if (total_shift >= 256 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) { return 0; } @@ -330,7 +329,7 @@ constexpr uint256_t uint256_t::operator>>(const uint256_t& other) const uint64_t num_shifted_limbs = total_shift >> 6ULL; uint64_t limb_shift = total_shift & 63ULL; - uint64_t shifted_limbs[4] = { 0 }; + std::array shifted_limbs = { 0, 0, 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = data[0]; @@ -356,8 +355,8 @@ constexpr uint256_t uint256_t::operator>>(const uint256_t& other) const } uint256_t result(0); - for (uint64_t i = 0; i < 4 - num_shifted_limbs; ++i) { - result.data[i] = shifted_limbs[i + num_shifted_limbs]; + for (size_t i = 0; i < 4 - num_shifted_limbs; ++i) { + result.data[i] = shifted_limbs[static_cast(i + num_shifted_limbs)]; } return result; @@ -367,7 +366,7 @@ constexpr uint256_t uint256_t::operator<<(const uint256_t& other) const { uint64_t total_shift = other.data[0]; - if (total_shift >= 256 || other.data[1] || other.data[2] || other.data[3]) { + if (total_shift >= 256 || (other.data[1] != 0U) || (other.data[2] != 0U) || (other.data[3] != 0U)) { return 0; } @@ -377,7 +376,7 @@ constexpr uint256_t uint256_t::operator<<(const uint256_t& other) const uint64_t num_shifted_limbs = total_shift >> 6ULL; uint64_t limb_shift = total_shift & 63ULL; - uint64_t shifted_limbs[4]{ 0 }; + std::array shifted_limbs = { 0, 0, 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = data[0]; @@ -403,8 +402,8 @@ constexpr uint256_t uint256_t::operator<<(const uint256_t& other) const } uint256_t result(0); - for (uint64_t i = 0; i < 4 - num_shifted_limbs; ++i) { - result.data[i + num_shifted_limbs] = shifted_limbs[i]; + for (size_t i = 0; i < 4 - num_shifted_limbs; ++i) { + result.data[static_cast(i + num_shifted_limbs)] = shifted_limbs[i]; } return result; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.hpp index 39827bdace0..aa5fc07b097 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.hpp @@ -42,9 +42,13 @@ template class uintx { , hi(other.hi) {} + constexpr uintx(uintx&& other) noexcept = default; + static constexpr size_t length() { return 2 * base_uint::length(); } constexpr uintx& operator=(const uintx& other) = default; + constexpr uintx& operator=(uintx&& other) noexcept = default; + constexpr ~uintx() = default; explicit constexpr operator bool() const { return static_cast(lo.data[0]); }; explicit constexpr operator uint8_t() const { return static_cast(lo.data[0]); }; explicit constexpr operator uint16_t() const { return static_cast(lo.data[0]); }; @@ -53,9 +57,9 @@ template class uintx { explicit constexpr operator base_uint() const { return lo; } - constexpr bool get_bit(const uint64_t bit_index) const; - constexpr uint64_t get_msb() const; - constexpr uintx slice(const uint64_t start, const uint64_t end) const; + [[nodiscard]] constexpr bool get_bit(uint64_t bit_index) const; + [[nodiscard]] constexpr uint64_t get_msb() const; + constexpr uintx slice(uint64_t start, uint64_t end) const; constexpr uintx operator+(const uintx& other) const; constexpr uintx operator-(const uintx& other) const; @@ -67,8 +71,8 @@ template class uintx { constexpr std::pair mul_extended(const uintx& other) const; - constexpr uintx operator>>(const uint64_t other) const; - constexpr uintx operator<<(const uint64_t other) const; + constexpr uintx operator>>(uint64_t other) const; + constexpr uintx operator<<(uint64_t other) const; constexpr uintx operator&(const uintx& other) const; constexpr uintx operator^(const uintx& other) const; @@ -174,18 +178,18 @@ template inline void write(B& it, uintx co write(it, value.lo); } -#include "./uintx_impl.hpp" - template inline std::ostream& operator<<(std::ostream& os, uintx const& a) { os << a.lo << ", " << a.hi << std::endl; return os; } -typedef uintx uint512_t; -typedef uintx uint1024_t; +using uint512_t = uintx; +using uint1024_t = uintx; } // namespace numeric -using numeric::uint1024_t; -using numeric::uint512_t; +#include "./uintx_impl.hpp" + +using numeric::uint1024_t; // NOLINT +using numeric::uint512_t; // NOLINT diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.test.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.test.cpp index 72fb9bd4056..91bc84f532b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.test.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx.test.cpp @@ -6,7 +6,7 @@ namespace { auto& engine = numeric::random::get_debug_engine(); } // namespace -TEST(uintx, get_bit) +TEST(uintx, GetBit) { constexpr uint256_t lo{ 0b0110011001110010011001100111001001100110011100100110011001110011, 0b1001011101101010101010100100101101101001001010010101110101010111, @@ -27,7 +27,7 @@ TEST(uintx, get_bit) EXPECT_EQ(a, res); } -TEST(uintx, mul) +TEST(uintx, Mul) { uint1024_t a = engine.get_random_uint1024(); uint1024_t b = engine.get_random_uint1024(); @@ -37,7 +37,7 @@ TEST(uintx, mul) EXPECT_EQ(c, d); } -TEST(uintx, div_and_mod) +TEST(uintx, DivAndMod) { for (size_t i = 0; i < 256; ++i) { uint1024_t a = engine.get_random_uint1024(); @@ -64,7 +64,7 @@ TEST(uintx, div_and_mod) } // We should not be depending on ecc in numeric. -TEST(uintx, DISABLED_mulmod) +TEST(uintx, DISABLEDMulmod) { /* barretenberg::fq a = barretenberg::fq::random_element(); @@ -100,7 +100,7 @@ TEST(uintx, DISABLED_mulmod) */ } -TEST(uintx, sub) +TEST(uintx, Sub) { uint1024_t a = engine.get_random_uint1024(); uint1024_t b = engine.get_random_uint1024(); @@ -131,7 +131,7 @@ TEST(uintx, sub) EXPECT_EQ(e.hi.hi.data[3], UINT64_MAX); } -TEST(uintx, and) +TEST(uintx, And) { uint1024_t a = engine.get_random_uint1024(); uint1024_t b = engine.get_random_uint1024(); @@ -142,7 +142,7 @@ TEST(uintx, and) EXPECT_EQ(c.hi, a.hi & b.hi); } -TEST(uintx, or) +TEST(uintx, Or) { uint1024_t a = engine.get_random_uint1024(); uint1024_t b = engine.get_random_uint1024(); @@ -153,7 +153,7 @@ TEST(uintx, or) EXPECT_EQ(c.hi, a.hi | b.hi); } -TEST(uintx, xor) +TEST(uintx, Xor) { uint1024_t a = engine.get_random_uint1024(); uint1024_t b = engine.get_random_uint1024(); @@ -164,7 +164,7 @@ TEST(uintx, xor) EXPECT_EQ(c.hi, a.hi ^ b.hi); } -TEST(uintx, bit_not) +TEST(uintx, BitNot) { uint1024_t a = engine.get_random_uint1024(); @@ -174,7 +174,7 @@ TEST(uintx, bit_not) EXPECT_EQ(c.hi, ~a.hi); } -TEST(uintx, logic_not) +TEST(uintx, LogicNot) { uint1024_t a(1); @@ -187,7 +187,7 @@ TEST(uintx, logic_not) EXPECT_EQ(!c, true); } -TEST(uintx, not_equal) +TEST(uintx, NotEqual) { uint1024_t a(1); uint1024_t b(1); @@ -198,7 +198,7 @@ TEST(uintx, not_equal) } // We should not be depending on ecc in numeric. -TEST(uintx, DISABLED_invmod) +TEST(uintx, DISABLEDInvmod) { /* uint256_t prime_lo = prime_256; @@ -212,43 +212,44 @@ TEST(uintx, DISABLED_invmod) */ } -TEST(uintx, invmod_regression_check) +TEST(uintx, InvmodRegressionCheck) { - const uint8_t _a[] = { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x0D, 0x6A, 0x2B, 0x19, 0x52, - 0x2D, 0xF7, 0xAF, 0xC7, 0x95, 0x68, 0x22, 0xD7, 0xF2, 0x21, 0xA3, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; - - const uint8_t _b[] = { 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x5D, 0x32, 0xDA, 0x10, - 0x4F, 0x1D, 0xD6, 0xCA, 0x50, 0x56, 0x11, 0x18, 0x18, 0xC2, 0xD4, 0x6C, 0x70, - 0x60, 0xD9, 0xB8, 0xFA, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF }; - - const uint8_t expected_result[] = { + const std::array _a = { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x0D, 0x6A, 0x2B, 0x19, 0x52, + 0x2D, 0xF7, 0xAF, 0xC7, 0x95, 0x68, 0x22, 0xD7, 0xF2, 0x21, 0xA3, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + + const std::array _b = { 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x5D, 0x32, 0xDA, 0x10, + 0x4F, 0x1D, 0xD6, 0xCA, 0x50, 0x56, 0x11, 0x18, 0x18, 0xC2, 0xD4, 0x6C, 0x70, + 0x60, 0xD9, 0xB8, 0xFA, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE2, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF }; + + const std::array expected_result = { 0x9F, 0x2F, 0xAA, 0x7B, 0xD7, 0x5A, 0x99, 0x56, 0x04, 0x68, 0x6C, 0x9D, 0xD8, 0x47, 0x6B, 0x52, 0xF0, 0x10, 0xD2, 0xA8, 0x62, 0x96, 0x60, 0x68, 0xBE, 0x18, 0x21, 0xA1, 0xCA, 0x6F, 0x41, 0x9C, 0x37, 0x42, 0x2F, 0xA3, 0x1B, 0x41, 0x7B, 0xAA, 0xEE, 0x6D, 0x9E, 0x03, 0x78, 0x71, 0xEF, 0xCF, 0x90, 0x85, 0xEF, 0x17, 0x59, 0xC4, 0xEE, 0x24, 0x80, 0xDE, 0x7A, 0x58, 0xA5, 0x42, 0x8F, 0x97, }; - uint8_t _res[64]; + std::array _res; - uint512_t a, b; + uint512_t a; + uint512_t b; - memcpy(a.lo.data, _a, 32); - memcpy(a.hi.data, _a + 32, 32); + memcpy(a.lo.data, &_a[0], 32); + memcpy(a.hi.data, &_a[0] + 32, 32); - memcpy(b.lo.data, _b, 32); - memcpy(b.hi.data, _b + 32, 32); + memcpy(b.lo.data, &_b[0], 32); + memcpy(b.hi.data, &_b[0] + 32, 32); const auto res = a.invmod(b); - memcpy(_res, res.lo.data, 32); - memcpy(_res + 32, res.hi.data, 32); + memcpy(&_res[0], res.lo.data, 32); + memcpy(&_res[0] + 32, res.hi.data, 32); - EXPECT_EQ(memcmp(_res, expected_result, sizeof(expected_result)), 0); + EXPECT_EQ(memcmp(&_res[0], &expected_result[0], sizeof(expected_result)), 0); } -TEST(uintx, DISABLED_r_inv) +TEST(uintx, DISABLEDRInv) { /* uint512_t r{ 0, 1 }; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx_impl.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx_impl.hpp index 7a0a653e3ba..314a754b6ae 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx_impl.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/numeric/uintx/uintx_impl.hpp @@ -1,17 +1,22 @@ #pragma once +#include "./uintx.hpp" #include "barretenberg/common/assert.hpp" +namespace numeric { template constexpr std::pair, uintx> uintx::divmod(const uintx& b) const { ASSERT(b != 0); if (*this == 0) { return { uintx(0), uintx(0) }; - } else if (b == 1) { + } + if (b == 1) { return { *this, uintx(0) }; - } else if (*this == b) { + } + if (*this == b) { return { uintx(1), uintx(0) }; - } else if (b > *this) { + } + if (b > *this) { return { uintx(0), *this }; } @@ -135,7 +140,7 @@ template constexpr uintx uintx::operator { base_uint res_lo = lo + other.lo; bool carry = res_lo < lo; - base_uint res_hi = hi + other.hi + ((carry == true) ? base_uint(1) : base_uint(0)); + base_uint res_hi = hi + other.hi + ((carry) ? base_uint(1) : base_uint(0)); return { res_lo, res_hi }; }; @@ -143,7 +148,7 @@ template constexpr uintx uintx::operator { base_uint res_lo = lo - other.lo; bool borrow = res_lo > lo; - base_uint res_hi = hi - other.hi - ((borrow == true) ? base_uint(1) : base_uint(0)); + base_uint res_hi = hi - other.hi - ((borrow) ? base_uint(1) : base_uint(0)); return { res_lo, res_hi }; } @@ -274,7 +279,7 @@ template constexpr uintx uintx::operator const uint64_t limb_shift = total_shift & static_cast(base_uint::length() - 1); - base_uint shifted_limbs[2] = { 0 }; + std::array shifted_limbs = { 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = lo; shifted_limbs[1] = hi; @@ -309,7 +314,7 @@ template constexpr uintx uintx::operator const uint64_t num_shifted_limbs = total_shift >> (base_uint(base_uint::length()).get_msb()); const uint64_t limb_shift = total_shift & static_cast(base_uint::length() - 1); - base_uint shifted_limbs[2] = { 0 }; + std::array shifted_limbs = { 0, 0 }; if (limb_shift == 0) { shifted_limbs[0] = lo; shifted_limbs[1] = hi; @@ -331,3 +336,4 @@ template constexpr uintx uintx::operator } return result; } +} // namespace numeric \ No newline at end of file