\begin{figure}[tb]
\centering
\begin{tikzpicture}[
  scale=.5, 
  every node/.style={scale=.5},
  node distance = 8pt and 40pt,
  start chain   = going below,
  emp_b/.style = {
    draw = none,
    fill = none,
    inner sep = 4pt,
    minimum width = 120pt,
    minimum height = 50pt,
    rectangle,
    on chain
  },
  emp_s/.style    = {
    draw = none,
    fill = none,
    inner sep = 4pt,
    minimum width = 90pt,
    minimum height = 50pt,
    rectangle,
    on chain
  },
  prt/.style    = {
    draw=gray,
    fill = white,
    inner sep = 4pt,
    outer sep = 3pt,
    minimum width = 84.4pt,
    minimum height = 44.4pt,
    rectangle,
    font=\fontsize{22}{22}\selectfont,
    on chain
  },
  ins/.style    = {
    draw = none,
    fill = none,
    rectangle,
    minimum width=100pt,
    minimum height=20pt,
    text width=100pt,
    align=right,
    font=\fontsize{18}{18}\selectfont,
    on chain
  }
]
%
\pgfdeclarelayer{bg}
\pgfsetlayers{bg,main}

\node[emp_b] (e_11) {};


\node[prt, draw=gray, fill=gray!25, right = of e_11] (p_12) {$key\_gen$};
\node[prt, dashed, line width=1pt,] (p_22) {$ \mathcal{A}_1$};
\node[prt, draw=gray, fill=gray!25] (p_32) {$encrypt$};
\node[prt, dashed, line width=1pt,] (p_42) {$\mathcal{A}_2$};

\node[emp_s, right = of p_12] (e_13) {};
\node[emp_s] (e_23) {};
\node[emp_s] (e_33) {};
\node[emp_s] (e_43) {};

\draw[draw=gray, fill=gray!25] 
  ($(e_13.north west)+(3pt,-3pt)$) 
    rectangle  node[font=\fontsize{20}{20}\selectfont] (o_13) {ro.oracle} 
  ($(e_23.south east)+(-3pt,33pt)$);
\draw[draw=gray, fill=gray!25] 
  ($(e_33.north west)+(3pt,-33pt)$)
    rectangle  node[font=\fontsize{16}{16}\selectfont] (o_33) {interceptor}
  ($(e_43.south east)+(-3pt,3pt)$);

\node[ins, above = -10pt of e_11] (ins_1) {$\mathit{sk}, \mathit{pk}$};
\node[ins, below = 4pt of ins_1] (ins_2) {$b \leftarrow \{0, 1\};$};
\node[ins, below = 4pt of ins_2] (ins_3) {$pk$};
\node[ins, below = 4pt of ins_3] (ins_4) {$m_0, m_1, \sigma$};
\node[ins, below = 4pt of ins_4] (ins_5) {$m_b, pk$};
\node[ins, below = 4pt of ins_5] (ins_6) {$c$};
\node[ins, below = 4pt of ins_6] (ins_7) {$c, \sigma$};
\node[ins, below = 4pt of ins_7] (ins_8) {$b', \sigma'$};
\node[ins, below = 4pt of ins_8] (ins_9) {$b'=b$};


\begin{pgfonlayer}{bg}
  \draw[draw=none, fill=gray!15, rounded corners=10pt] 
    ($(e_11.north west)+(-36pt,18pt)$) 
      rectangle 
    ($(p_42.south east)+(18pt,-18pt)$);
  
\draw[draw=none, fill=gray, rounded corners=3pt]
    ($(p_12.north west)+(-3pt,3pt)$)
     rectangle
    ($(p_12.south east)+(3pt,-3pt)$);
 
\path[fill=red!40, draw=red, rounded corners=5pt, opacity=.5]
    ([xshift=6pt]e_43.east) |- 
    ([xshift=-17pt, yshift=-17pt]e_43.south west)  |-
    (p_22.south east)  -|
    (p_22.north west) -|
    ([yshift=15pt]p_22.south east) -|
    ([yshift=-10pt]e_33.north west) -|
    ([xshift=6pt]e_43.east);

\path[fill=red!40, draw=red, rounded corners=5pt, opacity=.5]
    ([xshift=17pt]e_43.east) |- 
    ([xshift=-7pt, yshift=-8pt]e_43.south west) |-
    ([yshift=10pt]p_42.east) |-
    (p_42.south)  -|
    (p_42.north west)  -|
    ([xshift=-7pt, yshift=-21pt]e_33.north west) -|
    ([xshift= 17pt]e_43.east);

\end{pgfonlayer}

\draw[latex-latex] (p_32.east) -| ++(38pt,0) |- ($(o_13.west)+(-3pt,-7pt)$);
\draw[latex-latex] ($(o_33.north)+(0pt,29pt)$) to ($(o_13.south)+(0pt,-29pt)$);

\draw[latex-] (ins_1.east) -| ++(35pt, 0) |- ($(p_12.west)+(0pt,0pt)$);
\draw[-latex] (ins_3.east) -| ++(35pt, 0) |- ($(p_22.west)+(0pt,7pt)$);
\draw[latex-, line width=1.5pt, name path=line1] (ins_4.east) -| ++(25pt, 0) |- ($(p_22.west)+(0pt,-8pt)$);
\draw[-latex, name path=line2] (ins_5.east) -| ++(35pt, 0) |- ($(p_32.west)+(0pt,7pt)$);
\draw[latex-, name path=line3] (ins_6.east) -| ++(25pt, 0) |- ($(p_32.west)+(0pt,-8pt)$);
\draw[-latex, line width=1.5pt] (ins_7.east) -| ++(35pt, 0) |- ($(p_42.west)+(0pt,7pt)$);
\draw[latex-, line width=1.5pt] (ins_8.east) -| ++(25pt, 0) |- ($(p_42.west)+(0pt,-8pt)$);
\draw[-implies, double, line width=1.5pt] ($(ins_9.west)+(0pt,0pt)$) - ++(-80pt, 0) ;

\end{tikzpicture}
\caption{The IND-CPA game after expanding the key generation algorithm's definition and inlining the query-recording hash oracle. The red boxes
represent the inline operator. }
\label{fig:4}
\end{figure}
