Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated blst to latest c-kzg hash #237

Merged
merged 12 commits into from
Oct 12, 2023
2 changes: 1 addition & 1 deletion .github/workflows/blst-benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: blst benchmarks
on: [push, pull_request, workflow_dispatch]

env:
C_KZG_4844_GIT_HASH: 'b2e41491ad1859f1792964a2432a419b64dc6fb2'
C_KZG_4844_GIT_HASH: 'a1d03803059471948e518820afa8518643e1b638'

jobs:
benchmarks:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/blst-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: blst tests
on: [push, pull_request, workflow_dispatch]

env:
C_KZG_4844_GIT_HASH: 'b2e41491ad1859f1792964a2432a419b64dc6fb2'
C_KZG_4844_GIT_HASH: 'a1d03803059471948e518820afa8518643e1b638'

jobs:
tests:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 23 additions & 32 deletions blst/src/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,14 @@ pub fn verify_kzg_proof_rust(
proof: &FsG1,
s: &FsKZGSettings,
) -> Result<bool, String> {
if !commitment.is_valid() {
if !commitment.is_inf() && !commitment.is_valid() {
return Err("Invalid commitment".to_string());
}
if !proof.is_valid() {
if !proof.is_inf() && !proof.is_valid() {
return Err("Invalid proof".to_string());
}

Ok(s.check_proof_single(commitment, proof, z, y)
.unwrap_or(false))
s.check_proof_single(commitment, proof, z, y)
}

pub fn verify_kzg_proof_batch(
Expand Down Expand Up @@ -249,10 +248,6 @@ pub fn compute_kzg_proof_rust(
z: &FsFr,
s: &FsKZGSettings,
) -> Result<(FsG1, FsFr), String> {
if blob.len() != FIELD_ELEMENTS_PER_BLOB {
return Err(String::from("Incorrect field elements count."));
}

let polynomial = blob_to_polynomial_rust(blob)?;
let y = evaluate_polynomial_in_evaluation_form_rust(&polynomial, z, s)?;

Expand Down Expand Up @@ -370,7 +365,7 @@ pub fn compute_powers(base: &FsFr, num_powers: usize) -> Vec<FsFr> {
powers
}

fn compute_challenge(blob: &[FsFr], commitment: &FsG1) -> Result<FsFr, String> {
fn compute_challenge(blob: &[FsFr], commitment: &FsG1) -> FsFr {
let mut bytes: Vec<u8> = vec![0; CHALLENGE_INPUT_SIZE];

// Copy domain separator
Expand All @@ -380,13 +375,11 @@ fn compute_challenge(blob: &[FsFr], commitment: &FsG1) -> Result<FsFr, String> {
bytes_of_uint64(&mut bytes[24..32], FIELD_ELEMENTS_PER_BLOB as u64);

// Copy blob
for (i, _) in blob.iter().enumerate() {
let v = blob[i].to_bytes();
for (i, field) in blob.iter().enumerate() {
let v = field.to_bytes();
let size = (32 + i * BYTES_PER_FIELD_ELEMENT)..(32 + (i + 1) * BYTES_PER_FIELD_ELEMENT);

if size.len() != v.len() {
return Err(String::from("Sizes of src and dest are different"));
}
assert!(size.len() == v.len());

bytes[size].copy_from_slice(&v);
}
Expand All @@ -399,7 +392,7 @@ fn compute_challenge(blob: &[FsFr], commitment: &FsG1) -> Result<FsFr, String> {

// Now let's create the challenge!
let eval_challenge = hash(&bytes);
Ok(hash_to_bls_field(&eval_challenge))
hash_to_bls_field(&eval_challenge)
}

fn compute_r_powers(
Expand All @@ -412,15 +405,13 @@ fn compute_r_powers(
let input_size =
32 + n * (BYTES_PER_COMMITMENT + 2 * BYTES_PER_FIELD_ELEMENT + BYTES_PER_PROOF);

#[allow(unused_assignments)]
let mut offset = 0;
let mut bytes: Vec<u8> = vec![0; input_size];

// Copy domain separator
bytes[..16].copy_from_slice(&RANDOM_CHALLENGE_KZG_BATCH_DOMAIN);
bytes_of_uint64(&mut bytes[16..24], FIELD_ELEMENTS_PER_BLOB as u64);
bytes_of_uint64(&mut bytes[24..32], n as u64);
offset = 32;
let mut offset = 32;

for i in 0..n {
// Copy commitment
Expand Down Expand Up @@ -478,7 +469,7 @@ pub fn compute_blob_kzg_proof_rust(
return Err("Invalid commitment".to_string());
}

let evaluation_challenge_fr = compute_challenge(blob, commitment)?;
let evaluation_challenge_fr = compute_challenge(blob, commitment);
let (proof, _) = compute_kzg_proof_rust(blob, &evaluation_challenge_fr, ts)?;
Ok(proof)
}
Expand All @@ -489,15 +480,15 @@ pub fn verify_blob_kzg_proof_rust(
proof_g1: &FsG1,
ts: &FsKZGSettings,
) -> Result<bool, String> {
if !commitment_g1.is_valid() {
if !commitment_g1.is_inf() && !commitment_g1.is_valid() {
return Err("Invalid commitment".to_string());
}
if !proof_g1.is_valid() {
if !proof_g1.is_inf() && !proof_g1.is_valid() {
return Err("Invalid proof".to_string());
}

let polynomial = blob_to_polynomial_rust(blob)?;
let evaluation_challenge_fr = compute_challenge(blob, commitment_g1)?;
let evaluation_challenge_fr = compute_challenge(blob, commitment_g1);
let y_fr =
evaluate_polynomial_in_evaluation_form_rust(&polynomial, &evaluation_challenge_fr, ts)?;
verify_kzg_proof_rust(commitment_g1, &evaluation_challenge_fr, &y_fr, proof_g1, ts)
Expand All @@ -513,7 +504,7 @@ fn compute_challenges_and_evaluate_polynomial(

for i in 0..blobs.len() {
let polynomial = blob_to_polynomial_rust(&blobs[i])?;
let evaluation_challenge_fr = compute_challenge(&blobs[i], &commitments_g1[i])?;
let evaluation_challenge_fr = compute_challenge(&blobs[i], &commitments_g1[i]);
let y_fr =
evaluate_polynomial_in_evaluation_form_rust(&polynomial, &evaluation_challenge_fr, ts)?;

Expand All @@ -525,8 +516,9 @@ fn compute_challenges_and_evaluate_polynomial(
}

fn validate_batched_input(commitments: &[FsG1], proofs: &[FsG1]) -> Result<(), String> {
let invalid_commitment = cfg_into_iter!(commitments).any(|&commitment| !commitment.is_valid());
let invalid_proof = cfg_into_iter!(proofs).any(|&proof| !proof.is_valid());
let invalid_commitment = cfg_into_iter!(commitments)
.any(|&commitment| !commitment.is_inf() && !commitment.is_valid());
let invalid_proof = cfg_into_iter!(proofs).any(|&proof| !proof.is_inf() && !proof.is_valid());

if invalid_commitment {
return Err("Invalid commitment".to_string());
Expand Down Expand Up @@ -607,7 +599,7 @@ pub fn verify_blob_kzg_proof_batch_rust(
}

#[cfg(not(feature = "parallel"))]
Ok({
{
validate_batched_input(commitments_g1, proofs_g1)?;
let (evaluation_challenges_fr, ys_fr) =
compute_challenges_and_evaluate_polynomial(blobs, commitments_g1, ts)?;
Expand All @@ -618,8 +610,8 @@ pub fn verify_blob_kzg_proof_batch_rust(
&ys_fr,
proofs_g1,
ts,
)?
})
)
}
}

fn fft_settings_to_rust(c_settings: *const CKZGSettings) -> Result<FsFFTSettings, String> {
Expand Down Expand Up @@ -797,7 +789,7 @@ pub unsafe extern "C" fn load_trusted_setup_file(
pub unsafe extern "C" fn compute_blob_kzg_proof(
out: *mut KZGProof,
blob: *const Blob,
commitment_bytes: *mut Bytes48,
commitment_bytes: *const Bytes48,
s: &CKZGSettings,
) -> C_KZG_RET {
let deserialized_blob = match deserialize_blob(blob) {
Expand Down Expand Up @@ -845,6 +837,7 @@ pub unsafe extern "C" fn free_trusted_setup(s: *mut CKZGSettings) {
));
drop(g2);
(*s).g2_values = null_mut();
(*s).max_width = 0;
}

/// # Safety
Expand Down Expand Up @@ -886,10 +879,8 @@ pub unsafe extern "C" fn verify_blob_kzg_proof(
s: &CKZGSettings,
) -> C_KZG_RET {
let deserialized_blob = handle_ckzg_badargs!(deserialize_blob(blob));

let commitment_g1 = handle_ckzg_badargs!(FsG1::from_bytes(&(*commitment_bytes).bytes));
let proof_g1 = handle_ckzg_badargs!(FsG1::from_bytes(&(*proof_bytes).bytes));

let settings = handle_ckzg_badargs!(kzg_settings_to_rust(s));

let result = handle_ckzg_badargs!(verify_blob_kzg_proof_rust(
Expand Down Expand Up @@ -991,7 +982,7 @@ pub unsafe extern "C" fn compute_kzg_proof(

#[cfg(test)]
mod tests {
use kzg_bench::tests::eip_4844::get_trusted_setup_path;
use kzg_bench::tests::utils::get_trusted_setup_path;

use crate::eip_4844::{kzg_settings_to_c, kzg_settings_to_rust};

Expand Down
80 changes: 80 additions & 0 deletions blst/tests/c_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[cfg(test)]
mod tests {
use kzg_bench::tests::c_bindings::{
blob_to_kzg_commitment_invalid_blob_test,
compute_blob_kzg_proof_commitment_is_point_at_infinity_test,
compute_blob_kzg_proof_invalid_blob_test, free_trusted_setup_null_ptr_test,
free_trusted_setup_set_all_values_to_null_test,
load_trusted_setup_file_invalid_format_test, load_trusted_setup_file_valid_format_test,
load_trusted_setup_invalid_form_test, load_trusted_setup_invalid_g1_byte_length_test,
load_trusted_setup_invalid_g1_point_test, load_trusted_setup_invalid_g2_byte_length_test,
load_trusted_setup_invalid_g2_point_test,
};
use rust_kzg_blst::eip_4844::{
blob_to_kzg_commitment, compute_blob_kzg_proof, free_trusted_setup, load_trusted_setup,
load_trusted_setup_file,
};

#[test]
fn blob_to_kzg_commitment_invalid_blob() {
blob_to_kzg_commitment_invalid_blob_test(blob_to_kzg_commitment, load_trusted_setup_file);
}

#[test]
fn load_trusted_setup_invalid_g1_byte_length() {
load_trusted_setup_invalid_g1_byte_length_test(load_trusted_setup);
}

#[test]
fn load_trusted_setup_invalid_g1_point() {
load_trusted_setup_invalid_g1_point_test(load_trusted_setup);
}

#[test]
fn load_trusted_setup_invalid_g2_byte_length() {
load_trusted_setup_invalid_g2_byte_length_test(load_trusted_setup);
}

#[test]
fn load_trusted_setup_invalid_g2_point() {
load_trusted_setup_invalid_g2_point_test(load_trusted_setup);
}

#[test]
fn load_trusted_setup_invalid_form() {
load_trusted_setup_invalid_form_test(load_trusted_setup);
}

#[test]
fn load_trusted_setup_file_invalid_format() {
load_trusted_setup_file_invalid_format_test(load_trusted_setup_file);
}

#[test]
fn load_trusted_setup_file_valid_format() {
load_trusted_setup_file_valid_format_test(load_trusted_setup_file);
}

#[test]
fn free_trusted_setup_null_ptr() {
free_trusted_setup_null_ptr_test(free_trusted_setup);
}

#[test]
fn free_trusted_setup_set_all_values_to_null() {
free_trusted_setup_set_all_values_to_null_test(free_trusted_setup, load_trusted_setup_file);
}

#[test]
fn compute_blob_kzg_proof_invalid_blob() {
compute_blob_kzg_proof_invalid_blob_test(compute_blob_kzg_proof, load_trusted_setup_file);
}

#[test]
fn compute_blob_kzg_proof_commitment_is_point_at_infinity() {
compute_blob_kzg_proof_commitment_is_point_at_infinity_test(
compute_blob_kzg_proof,
load_trusted_setup_file,
);
}
}
Loading
Loading