Skip to content

Commit

Permalink
casting to u64 explicitly (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
anupsv authored Feb 11, 2025
1 parent d548356 commit df1ac7a
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ env:

jobs:
build:

runs-on: ubuntu-latest
timeout-minutes: 30
steps:
Expand All @@ -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


2 changes: 1 addition & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 14 additions & 1 deletion primitives/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ pub fn compute_challenge(blob: &Blob, commitment: &G1Affine) -> Result<Fr, KzgEr

// Step 2: Copy the number of field elements (blob polynomial length)
// Convert to bytes using the configured endianness
let number_of_field_elements = blob_poly.len().to_be_bytes();
let number_of_field_elements = usize_to_be_bytes(blob_poly.len());
digest_bytes[offset..offset + 8].copy_from_slice(&number_of_field_elements);
offset += 8;

Expand Down Expand Up @@ -717,3 +717,16 @@ pub fn compute_challenges_and_evaluate_polynomial(
// These will be used in the KZG proof verification process
Ok((evaluation_challenges, ys))
}

/// Converts a usize to a byte array in big-endian format always returning 8 bytes.
pub fn usize_to_be_bytes(number: usize) -> [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
}
2 changes: 1 addition & 1 deletion prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 2 additions & 2 deletions verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions verifier/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)];
Expand Down

0 comments on commit df1ac7a

Please sign in to comment.