From 0d447c9b8791d2f4f12f4066243a2c763585d888 Mon Sep 17 00:00:00 2001 From: yshekel Date: Wed, 21 Aug 2024 22:06:51 +0300 Subject: [PATCH] make features (G2,ecntt) default on (#583) Default on is necessary when loading a pre-built backend to avoid link errors. --- .github/workflows/v3.yml | 4 +- .github/workflows/v3_rust.yml | 8 +- docs/docs/icicle/getting_started.md | 141 +++++++++++------- examples/c++/best-practice-ntt/run.sh | 5 +- examples/c++/msm/run.sh | 5 +- examples/c++/ntt/run.sh | 5 +- examples/c++/pedersen-commitment/run.sh | 3 +- examples/c++/polynomial-api/example.cpp | 2 +- examples/c++/polynomial-api/run.sh | 3 +- examples/c++/polynomial_multiplication/run.sh | 5 +- examples/c++/risc0/run.sh | 3 +- examples/rust/msm/Cargo.toml | 9 +- examples/rust/msm/run.sh | 2 +- examples/rust/ntt/Cargo.toml | 11 +- examples/rust/polynomials/Cargo.toml | 9 +- icicle_v3/CMakeLists.txt | 32 ++-- icicle_v3/backend/cpu/CMakeLists.txt | 10 +- icicle_v3/cmake/curve.cmake | 86 +++++++---- icicle_v3/cmake/field.cmake | 102 +++++++------ icicle_v3/cmake/fields_and_curves.cmake | 17 +++ icicle_v3/cmake/target_editor.cmake | 72 +++++++++ icicle_v3/include/icicle/ntt.h | 2 +- icicle_v3/src/ntt.cpp | 13 +- icicle_v3/src/runtime.cpp | 2 +- icicle_v3/tests/test_curve_api.cpp | 9 +- icicle_v3/tests/test_field_api.cpp | 4 +- wrappers/rust_v3/icicle-core/Cargo.toml | 4 +- wrappers/rust_v3/icicle-core/src/ecntt/mod.rs | 1 - .../rust_v3/icicle-core/src/ecntt/tests.rs | 1 - wrappers/rust_v3/icicle-core/src/ntt/mod.rs | 8 +- .../icicle-curves/icicle-bls12-377/Cargo.toml | 5 +- .../icicle-bls12-377/benches/ecntt.rs | 8 +- .../icicle-curves/icicle-bls12-377/build.rs | 53 ++++--- .../icicle-bls12-377/src/curve.rs | 10 +- .../icicle-bls12-377/src/ecntt/mod.rs | 1 - .../icicle-curves/icicle-bls12-377/src/lib.rs | 4 +- .../icicle-bls12-377/src/msm/mod.rs | 8 +- .../icicle-curves/icicle-bls12-381/Cargo.toml | 4 +- .../icicle-bls12-381/benches/ecntt.rs | 8 +- .../icicle-curves/icicle-bls12-381/build.rs | 27 ++-- .../icicle-bls12-381/src/curve.rs | 10 +- .../icicle-bls12-381/src/ecntt/mod.rs | 1 - .../icicle-curves/icicle-bls12-381/src/lib.rs | 4 +- .../icicle-bls12-381/src/msm/mod.rs | 8 +- .../icicle-curves/icicle-bn254/Cargo.toml | 4 +- .../icicle-bn254/benches/ecntt.rs | 8 +- .../icicle-curves/icicle-bn254/build.rs | 26 ++-- .../icicle-curves/icicle-bn254/src/curve.rs | 10 +- .../icicle-bn254/src/ecntt/mod.rs | 1 - .../icicle-curves/icicle-bn254/src/lib.rs | 4 +- .../icicle-curves/icicle-bn254/src/msm/mod.rs | 8 +- .../icicle-curves/icicle-bw6-761/Cargo.toml | 4 +- .../icicle-curves/icicle-bw6-761/src/curve.rs | 6 +- .../icicle-bw6-761/src/msm/mod.rs | 8 +- .../icicle-curves/icicle-grumpkin/Cargo.toml | 3 +- .../icicle-curves/icicle-grumpkin/build.rs | 12 +- .../icicle-grumpkin/src/msm/mod.rs | 3 +- .../icicle-fields/icicle-babybear/build.rs | 13 +- wrappers/rust_v3/icicle-runtime/build.rs | 12 +- 59 files changed, 533 insertions(+), 318 deletions(-) create mode 100644 icicle_v3/cmake/fields_and_curves.cmake create mode 100644 icicle_v3/cmake/target_editor.cmake diff --git a/.github/workflows/v3.yml b/.github/workflows/v3.yml index 3ca9714b9..43d42a776 100644 --- a/.github/workflows/v3.yml +++ b/.github/workflows/v3.yml @@ -89,7 +89,7 @@ jobs: - name: Run C++ curve Tests working-directory: ./icicle_v3/build/tests if: needs.check-changed-files.outputs.cpp_cuda == 'true' - run: ctest --verbose + run: ctest test-linux-field: name: Test on Linux @@ -123,4 +123,4 @@ jobs: - name: Run C++ field Tests working-directory: ./icicle_v3/build/tests if: needs.check-changed-files.outputs.cpp_cuda == 'true' - run: ctest --verbose \ No newline at end of file + run: ctest \ No newline at end of file diff --git a/.github/workflows/v3_rust.yml b/.github/workflows/v3_rust.yml index 94d920514..4482cee56 100644 --- a/.github/workflows/v3_rust.yml +++ b/.github/workflows/v3_rust.yml @@ -76,7 +76,7 @@ jobs: if: needs.check-changed-files.outputs.rust == 'true' || needs.check-changed-files.outputs.cpp_cuda == 'true' # tests are split to phases since NTT domain is global but tests have conflicting requirements run: | - cargo build --release --verbose --features=g2,ec_ntt - cargo test --workspace --release --verbose --features=g2,ec_ntt,cuda_backend -- --skip phase - cargo test phase2 --workspace --release --verbose --features=g2,ec_ntt,cuda_backend - cargo test phase3 --workspace --release --verbose --features=g2,ec_ntt,cuda_backend + cargo build --workspace --release --features=cuda_backend + cargo test --workspace --release --verbose --features=cuda_backend -- --skip phase + cargo test phase2 --workspace --release --features=cuda_backend + cargo test phase3 --workspace --release --features=cuda_backend diff --git a/docs/docs/icicle/getting_started.md b/docs/docs/icicle/getting_started.md index 0cf1401df..c728a0cd5 100644 --- a/docs/docs/icicle/getting_started.md +++ b/docs/docs/icicle/getting_started.md @@ -18,14 +18,21 @@ ICICLE can be built and tested in C++ using CMake. The build process is straight ``` 2. **Configure the build:** - ```bash + ```bash mkdir -p build && rm -rf build/* cmake -S icicle -B build -DFIELD=babybear ``` -:::note - To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31. - To specify a curve, use the flag -DCURVE=curve, where curve can be one of the following: bn254, bls12_377, bls12_381, bw6_761, grumpkin. +:::info +To specify the field, use the flag -DFIELD=field, where field can be one of the following: babybear, stark252, m31. + +To specify a curve, use the flag -DCURVE=curve, where curve can be one of the following: bn254, bls12_377, bls12_381, bw6_761, grumpkin. +::: + +:::tip +If you have access to cuda backend repo, it can be built along ICICLE frontend by adding the following to the cmake command +- `-DCUDA_BACKEND=local` # if you have it locally +- `-DCUDA_BACKEND=` # to pull CUDA backend, given you have access ::: 3. **Build the project:** @@ -35,39 +42,39 @@ ICICLE can be built and tested in C++ using CMake. The build process is straight This is building the [libicicle_device](./libraries.md#icicle-device) and the [libicicle_field_babybear](./libraries.md#icicle-core) frontend lib that correspond to the field or curve. 4. **Link:** - Link you application (or library) to ICICLE: - ```cmake - target_link_libraries(yourApp PRIVATE icicle_field_babybear icicle_device) - ``` +Link you application (or library) to ICICLE: +```cmake +target_link_libraries(yourApp PRIVATE icicle_field_babybear icicle_device) +``` 5. **Installation (optional):** - To install the libs, specify the install prefix in the [cmake command](./getting_started.md#build-commands) - `-DCMAKE_INSTALL_PREFIX=/install/dir/`. Default install path on linux is `/usr/local` if not specified. For other systems it may differ. The cmake command will print it to the log - ``` - -- CMAKE_INSTALL_PREFIX=/install/dir/for/cmake/install - ``` - Then after building, use cmake to install the libraries: - ``` - cmake -S icicle -B build -DFIELD=babybear -DCMAKE_INSTALL_PREFIX=/path/to/install/dir/ - cmake --build build -j # build - cmake --install build # install icicle to /path/to/install/dir/ - ``` +To install the libs, specify the install prefix in the [cmake command](./getting_started.md#build-commands) +`-DCMAKE_INSTALL_PREFIX=/install/dir/`. Default install path on linux is `/usr/local` if not specified. For other systems it may differ. The cmake command will print it to the log +``` +-- CMAKE_INSTALL_PREFIX=/install/dir/for/cmake/install +``` +Then after building, use cmake to install the libraries: +``` +cmake -S icicle -B build -DFIELD=babybear -DCMAKE_INSTALL_PREFIX=/path/to/install/dir/ +cmake --build build -j # build +cmake --install build # install icicle to /path/to/install/dir/ +``` 6. **Run tests (optional):** - Add `-DBUILD_TESTS=ON` to the [cmake command](./getting_started.md#build-commands) and build. - Execute all tests - ```bash - cmake -S icicle -B build -DFIELD=babybear -DBUILD_TESTS=ON - cmake --build build -j - cd build/tests - ctest - ``` - or choose the test-suite - ```bash - ./build/tests/test_field_api # or another test suite - # can specify tests using regex. For example for tests with ntt in the name: - ./build/tests/test_field_api --gtest_filter="*ntt*" - ``` +Add `-DBUILD_TESTS=ON` to the [cmake command](./getting_started.md#build-commands) and build. +Execute all tests +```bash +cmake -S icicle -B build -DFIELD=babybear -DBUILD_TESTS=ON +cmake --build build -j +cd build/tests +ctest +``` +or choose the test-suite +```bash +./build/tests/test_field_api # or another test suite +# can specify tests using regex. For example for tests with ntt in the name: +./build/tests/test_field_api --gtest_filter="*ntt*" +``` :::note Most tests assume a cuda backend exists and will fail otherwise if cannot find a CUDA device. ::: @@ -81,30 +88,61 @@ You can customize your ICICLE build with the following flags: - `-DBUILD_TESTS=ON/OFF`: Enable or disable tests. `default=OFF`. - `-DBUILD_BENCHMARKS=ON/OFF`: Enable or disable benchmarks. `default=OFF`. +#### Features + +By default, all [features](./libraries.md#supported-curves-and-operations) are enabled. +This is since installed backends may implement and register all APIs. Missing APIs in the frontend would cause linkage to fail due to missing symbols. Therefore by default we include them in the frontend part too. + +To disable features, add the following to the cmake command. +- ntt: `-DNTT=OFF` +- msm: `-DMSM=OFF` +- g2 msm: `-DG2=OFF` +- ecntt: `-DECNTT=OFF` +- extension field: `-DEXT_FIELD=OFF` + +:::tip +Disabling features is useful when developing with a backend that is slow to compile (e.g. CUDA backend); +::: + ### Rust: Build, Test, and Install To build and test ICICLE in Rust, follow these steps: 1. **Navigate to the Rust bindings directory:** - ```bash - cd wrappers/rust # or go to a specific field/curve 'cd wrappers/rust/icicle-fields/icicle-babybear' - ``` +```bash +cd wrappers/rust # or go to a specific field/curve 'cd wrappers/rust/icicle-fields/icicle-babybear' +``` 2. **Build the Rust project:** -TODO what about features? Now it doesn't make sense to disable features. - ```bash - cargo build --release - ``` +```bash +cargo build --release +``` +By default, all [supported features are enabled](#features). +Cargo features are used to disable features, rather than enable them, for the reason explained [here](#features): +- `no_g2` to disable G2 MSM +- `no_ecntt` to disable ECNTT + +They can be disabled as follows: +```bash +cargo build --release --no-default-features --features=no_ecntt,no_g2 +``` -4. **Run tests:** - ```bash - cargo test - ``` +:::note +If you have access to cuda backend repo, it can be built along ICICLE frontend by using the following cargo features: +- `cuda_backend` : if the cuda backend resides in `icicle/backend/cuda` +- `pull_cuda_backend` : to pull main branch and build it +::: + + +3. **Run tests:** +```bash +cargo test # optional: --features=no_ecntt,no_g2,cuda_backend +``` :::note Most tests assume a CUDA backend is installed and fail otherwise. ::: -5. **Install the library:** +4. **Install the library:** By default, the libraries are installed to the `target//deps/icicle` dir. For custom install dir. define the env variable: ```bash @@ -114,7 +152,6 @@ export ICICLE_INSTALL_DIR=/path/to/install/dir (TODO: cargo install ?) #### Use as cargo dependency - In cargo.toml, specify the ICICLE libs to use: ```bash @@ -125,13 +162,17 @@ icicle-bls12-377 = { path = "git = "https://github.com/ingonyama-zk/icicle.git" # add other ICICLE crates here if need additional fields/curves ``` -:::note Can specify `branch = ` or `tag = ` or `rev = `. -::: + +To disable features: +```bash +icicle-bls12-377 = { path = "git = "https://github.com/ingonyama-zk/icicle.git", features = ["no_g2"] } +``` As explained above, the libs will be built and installed to `target//deps/icicle` so you can easily link to them. Alternatively you can set `ICICLE_INSTALL_DIR` env variable for a custom install directory. -:::note -Make sure to install the icicle libs when installing a library/application that depends on icicle. + +:::warning +Make sure to install icicle libs when installing a library/application that depends on icicle such that it is located at runtime. ::: ### Go: Build, Test, and Install (TODO) diff --git a/examples/c++/best-practice-ntt/run.sh b/examples/c++/best-practice-ntt/run.sh index d8062fe53..6e5c102e3 100755 --- a/examples/c++/best-practice-ntt/run.sh +++ b/examples/c++/best-practice-ntt/run.sh @@ -51,11 +51,12 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -S "${ICILE_DIR}" -B build/icicle + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/msm/run.sh b/examples/c++/msm/run.sh index db4529b87..e15be1c87 100755 --- a/examples/c++/msm/run.sh +++ b/examples/c++/msm/run.sh @@ -51,11 +51,12 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DG2=ON -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DG2=ON -S "${ICILE_DIR}" -B build/icicle + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/ntt/run.sh b/examples/c++/ntt/run.sh index dbf7cceda..aa3d714c1 100755 --- a/examples/c++/ntt/run.sh +++ b/examples/c++/ntt/run.sh @@ -51,11 +51,12 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -S "${ICILE_DIR}" -B build/icicle + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/pedersen-commitment/run.sh b/examples/c++/pedersen-commitment/run.sh index 0f2729f0d..ed295689e 100755 --- a/examples/c++/pedersen-commitment/run.sh +++ b/examples/c++/pedersen-commitment/run.sh @@ -51,10 +51,11 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/polynomial-api/example.cpp b/examples/c++/polynomial-api/example.cpp index 79aa3caa6..06e33fa5c 100644 --- a/examples/c++/polynomial-api/example.cpp +++ b/examples/c++/polynomial-api/example.cpp @@ -316,7 +316,7 @@ void example_device_memory_view() auto coset_evals = std::make_unique(size); auto ntt_config = default_ntt_config(); ntt_config.are_inputs_on_device = true; // using the device data directly as a view - ntt_config.coset_gen = get_root_of_unity(size * 2); + ICICLE_CHECK(get_root_of_unity(size * 2, &ntt_config.coset_gen)); ntt(d_coeffs.get(), size, NTTDir::kForward, ntt_config, coset_evals.get()); } diff --git a/examples/c++/polynomial-api/run.sh b/examples/c++/polynomial-api/run.sh index 0f2729f0d..ed295689e 100755 --- a/examples/c++/polynomial-api/run.sh +++ b/examples/c++/polynomial-api/run.sh @@ -51,10 +51,11 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/polynomial_multiplication/run.sh b/examples/c++/polynomial_multiplication/run.sh index d8062fe53..6e5c102e3 100755 --- a/examples/c++/polynomial_multiplication/run.sh +++ b/examples/c++/polynomial_multiplication/run.sh @@ -51,11 +51,12 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" - cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -DMSM=OFF -S "${ICILE_DIR}" -B build/icicle + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" + cmake -DCMAKE_BUILD_TYPE=Release -DCURVE=bn254 -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/c++/risc0/run.sh b/examples/c++/risc0/run.sh index de75fff93..4488aef03 100755 --- a/examples/c++/risc0/run.sh +++ b/examples/c++/risc0/run.sh @@ -51,10 +51,11 @@ ICICLE_CUDA_SOURCE_DIR="${ICILE_DIR}/backend/cuda" # Build Icicle and the example app that links to it if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ -d "${ICICLE_CUDA_SOURCE_DIR}" ]; then echo "Building icicle with CUDA backend" - cmake -DCMAKE_BUILD_TYPE=Release -DFIELD=babybear -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle + cmake -DCMAKE_BUILD_TYPE=Release -DFIELD=babybear -DMSM=OFF -DG2=OFF -DECNTT=OFF -DCUDA_BACKEND=local -S "${ICILE_DIR}" -B build/icicle export ICICLE_BACKEND_INSTALL_DIR=$(realpath "build/icicle/backend") else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" cmake -DCMAKE_BUILD_TYPE=Release -DFIELD=babybear -S "${ICILE_DIR}" -B build/icicle fi cmake -DCMAKE_BUILD_TYPE=Release -S . -B build/example diff --git a/examples/rust/msm/Cargo.toml b/examples/rust/msm/Cargo.toml index 7e4489ea3..9d80f56dd 100644 --- a/examples/rust/msm/Cargo.toml +++ b/examples/rust/msm/Cargo.toml @@ -6,9 +6,14 @@ edition = "2018" [dependencies] icicle-runtime = { path = "../../../wrappers/rust_v3/icicle-runtime" } icicle-core = { path = "../../../wrappers/rust_v3/icicle-core" } -icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254", features = ["g2"] } +icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254" } icicle-bls12-377 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bls12-377" } clap = { version = "<=4.4.12", features = ["derive"] } [features] -cuda = ["icicle-runtime/cuda_backend", "icicle-bn254/cuda_backend", "icicle-bls12-377/cuda_backend"] +cuda = ["icicle-runtime/cuda_backend", + "icicle-bn254/cuda_backend", + "icicle-bls12-377/cuda_backend", + "icicle-bn254/no_ecntt", + "icicle-bls12-377/no_ecntt" +] diff --git a/examples/rust/msm/run.sh b/examples/rust/msm/run.sh index f8feb4306..817766fb7 100755 --- a/examples/rust/msm/run.sh +++ b/examples/rust/msm/run.sh @@ -56,6 +56,6 @@ if [ "$DEVICE_TYPE" == "CUDA" ] && [ ! -d "${ICICLE_BACKEND_INSTALL_DIR}" ] && [ cargo run --release --features=cuda -- --device-type "${DEVICE_TYPE}" else echo "Building icicle without CUDA backend, ICICLE_BACKEND_INSTALL_DIR=${ICICLE_BACKEND_INSTALL_DIR}" - export ICICLE_BACKEND_INSTALL_DIR="$ICICLE_BACKEND_INSTALL_DIR"; + export ICICLE_BACKEND_INSTALL_DIR="${ICICLE_BACKEND_INSTALL_DIR}" cargo run --release -- --device-type "${DEVICE_TYPE}" fi diff --git a/examples/rust/ntt/Cargo.toml b/examples/rust/ntt/Cargo.toml index 0e6dd3241..b8d9117ac 100644 --- a/examples/rust/ntt/Cargo.toml +++ b/examples/rust/ntt/Cargo.toml @@ -6,10 +6,17 @@ edition = "2018" [dependencies] icicle-runtime = { path = "../../../wrappers/rust_v3/icicle-runtime" } icicle-core = { path = "../../../wrappers/rust_v3/icicle-core" } -icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254", features = ["g2"] } +icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254" } icicle-bls12-377 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bls12-377" } clap = { version = "<=4.4.12", features = ["derive"] } [features] -cuda = ["icicle-runtime/cuda_backend", "icicle-bn254/cuda_backend", "icicle-bls12-377/cuda_backend"] +cuda = ["icicle-runtime/cuda_backend", + "icicle-bn254/cuda_backend", + "icicle-bn254/no_ecntt", + "icicle-bn254/no_g2", + "icicle-bls12-377/cuda_backend", + "icicle-bls12-377/no_ecntt", + "icicle-bls12-377/no_g2" +] diff --git a/examples/rust/polynomials/Cargo.toml b/examples/rust/polynomials/Cargo.toml index 0553e9594..ad038cb73 100644 --- a/examples/rust/polynomials/Cargo.toml +++ b/examples/rust/polynomials/Cargo.toml @@ -6,11 +6,16 @@ edition = "2018" [dependencies] icicle-runtime = { path = "../../../wrappers/rust_v3/icicle-runtime" } icicle-core = { path = "../../../wrappers/rust_v3/icicle-core" } -icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254", features = ["g2"] } +icicle-bn254 = { path = "../../../wrappers/rust_v3/icicle-curves/icicle-bn254" } icicle-babybear = { path = "../../../wrappers/rust_v3/icicle-fields/icicle-babybear" } clap = { version = "<=4.4.12", features = ["derive"] } [features] -cuda = ["icicle-runtime/cuda_backend", "icicle-bn254/cuda_backend", "icicle-babybear/cuda_backend"] +cuda = ["icicle-runtime/cuda_backend", + "icicle-bn254/cuda_backend", + "icicle-babybear/cuda_backend", + "icicle-bn254/no_ecntt", + "icicle-bn254/no_g2", +] diff --git a/icicle_v3/CMakeLists.txt b/icicle_v3/CMakeLists.txt index 78d1cb65f..89d23255e 100644 --- a/icicle_v3/CMakeLists.txt +++ b/icicle_v3/CMakeLists.txt @@ -19,17 +19,17 @@ message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") set(CMAKE_POSITION_INDEPENDENT_CODE ON) -# TODO Yuval remove those flags. Frontend will always have everything. -# CUDA backend may do it for faster dev. +# Build options +option(BUILD_TESTS "Build unit tests. Default=OFF" OFF) +option(CPU_BACKEND "Build CPU backend. Default=ON" ON) +option(CUDA_BACKEND "Branch/commit to pull for CUDA backend or `local` if under icicle/backend/cuda. Default=OFF" OFF) + +# features that some fields/curves have and some don't. option(NTT "Build NTT" ON) +option(ECNTT "Build ECNTT" ON) option(MSM "Build MSM" ON) -option(EXT_FIELD "Build extension field" OFF) -option(ECNTT "Build ECNTT" OFF) -option(G2 "Build G2" OFF) -# REMOVE END -option(BUILD_TESTS "Build unit tests" OFF) -option(CPU_BACKEND "Build CPU backend" ON) -option(CUDA_BACKEND "Branch/commit to pull for CUDA backend, local path or OFF to disable pulling" OFF) +option(G2 "Build G2 MSM" ON) +option(EXT_FIELD "Build extension field" ON) # device API library add_library(icicle_device SHARED @@ -62,11 +62,15 @@ endif() # curve is building the scalar field too if(CURVE) - check_curve() - setup_curve_target() + set(CURVE_INDEX -1) + set(FEATURES_STRING "") + check_curve(${CURVE} CURVE_INDEX FEATURES_STRING) + setup_curve_target(${CURVE} ${CURVE_INDEX} ${FEATURES_STRING}) elseif(FIELD) - check_field() - setup_field_target() + set(FIELD_INDEX -1) + set(FEATURES_STRING "") + check_field(${FIELD} FIELD_INDEX FEATURES_STRING) + setup_field_target(${FIELD} ${FIELD_INDEX} ${FEATURES_STRING}) endif() if (CPU_BACKEND) @@ -86,7 +90,7 @@ if (CUDA_BACKEND) set(CUDA_BACKEND_URL "git@github.com:ingonyama-zk/icicle-cuda-backend.git") include(FetchContent) - message("-- Fetching cuda backend from ${CUDA_BACKEND_URL}:${CUDA_BACKEND}") + message(STATUS "Fetching cuda backend from ${CUDA_BACKEND_URL}:${CUDA_BACKEND}") FetchContent_Declare( cuda_backend GIT_REPOSITORY ${CUDA_BACKEND_URL} diff --git a/icicle_v3/backend/cpu/CMakeLists.txt b/icicle_v3/backend/cpu/CMakeLists.txt index 733b3d6a1..5467d3da6 100644 --- a/icicle_v3/backend/cpu/CMakeLists.txt +++ b/icicle_v3/backend/cpu/CMakeLists.txt @@ -7,20 +7,22 @@ target_sources(icicle_device PRIVATE src/cpu_device_api.cpp) # field API library if (FIELD) target_sources(icicle_field PRIVATE - src/field/cpu_vec_ops.cpp + src/field/cpu_vec_ops.cpp ) if (NTT) target_sources(icicle_field PRIVATE src/field/cpu_ntt.cpp src/polynomials/cpu_polynomial_backend.cpp) - endif() + endif() target_include_directories(icicle_field PRIVATE include) endif() # FIELD # curve API library if (CURVE) -target_sources(icicle_curve PRIVATE - src/curve/cpu_msm.cpp + target_sources(icicle_curve PRIVATE src/curve/cpu_mont_conversion.cpp ) + if (MSM) + target_sources(icicle_curve PRIVATE src/curve/cpu_msm.cpp) + endif() if (ECNTT) target_sources(icicle_curve PRIVATE src/curve/cpu_ecntt.cpp) endif() diff --git a/icicle_v3/cmake/curve.cmake b/icicle_v3/cmake/curve.cmake index 73e6067bc..c82d1b90b 100644 --- a/icicle_v3/cmake/curve.cmake +++ b/icicle_v3/cmake/curve.cmake @@ -1,49 +1,71 @@ -function(check_curve) - set(SUPPORTED_CURVES bn254;bls12_381;bls12_377;bw6_761;grumpkin) +include(cmake/fields_and_curves.cmake) +include(cmake/target_editor.cmake) +function(extract_curve_names CURVE_NAMES_OUT) + set(CURVE_NAMES "") + + foreach (ITEM ${ICICLE_CURVES}) + string(REPLACE ":" ";" ITEM_SPLIT ${ITEM}) + list(GET ITEM_SPLIT 1 CURVE_NAME) + list(APPEND CURVE_NAMES ${CURVE_NAME}) + endforeach() + + # Output the list of curve names + set(${CURVE_NAMES_OUT} "${CURVE_NAMES}" PARENT_SCOPE) +endfunction() + +function(check_curve CURVE CURVE_INDEX_OUT FEATURES_STRING_OUT) set(IS_CURVE_SUPPORTED FALSE) - set(I 0) - foreach (SUPPORTED_CURVE ${SUPPORTED_CURVES}) - math(EXPR I "${I} + 1") - if (CURVE STREQUAL SUPPORTED_CURVE) - add_compile_definitions(FIELD_ID=${I}) - add_compile_definitions(CURVE_ID=${I}) + foreach (ITEM ${ICICLE_CURVES}) + string(REPLACE ":" ";" ITEM_SPLIT ${ITEM}) + list(GET ITEM_SPLIT 0 CURVE_INDEX) + list(GET ITEM_SPLIT 1 CURVE_NAME) + list(GET ITEM_SPLIT 2 FEATURES_STRING) + + if (CURVE STREQUAL CURVE_NAME) set(IS_CURVE_SUPPORTED TRUE) + message(STATUS "building CURVE_NAME=${CURVE_NAME} ; CURVE_INDEX=${CURVE_INDEX} ; SUPPORTED_FEATURES=${FEATURES_STRING}") + # Output the CURVE_INDEX and FEATURES_STRING + set(${CURVE_INDEX_OUT} "${CURVE_INDEX}" PARENT_SCOPE) + set(${FEATURES_STRING_OUT} "${FEATURES_STRING}" PARENT_SCOPE) + break() endif () endforeach() if (NOT IS_CURVE_SUPPORTED) - message( FATAL_ERROR "The value of CURVE variable: ${CURVE} is not one of the supported curves: ${SUPPORTED_CURVES}" ) + set(ALL_CURVES "") + extract_curve_names(ALL_CURVES) + message(FATAL_ERROR "The value of CURVE variable: ${CURVE} is not supported: choose from [${ALL_CURVES}]") endif () endfunction() -function(setup_curve_target) - set(FIELD ${CURVE}) - setup_field_target() - add_library(icicle_curve SHARED - src/msm.cpp - src/curves/ffi_extern.cpp - src/curves/montgomery_conversion.cpp - ) - target_link_libraries(icicle_curve PUBLIC icicle_device icicle_field pthread) +function(setup_curve_target CURVE CURVE_INDEX FEATURES_STRING) + # the scalar field of the curve is built to a field library (like babybear is built) + setup_field_target(${CURVE} ${CURVE_INDEX} ${FEATURES_STRING}) + + add_library(icicle_curve SHARED) + + # Split FEATURES_STRING into a list using "," as the separator + string(REPLACE "," ";" FEATURES_LIST ${FEATURES_STRING}) + + # customize the curve lib to choose what to include + handle_curve(icicle_curve) # basic curve and field methods, including vec ops + # Handle features + handle_msm(icicle_curve "${FEATURES_LIST}") + handle_g2(icicle_curve "${FEATURES_LIST}") + handle_ecntt(icicle_curve "${FEATURES_LIST}") + # Add additional feature handling calls here + set_target_properties(icicle_curve PROPERTIES OUTPUT_NAME "icicle_curve_${CURVE}") + target_link_libraries(icicle_curve PUBLIC icicle_device icicle_field pthread) - # Make sure CURVE is defined in the cache for backends to see + # Ensure CURVE is defined in the cache for backends to see set(CURVE "${CURVE}" CACHE STRING "") - target_compile_definitions(icicle_curve PUBLIC CURVE=${CURVE}) - if (G2) - set(G2 "${G2}" CACHE STRING "") - target_compile_definitions(icicle_curve PUBLIC G2=${G2}) - endif() - if (ECNTT) - target_sources(icicle_curve PRIVATE src/ecntt.cpp) - set(ECNTT "${ECNTT}" CACHE STRING "") - target_compile_definitions(icicle_curve PUBLIC ECNTT=${ECNTT}) - endif() + target_compile_definitions(icicle_curve PUBLIC CURVE=${CURVE} CURVE_ID=${CURVE_INDEX}) install(TARGETS icicle_curve - RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" - LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" - ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib") + RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" + LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/") endfunction() \ No newline at end of file diff --git a/icicle_v3/cmake/field.cmake b/icicle_v3/cmake/field.cmake index f783ce1f6..92f668539 100644 --- a/icicle_v3/cmake/field.cmake +++ b/icicle_v3/cmake/field.cmake @@ -1,59 +1,67 @@ -function(check_field) - set(SUPPORTED_FIELDS babybear;stark252) +include(cmake/fields_and_curves.cmake) +include(cmake/target_editor.cmake) +function(extract_field_names FIELD_NAMES_OUT) + set(FIELD_NAMES "") + + foreach (ITEM ${ICICLE_FIELDS}) + string(REPLACE ":" ";" ITEM_SPLIT ${ITEM}) + list(GET ITEM_SPLIT 1 FIELD_NAME) + list(APPEND FIELD_NAMES ${FIELD_NAME}) + endforeach() + + # Output the list of field names + set(${FIELD_NAMES_OUT} "${FIELD_NAMES}" PARENT_SCOPE) +endfunction() + +function(check_field FIELD FIELD_INDEX_OUT FEATURES_STRING_OUT) set(IS_FIELD_SUPPORTED FALSE) - set(I 1000) - foreach (SUPPORTED_FIELD ${SUPPORTED_FIELDS}) - math(EXPR I "${I} + 1") - if (FIELD STREQUAL SUPPORTED_FIELD) - add_compile_definitions(FIELD_ID=${I}) + foreach (ITEM ${ICICLE_FIELDS}) + string(REPLACE ":" ";" ITEM_SPLIT ${ITEM}) + list(GET ITEM_SPLIT 0 FIELD_INDEX) + list(GET ITEM_SPLIT 1 FIELD_NAME) + list(GET ITEM_SPLIT 2 FEATURES_STRING) + + if (FIELD STREQUAL FIELD_NAME) set(IS_FIELD_SUPPORTED TRUE) + message(STATUS "building FIELD_NAME=${FIELD_NAME} ; FIELD_INDEX=${FIELD_INDEX} ; SUPPORTED_FEATURES=${FEATURES_STRING}") + # Output the FIELD_INDEX and FEATURES_STRING + set(${FIELD_INDEX_OUT} "${FIELD_INDEX}" PARENT_SCOPE) + set(${FEATURES_STRING_OUT} "${FEATURES_STRING}" PARENT_SCOPE) + break() endif () endforeach() if (NOT IS_FIELD_SUPPORTED) - message( FATAL_ERROR "The value of FIELD variable: ${FIELD} is not one of the supported fields: ${SUPPORTED_FIELDS}" ) + set(ALL_FIELDS "") + extract_field_names(ALL_FIELDS) + message(FATAL_ERROR "The value of FIELD variable: ${FIELD} is not supported: choose from [${ALL_FIELDS}]") endif () endfunction() -function(setup_field_target) - add_library(icicle_field SHARED - src/fields/ffi_extern.cpp - src/vec_ops.cpp - src/matrix_ops.cpp - ) - # handle APIs that are for some curves only - add_ntt_sources_or_disable() - set_target_properties(icicle_field PROPERTIES OUTPUT_NAME "icicle_field_${FIELD}") - target_link_libraries(icicle_field PUBLIC icicle_device pthread) - - # Make sure FIELD is defined in the cache for backends to see - set(FIELD "${FIELD}" CACHE STRING "") - target_compile_definitions(icicle_field PUBLIC FIELD=${FIELD}) - if (EXT_FIELD) - set(EXT_FIELD "${EXT_FIELD}" CACHE STRING "") - target_compile_definitions(icicle_field PUBLIC EXT_FIELD=${EXT_FIELD}) - endif() - - install(TARGETS icicle_field - RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" - LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" - ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/") +function(setup_field_target FIELD FIELD_INDEX FEATURES_STRING) + add_library(icicle_field SHARED) + + # Split FEATURES_STRING into a list using "," as the separator + string(REPLACE "," ";" FEATURES_LIST ${FEATURES_STRING}) + + # customize the field lib to choose what to include + handle_field(icicle_field) # basic field methods, including vec ops + # Handle features + handle_ntt(icicle_field "${FEATURES_LIST}") + handle_ext_field(icicle_field "${FEATURES_LIST}") + # Add additional feature handling calls here + + set_target_properties(icicle_field PROPERTIES OUTPUT_NAME "icicle_field_${FIELD}") + target_link_libraries(icicle_field PUBLIC icicle_device pthread) + + # Ensure FIELD is defined in the cache for backends to see + set(FIELD "${FIELD}" CACHE STRING "") + add_compile_definitions(FIELD=${FIELD} FIELD_ID=${FIELD_INDEX}) + + install(TARGETS icicle_field + RUNTIME DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" + LIBRARY DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/" + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/") endfunction() -function(add_ntt_sources_or_disable) - set(SUPPORTED_FIELDS_WITHOUT_NTT grumpkin) - - if (NOT FIELD IN_LIST SUPPORTED_FIELDS_WITHOUT_NTT) - add_compile_definitions(NTT_ENABLED) - target_sources(icicle_field PRIVATE - src/ntt.cpp - src/polynomials/polynomials.cpp - src/polynomials/polynomials_c_api.cpp - src/polynomials/polynomials_abstract_factory.cpp - ) - else() - set(NTT OFF CACHE BOOL "NTT not available for field" FORCE) - endif() - -endfunction() \ No newline at end of file diff --git a/icicle_v3/cmake/fields_and_curves.cmake b/icicle_v3/cmake/fields_and_curves.cmake new file mode 100644 index 000000000..f5022eb66 --- /dev/null +++ b/icicle_v3/cmake/fields_and_curves.cmake @@ -0,0 +1,17 @@ + +# Define available fields with an index and their supported features +# Format: index:field:features +set(ICICLE_FIELDS + 1001:babybear:NTT,EXT_FIELD + 1002:stark252:NTT +) + +# Define available curves with an index and their supported features +# Format: index:curve:features +set(ICICLE_CURVES + 1:bn254:NTT,MSM,G2,ECNTT + 2:bls12_381:NTT,MSM,G2,ECNTT + 3:bls12_377:NTT,MSM,G2,ECNTT + 4:bw6_761:NTT,MSM,G2,ECNTT + 5:grumpkin:MSM +) \ No newline at end of file diff --git a/icicle_v3/cmake/target_editor.cmake b/icicle_v3/cmake/target_editor.cmake new file mode 100644 index 000000000..33b9e3f19 --- /dev/null +++ b/icicle_v3/cmake/target_editor.cmake @@ -0,0 +1,72 @@ + +# The following functions, each adds a function to a target. +# In addition, some can check feature is enabled for target, given FEATURE_LIST. + +function(handle_field TARGET) + target_sources(${TARGET} PRIVATE + src/fields/ffi_extern.cpp + src/vec_ops.cpp + src/matrix_ops.cpp + ) +endfunction() + +function(handle_curve TARGET) + target_sources(${TARGET} PRIVATE + src/curves/ffi_extern.cpp + src/curves/montgomery_conversion.cpp + ) +endfunction() + +function(handle_ntt TARGET FEATURE_LIST) + if(NTT AND "NTT" IN_LIST FEATURE_LIST) + target_compile_definitions(${TARGET} PUBLIC NTT=${NTT}) + target_sources(${TARGET} PRIVATE + src/ntt.cpp + src/polynomials/polynomials.cpp + src/polynomials/polynomials_c_api.cpp + src/polynomials/polynomials_abstract_factory.cpp + ) + set(NTT ON CACHE BOOL "Enable NTT feature" FORCE) + else() + set(NTT OFF CACHE BOOL "NTT not available for this field" FORCE) + endif() +endfunction() + +function(handle_ext_field TARGET FEATURE_LIST) + if(EXT_FIELD AND "EXT_FIELD" IN_LIST FEATURE_LIST) + target_compile_definitions(${TARGET} PUBLIC EXT_FIELD=${EXT_FIELD}) + set(EXT_FIELD ON CACHE BOOL "Enable EXT_FIELD feature" FORCE) + else() + set(EXT_FIELD OFF CACHE BOOL "EXT_FIELD not available for this field" FORCE) + endif() +endfunction() + +function(handle_msm TARGET FEATURE_LIST) + if(MSM AND "MSM" IN_LIST FEATURE_LIST) + target_compile_definitions(${TARGET} PUBLIC MSM=${MSM}) + target_sources(${TARGET} PRIVATE src/msm.cpp) + set(MSM ON CACHE BOOL "Enable MSM feature" FORCE) + else() + set(MSM OFF CACHE BOOL "MSM not available for this curve" FORCE) + endif() +endfunction() + +function(handle_g2 TARGET FEATURE_LIST) + if(G2 AND "G2" IN_LIST FEATURE_LIST) + target_compile_definitions(${TARGET} PUBLIC G2=${G2}) + set(G2 ON CACHE BOOL "Enable G2 feature" FORCE) + else() + set(G2 OFF CACHE BOOL "G2 not available for this curve" FORCE) + endif() +endfunction() + +function(handle_ecntt TARGET FEATURE_LIST) + if(ECNTT AND "ECNTT" IN_LIST FEATURE_LIST) + target_compile_definitions(${TARGET} PUBLIC ECNTT=${ECNTT}) + target_sources(icicle_curve PRIVATE src/ecntt.cpp) + set(ECNTT ON CACHE BOOL "Enable ECNTT feature" FORCE) + else() + set(ECNTT OFF CACHE BOOL "ECNTT not available for this curve" FORCE) + endif() +endfunction() + diff --git a/icicle_v3/include/icicle/ntt.h b/icicle_v3/include/icicle/ntt.h index 0d7a827c3..ca71e5373 100644 --- a/icicle_v3/include/icicle/ntt.h +++ b/icicle_v3/include/icicle/ntt.h @@ -152,7 +152,7 @@ namespace icicle { * @return S Root of unity. */ template - S get_root_of_unity(uint64_t max_size); + eIcicleError get_root_of_unity(uint64_t max_size, S* rou); /** * @brief Gets the root of unity from the NTT domain for a given logarithmic size. diff --git a/icicle_v3/src/ntt.cpp b/icicle_v3/src/ntt.cpp index 39c894e76..294ef26ca 100644 --- a/icicle_v3/src/ntt.cpp +++ b/icicle_v3/src/ntt.cpp @@ -68,16 +68,21 @@ namespace icicle { } /*************************** GET ROOT OF UNITY ***************************/ - extern "C" scalar_t CONCAT_EXPAND(FIELD, get_root_of_unity)(uint64_t max_size) + extern "C" eIcicleError CONCAT_EXPAND(FIELD, get_root_of_unity)(uint64_t max_size, scalar_t* rou) { const auto log_max_size = static_cast(std::ceil(std::log2(max_size))); - return scalar_t::omega(log_max_size); + if (scalar_t::get_omegas_count() < log_max_size) { + ICICLE_LOG_ERROR << "no root-of-unity of order " << log_max_size << " in field " << typeid(scalar_t).name(); + return eIcicleError::INVALID_ARGUMENT; + } + *rou = scalar_t::omega(log_max_size); + return eIcicleError::SUCCESS; } template <> - scalar_t get_root_of_unity(uint64_t max_size) + eIcicleError get_root_of_unity(uint64_t max_size, scalar_t* rou) { - return CONCAT_EXPAND(FIELD, get_root_of_unity)(max_size); + return CONCAT_EXPAND(FIELD, get_root_of_unity)(max_size, rou); } /*************************** GET ROOT OF UNITY FROM DOMAIN ***************************/ diff --git a/icicle_v3/src/runtime.cpp b/icicle_v3/src/runtime.cpp index 4871c6b45..0180467f5 100644 --- a/icicle_v3/src/runtime.cpp +++ b/icicle_v3/src/runtime.cpp @@ -275,7 +275,7 @@ extern "C" eIcicleError icicle_load_backend(const char* path, bool is_recursive) auto load_library = [](const char* filePath) { ICICLE_LOG_DEBUG << "Attempting load: " << filePath; - void* handle = dlopen(filePath, RTLD_LAZY | RTLD_GLOBAL); + void* handle = dlopen(filePath, RTLD_LAZY); if (!handle) { ICICLE_LOG_ERROR << "Failed to load " << filePath << ": " << dlerror(); } }; diff --git a/icicle_v3/tests/test_curve_api.cpp b/icicle_v3/tests/test_curve_api.cpp index 56c10bc93..b42887d7a 100644 --- a/icicle_v3/tests/test_curve_api.cpp +++ b/icicle_v3/tests/test_curve_api.cpp @@ -161,14 +161,17 @@ TEST_F(CurveApiTest, ecntt) icicle_set_device(dev); auto init_domain_config = default_ntt_init_domain_config(); - ntt_init_domain(scalar_t::omega(logn), init_domain_config); + ICICLE_CHECK(ntt_init_domain(scalar_t::omega(logn), init_domain_config)); + + std::ostringstream oss; + oss << dev_type << " " << msg; auto config = default_ntt_config(); START_TIMER(NTT_sync) for (int i = 0; i < iters; ++i) - ntt(input.get(), N, NTTDir::kForward, config, out); - END_TIMER(NTT_sync, msg, measure); + ICICLE_CHECK(ntt(input.get(), N, NTTDir::kForward, config, out)); + END_TIMER(NTT_sync, oss.str().c_str(), measure); ntt_release_domain(); }; diff --git a/icicle_v3/tests/test_field_api.cpp b/icicle_v3/tests/test_field_api.cpp index cfd9e5fb7..593c9b809 100644 --- a/icicle_v3/tests/test_field_api.cpp +++ b/icicle_v3/tests/test_field_api.cpp @@ -294,7 +294,7 @@ TYPED_TEST(FieldApiTest, Slice) ASSERT_EQ(0, memcmp(elements_ref.get(), elements_out.get(), size * sizeof(TypeParam))); } -#ifdef NTT_ENABLED +#ifdef NTT TYPED_TEST(FieldApiTest, ntt) { srand(time(0)); @@ -383,7 +383,7 @@ TYPED_TEST(FieldApiTest, ntt) ASSERT_EQ(0, memcmp(out_main.get(), out_ref.get(), total_size * sizeof(scalar_t))); } -#endif // NTT_ENABLED +#endif // NTT int main(int argc, char** argv) { diff --git a/wrappers/rust_v3/icicle-core/Cargo.toml b/wrappers/rust_v3/icicle-core/Cargo.toml index 19d11e3ca..48c869ccf 100644 --- a/wrappers/rust_v3/icicle-core/Cargo.toml +++ b/wrappers/rust_v3/icicle-core/Cargo.toml @@ -19,5 +19,5 @@ serial_test = "3.0.0" once_cell = "1.10.0" [features] -g2 = [] -ec_ntt = [] +no_g2 = [] +no_ecntt = [] diff --git a/wrappers/rust_v3/icicle-core/src/ecntt/mod.rs b/wrappers/rust_v3/icicle-core/src/ecntt/mod.rs index b130beef9..242ba3031 100644 --- a/wrappers/rust_v3/icicle-core/src/ecntt/mod.rs +++ b/wrappers/rust_v3/icicle-core/src/ecntt/mod.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "ec_ntt")] use icicle_runtime::{errors::eIcicleError, memory::HostOrDeviceSlice}; pub use crate::curve::Projective; diff --git a/wrappers/rust_v3/icicle-core/src/ecntt/tests.rs b/wrappers/rust_v3/icicle-core/src/ecntt/tests.rs index 3623a53c4..9d9c42e83 100644 --- a/wrappers/rust_v3/icicle-core/src/ecntt/tests.rs +++ b/wrappers/rust_v3/icicle-core/src/ecntt/tests.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "ec_ntt")] use icicle_runtime::memory::HostSlice; use crate::curve::Curve; diff --git a/wrappers/rust_v3/icicle-core/src/ntt/mod.rs b/wrappers/rust_v3/icicle-core/src/ntt/mod.rs index 800d759ff..ba4fbc854 100644 --- a/wrappers/rust_v3/icicle-core/src/ntt/mod.rs +++ b/wrappers/rust_v3/icicle-core/src/ntt/mod.rs @@ -326,7 +326,7 @@ macro_rules! impl_ntt { fn release_ntt_domain() -> eIcicleError; #[link_name = concat!($field_prefix, "_get_root_of_unity")] - fn get_root_of_unity(max_size: u64) -> $field; + fn get_root_of_unity(max_size: u64, rou: *mut $field) -> eIcicleError; } impl NTTDomain<$field> for $field_config { @@ -339,7 +339,11 @@ macro_rules! impl_ntt { } fn get_root_of_unity(max_size: u64) -> $field { - unsafe { get_root_of_unity(max_size) } + let mut rou = std::mem::MaybeUninit::<$field>::uninit(); // Prepare uninitialized memory for rou + unsafe { + get_root_of_unity(max_size, rou.as_mut_ptr()); + return rou.assume_init(); + } } } diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/Cargo.toml b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/Cargo.toml index b6469bce2..6f410540f 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/Cargo.toml +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/Cargo.toml @@ -22,9 +22,8 @@ cmake = "0.1.50" [features] default = [] bw6-761 = [] -bw6-761-g2 = ["bw6-761"] -g2 = ["icicle-core/g2"] -ec_ntt = ["icicle-core/ec_ntt"] +no_g2 = ["icicle-core/no_g2"] +no_ecntt = ["icicle-core/no_ecntt"] cuda_backend = ["icicle-runtime/cuda_backend"] pull_cuda_backend = ["icicle-runtime/pull_cuda_backend"] diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/benches/ecntt.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/benches/ecntt.rs index 0fd2c027c..165683b75 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/benches/ecntt.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/benches/ecntt.rs @@ -1,10 +1,10 @@ -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_bls12_377::curve::{CurveCfg, ScalarField}; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_core::impl_ecntt_bench; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] impl_ecntt_bench!("bls12_377", ScalarField, CurveCfg); -#[cfg(not(feature = "ec_ntt"))] +#[cfg(feature = "no_ecntt")] fn main() {} diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/build.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/build.rs index 758e75b1a..8403b29e6 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/build.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/build.rs @@ -29,18 +29,20 @@ fn main() { .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); - - // Optional Features - #[cfg(feature = "g2")] - config.define("G2", "ON"); - - #[cfg(feature = "ec_ntt")] - config.define("ECNTT", "ON"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } + // Optional Features that are default ON (so that default matches any backend) + if cfg!(feature = "no_g2") { + config.define("G2", "OFF"); + } + if cfg!(feature = "no_ecntt") { + config.define("ECNTT", "OFF"); + } // Build let _ = config @@ -60,18 +62,21 @@ fn main() { .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config_bw.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config_bw.define("CUDA_BACKEND", "main"); - - // Optional Features - #[cfg(feature = "bw6-761-g2")] - config_bw.define("G2", "ON"); - - #[cfg(feature = "ec_ntt")] - config_bw.define("ECNTT", "OFF"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config_bw.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config_bw.define("CUDA_BACKEND", "main"); + } + + // Optional Features that are default ON (so that default matches any backend) + if cfg!(feature = "no_g2") { + config_bw.define("G2", "OFF"); + } + if cfg!(feature = "no_ecntt") { + config_bw.define("ECNTT", "OFF"); + } // Build let _ = config_bw diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/curve.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/curve.rs index 6fcf6717c..16ae42b15 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/curve.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/curve.rs @@ -13,7 +13,7 @@ use icicle_runtime::{ pub(crate) const SCALAR_LIMBS: usize = 8; pub(crate) const BASE_LIMBS: usize = 12; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] pub(crate) const G2_BASE_LIMBS: usize = 24; impl_scalar_field!("bls12_377", bls12_377_sf, SCALAR_LIMBS, ScalarField, ScalarCfg); @@ -34,9 +34,9 @@ impl_curve!( G1Projective ); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_field!(G2_BASE_LIMBS, G2BaseField, G2BaseCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_curve!( "bls12_377_g2", bls12_377_g2, @@ -50,7 +50,7 @@ impl_curve!( #[cfg(test)] mod tests { use super::{CurveCfg, ScalarField, BASE_LIMBS}; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use super::{G2CurveCfg, G2_BASE_LIMBS}; use icicle_core::curve::Curve; use icicle_core::test_utilities; @@ -60,7 +60,7 @@ mod tests { impl_field_tests!(ScalarField); impl_curve_tests!(BASE_LIMBS, CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_curve_tests!(G2_BASE_LIMBS, G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/ecntt/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/ecntt/mod.rs index 5fd84e76d..8f41cdb86 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/ecntt/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/ecntt/mod.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "ec_ntt")] use crate::curve::{CurveCfg, ScalarCfg, ScalarField}; use icicle_core::ecntt::Projective; use icicle_core::impl_ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/lib.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/lib.rs index b33cfe396..346974b8e 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/lib.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/lib.rs @@ -1,6 +1,8 @@ pub mod curve; -pub mod ecntt; pub mod msm; pub mod ntt; pub mod polynomials; pub mod vec_ops; + +#[cfg(not(feature = "no_ecntt"))] +pub mod ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/msm/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/msm/mod.rs index 54424afbc..9155fa3af 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/msm/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-377/src/msm/mod.rs @@ -1,5 +1,5 @@ use crate::curve::CurveCfg; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::{ curve::{Affine, Curve, Projective}, @@ -12,19 +12,19 @@ use icicle_runtime::{ }; impl_msm!("bls12_377", bls12_377, CurveCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_msm!("bls12_377_g2", bls12_377_g2, G2CurveCfg); #[cfg(test)] pub(crate) mod tests { use crate::curve::CurveCfg; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::impl_msm_tests; use icicle_core::msm::tests::*; impl_msm_tests!(CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_msm_tests!(G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/Cargo.toml b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/Cargo.toml index 09ff44c0e..33fa839d3 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/Cargo.toml +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/Cargo.toml @@ -21,8 +21,8 @@ cmake = "0.1.50" [features] default = [] -g2 = ["icicle-core/g2"] -ec_ntt = ["icicle-core/ec_ntt"] +no_g2 = ["icicle-core/no_g2"] +no_ecntt = ["icicle-core/no_ecntt"] cuda_backend = ["icicle-runtime/cuda_backend"] pull_cuda_backend = ["icicle-runtime/pull_cuda_backend"] diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/benches/ecntt.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/benches/ecntt.rs index 28371a5c1..4ee5ac940 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/benches/ecntt.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/benches/ecntt.rs @@ -1,10 +1,10 @@ -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_bls12_381::curve::{CurveCfg, ScalarField}; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_core::impl_ecntt_bench; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] impl_ecntt_bench!("bls12_381", ScalarField, CurveCfg); -#[cfg(not(feature = "ec_ntt"))] +#[cfg(feature = "no_ecntt")] fn main() {} diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/build.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/build.rs index 19681eb59..50403f4c0 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/build.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/build.rs @@ -25,21 +25,24 @@ fn main() { }; config .define("CURVE", "bls12_381") + .define("FIELD", "bls12_381") .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); - - // Optional Features - #[cfg(feature = "g2")] - config.define("G2", "ON"); - - #[cfg(feature = "ec_ntt")] - config.define("ECNTT", "ON"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } + // Optional Features that are default ON (so that default matches any backend) + if cfg!(feature = "no_g2") { + config.define("G2", "OFF"); + } + if cfg!(feature = "no_ecntt") { + config.define("ECNTT", "OFF"); + } // Build let _ = config diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/curve.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/curve.rs index 911ce7150..382266917 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/curve.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/curve.rs @@ -13,7 +13,7 @@ use icicle_runtime::{ pub(crate) const SCALAR_LIMBS: usize = 8; pub(crate) const BASE_LIMBS: usize = 12; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] pub(crate) const G2_BASE_LIMBS: usize = 24; impl_scalar_field!("bls12_381", bls12_381_sf, SCALAR_LIMBS, ScalarField, ScalarCfg); @@ -29,9 +29,9 @@ impl_curve!( G1Projective ); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_field!(G2_BASE_LIMBS, G2BaseField, G2BaseCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_curve!( "bls12_381_g2", bls12_381_g2, @@ -45,7 +45,7 @@ impl_curve!( #[cfg(test)] mod tests { use super::{CurveCfg, ScalarField, BASE_LIMBS}; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use super::{G2CurveCfg, G2_BASE_LIMBS}; use icicle_core::curve::Curve; use icicle_core::test_utilities; @@ -55,7 +55,7 @@ mod tests { impl_field_tests!(ScalarField); impl_curve_tests!(BASE_LIMBS, CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_curve_tests!(G2_BASE_LIMBS, G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/ecntt/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/ecntt/mod.rs index d1a795385..72084a28a 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/ecntt/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/ecntt/mod.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "ec_ntt")] use crate::curve::{CurveCfg, ScalarCfg, ScalarField}; use icicle_core::ecntt::Projective; use icicle_core::impl_ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/lib.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/lib.rs index b33cfe396..346974b8e 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/lib.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/lib.rs @@ -1,6 +1,8 @@ pub mod curve; -pub mod ecntt; pub mod msm; pub mod ntt; pub mod polynomials; pub mod vec_ops; + +#[cfg(not(feature = "no_ecntt"))] +pub mod ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/msm/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/msm/mod.rs index 1d732923b..f4b4bd17e 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/msm/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bls12-381/src/msm/mod.rs @@ -1,5 +1,5 @@ use crate::curve::CurveCfg; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::{ curve::{Affine, Curve, Projective}, @@ -12,19 +12,19 @@ use icicle_runtime::{ }; impl_msm!("bls12_381", bls12_381, CurveCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_msm!("bls12_381_g2", bls12_381_g2, G2CurveCfg); #[cfg(test)] pub(crate) mod tests { use crate::curve::CurveCfg; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::impl_msm_tests; use icicle_core::msm::tests::*; impl_msm_tests!(CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_msm_tests!(G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/Cargo.toml b/wrappers/rust_v3/icicle-curves/icicle-bn254/Cargo.toml index 152a324c3..be6529fe6 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/Cargo.toml +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/Cargo.toml @@ -21,8 +21,8 @@ cmake = "0.1.50" [features] default = [] -g2 = ["icicle-core/g2"] -ec_ntt = ["icicle-core/ec_ntt"] +no_g2 = ["icicle-core/no_g2"] +no_ecntt = ["icicle-core/no_ecntt"] cuda_backend = ["icicle-runtime/cuda_backend"] pull_cuda_backend = ["icicle-runtime/pull_cuda_backend"] diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/benches/ecntt.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/benches/ecntt.rs index 37b5b80e6..7e2528d40 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/benches/ecntt.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/benches/ecntt.rs @@ -1,10 +1,10 @@ -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_bn254::curve::{CurveCfg, ScalarField}; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] use icicle_core::impl_ecntt_bench; -#[cfg(feature = "ec_ntt")] +#[cfg(not(feature = "no_ecntt"))] impl_ecntt_bench!("bn254", ScalarField, CurveCfg); -#[cfg(not(feature = "ec_ntt"))] +#[cfg(feature = "no_ecntt")] fn main() {} diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/build.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/build.rs index 9272da6f2..b2542ba8d 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/build.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/build.rs @@ -28,18 +28,20 @@ fn main() { .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); - - // Optional Features - #[cfg(feature = "g2")] - config.define("G2", "ON"); - - #[cfg(feature = "ec_ntt")] - config.define("ECNTT", "ON"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } + // Optional Features that are default ON (so that default matches any backend) + if cfg!(feature = "no_g2") { + config.define("G2", "OFF"); + } + if cfg!(feature = "no_ecntt") { + config.define("ECNTT", "OFF"); + } // Build let _ = config diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/curve.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/curve.rs index ec93a8338..45b9063d7 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/curve.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/curve.rs @@ -13,16 +13,16 @@ use icicle_runtime::{ pub(crate) const SCALAR_LIMBS: usize = 8; pub(crate) const BASE_LIMBS: usize = 8; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] pub(crate) const G2_BASE_LIMBS: usize = 16; impl_scalar_field!("bn254", bn254_sf, SCALAR_LIMBS, ScalarField, ScalarCfg); impl_field!(BASE_LIMBS, BaseField, BaseCfg); impl_curve!("bn254", bn254, CurveCfg, ScalarField, BaseField, G1Affine, G1Projective); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_field!(G2_BASE_LIMBS, G2BaseField, G2BaseCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_curve!( "bn254_g2", bn254_g2, @@ -36,7 +36,7 @@ impl_curve!( #[cfg(test)] mod tests { use super::{CurveCfg, ScalarField, BASE_LIMBS}; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use super::{G2CurveCfg, G2_BASE_LIMBS}; use icicle_core::curve::Curve; use icicle_core::test_utilities; @@ -46,7 +46,7 @@ mod tests { impl_field_tests!(ScalarField); impl_curve_tests!(BASE_LIMBS, CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_curve_tests!(G2_BASE_LIMBS, G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/ecntt/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/ecntt/mod.rs index 87b371b27..a64a03bed 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/ecntt/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/ecntt/mod.rs @@ -1,4 +1,3 @@ -#![cfg(feature = "ec_ntt")] use crate::curve::{CurveCfg, ScalarCfg, ScalarField}; use icicle_core::ecntt::Projective; use icicle_core::impl_ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/lib.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/lib.rs index b33cfe396..346974b8e 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/lib.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/lib.rs @@ -1,6 +1,8 @@ pub mod curve; -pub mod ecntt; pub mod msm; pub mod ntt; pub mod polynomials; pub mod vec_ops; + +#[cfg(not(feature = "no_ecntt"))] +pub mod ecntt; diff --git a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/msm/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/msm/mod.rs index f727746e9..dd77b56b3 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bn254/src/msm/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bn254/src/msm/mod.rs @@ -1,5 +1,5 @@ use crate::curve::CurveCfg; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::{ curve::{Affine, Curve, Projective}, @@ -12,19 +12,19 @@ use icicle_runtime::{ }; impl_msm!("bn254", bn254, CurveCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_msm!("bn254_g2", bn254_g2, G2CurveCfg); #[cfg(test)] pub(crate) mod tests { use crate::curve::CurveCfg; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::impl_msm_tests; use icicle_core::msm::tests::*; impl_msm_tests!(CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_msm_tests!(G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/Cargo.toml b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/Cargo.toml index f76206c5b..e13159098 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/Cargo.toml +++ b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/Cargo.toml @@ -11,7 +11,6 @@ repository.workspace = true icicle-core = { workspace = true } icicle-runtime = { workspace = true } icicle-bls12-377 = { path = "../../icicle-curves/icicle-bls12-377", features = ["bw6-761"] } -# criterion = "0.3" [dev-dependencies] criterion = "0.3" @@ -22,7 +21,8 @@ cmake = "0.1.50" [features] default = [] -g2 = ["icicle-bls12-377/bw6-761-g2"] +no_g2 = ["icicle-bls12-377/no_g2"] +no_ecntt = ["icicle-bls12-377/no_ecntt"] cuda_backend = ["icicle-bls12-377/cuda_backend"] pull_cuda_backend = ["icicle-bls12-377/pull_cuda_backend"] diff --git a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/curve.rs b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/curve.rs index 9e02abbd7..16e9ed65f 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/curve.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/curve.rs @@ -21,7 +21,7 @@ impl_curve!( G1Affine, G1Projective ); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_curve!( "bw6_761_g2", bw6_761_g2, @@ -34,7 +34,7 @@ impl_curve!( #[cfg(test)] mod tests { - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use super::G2CurveCfg; use super::{CurveCfg, ScalarField, BASE_LIMBS}; use icicle_core::curve::Curve; @@ -45,7 +45,7 @@ mod tests { impl_field_tests!(ScalarField); impl_curve_tests!(BASE_LIMBS, CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_curve_tests!(BASE_LIMBS, G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/msm/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/msm/mod.rs index fc72055a7..600094ea5 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/msm/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-bw6-761/src/msm/mod.rs @@ -1,5 +1,5 @@ use crate::curve::CurveCfg; -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::{ curve::{Affine, Curve, Projective}, @@ -12,19 +12,19 @@ use icicle_runtime::{ }; impl_msm!("bw6_761", bw6_761, CurveCfg); -#[cfg(feature = "g2")] +#[cfg(not(feature = "no_g2"))] impl_msm!("bw6_761_g2", bw6_761_g2, G2CurveCfg); #[cfg(test)] pub(crate) mod tests { use crate::curve::CurveCfg; - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] use crate::curve::G2CurveCfg; use icicle_core::impl_msm_tests; use icicle_core::msm::tests::*; impl_msm_tests!(CurveCfg); - #[cfg(feature = "g2")] + #[cfg(not(feature = "no_g2"))] mod g2 { use super::*; impl_msm_tests!(G2CurveCfg); diff --git a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/Cargo.toml b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/Cargo.toml index 9350f3b82..d69018cef 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/Cargo.toml +++ b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/Cargo.toml @@ -8,7 +8,7 @@ homepage.workspace = true repository.workspace = true [dependencies] -icicle-core = { workspace = true } +icicle-core = { workspace = true, features=["no_ecntt","no_g2"] } icicle-runtime = { workspace = true } # criterion = "0.3" @@ -21,7 +21,6 @@ cmake = "0.1.50" [features] default = [] -ec_ntt = ["icicle-core/ec_ntt"] # why? cuda_backend = ["icicle-runtime/cuda_backend"] pull_cuda_backend = ["icicle-runtime/pull_cuda_backend"] diff --git a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/build.rs b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/build.rs index 85e9e4758..e4fd78cc6 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/build.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/build.rs @@ -28,11 +28,13 @@ fn main() { .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } // Build let _ = config diff --git a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/src/msm/mod.rs b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/src/msm/mod.rs index 0013c753c..524c50784 100644 --- a/wrappers/rust_v3/icicle-curves/icicle-grumpkin/src/msm/mod.rs +++ b/wrappers/rust_v3/icicle-curves/icicle-grumpkin/src/msm/mod.rs @@ -1,6 +1,5 @@ use crate::curve::CurveCfg; -#[cfg(feature = "g2")] -use crate::curve::G2CurveCfg; + use icicle_core::{ curve::{Affine, Curve, Projective}, impl_msm, diff --git a/wrappers/rust_v3/icicle-fields/icicle-babybear/build.rs b/wrappers/rust_v3/icicle-fields/icicle-babybear/build.rs index a367e4977..16aba61c4 100644 --- a/wrappers/rust_v3/icicle-fields/icicle-babybear/build.rs +++ b/wrappers/rust_v3/icicle-fields/icicle-babybear/build.rs @@ -25,15 +25,16 @@ fn main() { }; config .define("FIELD", "babybear") - .define("EXT_FIELD", "ON") .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } // Build let _ = config diff --git a/wrappers/rust_v3/icicle-runtime/build.rs b/wrappers/rust_v3/icicle-runtime/build.rs index 0896430b6..bee14d526 100644 --- a/wrappers/rust_v3/icicle-runtime/build.rs +++ b/wrappers/rust_v3/icicle-runtime/build.rs @@ -26,11 +26,13 @@ fn main() { .define("CMAKE_BUILD_TYPE", "Release") .define("CMAKE_INSTALL_PREFIX", &icicle_install_dir); - #[cfg(feature = "cuda_backend")] - config.define("CUDA_BACKEND", "local"); - - #[cfg(feature = "pull_cuda_backend")] - config.define("CUDA_BACKEND", "main"); + // build (or pull and build) cuda backend if feature enabled. + // Note: this requires access to the repo + if cfg!(feature = "cuda_backend") { + config.define("CUDA_BACKEND", "local"); + } else if cfg!(feature = "pull_cuda_backend") { + config.define("CUDA_BACKEND", "main"); + } // Build let _ = config