Modular arithmetics I've been reading about some examples concerning DSS/DSA signature security and there is one part of an example that I do not understand the maths. Namely, how do you calculate this:
$w = (s^{-1}$ $mod$ $q)$ 
In this example let's say $q = 13$ and $s = 10$.
So we have
$w = (10^{-1}$ $mod$ $13) = 4$ 
How do we get 4 as a result? 
 A: In general, if $q$ and $s$ are relatively prime, using e.g. the Euclidean algorithm, you can find intergers $x,y$ such that 
$$qx+sy=1.$$
Therefore, if you look at this equality modulo $q$, you see that
$$sy\equiv 1\pmod q.$$
This integer $y$ is the "inverse" of $s$ modulo $q$.
A: As $\displaystyle 10\cdot4=40\equiv1\pmod{13}$
$\displaystyle 10^{-1}\equiv4\pmod{13}$ dividing either sides by $10$ as $(10,13)=1$ 
A: While with these small numbers it is possible to just guess-and-check an inverse, generally you use the Euclidean algorithm. It goes like this: since $10$ and $13$ are relatively prime, there is a way to find integers $a$ and $b$ so that $$10a+13b=1$$ If you do this, then reducing that equation modulo $13$, you have $$10a\equiv 1$$ and $a$ is the inverse of $10$ mod $13$. So your task is to find that $a$. The algorithm linked above lets you find an $a$ (and a $b$) after several iterations, essentially replacing the pair $(10,13)$ with smaller and smaller pairs until $a$ and $b$ are apparent.
A: Inverses modulo small integers are usually quickly computable by fraction fiddling. For example
$\rm\ mod\ 13\!:\, \ \begin{eqnarray}1\,\equiv&& -12\\ 10\,\equiv&& -3\end{eqnarray}\,\ \Rightarrow\,\ \dfrac{1}{10}\,\equiv\, \dfrac{-12}{-3}\,\equiv\, 4,\ $ indeed $\,\ 4\cdot 10 = 3\cdot 13 + 1\equiv 1\pmod{13}$
For larger moduli one may use the Extended Euclidean Algorithm. Given integers $\rm\,x,y\,$ it yields integers $\rm\,a,b\,$ such that $\rm\, ax+by = gcd(x,y)\ $ (Bezout's identity). Thus, when the gcd $=1,\,$ 
$$\ \rm ax+by = 1\ \Rightarrow\ ax\equiv 1\!\!\pmod y$$
yielding the inverse of $\rm\,x,\,$ modulo $\rm\,y,\,$ i.e. $\,
 \rm x^{-1} = 1/x\, \equiv\, a\pmod y$
Beware $\ $ The use of fractions in modular arithmetic is valid only when the denominator is invertible. Otherwise the quotient need not be unique, e.g. mod $\rm\:10,\:$ $\rm\:4\,x\equiv 2\:$ has solutions $\rm\:x\equiv 3,8,\:$ so the "fraction" $\rm\:x \equiv 2/4\pmod{10}\,$ cannot designate a unique solution of $\,4x\equiv 2.\,$ Indeed, the solution is $\rm\:x\equiv 1/2\equiv 3\pmod 5,\,$ which requires canceling $\,2\,$ from the modulus too, since $\rm\:10\:|\:4x-2\iff5\:|\:2x-1.\:$
Generally the grade-school rules of fraction arithmetic apply universally (i.e. in all rings) where the denominators are invertible. This fundamental property will be clarified conceptually when one learns in university algebra about the universal properties of fractions rings and localizations.
