Skip to content

Commit 6ec797f

Browse files
committed
chore fully init the smaller fields' cache of lagrange basis
1 parent d0942dc commit 6ec797f

4 files changed

Lines changed: 29 additions & 22 deletions

File tree

core/service/src/bin/kms-server.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,14 @@ async fn main_exec() -> anyhow::Result<()> {
368368

369369
tracing::info!("Starting KMS Server with core config: {:?}", &core_config);
370370

371-
if let Some(threshold_config) = core_config.threshold.as_ref()
372-
&& let Some(peers) = threshold_config.peers.as_ref()
373-
{
374-
init_all_lagrange_stores(peers.len(), threshold_config.threshold as usize)?;
375-
}
371+
// NOTE: Cache for GF16 (which we use here -- for now) is fully
372+
// hence why the block below is commented out.
373+
374+
//if let Some(threshold_config) = core_config.threshold.as_ref()
375+
// && let Some(peers) = threshold_config.peers.as_ref()
376+
//{
377+
// init_all_lagrange_stores(peers.len(), threshold_config.threshold as usize)?;
378+
//}
376379

377380
tracing::info!(
378381
"Multi-threading values: tokio::num_workers: {}, rayon_num_threads: {}, total_num_cpus: {}",

core/threshold-algebra/src/galois_fields/common.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use itertools::Itertools;
77

88
/// Builds a LagrangeMap containing pre-computed Lagrange polynomials for all sorted subsets
99
/// of `{embed(1), ..., embed(num_parties)}` with size ≥ `threshold + 1`.
10-
fn build_lagrange_map<F: Field>(
10+
pub(crate) fn build_lagrange_map<F: Field>(
1111
num_parties: usize,
1212
threshold: usize,
1313
) -> anyhow::Result<LagrangeMap<F>> {
@@ -25,17 +25,13 @@ fn build_lagrange_map<F: Field>(
2525
Ok(map)
2626
}
2727

28+
#[allow(unused_variables)]
2829
/// Pre-computes Lagrange polynomial stores for all enabled Galois field types.
2930
/// Must be called at startup with the known number of parties and threshold.
31+
///
32+
/// __NOTE__: GF8 and GF16 are small enough that we can pre-compute all possible lagrange basis.
33+
/// so they are skipped here.
3034
pub fn init_all_lagrange_stores(num_parties: usize, threshold: usize) -> anyhow::Result<()> {
31-
#[cfg(feature = "extension_degree_3")]
32-
super::gf8::LAGRANGE_STORE
33-
.set(build_lagrange_map(num_parties, threshold)?)
34-
.ok();
35-
#[cfg(feature = "extension_degree_4")]
36-
super::gf16::LAGRANGE_STORE
37-
.set(build_lagrange_map(num_parties, threshold)?)
38-
.ok();
3935
#[cfg(feature = "extension_degree_5")]
4036
super::gf32::LAGRANGE_STORE
4137
.set(build_lagrange_map(num_parties, threshold)?)

core/threshold-algebra/src/galois_fields/gf16.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::{
2-
galois_fields::LagrangeMap,
2+
galois_fields::{LagrangeMap, common::build_lagrange_map},
33
poly::Poly,
44
structure_traits::{Field, FromU128, One, Ring, RingWithExceptionalSequence, Sample, Zero},
55
};
66
use g2p::{GaloisField, g2p};
77
use serde::{Deserialize, Serialize};
8-
use std::sync::{LazyLock, OnceLock};
8+
use std::sync::LazyLock;
99

1010
g2p!(
1111
GF16,
@@ -84,11 +84,15 @@ impl RingWithExceptionalSequence for GF16 {
8484
}
8585
}
8686

87-
pub(crate) static LAGRANGE_STORE: OnceLock<LagrangeMap<GF16>> = OnceLock::new();
87+
pub(crate) static LAGRANGE_STORE: LazyLock<LagrangeMap<GF16>> = LazyLock::new(|| {
88+
build_lagrange_map::<GF16>(0, 15).expect(
89+
" Initializaiton of the GF16 Lagrange basis can't fail with 15 parties and threshold 0",
90+
)
91+
});
8892

8993
impl Field for GF16 {
9094
fn cached_lagrange_polys(points: &[Self]) -> Option<&'static [Poly<Self>]> {
91-
LAGRANGE_STORE.get()?.get(points).map(|v| v.as_slice())
95+
LAGRANGE_STORE.get(points).map(|v| v.as_slice())
9296
}
9397
}
9498

core/threshold-algebra/src/galois_fields/gf8.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::{
2-
galois_fields::LagrangeMap,
2+
galois_fields::{LagrangeMap, common::build_lagrange_map},
33
poly::Poly,
44
structure_traits::{Field, FromU128, One, Ring, RingWithExceptionalSequence, Sample, Zero},
55
};
66
use g2p::{GaloisField, g2p};
77
use serde::{Deserialize, Serialize};
8-
use std::sync::{LazyLock, OnceLock};
8+
use std::sync::LazyLock;
99

1010
g2p!(
1111
GF8,
@@ -84,11 +84,15 @@ impl RingWithExceptionalSequence for GF8 {
8484
}
8585
}
8686

87-
pub(crate) static LAGRANGE_STORE: OnceLock<LagrangeMap<GF8>> = OnceLock::new();
87+
pub(crate) static LAGRANGE_STORE: LazyLock<LagrangeMap<GF8>> = LazyLock::new(|| {
88+
build_lagrange_map::<GF8>(0, 7).expect(
89+
" Initializaiton of the GF8 Lagrange basis can't fail with 7 parties and threshold 0",
90+
)
91+
});
8892

8993
impl Field for GF8 {
9094
fn cached_lagrange_polys(points: &[Self]) -> Option<&'static [Poly<Self>]> {
91-
LAGRANGE_STORE.get()?.get(points).map(|v| v.as_slice())
95+
LAGRANGE_STORE.get(points).map(|v| v.as_slice())
9296
}
9397
}
9498

0 commit comments

Comments
 (0)