Skip to content

Commit

Permalink
WIP: Add instances for roundtrip tests of MLS types
Browse files Browse the repository at this point in the history
  • Loading branch information
pcapriotti committed Apr 5, 2023
1 parent 56bccaf commit f91cdc5
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 21 deletions.
8 changes: 8 additions & 0 deletions libs/wire-api/src/Wire/API/MLS/Capabilities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ instance ParseMLS Capabilities where
<*> parseMLSVector @VarInt parseMLS
<*> parseMLSVector @VarInt parseMLS
<*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS Capabilities where
serialiseMLS caps = do
serialiseMLSVector @VarInt serialiseMLS caps.versions
serialiseMLSVector @VarInt serialiseMLS caps.ciphersuites
serialiseMLSVector @VarInt serialiseMLS caps.extensions
serialiseMLSVector @VarInt serialiseMLS caps.proposals
serialiseMLSVector @VarInt serialiseMLS caps.credentials
28 changes: 23 additions & 5 deletions libs/wire-api/src/Wire/API/MLS/Commit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,50 @@ data Commit = Commit
{ cProposals :: [ProposalOrRef],
cPath :: Maybe UpdatePath
}
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform Commit)

instance ParseMLS Commit where
parseMLS =
Commit
<$> traceMLS "proposals" (parseMLSVector @VarInt parseMLS)
<*> traceMLS "update path" (parseMLSOptional parseMLS)
<$> parseMLSVector @VarInt parseMLS
<*> parseMLSOptional parseMLS

instance SerialiseMLS Commit where
serialiseMLS c = do
serialiseMLSVector @VarInt serialiseMLS c.cProposals
serialiseMLSOptional serialiseMLS c.cPath

data UpdatePath = UpdatePath
{ upLeaf :: RawMLS LeafNode,
upNodes :: [UpdatePathNode]
}
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform UpdatePath)

instance ParseMLS UpdatePath where
parseMLS = UpdatePath <$> parseMLS <*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS UpdatePath where
serialiseMLS up = do
serialiseMLS up.upLeaf
serialiseMLSVector @VarInt serialiseMLS up.upNodes

data UpdatePathNode = UpdatePathNode
{ upnPublicKey :: ByteString,
upnSecret :: [HPKECiphertext]
}
deriving (Eq, Show)
deriving (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform UpdatePathNode)

instance ParseMLS UpdatePathNode where
parseMLS = UpdatePathNode <$> parseMLSBytes @VarInt <*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS UpdatePathNode where
serialiseMLS upn = do
serialiseMLSBytes @VarInt upn.upnPublicKey
serialiseMLSVector @VarInt serialiseMLS upn.upnSecret

data HPKECiphertext = HPKECiphertext
{ hcOutput :: ByteString,
hcCiphertext :: ByteString
Expand Down
2 changes: 1 addition & 1 deletion libs/wire-api/src/Wire/API/MLS/CommitBundle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data CommitBundle = CommitBundle
cbWelcome :: Maybe (RawMLS Welcome),
cbGroupInfo :: RawMLS GroupInfo
}
deriving (Eq, Show)
deriving stock (Eq, Show, Generic)

