Skip to content

Commit 78928e7

Browse files
committed
Add constant for length of Share.
All of the `{ KEY_LEN + 1 }` was a bit much,
1 parent f944fd9 commit 78928e7

File tree

1 file changed

+23
-34
lines changed

1 file changed

+23
-34
lines changed

src/hsm.rs

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const DOMAIN: Domain = Domain::all();
3939
const ID: Id = 0x1;
4040
const SEED_LEN: usize = 32;
4141
const KEY_LEN: usize = 32;
42+
const SHARE_LEN: usize = KEY_LEN + 1;
4243
const LABEL: &str = "backup";
4344

4445
const SHARES: usize = 5;
@@ -229,7 +230,7 @@ impl Hsm {
229230
Scalar,
230231
ProjectivePoint,
231232
ChaCha20Rng,
232-
{ KEY_LEN + 1 },
233+
SHARE_LEN,
233234
>(*nzs.as_ref(), None, &mut rng)
234235
.map_err(|e| HsmError::SplitKeyFailed { e })?;
235236

@@ -424,18 +425,15 @@ impl Hsm {
424425
info!("Restoring HSM from backup");
425426
info!("Restoring backup / wrap key from shares");
426427
// vector used to collect shares
427-
let mut shares: Vec<Share<{ KEY_LEN + 1 }>> = Vec::new();
428+
let mut shares: Vec<Share<SHARE_LEN>> = Vec::new();
428429

429430
// deserialize verifier:
430431
// verifier was serialized to output/verifier.json in the provisioning ceremony
431432
// it must be included in and deserialized from the ceremony inputs
432433
let verifier = self.out_dir.join("verifier.json");
433434
let verifier = fs::read_to_string(verifier)?;
434-
let verifier: FeldmanVerifier<
435-
Scalar,
436-
ProjectivePoint,
437-
{ KEY_LEN + 1 },
438-
> = serde_json::from_str(&verifier)?;
435+
let verifier: FeldmanVerifier<Scalar, ProjectivePoint, SHARE_LEN> =
436+
serde_json::from_str(&verifier)?;
439437

440438
// get enough shares to recover backup key
441439
for _ in 1..=THRESHOLD {
@@ -499,7 +497,7 @@ impl Hsm {
499497
};
500498

501499
// construct a Share from the decoded hex string
502-
let share: Share<{ KEY_LEN + 1 }> =
500+
let share: Share<SHARE_LEN> =
503501
match Share::try_from(&share_vec[..]) {
504502
Ok(share) => share,
505503
Err(_) => {
@@ -536,7 +534,7 @@ impl Hsm {
536534

537535
let scalar = Feldman::<THRESHOLD, SHARES>::combine_shares::<
538536
Scalar,
539-
{ KEY_LEN + 1 },
537+
SHARE_LEN,
540538
>(&shares)
541539
.map_err(|e| HsmError::CombineKeyFailed { e })?;
542540

@@ -936,7 +934,7 @@ mod tests {
936934
secret
937935
}
938936

939-
fn deserialize_share(share: &str) -> Result<Share<{ KEY_LEN + 1 }>> {
937+
fn deserialize_share(share: &str) -> Result<Share<SHARE_LEN>> {
940938
// filter out whitespace to keep hex::decode happy
941939
let share: String =
942940
share.chars().filter(|c| !c.is_whitespace()).collect();
@@ -960,7 +958,7 @@ mod tests {
960958
Scalar,
961959
ProjectivePoint,
962960
ThreadRng,
963-
{ KEY_LEN + 1 },
961+
SHARE_LEN,
964962
>(*nzs.as_ref(), None, &mut rng)
965963
.map_err(|e| anyhow::anyhow!("failed to split secret: {}", e))?;
966964

@@ -970,7 +968,7 @@ mod tests {
970968

971969
let scalar = Feldman::<THRESHOLD, SHARES>::combine_shares::<
972970
Scalar,
973-
{ KEY_LEN + 1 },
971+
SHARE_LEN,
974972
>(&shares)
975973
.map_err(|e| anyhow::anyhow!("failed to combine secret: {}", e))?;
976974

@@ -986,12 +984,9 @@ mod tests {
986984
// deserialize a verifier & use it to verify the shares in SHARE_ARRAY
987985
#[test]
988986
fn verify_shares() -> Result<()> {
989-
let verifier: FeldmanVerifier<
990-
Scalar,
991-
ProjectivePoint,
992-
{ KEY_LEN + 1 },
993-
> = serde_json::from_str(VERIFIER)
994-
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
987+
let verifier: FeldmanVerifier<Scalar, ProjectivePoint, SHARE_LEN> =
988+
serde_json::from_str(VERIFIER)
989+
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
995990

996991
for share in SHARE_ARRAY {
997992
let share = deserialize_share(share)?;
@@ -1003,15 +998,12 @@ mod tests {
1003998

1004999
#[test]
10051000
fn verify_zero_share() -> Result<()> {
1006-
let verifier: FeldmanVerifier<
1007-
Scalar,
1008-
ProjectivePoint,
1009-
{ KEY_LEN + 1 },
1010-
> = serde_json::from_str(VERIFIER)
1011-
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
1001+
let verifier: FeldmanVerifier<Scalar, ProjectivePoint, SHARE_LEN> =
1002+
serde_json::from_str(VERIFIER)
1003+
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
10121004

1013-
let share: Share<{ KEY_LEN + 1 }> =
1014-
Share::try_from([0u8; KEY_LEN + 1].as_ref())
1005+
let share: Share<SHARE_LEN> =
1006+
Share::try_from([0u8; SHARE_LEN].as_ref())
10151007
.context("Failed to create Share from static array.")?;
10161008

10171009
assert!(!verifier.verify(&share));
@@ -1023,12 +1015,9 @@ mod tests {
10231015
// the verifier to fail but that seems to be very wrong.
10241016
#[test]
10251017
fn verify_share_with_changed_byte() -> Result<()> {
1026-
let verifier: FeldmanVerifier<
1027-
Scalar,
1028-
ProjectivePoint,
1029-
{ KEY_LEN + 1 },
1030-
> = serde_json::from_str(VERIFIER)
1031-
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
1018+
let verifier: FeldmanVerifier<Scalar, ProjectivePoint, SHARE_LEN> =
1019+
serde_json::from_str(VERIFIER)
1020+
.context("Failed to deserialize FeldmanVerifier from JSON.")?;
10321021

10331022
let mut share = deserialize_share(SHARE_ARRAY[0])?;
10341023
println!("share: {}", share.0[0]);
@@ -1047,14 +1036,14 @@ mod tests {
10471036

10481037
#[test]
10491038
fn recover_secret() -> Result<()> {
1050-
let mut shares: Vec<Share<{ KEY_LEN + 1 }>> = Vec::new();
1039+
let mut shares: Vec<Share<SHARE_LEN>> = Vec::new();
10511040
for share in SHARE_ARRAY {
10521041
shares.push(deserialize_share(share)?);
10531042
}
10541043

10551044
let scalar = Feldman::<THRESHOLD, SHARES>::combine_shares::<
10561045
Scalar,
1057-
{ KEY_LEN + 1 },
1046+
SHARE_LEN,
10581047
>(&shares)
10591048
.map_err(|e| anyhow::anyhow!("failed to combine secret: {}", e))?;
10601049

0 commit comments

Comments
 (0)