From df1ac7ad835894a42dd9d384c92c59d647a4e334 Mon Sep 17 00:00:00 2001 From: anupsv Date: Tue, 11 Feb 2025 14:52:44 -0800 Subject: [PATCH] casting to u64 explicitly (#45) * casting to u64 explicitly * casting to u64 in helpers. * length already is u64 * adding 32bit test workflow * cleaning up rutst-toolchain * adding 32bit test target * using cross test * adding helper function for conversion * cargo fmt --- .github/workflows/rust.yml | 5 ++++- primitives/Cargo.toml | 2 +- primitives/src/helpers.rs | 15 ++++++++++++++- prover/Cargo.toml | 2 +- rust-toolchain | 2 +- verifier/Cargo.toml | 4 ++-- verifier/src/batch.rs | 5 ++--- 7 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5bb0da8..c9b9949 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,7 +11,6 @@ env: jobs: build: - runs-on: ubuntu-latest timeout-minutes: 30 steps: @@ -24,5 +23,9 @@ jobs: run: cargo clippy --all --manifest-path Cargo.toml -- -D warnings - name: Run tests run: cargo test --verbose + - name: 32bit compilation test + run: cargo build --target i686-unknown-linux-gnu --verbose + - name: Running tests under 32bit architecture + run: cargo install cross && cross test --target i686-unknown-linux-gnu --verbose diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 7e71bf0..d2747a8 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust-kzg-bn254-primitives" -version = "0.1.0" +version = "0.1.1" description = "This library offers a set of structs, traits and functions for generating Blobs and Polynomials which are used to interact with rust-kzg-bn254-prover and rust-kzg-bn254-verifier crates." edition.workspace = true repository.workspace = true diff --git a/primitives/src/helpers.rs b/primitives/src/helpers.rs index 9aa6279..60af0e7 100644 --- a/primitives/src/helpers.rs +++ b/primitives/src/helpers.rs @@ -518,7 +518,7 @@ pub fn compute_challenge(blob: &Blob, commitment: &G1Affine) -> Result [u8; 8] { + let mut result = [0u8; 8]; + let number_bytes = number.to_be_bytes(); + + if number_bytes.len() == 4 { + result[4..].copy_from_slice(&number_bytes); + } else { + result.copy_from_slice(&number_bytes); + } + result +} diff --git a/prover/Cargo.toml b/prover/Cargo.toml index 5d950e0..1990b45 100644 --- a/prover/Cargo.toml +++ b/prover/Cargo.toml @@ -10,7 +10,7 @@ license-file.workspace = true exclude = ["tests/*", "benches/*"] [dependencies] -rust-kzg-bn254-primitives = { version = "0.1.0" } +rust-kzg-bn254-primitives = { version = "0.1.1", path = "../primitives" } ark-bn254 = "0.5.0" ark-ec = { version = "0.5.0", features = ["parallel"] } ark-ff = { version = "0.5.0", features = ["parallel"] } diff --git a/rust-toolchain b/rust-toolchain index 9b97c89..cbdb9fc 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -2,4 +2,4 @@ channel = '1.81' profile = 'minimal' components = ['clippy', 'rustfmt'] -targets = ["x86_64-unknown-linux-gnu", "x86_64-pc-windows-gnu", "wasm32-unknown-unknown", "aarch64-apple-darwin"] +targets = ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "i686-unknown-linux-gnu"] diff --git a/verifier/Cargo.toml b/verifier/Cargo.toml index a84eaf7..dd7028c 100644 --- a/verifier/Cargo.toml +++ b/verifier/Cargo.toml @@ -8,7 +8,7 @@ rust-version.workspace = true repository.workspace = true [dependencies] -rust-kzg-bn254-primitives = { version = "0.1.0" } +rust-kzg-bn254-primitives = {version = "0.1.1", path = "../primitives" } ark-bn254 = "0.5.0" ark-ec = { version = "0.5.0", features = ["parallel"] } ark-ff = { version = "0.5.0", features = ["parallel"] } @@ -19,7 +19,7 @@ rand = "0.8.5" criterion = "0.5" lazy_static = "1.5" ark-std = { version = "0.5.0", features = ["parallel"] } -rust-kzg-bn254-prover = { version = "0.1.0" } +rust-kzg-bn254-prover = { version = "0.1.0", path = "../prover" } [[bench]] name = "bench_kzg_verify" diff --git a/verifier/src/batch.rs b/verifier/src/batch.rs index ab9b93f..412b4f4 100644 --- a/verifier/src/batch.rs +++ b/verifier/src/batch.rs @@ -6,7 +6,7 @@ use rust_kzg_bn254_primitives::{ blob::Blob, consts::{BYTES_PER_FIELD_ELEMENT, G2_TAU, RANDOM_CHALLENGE_KZG_BATCH_DOMAIN}, errors::KzgError, - helpers::{self, is_on_curve_g1}, + helpers::{self, is_on_curve_g1, usize_to_be_bytes}, }; /// Ref: https://github.com/ethereum/consensus-specs/blob/master/specs/deneb/polynomial-commitments.md#verify_blob_kzg_proof_batch @@ -114,8 +114,7 @@ fn compute_r_powers( data_to_be_hashed[0..24].copy_from_slice(RANDOM_CHALLENGE_KZG_BATCH_DOMAIN); // Convert number of commitments to bytes and copy to buffer - // Uses configured endianness (Big or Little) - let n_bytes: [u8; 8] = n.to_be_bytes(); + let n_bytes: [u8; 8] = usize_to_be_bytes(n); data_to_be_hashed[32..40].copy_from_slice(&n_bytes); let target_slice = &mut data_to_be_hashed[24..24 + (n * 8)];