data CommitBundleF f = CommitBundleF
{ cbCommitMsg :: f (RawMLS Message),
Expand Down
8 changes: 8 additions & 0 deletions libs/wire-api/src/Wire/API/MLS/Credential.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,21 @@ data CredentialTag where
instance ParseMLS CredentialTag where
parseMLS = parseMLSEnum @Word16 "credential type"

instance SerialiseMLS CredentialTag where
serialiseMLS = serialiseMLSEnum @Word16

instance ParseMLS Credential where
parseMLS =
parseMLS >>= \case
BasicCredentialTag ->
BasicCredential
<$> parseMLSBytes @VarInt

instance SerialiseMLS Credential where
serialiseMLS (BasicCredential i) = do
serialiseMLS BasicCredentialTag
serialiseMLSBytes @VarInt i

credentialTag :: Credential -> CredentialTag
credentialTag BasicCredential {} = BasicCredentialTag

Expand Down
22 changes: 22 additions & 0 deletions libs/wire-api/src/Wire/API/MLS/GroupInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ instance ParseMLS GroupContext where
<*> parseMLSBytes @VarInt
<*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS GroupContext where
serialiseMLS gc = do
serialiseMLS gc.protocolVersion
serialiseMLS gc.cipherSuite
serialiseMLS gc.groupId
serialiseMLS gc.epoch
serialiseMLSBytes @VarInt gc.treeHash
serialiseMLSBytes @VarInt gc.confirmedTranscriptHash
serialiseMLSVector @VarInt serialiseMLS gc.extensions

data GroupInfoTBS = GroupInfoTBS
{ groupContext :: GroupContext,
extensions :: [Extension],
Expand All @@ -75,6 +85,13 @@ instance ParseMLS GroupInfoTBS where
<*> parseMLSBytes @VarInt
<*> parseMLS

instance SerialiseMLS GroupInfoTBS where
serialiseMLS tbs = do
serialiseMLS tbs.groupContext
serialiseMLSVector @VarInt serialiseMLS tbs.extensions
serialiseMLSBytes @VarInt tbs.confirmationTag
serialiseMLS tbs.signer

data GroupInfo = GroupInfo
{ tbs :: GroupInfoTBS,
signature_ :: ByteString
Expand All @@ -88,6 +105,11 @@ instance ParseMLS GroupInfo where
<$> parseMLS
<*> parseMLSBytes @VarInt

instance SerialiseMLS GroupInfo where
serialiseMLS gi = do
serialiseMLS gi.tbs
serialiseMLSBytes @VarInt gi.signature_

instance HasField "groupContext" GroupInfo GroupContext where
getField = (.tbs.groupContext)

Expand Down
3 changes: 3 additions & 0 deletions libs/wire-api/src/Wire/API/MLS/HPKEPublicKey.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ newtype HPKEPublicKey = HPKEPublicKey {unHPKEPublicKey :: ByteString}

instance ParseMLS HPKEPublicKey where
parseMLS = HPKEPublicKey <$> parseMLSBytes @VarInt

instance SerialiseMLS HPKEPublicKey where
serialiseMLS = serialiseMLSBytes @VarInt . unHPKEPublicKey
16 changes: 15 additions & 1 deletion libs/wire-api/src/Wire/API/MLS/KeyPackage.hs
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,20 @@ instance ParseMLS KeyPackageTBS where
<*> parseMLS
<*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS KeyPackageTBS where
serialiseMLS tbs = do
serialiseMLS tbs.protocolVersion
serialiseMLS tbs.cipherSuite
serialiseMLS tbs.initKey
serialiseMLS tbs.leafNode
serialiseMLSVector @VarInt serialiseMLS tbs.extensions

data KeyPackage = KeyPackage
{ tbs :: RawMLS KeyPackageTBS,
signature_ :: ByteString
}
deriving stock (Eq, Show)
deriving stock (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform KeyPackage)

instance S.ToSchema KeyPackage where
declareNamedSchema _ = pure (mlsSwagger "KeyPackage")
Expand Down Expand Up @@ -221,6 +230,11 @@ instance ParseMLS KeyPackage where
<$> parseRawMLS parseMLS
<*> parseMLSBytes @VarInt

instance SerialiseMLS KeyPackage where
serialiseMLS kp = do
serialiseMLS kp.tbs
serialiseMLSBytes @VarInt kp.signature_

--------------------------------------------------------------------------------

data KeyPackageUpdate = KeyPackageUpdate
Expand Down
30 changes: 29 additions & 1 deletion libs/wire-api/src/Wire/API/MLS/LeafNode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
module Wire.API.MLS.LeafNode
( LeafIndex,
LeafNode (..),
LeafNodeTBS (..),
LeafNodeSource (..),
LeafNodeSourceTag (..),
leafNodeSourceTag,
Expand Down Expand Up @@ -59,6 +60,15 @@ instance ParseMLS LeafNodeTBS where
<*> parseMLS
<*> parseMLSVector @VarInt parseMLS

instance SerialiseMLS LeafNodeTBS where
serialiseMLS tbs = do
serialiseMLS tbs.encryptionKey
serialiseMLSBytes @VarInt tbs.signatureKey
serialiseMLS tbs.credential
serialiseMLS tbs.capabilities
serialiseMLS tbs.source
serialiseMLSVector @VarInt serialiseMLS tbs.extensions

-- | This type can only verify the signature when the LeafNodeSource is
-- LeafNodeSourceKeyPackage
data LeafNode = LeafNode
Expand All @@ -74,6 +84,11 @@ instance ParseMLS LeafNode where
<$> parseMLS
<*> parseMLSBytes @VarInt

instance SerialiseMLS LeafNode where
serialiseMLS ln = do
serialiseMLS ln.tbs
serialiseMLSBytes @VarInt ln.signature_

instance S.ToSchema LeafNode where
declareNamedSchema _ = pure (mlsSwagger "LeafNode")

Expand Down Expand Up @@ -109,15 +124,28 @@ instance ParseMLS LeafNodeSource where
LeafNodeSourceUpdateTag -> pure LeafNodeSourceUpdate
LeafNodeSourceCommitTag -> LeafNodeSourceCommit <$> parseMLSBytes @VarInt

instance SerialiseMLS LeafNodeSource where
serialiseMLS (LeafNodeSourceKeyPackage lt) = do
serialiseMLS LeafNodeSourceKeyPackageTag
serialiseMLS lt
serialiseMLS LeafNodeSourceUpdate =
serialiseMLS LeafNodeSourceUpdateTag
serialiseMLS (LeafNodeSourceCommit bs) = do
serialiseMLS LeafNodeSourceCommitTag
serialiseMLSBytes @VarInt bs

data LeafNodeSourceTag
= LeafNodeSourceKeyPackageTag
| LeafNodeSourceUpdateTag
| LeafNodeSourceCommitTag
deriving (Show, Eq, Ord, Enum, Bounded)

instance Bounded LeafNodeSourceTag => ParseMLS LeafNodeSourceTag where
instance ParseMLS LeafNodeSourceTag where
parseMLS = parseMLSEnum @Word8 "leaf node source"

instance SerialiseMLS LeafNodeSourceTag where
serialiseMLS = serialiseMLSEnum @Word8

instance HasField "name" LeafNodeSourceTag Text where
getField LeafNodeSourceKeyPackageTag = "key_package"
getField LeafNodeSourceUpdateTag = "update"
Expand Down
7 changes: 6 additions & 1 deletion libs/wire-api/src/Wire/API/MLS/Lifetime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Wire.Arbitrary

-- | Seconds since the UNIX epoch.
newtype Timestamp = Timestamp {timestampSeconds :: Word64}
deriving newtype (Eq, Show, Arbitrary, ParseMLS)
deriving newtype (Eq, Show, Arbitrary, ParseMLS, SerialiseMLS)

tsPOSIX :: Timestamp -> POSIXTime
tsPOSIX = fromIntegral . timestampSeconds
Expand All @@ -40,3 +40,8 @@ data Lifetime = Lifetime

instance ParseMLS Lifetime where
parseMLS = Lifetime <$> parseMLS <*> parseMLS

instance SerialiseMLS Lifetime where
serialiseMLS lt = do
serialiseMLS lt.ltNotBefore
serialiseMLS lt.ltNotAfter
2 changes: 2 additions & 0 deletions libs/wire-api/src/Wire/API/MLS/Message.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import Wire.API.MLS.Proposal
import Wire.API.MLS.ProtocolVersion
import Wire.API.MLS.Serialisation
import Wire.API.MLS.Welcome
import Wire.Arbitrary

data WireFormatTag
= WireFormatPublicTag
Expand Down Expand Up @@ -208,6 +209,7 @@ data Sender
| SenderNewMemberProposal
| SenderNewMemberCommit
deriving (Eq, Show, Generic)
deriving (Arbitrary) via (GenericUniform Sender)

instance ParseMLS Sender where
parseMLS =
Expand Down
Loading

0 comments on commit f91cdc5

Please sign in to comment.