Skip to content

Organize STM - Blst multi_sig module #2405

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

Merged
merged 3 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 6 additions & 0 deletions mithril-stm/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.3.43 (05-04-2025)

### Added

- Added a `bls_multi_signature` module and multi-signature functionality covered by its submodules.

## 0.3.41 (20-03-2025)

### Added
Expand Down
2 changes: 1 addition & 1 deletion mithril-stm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-stm"
version = "0.3.42"
version = "0.3.43"
edition = { workspace = true }
authors = { workspace = true }
homepage = { workspace = true }
Expand Down
2 changes: 1 addition & 1 deletion mithril-stm/benches/multi_sig.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use blake2::{digest::consts::U64, Blake2b, Digest};
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use mithril_stm::multi_sig::{Signature, SigningKey, VerificationKey};
use mithril_stm::bls_multi_signature::{Signature, SigningKey, VerificationKey};
use rand_chacha::ChaCha20Rng;
use rand_core::{RngCore, SeedableRng};

Expand Down
95 changes: 95 additions & 0 deletions mithril-stm/src/bls_multi_signature/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
pub(crate) mod unsafe_helpers {
use crate::bls_multi_signature::{ProofOfPossession, VerificationKey};
use crate::error::MultiSignatureError;
use crate::error::MultiSignatureError::SerializationError;
use blst::min_sig::{PublicKey as BlstVk, SecretKey as BlstSk, Signature as BlstSig};
use blst::{
blst_fp12, blst_fp12_finalverify, blst_p1, blst_p1_affine, blst_p1_affine_generator,
blst_p1_compress, blst_p1_from_affine, blst_p1_to_affine, blst_p1_uncompress, blst_p2,
blst_p2_affine, blst_p2_affine_generator, blst_p2_from_affine, blst_p2_to_affine,
blst_scalar, blst_sk_to_pk_in_g1,
};

/// Check manually if the pairing `e(g1,mvk) = e(k2,g2)` holds.
pub(crate) fn verify_pairing(vk: &VerificationKey, pop: &ProofOfPossession) -> bool {
unsafe {
let g1_p = *blst_p1_affine_generator();
let mvk_p = std::mem::transmute::<BlstVk, blst_p2_affine>(vk.to_blst_vk());
let ml_lhs = blst_fp12::miller_loop(&mvk_p, &g1_p);

let mut k2_p = blst_p1_affine::default();
blst_p1_to_affine(&mut k2_p, &pop.to_k2());
let g2_p = *blst_p2_affine_generator();
let ml_rhs = blst_fp12::miller_loop(&g2_p, &k2_p);

blst_fp12_finalverify(&ml_lhs, &ml_rhs)
}
}

pub(crate) fn compress_p1(k2: &blst_p1) -> [u8; 48] {
let mut bytes = [0u8; 48];
unsafe { blst_p1_compress(bytes.as_mut_ptr(), k2) }
bytes
}

pub(crate) fn uncompress_p1(bytes: &[u8]) -> Result<blst_p1, MultiSignatureError> {
unsafe {
if bytes.len() == 48 {
let mut point = blst_p1_affine::default();
let mut out = blst_p1::default();
blst_p1_uncompress(&mut point, bytes.as_ptr());
blst_p1_from_affine(&mut out, &point);
Ok(out)
} else {
Err(SerializationError)
}
}
}

pub(crate) fn scalar_to_pk_in_g1(sk: &BlstSk) -> blst_p1 {
unsafe {
let sk_scalar = std::mem::transmute::<&BlstSk, &blst_scalar>(sk);
let mut out = blst_p1::default();
blst_sk_to_pk_in_g1(&mut out, sk_scalar);
out
}
}

pub(crate) fn vk_from_p2_affine(vk: &VerificationKey) -> blst_p2 {
unsafe {
let mut projective_p2 = blst_p2::default();
blst_p2_from_affine(
&mut projective_p2,
&std::mem::transmute::<BlstVk, blst_p2_affine>(vk.to_blst_vk()),
);
projective_p2
}
}

pub(crate) fn sig_to_p1(sig: &BlstSig) -> blst_p1 {
unsafe {
let mut projective_p1 = blst_p1::default();
blst_p1_from_affine(
&mut projective_p1,
&std::mem::transmute::<BlstSig, blst_p1_affine>(*sig),
);
projective_p1
}
}

pub(crate) fn p2_affine_to_vk(grouped_vks: &blst_p2) -> BlstVk {
unsafe {
let mut affine_p2 = blst_p2_affine::default();
blst_p2_to_affine(&mut affine_p2, grouped_vks);
std::mem::transmute::<blst_p2_affine, BlstVk>(affine_p2)
}
}

pub(crate) fn p1_affine_to_sig(grouped_sigs: &blst_p1) -> BlstSig {
unsafe {
let mut affine_p1 = blst_p1_affine::default();
blst_p1_to_affine(&mut affine_p1, grouped_sigs);
std::mem::transmute::<blst_p1_affine, BlstSig>(affine_p1)
}
}
}
Loading