replace unsafe static values by function calls

This commit is contained in:
Georgio Nicolas
2025-07-29 15:04:11 +02:00
parent a4e674c98b
commit 24f877fda5
3 changed files with 19 additions and 20 deletions
-1
View File
@@ -17,7 +17,6 @@ nym-contracts-common = { path = "../cosmwasm-smart-contracts/contracts-common",
bs58 = { workspace = true }
lazy_static = { workspace = true }
rand = { workspace = true }
rand_chacha = { workspace = true }
rand_core = { workspace = true }
+14 -11
View File
@@ -2,7 +2,10 @@
// SPDX-License-Identifier: Apache-2.0
use crate::bte::keys::{DecryptionKey, PublicKey};
use crate::bte::{evaluate_f, Params, CHUNK_SIZE, G2_GENERATOR_PREPARED, NUM_CHUNKS, PAIRING_BASE};
use crate::bte::{
evaluate_f, precompute_g2_generator_prepared, precompute_pairing_base, Params, CHUNK_SIZE,
NUM_CHUNKS,
};
use crate::error::DkgError;
use crate::utils::{combine_g1_chunks, combine_scalar_chunks, deserialize_g1, deserialize_g2};
use crate::{Chunk, ChunkedShare, Share};
@@ -296,14 +299,14 @@ pub fn decrypt_share(
let cc_ij = &ciphertext.ciphertext_chunks[i][j];
let miller = bls12_381::multi_miller_loop(&[
(&cc_ij.to_affine(), &G2_GENERATOR_PREPARED),
(&cc_ij.to_affine(), &precompute_g2_generator_prepared()),
(&rr_j.to_affine(), &G2Prepared::from(b_neg)),
(&dk.a.to_affine(), &G2Prepared::from(zz_j)),
(&ss_j.to_affine(), &G2Prepared::from(e_neg)),
]);
let m = miller.final_exponentiation();
plaintext.chunks[j] = baby_step_giant_step(&m, &PAIRING_BASE, lookup_table)?;
plaintext.chunks[j] = baby_step_giant_step(&m, &precompute_pairing_base(), lookup_table)?;
}
plaintext.try_into()
@@ -363,7 +366,7 @@ impl BabyStepGiantStepLookup {
impl Default for BabyStepGiantStepLookup {
fn default() -> Self {
BabyStepGiantStepLookup::precompute(&PAIRING_BASE)
BabyStepGiantStepLookup::precompute(&precompute_pairing_base())
}
}
@@ -399,7 +402,7 @@ pub fn baby_step_giant_step(
#[cfg(test)]
mod tests {
use super::*;
use crate::bte::{keygen, setup, DEFAULT_BSGS_TABLE};
use crate::bte::{encryption, keygen, setup};
use rand_core::SeedableRng;
fn verify_hazmat_rand(ciphertext: &Ciphertexts, randomness: &HazmatRandomness) {
@@ -457,7 +460,7 @@ mod tests {
let (decryption_key1, public_key1) = keygen(&params, &mut rng);
let (decryption_key2, public_key2) = keygen(&params, &mut rng);
let lookup_table = &DEFAULT_BSGS_TABLE;
let lookup_table = encryption::BabyStepGiantStepLookup::default();
for _ in 0..10 {
let m1 = Share::random(&mut rng);
@@ -472,7 +475,7 @@ mod tests {
&decryption_key1,
0,
&ciphertext,
Some(lookup_table),
Some(&lookup_table),
)
.unwrap();
let recovered2 = decrypt_share(
@@ -480,7 +483,7 @@ mod tests {
&decryption_key2,
1,
&ciphertext,
Some(lookup_table),
Some(&lookup_table),
)
.unwrap();
assert_eq!(m1, recovered1);
@@ -498,7 +501,7 @@ mod tests {
let (decryption_key1, public_key1) = keygen(&params, &mut rng);
let (decryption_key2, public_key2) = keygen(&params, &mut rng);
let lookup_table = &DEFAULT_BSGS_TABLE;
let lookup_table = encryption::BabyStepGiantStepLookup::default();
for _ in 0..10 {
let m1 = Share::random(&mut rng);
@@ -513,7 +516,7 @@ mod tests {
&decryption_key1,
0,
&ciphertext,
Some(lookup_table),
Some(&lookup_table),
)
.unwrap();
let recovered2 = decrypt_share(
@@ -521,7 +524,7 @@ mod tests {
&decryption_key2,
1,
&ciphertext,
Some(lookup_table),
Some(&lookup_table),
)
.unwrap();
assert_eq!(m1, recovered1);
+5 -8
View File
@@ -5,7 +5,6 @@ use crate::utils::hash_g2;
use crate::{Chunk, Share};
use bls12_381::{G1Affine, G2Affine, G2Prepared, G2Projective, Gt};
use group::Curve;
use lazy_static::lazy_static;
pub mod encryption;
pub mod keys;
@@ -16,13 +15,11 @@ pub mod proof_sharing;
pub use encryption::{decrypt_share, encrypt_shares, Ciphertexts};
pub use keys::{keygen, DecryptionKey, PublicKey, PublicKeyWithProof};
lazy_static! {
pub(crate) static ref PAIRING_BASE: Gt =
bls12_381::pairing(&G1Affine::generator(), &G2Affine::generator());
pub(crate) static ref G2_GENERATOR_PREPARED: G2Prepared =
G2Prepared::from(G2Affine::generator());
pub(crate) static ref DEFAULT_BSGS_TABLE: encryption::BabyStepGiantStepLookup =
encryption::BabyStepGiantStepLookup::default();
pub(crate) fn precompute_pairing_base() -> Gt {
bls12_381::pairing(&G1Affine::generator(), &G2Affine::generator())
}
pub(crate) fn precompute_g2_generator_prepared() -> G2Prepared {
G2Prepared::from(G2Affine::generator())
}
// Domain tries to follow guidelines specified by: