[Tex/LaTex] Cases inside an algorithm environment

algorithm2ealgorithmsmath-mode

I need to write an algorithm where multiple cases are handled. I have succeeded but the output shows a lot of vertical space below and above the cases statement. Anyone help me to fix this. I am using algorithm2e package, a minimal working example with output is attached.

\documentclass[hidelinks,12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath}
\begin{document}

\begin{algorithm}
    \DontPrintSemicolon                 
    \caption{BE radix-8 IMML Modular Multiplication}          
    \label{alg1}   
    \KwIn{ $x=\sum_{i=o}^{n-1} x_i.2^i, y=\sum_{i=o}^{n-1} y_i.2^i, p=\sum_{i=o}^{n-1}p_i.2^i$ }
    \KwOut {$z = x \times y$ \text{mod} $p$}
    $z := 0$  $R_1 := 2x$ mod $p$ 
    $R_2 := 3x$ mod $p$, $R_3 := 4x$ mod $p$\; 
    \[
    N= 
    \begin{cases}
    n+3,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{\scriptsize append three 0 to the left of MSB of $y$}\\
    n+2,& \text{if } n \text{ mod } 3 = 1 \quad \tcc{ \scriptsize append two 0 to the left of MSB of $y$}\\
    n+1,& \text{if } n \text{ mod } 3 = 0 \quad \tcc{ \scriptsize append single 0 to the left of MSB of $y$}
    \end{cases}
    \] 
\For {($i=N+1; i\geq 3;  i=i-3 $) }{
    $z := 8z $\text{ mod } $p$\;
\Switch{$(y_{(i:i-3)})$}{
\textbf{when} $  0000 | 1111 \quad z:=z$ \;
\textbf{when}$\lbrace0001\rbrace|\lbrace0010\rbrace\lbrace1101\rbrace|\lbrace1110\rbrace \Longrightarrow z:=z \pm x$ mod $p$ \;
\textbf{when}$\lbrace0011\rbrace|\lbrace0100\rbrace\lbrace1011\rbrace|\lbrace1100\rbrace  \Longrightarrow z:= z \pm R_1$ mod $p$ \;
\textbf{when}$\lbrace0101\rbrace|\lbrace0110\rbrace\lbrace1001\rbrace|\lbrace1010\rbrace$ $\Longrightarrow z:= z \pm R_2$ mod $p$\;
\textbf{else} $\Longrightarrow z:= z \pm R_3$ mod $p$
        }
    }
    \Return{$z$}\;
\end{algorithm}
\end{document}

enter image description here

Best Answer

Is perhaps this that you want?

\documentclass[12pt]{report}
\usepackage[ruled,linesnumbered]{algorithm2e}
\usepackage{amsmath}
\begin{document}

\begin{algorithm}
\DontPrintSemicolon
\caption{BE radix-8 IMML Modular Multiplication}\label{alg1}   
\KwIn{
  $x=\sum_{i=0}^{n-1} x_i\cdot2^i$,
  $y=\sum_{i=0}^{n-1} y_i\cdot2^i$,
  $p=\sum_{i=0}^{n-1}p_i\cdot2^i$
 }
\KwOut {$z = x \times y \bmod p$}
  $z := 0$,  $R_1 := 2x \bmod p$,
  $R_2 := 3x \bmod p$, $R_3 := 4x \bmod p$,
  $N= 
  \begin{cases}
  n+3,& \text{\small if $n \bmod 3 = 0$ append three $0$ to the left of MSB of $y$} \\
  n+2,& \text{\small if $n \bmod 3 = 1$ append two $0$ to the left of MSB of $y$}\\
  n+1,& \text{\small if $n \bmod 3 = 0$ append single $0$ to the left of MSB of $y$}
  \end{cases}
  $\;
\For{$(i=N+1$; $i\geq 3$;  $i=i-3)$}{
  $z := 8z \bmod p$\;
  \Switch{$(y_{(i:i-3)})$}{
    \textbf{when} $0000 \mid 1111$ $z:=z$\;
    \textbf{when} $\{0001\}\mid\{0010\}\{1101\}\mid\{1110\}\Longrightarrow z:=z \pm x \bmod p$\;
    \textbf{when} $\{0011\}\mid\{0100\}\{1011\}\mid\{1100\}\Longrightarrow z:=z \pm R_1 \bmod p$\;
    \textbf{when} $\{0101\}\mid\{0110\}\{1001\}\mid\{1010\}\Longrightarrow z:=z \pm R_2 \bmod p$\;
    \textbf{else} $\Longrightarrow z:= z \pm R_3 \bmod p$
        }
    }
    \Return{$z$}
\end{algorithm}
\end{document}

enter image description here