Crypto Protocol Diagram
Produces a Mermaid sequenceDiagram (written to file) and an ASCII sequence
diagram (printed inline) from either:
- Source code implementing a cryptographic protocol, or
- A specification — RFC, academic paper, pseudocode, informal prose,
ProVerif (
.pv), or Tamarin (.spthy) model.
Tools used: Read, Write, Grep, Glob, Bash, WebFetch (for URL specs).
Unlike the diagramming-code skill (which visualizes code structure), this skill
extracts protocol semantics: who sends what to whom, what cryptographic
transformations occur at each step, and what protocol phases exist.
For call graphs, class hierarchies, or module dependency maps, use the
diagramming-code skill instead.
When to Use
- User asks to diagram, visualize, or extract a cryptographic protocol
- Input is source code implementing a handshake, key exchange, or multi-party protocol
- Input is an RFC, academic paper, pseudocode, or formal model (ProVerif/Tamarin)
- User names a specific protocol (TLS, Noise, Signal, X3DH, FROST)
When NOT to Use
- User wants a call graph, class hierarchy, or module dependency map — use
diagramming-code - User wants to formally verify a protocol — use
mermaid-to-proverif(after generating the diagram) - Input has no cryptographic protocol semantics (no parties, no message exchange)
Rationalizations to Reject
Workflow
Step 0: Determine Input Type
Before doing anything else, classify the input:
- Code only → skip to Step 1 below
- Spec only → skip to Spec Workflow (S1–S5) below
- Both → run Spec Workflow first, then use the code-reading steps to verify
the implementation against the spec diagram and annotate any divergences with
⚠️ - Ambiguous → ask the user: "Is this a source code file, a specification document, or both?"
Step 1: Locate Protocol Entry Points
Grep for function names, type names, and comments that reveal the protocol:
Start reading from the highest-level orchestration function — the one that calls into handshake phases or the main protocol loop.
Step 2: Identify Parties and Roles
Extract participant names from:
- Struct/class names:
Client,Server,Initiator,Responder,Prover,Verifier,Dealer,Party,Coordinator - Function parameter names that carry state for a role
- Comments declaring the protocol role
- Test fixtures that set up two-party or N-party scenarios
Map these to Mermaid participant declarations. Use short, readable aliases:
Step 3: Trace Message Flow
Follow state transitions and network sends/receives. Look for patterns like:
For in-process protocol implementations (where both parties run in the same process), treat function call boundaries as logical message sends when they represent what would be a network boundary in deployment.
Step 4: Annotate Cryptographic Operations
At each protocol step, identify and label:
Keep annotations concise — use mathematical shorthand, not code.
Step 5: Identify Protocol Phases
Group message steps into named phases using rect or Note blocks:
Common phases to detect:
- Setup / Key Generation: party key creation, trusted setup, parameter gen
- Handshake / Init: ephemeral key exchange, nonce exchange, version negotiation
- Authentication: identity proof, certificate exchange, signature verification
- Key Derivation: session key derivation from shared secrets
- Data Transfer / Main Protocol: encrypted application data exchange
- Finalization / Teardown: session close, MAC verification, abort handling
Detect abort/error paths and show them with alt blocks.
Spec Workflow (S1–S5)
Use this path when the input is a specification document rather than source code. After completing S1–S5, continue with Step 6 (Generate sequenceDiagram) and Step 7 (Verify and deliver) from the code workflow above.
Step S1: Ingest the Spec
Obtain the full spec text:
- File path provided → read with the Read tool
- URL provided → fetch with WebFetch
- Pasted inline → work directly from conversation context
Then identify the spec format and read references/spec-parsing-patterns.md [blocked] for format-specific extraction guidance:
If the spec references a known named protocol (TLS, Noise, Signal, X3DH, Double Ratchet, FROST), also read references/protocol-patterns.md [blocked] to use its canonical flow as a skeleton and fill in spec-specific details.
Step S2: Extract Parties and Roles
Identify all protocol participants. Look for:
- Named roles in prose or pseudocode:
Alice,Bob,Client,Server,Initiator,Responder,Prover,Verifier,Dealer,Party_i,Coordinator,Signer - Section headers: "Parties", "Roles", "Participants", "Setup", "Notation"
- ProVerif: process names at top level (
let ClientProc(...),let ServerProc(...)) - Tamarin: rule names and fact arguments (e.g.
!Pk($A, pk)—$Ais a party)
Map each role to a Mermaid participant declaration. Use short IDs with
descriptive aliases (see naming conventions in
references/mermaid-sequence-syntax.md [blocked]).
Step S3: Extract Message Flow
Trace what each party sends to whom and in what order. Extraction patterns by format:
RFC / informal prose:
- Arrow notation:
A → B: msg,A -> B - Sentence patterns: "A sends B ...", "B responds with ...", "A transmits ...", "upon receiving X, B sends Y"
- Numbered steps: extract in order, inferring sender/receiver from context
Pseudocode:
- Function signatures with explicit
sender/receiverparameters send(party, msg)/receive(party)calls- Return values passed as inputs to the other party's function in the next step
ProVerif (.pv):
out(ch, msg)— send on channelchin(ch, x)— receive on channelch, bind tox- Match
out/inpairs on the same channel to identify message flows !(replication) signals a role that handles multiple sessions
Tamarin (.spthy):
In(m)premise — receive messagemOut(m)conclusion — send messagem- Rule name and ordering of rules reveal protocol rounds
Fr(~x)— fresh random value generated by a party--[ Label ]->facts — security annotations, not messages
Preserve the ordering and round structure. Group concurrent sends (broadcast)
using par blocks in the final diagram.
Step S4: Extract Cryptographic Operations
For each protocol step, identify the cryptographic operations performed and which party performs them:
Identify security conditions and abort paths:
- Prose: "if verification fails, abort", "only if ...", "reject if ..."
- Pseudocode:
assert,require,if ... abort - ProVerif:
if m = expected then ... else 0 - Tamarin: contradicting facts or restriction lemmas
These become alt blocks in the final diagram.
Step S5: Flag Spec Ambiguities
Before moving to Step 6, check for gaps:
- Unclear message ordering: infer from round structure or section order;
annotate with
⚠️ ordering inferred from spec structure - Implied parties: if a party's role is implied but unnamed, give it a descriptive name and note the inference
- Missing steps: if the spec omits a step that the canonical pattern for
this protocol requires, annotate:
⚠️ spec omits [step] — canonical protocol requires it - Underspecified crypto: if the spec says "encrypt" without specifying
the scheme, annotate:
⚠️ encryption scheme not specified - ProVerif/Tamarin: private channels (
cdeclared withnew cor as a private free name) represent out-of-band channels — note them
<!-- Both code path (Steps 1–5) and spec path (Steps S1–S5) continue here -->
Step 6: Generate sequenceDiagram
Produce Mermaid syntax following the rules in references/mermaid-sequence-syntax.md [blocked].
Completeness over brevity. Show every distinct message type. Omit repeated
loop iterations (use loop blocks instead), but never omit a distinct protocol
step.
Correctness over aesthetics. The diagram must match what the code actually does. If the code diverges from a known spec, annotate the divergence:
Step 7: Verify and Deliver
Before delivering:
- Every participant declared actually sends or receives at least one message
- Arrows point in the correct direction (sender → receiver)
- Cryptographic operations are on the correct party (the one computing them)
- If protocol phases are used, no arrows appear outside a phase block
-
altblocks cover known abort/error paths - Diagram renders without syntax errors (check references/mermaid-sequence-syntax.md [blocked] for common pitfalls)
- If spec divergence found, annotated with
⚠️
Write the diagram to a file. Choose a filename derived from the protocol
name, e.g. noise-xx-handshake.md or x3dh-key-agreement.md. Write a
Markdown file with this structure:
After writing the file, print an ASCII sequence diagram inline in the response, followed by the Protocol Summary. State the output filename so the user knows where to find the Mermaid source.
Follow all drawing conventions in references/ascii-sequence-diagram.md [blocked], including the inline output format.
Decision Tree
Examples
Code path — examples/simple-handshake/:
protocol.py— two-party authenticated key exchange (X25519 DH + Ed25519 signing + HKDF + ChaCha20-Poly1305)expected-output.md— exact ASCII diagram and Mermaid file the skill should produce for that protocol
Spec path (ProVerif) — examples/simple-proverif/:
model.pv— HMAC challenge-response authentication modeled in ProVerifexpected-output.md— step-by-step extraction walkthrough (parties, message flow, crypto ops) and the exact ASCII diagram and Mermaid file the skill should produce
Study the relevant example before working on an unfamiliar input.
Supporting Documentation
- references/spec-parsing-patterns.md [blocked] — Extraction rules for RFC, academic paper/pseudocode, informal prose, ProVerif, and Tamarin input formats; read during Step S1
- references/mermaid-sequence-syntax.md [blocked] — Participant syntax, arrow types, activations, grouping blocks, escaping rules, and common rendering pitfalls
- references/protocol-patterns.md [blocked] — Canonical message flows for TLS 1.3, Noise, X3DH, Double Ratchet, Shamir secret sharing, commit-reveal, and generic MPC rounds; use as a reference when comparing implementation against spec
- references/ascii-sequence-diagram.md [blocked] — Column layout, arrow conventions, self-loops, phase labels, and inline output format for the ASCII diagram

