Skip to content

Commit bbd76e1

Browse files
committed
And more review, signed int only, test cases groups
1 parent 6fad345 commit bbd76e1

4 files changed

Lines changed: 286 additions & 531 deletions

File tree

src/crypto/cbor.cpp

Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ namespace
2929
}
3030
}
3131

32-
Value consume_unsigned(cbor_nondet_t cbor)
33-
{
34-
Unsigned value{0};
35-
if (!cbor_nondet_read_uint64(cbor, &value))
36-
{
37-
throw CBORDecodeError("Failed to consume unsigned value");
38-
}
39-
return std::make_unique<ValueImpl>(value);
40-
}
41-
4232
Value consume_signed(cbor_nondet_t cbor)
4333
{
4434
Signed value{0};
@@ -151,7 +141,6 @@ namespace
151141
switch (mt)
152142
{
153143
case CBOR_MAJOR_TYPE_UINT64:
154-
return consume_unsigned(cbor);
155144
case CBOR_MAJOR_TYPE_NEG_INT64:
156145
return consume_signed(cbor);
157146
case CBOR_MAJOR_TYPE_BYTE_STRING:
@@ -184,12 +173,7 @@ namespace
184173
std::visit(
185174
[&os, indent](const auto& v) {
186175
using T = std::decay_t<decltype(v)>;
187-
if constexpr (std::is_same_v<T, Unsigned>)
188-
{
189-
print_indent(os, indent);
190-
os << "Unsigned: " << v << std::endl;
191-
}
192-
else if constexpr (std::is_same_v<T, Signed>)
176+
if constexpr (std::is_same_v<T, Signed>)
193177
{
194178
print_indent(os, indent);
195179
os << "Signed: " << v << std::endl;
@@ -268,24 +252,10 @@ namespace
268252
},
269253
value->value);
270254
}
271-
272-
bool signed_equals_unsigned(Signed a, Unsigned b)
273-
{
274-
if (a < 0 || b > std::numeric_limits<Signed>::max())
275-
{
276-
return false;
277-
}
278-
return a == b;
279-
}
280255
} // namespace
281256

282257
namespace ccf::cbor
283258
{
284-
Value make_unsigned(uint64_t value)
285-
{
286-
return std::make_unique<ValueImpl>(value);
287-
}
288-
289259
Value make_signed(int64_t value)
290260
{
291261
return std::make_unique<ValueImpl>(value);
@@ -382,21 +352,9 @@ namespace ccf::cbor
382352

383353
if constexpr (!std::is_same_v<TA, TB>)
384354
{
385-
// Handle unsigned/signed equally for key comparison.
386-
if constexpr (
387-
std::is_same_v<TA, Signed> && std::is_same_v<TB, Unsigned>)
388-
{
389-
return signed_equals_unsigned(a, b);
390-
}
391-
if constexpr (
392-
std::is_same_v<TA, Unsigned> && std::is_same_v<TB, Signed>)
393-
{
394-
return signed_equals_unsigned(b, a);
395-
}
396355
return false;
397356
}
398-
else if constexpr (
399-
std::is_same_v<TA, Unsigned> || std::is_same_v<TA, Signed>)
357+
else if constexpr (std::is_same_v<TA, Signed>)
400358
{
401359
return a == b;
402360
}
@@ -453,40 +411,15 @@ namespace ccf::cbor
453411
return tagged.item;
454412
}
455413

456-
Unsigned ValueImpl::as_unsigned() const
457-
{
458-
if (!std::holds_alternative<Unsigned>(value))
459-
{
460-
if (!std::holds_alternative<Signed>(value))
461-
{
462-
throw CBORDecodeError("Not an unsigned value");
463-
}
464-
const auto s_value = std::get<Signed>(value);
465-
if (s_value < 0)
466-
{
467-
throw CBORDecodeError("Casting signed to unsigned will overflow");
468-
}
469-
return static_cast<Unsigned>(s_value);
470-
}
471-
return std::get<Unsigned>(value);
472-
}
473414
Signed ValueImpl::as_signed() const
474415
{
475416
if (!std::holds_alternative<Signed>(value))
476417
{
477-
if (!std::holds_alternative<Unsigned>(value))
478-
{
479-
throw CBORDecodeError("Not a signed value");
480-
}
481-
const auto u_value = std::get<Unsigned>(value);
482-
if (u_value > std::numeric_limits<int64_t>::max())
483-
{
484-
throw CBORDecodeError("Casting unsigned to signed will overflow");
485-
}
486-
return static_cast<Signed>(u_value);
418+
throw CBORDecodeError("Not a signed value");
487419
}
488420
return std::get<Signed>(value);
489421
}
422+
490423
Bytes ValueImpl::as_bytes() const
491424
{
492425
if (!std::holds_alternative<Bytes>(value))
@@ -495,6 +428,7 @@ namespace ccf::cbor
495428
}
496429
return std::get<Bytes>(value);
497430
}
431+
498432
String ValueImpl::as_string() const
499433
{
500434
if (!std::holds_alternative<String>(value))
@@ -503,6 +437,7 @@ namespace ccf::cbor
503437
}
504438
return std::get<String>(value);
505439
}
440+
506441
Simple ValueImpl::as_simple() const
507442
{
508443
if (!std::holds_alternative<Simple>(value))

src/crypto/cbor.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace ccf::cbor
1919
struct ValueImpl;
2020
using Value = std::unique_ptr<ValueImpl>;
2121

22-
using Unsigned = uint64_t;
2322
using Signed = int64_t;
2423
using Bytes = std::span<const uint8_t>;
2524
using String = std::string_view;
@@ -52,8 +51,7 @@ namespace ccf::cbor
5251
Value item{nullptr};
5352
};
5453

55-
using Type =
56-
std::variant<Unsigned, Signed, Bytes, String, Array, Map, Tagged, Simple>;
54+
using Type = std::variant<Signed, Bytes, String, Array, Map, Tagged, Simple>;
5755

5856
using CBORDecodeError = std::runtime_error;
5957

@@ -65,15 +63,13 @@ namespace ccf::cbor
6563
[[nodiscard]] const Value& array_at(size_t index) const;
6664
[[nodiscard]] const Value& map_at(const Value& key) const;
6765
[[nodiscard]] const Value& tag_at(uint64_t tag) const;
68-
[[nodiscard]] Unsigned as_unsigned() const;
6966
[[nodiscard]] Signed as_signed() const;
7067
[[nodiscard]] Bytes as_bytes() const;
7168
[[nodiscard]] String as_string() const;
7269
[[nodiscard]] Simple as_simple() const;
7370
[[nodiscard]] size_t size() const;
7471
};
7572

76-
Value make_unsigned(uint64_t value);
7773
Value make_signed(int64_t value);
7874
Value make_string(std::string_view data);
7975
Value make_bytes(std::span<const uint8_t> data);

0 commit comments

Comments
 (0)