Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 6 additions & 13 deletions src/Codec/CBOR/Cuddle/CBOR/Canonical.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Comment thread
Soupstraw marked this conversation as resolved.
Resolved
mkList ts = CTList $ toCanonical <$> ts
tagBytes (TBytes bs) = Just bs
tagBytes (TBytesI bs) = Just $ BSL.toStrict bs
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/Test/Codec/CBOR/Cuddle/CBOR/Canonical.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading