Skip to content

Commit 1207eeb

Browse files
authored
fix: use default epoch_ID if not set (#483)
* fix: use default epoch_ID if not set * chore: tests around missing / malformed epoch_id for key and crs_gen
1 parent 0afb30c commit 1207eeb

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

core/service/src/engine/centralized/service/crs_gen.rs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,38 @@ mod tests {
440440
assert_eq!(err.code(), tonic::Code::InvalidArgument);
441441
}
442442

443-
// missing epoch ID
443+
// TODO: re-enable the following tests after the default epoch fallback is removed in validation
444+
// https://github.com/zama-ai/kms-internal/issues/2758
445+
//
446+
// // missing epoch ID
447+
// {
448+
// let request = CrsGenRequest {
449+
// request_id: Some(req_id.into()),
450+
// epoch_id: None, // missing
451+
// context_id: None,
452+
// params: FheParameter::Test.into(),
453+
// domain: Some(domain),
454+
// extra_data: vec![],
455+
// max_num_bits: None,
456+
// };
457+
// let err = crs_gen_impl(&kms, Request::new(request), false)
458+
// .await
459+
// .unwrap_err();
460+
// assert_eq!(err.code(), tonic::Code::InvalidArgument);
461+
// }
462+
}
463+
464+
// test the missing epoch ID case that is currently allowed with a warning, to make sure the default epoch fallback works, and to prepare for the future removal of the default epoch fallback
465+
// TODO: remove following tests after the default epoch fallback is no longer used in validation
466+
// https://github.com/zama-ai/kms-internal/issues/2758
467+
#[tokio::test]
468+
async fn default_epoch_id() {
469+
let mut rng = AesRng::seed_from_u64(54321);
470+
let (kms, _) = setup_central_test_kms(&mut rng).await;
471+
let req_id = derive_request_id("test_crs_gen_default_epoch").unwrap();
472+
let domain = alloy_to_protobuf_domain(&dummy_domain()).unwrap();
473+
474+
// missing epoch ID should use default epoch with a warning, but not fail
444475
{
445476
let request = CrsGenRequest {
446477
request_id: Some(req_id.into()),
@@ -451,10 +482,21 @@ mod tests {
451482
extra_data: vec![],
452483
max_num_bits: None,
453484
};
454-
let err = crs_gen_impl(&kms, Request::new(request), false)
485+
let _req = crs_gen_impl(&kms, Request::new(request), false)
455486
.await
456-
.unwrap_err();
457-
assert_eq!(err.code(), tonic::Code::InvalidArgument);
487+
.unwrap();
488+
let res = get_crs_gen_result_impl(&kms, Request::new(req_id.into()), false)
489+
.await
490+
.unwrap();
491+
492+
assert_eq!(
493+
res.into_inner()
494+
.request_id
495+
.unwrap()
496+
.request_id
497+
.to_ascii_lowercase(),
498+
req_id.as_str()
499+
);
458500
}
459501
}
460502

core/service/src/engine/centralized/service/key_gen.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,32 @@ pub(crate) mod tests {
690690
.unwrap_err();
691691
assert_eq!(err.code(), tonic::Code::InvalidArgument);
692692
}
693+
694+
// invalid epoch ID should fail
695+
{
696+
let request = KeyGenRequest {
697+
params: Some(FheParameter::Test.into()),
698+
keyset_config: None,
699+
keyset_added_info: None,
700+
request_id: Some(request_id.into()),
701+
context_id: None,
702+
preproc_id: Some(preproc_id.into()),
703+
domain: Some(domain.clone()),
704+
epoch_id: Some(kms_grpc::kms::v1::RequestId {
705+
request_id: "invalid-epoch-id".to_string(),
706+
}),
707+
extra_data: vec![],
708+
};
709+
let err = key_gen_impl(
710+
&kms,
711+
tonic::Request::new(request),
712+
#[cfg(feature = "insecure")]
713+
true,
714+
)
715+
.await
716+
.unwrap_err();
717+
assert_eq!(err.code(), tonic::Code::InvalidArgument);
718+
}
693719
}
694720

695721
#[tokio::test]

core/service/src/engine/validation_non_wasm.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,9 +761,6 @@ fn unpack_crs_gen_request(req: CrsGenRequest) -> anyhow::Result<VerifiedCrsGenRe
761761
"Received new crs generation request"
762762
);
763763

764-
let epoch_id =
765-
parse_optional_grpc_request_id(&req.epoch_id, RequestIdParsingErr::CrsGenRequest)?;
766-
767764
// This verification is more strict than the checks in [compute_witness_dim] below
768765
// because it only allows powers of 2. But there are no strong reasons
769766
// to use max_num_bits that are not powers of 2 so we enforce it here.
@@ -779,12 +776,17 @@ fn unpack_crs_gen_request(req: CrsGenRequest) -> anyhow::Result<VerifiedCrsGenRe
779776
let witness_dim = compute_witness_dim(&crs_params, req.max_num_bits.map(|x| x as usize))?;
780777

781778
// TODO(zama-ai/kms-internal/issues/2758)
782-
// remove the default context when all of context is ready
779+
// remove the default context and epoch when all of context is ready
783780
let context_id = match &req.context_id {
784781
Some(ctx) => parse_grpc_request_id(ctx, RequestIdParsingErr::Context)?,
785782
None => *DEFAULT_MPC_CONTEXT,
786783
};
787784

785+
let epoch_id = match &req.epoch_id {
786+
Some(epoch) => parse_grpc_request_id(epoch, RequestIdParsingErr::Epoch)?,
787+
None => *DEFAULT_EPOCH_ID,
788+
};
789+
788790
let eip712_domain = optional_protobuf_to_alloy_domain(req.domain.as_ref())?;
789791

790792
Ok(VerifiedCrsGenRequest {

0 commit comments

Comments
 (0)