Giter Club home page Giter Club logo

Comments (1)

PJK avatar PJK commented on May 30, 2024

Hi @al3xkr,

This is an expected (although somewhat tricky) behavior that follows from how integers are defined in the standard and stored in libcbor. Because CBOR allows 1, 2, 4, and 8 byte integers that are either positive or negative (not unsigned and signed!), there is no way of mapping the full range of negative 8B integers to any standard signed C99 type. The only way to map these values correctly to standard C99 integer types is to use uint8_t through uint64_t.

Specifically, CBOR negative 1B integer can represent values between -1 and -256 inclusive (see http://libcbor.readthedocs.io/en/v0.5.0/api/type_0_1.html#type-0-1), which would underflow an int8_t (representable range -128 through 127).

Therefore cbor_get_uint8 returns uint8_t (http://libcbor.readthedocs.io/en/v0.5.0/api/type_0_1.html#_CPPv214cbor_get_uint8PK11cbor_item_t), as opposed to int8_t. In order to get the 'logical' value represented by the item, you can do e.g.

        cbor_item_t *hkdf_alg = cbor_new_int8();
        cbor_mark_negint(hkdf_alg);
        int abs_i = abs(ECDH_SS_HKDF_256) - 1;
        cbor_set_uint8(hkdf_alg, abs_i);
        int16_t item_value = -1 *  (int16_t) cbor_get_uint8(hkdf_alg) - 1;

Note that int16_t is necessary to avoid the aforementioned underflow.

cbor.me automatically converts the displayed items to the corresponding logical values, so you see -27, but this unfortunately cannot be supported in C until we get a signed integer type that is wider than 64 bits.

Hope this helps,
Pavel

from libcbor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.