Support #1463

qkd::utility::memory - from_hex input VS as_hex output inconsistency

Added by Stefan Petscharnig 6 months ago. Updated 5 months ago.

Target version:
Start date:
Due date:
% Done:


Estimated time:
2.00 h


Between input of qkd::utility::memory::from_hex and output of qkd::utility::memory::as_hex there is an inconsistency.

  • From_hex creates a memory object by a given string with a hexadecimal memory representation.
    Internally, it is designed for allocation of memory objects with size increasing in steps of half a byte.
    Thus, we are for example able to allocate 44bit of data, which is 10 and a half byte, e.g. from the hex string "abcdef01234"-
  • If we now want a hex string representation from that memory object, we use as_hex. Internally, as_hex produces a even-length string.
    Memory with half bytes at end, are filled with a zero.
  • I see two problems in this:
    - first, if we interpret the data as a number, the behaviour of this is like a shift left by 4 bit
    - second, the hex string which has been input to from_hex differs to the output of from_hex. Z
    This may lead to a problem in understanding these functions.
  • Example:

    memory = qkd::utility::memory::from_hex("abcdef01234");
    ASSERT_EQ(memory.as_hex(), "abcdef012340");
  • My preferred solution is to specify that from_hex does not work for half-byte allocation: if the input is of odd length, it should throw an exception.
    • This would introduce an overhead of half a byte in some cases.
    • However, this would enforce input (from_hex) and output (as_hex) equality, which reduces at least the cognitive complexity of the test cases.
    • This would furthermore allow for using mpir functions in from_hex, as currently it is not used because
      "they yield erroneous results if the string has an odd number of chars and is meant to be read from left to right"
  • The second solution I see would be not to substitute the missing nibble with a zero within as_hex.
    • is easier to change
    • still allows half-byte data allocation


#1 Updated by Oliver Maurhart 6 months ago

  • Status changed from New to Assigned
  • Assignee set to Stefan Petscharnig
  • Target version set to v9.9999.9
  • Estimated time set to 2.00 h

Stefan Petscharnig wrote:

  • My preferred solution is to specify that from_hex does not work for half-byte allocation: if the input is of odd length, it should throw an exception.

Full ACK! I think this the way to go!

Is there any suitable class in the include/qkd/exception folder? I don's see any. What's the proper exception to chose?

#2 Updated by Stefan Petscharnig 6 months ago


qkd::utility::memory memory::from_hex(std::string const & hex) {

if( (hex.length() & 1) ) {
throw std::invalid_argument("input string to qkd::utility::memory::from_hex must have even length");

next action item here: run integration tests and check whether this has consequences

#3 Updated by Stefan Petscharnig 5 months ago

  • Status changed from Assigned to Closed

Commit 2fef0507:
drop support for odd inputs in qkd::utility::memory::from_hex

( )

Also available in: Atom PDF