Skip to content

Commit

Permalink
Fixes to support broader set of parameters (#60)
Browse files Browse the repository at this point in the history
* Copy full key pair address to support additional parameters.
* Remove undefined behavior when d equals 1.
  • Loading branch information
kste authored Apr 15, 2024
1 parent 035b394 commit 7ec789a
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
12 changes: 2 additions & 10 deletions ref/address.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ void copy_subtree_addr(uint32_t out[8], const uint32_t in[8])
*/
void set_keypair_addr(uint32_t addr[8], uint32_t keypair)
{
#if SPX_FULL_HEIGHT/SPX_D > 8
/* We have > 256 OTS at the bottom of the Merkle tree; to specify */
/* which one, we'd need to express it in two bytes */
((unsigned char *)addr)[SPX_OFFSET_KP_ADDR2] = (unsigned char)(keypair >> 8);
#endif
((unsigned char *)addr)[SPX_OFFSET_KP_ADDR1] = (unsigned char)keypair;
u32_to_bytes(&((unsigned char *)addr)[SPX_OFFSET_KP_ADDR], keypair);
}

/*
Expand All @@ -67,10 +62,7 @@ void set_keypair_addr(uint32_t addr[8], uint32_t keypair)
void copy_keypair_addr(uint32_t out[8], const uint32_t in[8])
{
memcpy( out, in, SPX_OFFSET_TREE+8 );
#if SPX_FULL_HEIGHT/SPX_D > 8
((unsigned char *)out)[SPX_OFFSET_KP_ADDR2] = ((unsigned char *)in)[SPX_OFFSET_KP_ADDR2];
#endif
((unsigned char *)out)[SPX_OFFSET_KP_ADDR1] = ((unsigned char *)in)[SPX_OFFSET_KP_ADDR1];
memcpy( (unsigned char *)out + SPX_OFFSET_KP_ADDR, (unsigned char *)in + SPX_OFFSET_KP_ADDR, 4);
}

/*
Expand Down
3 changes: 1 addition & 2 deletions ref/haraka_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#define SPX_OFFSET_LAYER 3 /* The byte used to specify the Merkle tree layer */
#define SPX_OFFSET_TREE 8 /* The start of the 8 byte field used to specify the tree */
#define SPX_OFFSET_TYPE 19 /* The byte used to specify the hash type (reason) */
#define SPX_OFFSET_KP_ADDR2 22 /* The high byte used to specify the key pair (which one-time signature) */
#define SPX_OFFSET_KP_ADDR1 23 /* The low byte used to specify the key pair */
#define SPX_OFFSET_KP_ADDR 20 /* The start of the 4 byte field used to specify the key pair address */
#define SPX_OFFSET_CHAIN_ADDR 27 /* The byte used to specify the chain address (which Winternitz chain) */
#define SPX_OFFSET_HASH_ADDR 31 /* The byte used to specify the hash address (where in the Winternitz chain) */
#define SPX_OFFSET_TREE_HGT 27 /* The byte used to specify the height of this node in the FORS or Merkle tree */
Expand Down
8 changes: 6 additions & 2 deletions ref/hash_haraka.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx,
#error For given height and depth, 64 bits cannot represent all subtrees
#endif

*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
if (SPX_D == 1) {
*tree = 0;
} else {
*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
}
bufp += SPX_TREE_BYTES;

*leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES);
Expand Down
8 changes: 6 additions & 2 deletions ref/hash_sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx,
#error For given height and depth, 64 bits cannot represent all subtrees
#endif

*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
if (SPX_D == 1) {
*tree = 0;
} else {
*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
}
bufp += SPX_TREE_BYTES;

*leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES);
Expand Down
8 changes: 6 additions & 2 deletions ref/hash_shake.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ void hash_message(unsigned char *digest, uint64_t *tree, uint32_t *leaf_idx,
#error For given height and depth, 64 bits cannot represent all subtrees
#endif

*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
if (SPX_D == 1) {
*tree = 0;
} else {
*tree = bytes_to_ull(bufp, SPX_TREE_BYTES);
*tree &= (~(uint64_t)0) >> (64 - SPX_TREE_BITS);
}
bufp += SPX_TREE_BYTES;

*leaf_idx = (uint32_t)bytes_to_ull(bufp, SPX_LEAF_BYTES);
Expand Down
3 changes: 1 addition & 2 deletions ref/sha2_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#define SPX_OFFSET_LAYER 0 /* The byte used to specify the Merkle tree layer */
#define SPX_OFFSET_TREE 1 /* The start of the 8 byte field used to specify the tree */
#define SPX_OFFSET_TYPE 9 /* The byte used to specify the hash type (reason) */
#define SPX_OFFSET_KP_ADDR2 12 /* The high byte used to specify the key pair (which one-time signature) */
#define SPX_OFFSET_KP_ADDR1 13 /* The low byte used to specify the key pair */
#define SPX_OFFSET_KP_ADDR 10 /* The start of the 4 byte field used to specify the key pair address */
#define SPX_OFFSET_CHAIN_ADDR 17 /* The byte used to specify the chain address (which Winternitz chain) */
#define SPX_OFFSET_HASH_ADDR 21 /* The byte used to specify the hash address (where in the Winternitz chain) */
#define SPX_OFFSET_TREE_HGT 17 /* The byte used to specify the height of this node in the FORS or Merkle tree */
Expand Down
3 changes: 1 addition & 2 deletions ref/shake_offsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#define SPX_OFFSET_LAYER 3 /* The byte used to specify the Merkle tree layer */
#define SPX_OFFSET_TREE 8 /* The start of the 8 byte field used to specify the tree */
#define SPX_OFFSET_TYPE 19 /* The byte used to specify the hash type (reason) */
#define SPX_OFFSET_KP_ADDR2 22 /* The high byte used to specify the key pair (which one-time signature) */
#define SPX_OFFSET_KP_ADDR1 23 /* The low byte used to specify the key pair */
#define SPX_OFFSET_KP_ADDR 20 /* The start of the 4 byte field used to specify the key pair address */
#define SPX_OFFSET_CHAIN_ADDR 27 /* The byte used to specify the chain address (which Winternitz chain) */
#define SPX_OFFSET_HASH_ADDR 31 /* The byte used to specify the hash address (where in the Winternitz chain) */
#define SPX_OFFSET_TREE_HGT 27 /* The byte used to specify the height of this node in the FORS or Merkle tree */
Expand Down

0 comments on commit 7ec789a

Please sign in to comment.