From 7441995797ce4a348c413481dd495b41133ba009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joosep=20J=C3=A4=C3=A4ger?= Date: Thu, 29 Oct 2026 15:29:32 +0200 Subject: [PATCH 1/2] Fix CanonicalTerm map equality --- CHANGELOG.md | 1 + src/Codec/CBOR/Cuddle/CBOR/Canonical.hs | 19 ++++++------------- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7de25acb..195fb0a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Add `twiddleUniform`, `twiddleAll`, `twiddleNothing` and `withTwiddle` * Add `isDefiniteAllowed` and `isIndefiniteAllowed` * Remove `enableTwiddle` +* Change `CTMap` to use a `Set` of pairs instead of `Map` ## 1.8.1.0 diff --git a/src/Codec/CBOR/Cuddle/CBOR/Canonical.hs b/src/Codec/CBOR/Cuddle/CBOR/Canonical.hs index 86a50434..89593bd2 100644 --- a/src/Codec/CBOR/Cuddle/CBOR/Canonical.hs +++ b/src/Codec/CBOR/Cuddle/CBOR/Canonical.hs @@ -15,8 +15,8 @@ import Data.Bifunctor (bimap) import Data.ByteString (ByteString) import Data.ByteString qualified as BS import Data.ByteString.Lazy qualified as BSL -import Data.Map.Strict (Map) -import Data.Map.Strict qualified as Map +import Data.Set (Set) +import Data.Set qualified as Set import Data.Text (Text) import Data.Text.Lazy qualified as TL import Data.Word (Word64, Word8) @@ -78,16 +78,7 @@ toCanonical = \case TFloat f -> CTFloat f TDouble d -> CTDouble d where - mkMap kvs = - let pairs = bimap toCanonical toCanonical <$> kvs - m = Map.fromList pairs - in if Map.size m == length pairs - then CTMap m - else - error - "toCanonical: encountered a map with duplicate keys, which is \ - \not currently supported. Please open an issue at \ - \https://github.com/input-output-hk/cuddle/issues" + mkMap kvs = CTMap . Set.fromList $ bimap toCanonical toCanonical <$> kvs mkList ts = CTList $ toCanonical <$> ts tagBytes (TBytes bs) = Just bs tagBytes (TBytesI bs) = Just $ BSL.toStrict bs @@ -138,7 +129,9 @@ data CanonicalTerm | CTBytes !ByteString | CTString !Text | CTList ![CanonicalTerm] - | CTMap !(Map CanonicalTerm CanonicalTerm) + | -- | RFC 8949 §5.6.1: "Two maps are equal if they have the same set of pairs + -- regardless of their order; pairs are equal if both the key and value are equal." + CTMap !(Set (CanonicalTerm, CanonicalTerm)) | CTTagged !Word64 !CanonicalTerm | CTSimple !Word8 | CTHalf !Half From b2bc5006565660b556f0537c5f54e9ce8528a035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joosep=20J=C3=A4=C3=A4ger?= Date: Tue, 3 Nov 2026 16:36:39 +0200 Subject: [PATCH 2/2] Add some tests --- test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs b/test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs index bf021172..46490427 100644 --- a/test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs +++ b/test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs @@ -13,6 +13,7 @@ import Codec.CBOR.Cuddle.CBOR.Canonical ( import Codec.CBOR.Term (Term (..)) import Data.ByteString qualified as BS import Data.ByteString.Lazy qualified as BSL +import Data.Set qualified as Set import Data.Text.Lazy qualified as TL import Data.Word (Word64) import Test.Hspec @@ -103,6 +104,38 @@ spec = do toCanonical (TMap [(TInt 1, TInt 10), (TInt 2, TInt 20)]) `shouldBe` toCanonical (TMap [(TInt 2, TInt 20), (TInt 1, TInt 10)]) + -- `TInt 1` and `TInteger 1` are distinct raw keys that canonicalize to + -- the same key, so these exercise duplicate-key handling after + -- canonicalization. + describe "pairs sharing a canonical key" $ do + let dupKeyMap = + TMap [(TInt 1, TInt 10), (TInteger 1, TInt 20)] + + it "retains pairs with different values without throwing" $ + case toCanonical dupKeyMap of + CTMap pairs -> + pairs + `shouldBe` Set.fromList + [ (CTInt 1, CTInt 10) + , (CTInt 1, CTInt 20) + ] + other -> expectationFailure $ "expected CTMap, got " <> show other + + it "compare equal after reordering" $ + toCanonical dupKeyMap + `shouldBe` toCanonical (TMap [(TInteger 1, TInt 20), (TInt 1, TInt 10)]) + + it "compare unequal when a distinct pair is removed" $ do + toCanonical dupKeyMap + `shouldNotBe` toCanonical (TMap [(TInt 1, TInt 10)]) + toCanonical dupKeyMap + `shouldNotBe` toCanonical (TMap [(TInteger 1, TInt 20)]) + + it "collapse identical canonical pairs" $ do + let collapsed = toCanonical (TMap [(TInt 1, TInt 10), (TInteger 1, TInt 10)]) + collapsed `shouldBe` CTMap (Set.singleton (CTInt 1, CTInt 10)) + collapsed `shouldBe` toCanonical (TMap [(TInt 1, TInt 10)]) + describe "floats stay distinct by width" $ do it "THalf 1.0 ≠ TFloat 1.0" $ toCanonical (THalf 1.0) `shouldNotBe` toCanonical (TFloat 1.0)