Project

General

Profile

Support #1463

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

Added by Stefan Petscharnig 18 days ago. Updated 18 days ago.

Status:
Assigned
Priority:
Low
Target version:
Start date:
31.07.2018
Due date:
% Done:

0%

Estimated time:
2.00 h

Description

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

History

#1 Updated by Oliver Maurhart 18 days 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 18 days ago

implemented:

@
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

Also available in: Atom PDF