Skip to content

Commit

Permalink
WIP: allow for two binary formats
Browse files Browse the repository at this point in the history
  • Loading branch information
aewag committed Sep 2, 2024
1 parent 0480313 commit 76dff01
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
7 changes: 4 additions & 3 deletions src/hss/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,7 @@ mod tests {

for index in 0..keypair_lifetime {
assert_eq!(
signing_key.as_slice()[REF_IMPL_SSTS_EXT_SIZE
..HSS_COMPRESSED_USED_LEAFS_SIZE + REF_IMPL_SSTS_EXT_SIZE],
signing_key.as_slice()[..HSS_COMPRESSED_USED_LEAFS_SIZE],
index.to_be_bytes(),
);
assert_eq!(
Expand Down Expand Up @@ -410,7 +409,9 @@ mod tests {
);
}
assert_eq!(
signing_key.as_slice()[(REF_IMPL_MAX_PRIVATE_KEY_SIZE - H::OUTPUT_SIZE as usize)..],
signing_key.as_slice()[(REF_IMPL_MAX_PRIVATE_KEY_SIZE
- REF_IMPL_SSTS_EXT_SIZE
- H::OUTPUT_SIZE as usize)..],
[0u8; H::OUTPUT_SIZE as usize],
);
}
Expand Down
18 changes: 13 additions & 5 deletions src/hss/reference_impl_private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ impl<H: HashChain> ReferenceImplPrivateKey<H> {
pub fn to_binary_representation(&self) -> ArrayVec<[u8; REF_IMPL_MAX_PRIVATE_KEY_SIZE]> {
let mut result = ArrayVec::new();

result.extend_from_slice(&self.sst_ext.signing_entity_idx.to_be_bytes());
result.extend_from_slice(&self.sst_ext.l0_top_div.to_be_bytes());
if self.sst_ext != SstExtension::default() {
result.extend_from_slice(&self.sst_ext.signing_entity_idx.to_be_bytes());
result.extend_from_slice(&self.sst_ext.l0_top_div.to_be_bytes());
}
result.extend_from_slice(&self.compressed_used_leafs_indexes.count.to_be_bytes());
result.extend_from_slice(&self.compressed_parameter.0);
result.extend_from_slice(self.seed.as_slice());
Expand All @@ -143,16 +145,22 @@ impl<H: HashChain> ReferenceImplPrivateKey<H> {
}

pub fn from_binary_representation(data: &[u8]) -> Result<Self, ()> {
if data.len() != REF_IMPL_MAX_PRIVATE_KEY_SIZE - MAX_SEED_LEN + H::OUTPUT_SIZE as usize {
if data.len() != REF_IMPL_MAX_PRIVATE_KEY_SIZE - MAX_SEED_LEN + H::OUTPUT_SIZE as usize
&& data.len()
!= REF_IMPL_MAX_PRIVATE_KEY_SIZE - MAX_SEED_LEN + H::OUTPUT_SIZE as usize
- REF_IMPL_SSTS_EXT_SIZE
{
// TODO/Review: why don't we just use REF_IMPL_MAX_PRIVATE_KEY_SIZE? (as in "SigningKey")?
return Err(());
}

let mut result = Self::default();
let mut index = 0;

let ssts_ext = read_and_advance(data, REF_IMPL_SSTS_EXT_SIZE, &mut index);
result.sst_ext = SstExtension::from_slice(ssts_ext)?;
if data.len() == REF_IMPL_MAX_PRIVATE_KEY_SIZE - MAX_SEED_LEN + H::OUTPUT_SIZE as usize {
let ssts_ext = read_and_advance(data, REF_IMPL_SSTS_EXT_SIZE, &mut index);
result.sst_ext = SstExtension::from_slice(ssts_ext)?;
}

let compressed_used_leafs_indexes =
read_and_advance(data, HSS_COMPRESSED_USED_LEAFS_SIZE, &mut index);
Expand Down
4 changes: 2 additions & 2 deletions tests/reference_implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ fn should_produce_same_private_key() {
let ref_signing_key = read_private_key(path);
let ref_verifying_key = read_public_key(path);

assert!(ref_signing_key == sk.as_slice());
assert!(ref_verifying_key == vk.as_slice());
assert_eq!(ref_signing_key, sk.as_slice());
assert_eq!(ref_verifying_key, vk.as_slice());
}

fn read_private_key(path: &Path) -> Vec<u8> {
Expand Down

0 comments on commit 76dff01

Please sign in to comment.