diff --git a/src/algs.rs b/src/algs.rs index a6c83db..d6e69e6 100644 --- a/src/algs.rs +++ b/src/algs.rs @@ -21,7 +21,7 @@ use crate::types::{FORS_PRF, FORS_ROOTS, FORS_TREE, TREE, WOTS_HASH, WOTS_PK, WO /// Output: Integer value of `X`. pub(crate) fn to_int(x: &[u8], n: usize) -> u64 { assert_eq!(x.len(), n); - println!("byte count {}", x.len()); + //println!("byte count {}", x.len()); // 1: total ← 0 let mut total = 0_u64; @@ -415,7 +415,7 @@ pub(crate) fn wots_pk_from_sig( &adrs, ) .expect("chain broke2!"); - println!("wots_pk_from_sig tmp: [{}] {}", i, hex::encode(&tmp[i])); // TODO <<<========== BROKE b4 HERE!!! + //println!("wots_pk_from_sig tmp: [{}] {}", i, hex::encode(&tmp[i])); // TODO <<<========== BROKE b4 HERE!!! // 14: end for } @@ -442,7 +442,7 @@ pub(crate) fn h( pk_seed: &[u8], adrs: &[u8], lnode: &[u8], rnode: &[u8], ) -> GenericArray { let mut hasher = Shake256::default(); - [pk_seed, &adrs, lnode, rnode] + [pk_seed, adrs, lnode, rnode] .iter() .for_each(|item| hasher.update(item)); let mut reader = hasher.finalize_xof(); @@ -584,10 +584,12 @@ pub(crate) fn xmss_pk_from_sig(sig, m, pk_seed, &adrs).0.clone(); - println!("verif node_0: {}", hex::encode(&node_0)); // TODO blah...3rd? time here - if node_0[0] == 0x9a { - println!("wogga"); - } + //println!("verif node_0: {}", hex::encode(&node_0)); // TODO blah...3rd? time here + //if node_0[0] == 0x9a { + //println!("wogga"); + //println!("auth 0 : {}", hex::encode(&auth[0])); + + //} // 6: // 7: ADRS.setTypeAndClear(TREE) ▷ Compute root from WOTS+ pk and AUTH adrs.set_type_and_clear(TREE); @@ -603,11 +605,11 @@ pub(crate) fn xmss_pk_from_sig> k) % 2) == 0 { + let node_1 = if ((idx >> k) & 1) == 0 { // 12: ADRS.setTreeIndex(ADRS.getTreeIndex()/2) let tmp = adrs.get_tree_index() / 2; adrs.set_tree_index(tmp); - + println!("hit even!! {:x?}", idx); // Odd, this is 4 but py is 5 // 13: node[1] ← H(PK.seed, ADRS, node[0] ∥ AUTH[k]) h(pk_seed, &adrs.to_bytes(), &node_0, &auth[k as usize]) @@ -619,8 +621,16 @@ pub(crate) fn xmss_pk_from_sig( m: &[u8], sk_seed: &[u8], pk_seed: &[u8], idx_tree: u64, idx_leaf: u32, ) -> Result, &'static str> { + let mut idx_tree = idx_tree; // // 1: ADRS ← toByte(0, 32) let mut adrs = Adrs::default(); @@ -676,7 +688,7 @@ pub(crate) fn ht_sign< let idx_leaf = idx_tree % 2u64.pow(HP::to_u32()); // 9: idx_tree ← idx_tree ≫ h′ ▷ Remove least significant h′ bits from idx_tree - let idx_tree = idx_tree >> HP::to_u32(); + idx_tree = idx_tree >> HP::to_u32(); // 10: ADRS.setLayerAddress(j) adrs.set_layer_address(j); @@ -693,15 +705,18 @@ pub(crate) fn ht_sign< // 14: if j < d − 1 then if j < (D::to_u32() - 1) { // + println!("rooooooot before --> : {}", hex::encode(&root)); // we are golden here YYYYYYYYYY + // 15: root ← xmss_PKFromSig(idx_leaf, SIG_tmp, root, PK.seed, ADRS) root = xmss_pk_from_sig::(idx_leaf as u32, &sig_tmp, &root, pk_seed, &adrs); + println!("rooooooot after --> : {}", hex::encode(&root)); // we are golden here YYYYYYYYYY // 16: end if } // 17: end for } - + println!("sig_ht (maybe several): {}", hex::encode(&sig_ht.xmss_sigs[0].sig_wots.data[0])); // we are golden here YYYYYYYYYY // 18: return SIGHT Ok(sig_ht) } @@ -716,6 +731,7 @@ pub(crate) fn ht_verify, pk_seed: &[u8], idx_tree: u64, idx_leaf: u32, pk_root: &GenericArray, ) -> bool { + let mut idx_tree = idx_tree; // // 1: ADRS ← toByte(0, 32) let mut adrs = Adrs::default(); @@ -729,7 +745,7 @@ pub(crate) fn ht_verify> HP::to_u32(); + idx_tree = idx_tree >> HP::to_u32(); // 9: ADRS.setLayerAddress(j) adrs.set_layer_address(j); @@ -760,6 +776,9 @@ pub(crate) fn ht_verify, } +impl< + A: ArrayLength, + D: ArrayLength, + HP: ArrayLength, + K: ArrayLength, + LEN: ArrayLength, + N: ArrayLength, +> SlhDsaSig { + pub fn deser(self, out: &mut [u8]) { + assert_eq!(out.len(), N::to_usize() + // randomness + N::to_usize() * K::to_usize() + K::to_usize() * A::to_usize() * N::to_usize() + // ForsSig + D::to_usize() * (HP::to_usize() * N::to_usize() + LEN::to_usize() * N::to_usize()) + ); + out[0..N::to_usize()].copy_from_slice(&self.randomness); + let mut start = N::to_usize(); + // for k in 0..K::to_usize() { + // out[start..(start+N::to_usize())].copy_from_slice(&self.fors_sig.private_key_value[k]); + // start += N::to_usize(); + // } + for k in 0..K::to_usize() { + out[start..(start+N::to_usize())].copy_from_slice(&self.fors_sig.private_key_value[k]); + start += N::to_usize(); + for a in 0..A::to_usize() { + out[start..(start+N::to_usize())].copy_from_slice(&self.fors_sig.auth[k].tree[a]); + start += N::to_usize(); + } + } + for d in 0..D::to_usize() { + println!("and we move to xmss {} starting at {}", d, start); + + for len in 0..LEN::to_usize() { + out[start..(start+N::to_usize())].copy_from_slice(&self.ht_sig.xmss_sigs[d].sig_wots.data[len]); + start += N::to_usize(); + } + for hp in 0..HP::to_usize() { + out[start..(start+N::to_usize())].copy_from_slice(&self.ht_sig.xmss_sigs[d].auth[hp]); + start += N::to_usize(); + } + } + assert_eq!(start, out.len()) + } +} + #[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] pub struct SlhPublicKey { pub(crate) pk_seed: GenericArray, @@ -32,7 +75,7 @@ pub struct SlhPrivateKey { /// Fig 13 on page 29 -#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, Debug, Default, Zeroize, ZeroizeOnDrop)] pub(crate) struct ForsSig { pub(crate) private_key_value: GenericArray, K>, pub(crate) auth: GenericArray, K>, @@ -44,17 +87,17 @@ pub(crate) struct ForsPk { } /// Fig 10? -#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, Debug, Default, Zeroize, ZeroizeOnDrop)] pub(crate) struct Auth { pub(crate) tree: GenericArray, A>, } -#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, Debug, Default, Zeroize, ZeroizeOnDrop)] pub(crate) struct HtSig { pub(crate) xmss_sigs: GenericArray, D>, } -#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, Debug, Default, Zeroize, ZeroizeOnDrop)] pub struct WotsSig { pub(crate) data: GenericArray, LEN>, } @@ -63,7 +106,7 @@ pub struct WotsSig { pub struct WotsPk(pub(crate) GenericArray); -#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)] +#[derive(Clone, Debug, Default, Zeroize, ZeroizeOnDrop)] pub struct XmssSig { pub(crate) sig_wots: WotsSig, pub(crate) auth: GenericArray, HP>,