## 1 Introduction
### MAM in IOTA  
IOTA serves as the transport layer of the MAM data layer protocol; thus, MAM messages are transported using the Tangle. MAM messages are split into fragments and each fragment is encapsulated into an IOTA transaction. Transactions are divided into 2 categories: Header transactions that encapsulate the Channel, the Endpoint and the Header data structures, and the Packet transactions that encapsulate data packets. Transaction objects have numerous metadata but two of them are relevant to the MAM protocol: (i) the tryte address field (81 trytes) = _chid_ and (ii) the tryte metadata field (27 trytes) = _msgid_ + (0 OR packet order). Both address field and metadata field are strings made of [X] trytes. The metadata_field is a contention of the message identifier and a \"0\" if it is a header message or the packet's number if it is a packet transaction. The latter is very important, because using the IRI's API, an agent can easily find all the header transactions registered to a specific address, read the session key and then again using the metadata, find and decrypt all the transactions with the specific _msgid_. The aforementioned data structures are presented below, in pseudocode. An IOTA transaction will either have the mam_header structure or the mam_packet structure encapsulated in the data field.  
{ struct mam_endpoint { trits name; mam_msgs s; }; struct mam_channel { trits name; trits msg_ord; mam_endpoint_set endpoints; trint endpoint_ord; } where **mam_msgs** is a data structure that refers to a Merklee Tree which holds the signatures as described, msg_ord a trit that is incremented each time a message is added in the channel, **mam_endpoint_set** is a set of all the active endpoints in the channel and **endpoint_ord** is a trint that is incremented with every endpoint added to the channel  
struct mam_header { mam_channel channel; mam_endpoint endpoint; mam_channel new_channel: mam_endpoint: new_endpoint; mam_psk psk_keys; mam_ntru ntru_keys; mam_msg_id msg_id; mam_msg_id_type type; } where **mam_channel** is a reference to the mam_channel data structure (it either references the current channel or a new one (fork), **mam_endpoint** a reference to the mam_endpoint data structure (it either references the current endpoint or a new one), **mam_psk** a set of the relevant Pre-Shared Keys (PSKs) that are required to unlock the session_key, **mam_ntru** a set of the relevant NTRU public keys which belong to a private key that unlock the session_key, **mam_msg_id**: the id of the message and **mam_msg_id_type type**: a message that can either be a MAM message or a Public Key Certificate.  
struct mam_packet { context ctx;  mam_checksum checksum;  mam_payload payload; } where context references the current state of the MAM library (and its various layers), mam_checksum the checksum of the packet so as the reader can verify it's integrity and mam_payload the payload that encapsulates a segment of the streamed data.  
The above data structures are used in the MAM2 reference implementation in C and are based on the MAM specification. The structures are different from those described in the specification as these are the actual data structures that are encapsulated into the IOTA transactions while the others are Protobuf3 models.