Explanation of the proof of $\phi(n)\cdot \frac{n}{2}=\sum_{a\in R}a$ In my number theory textbook, there is a theorem about Euler's Phi Function it says:
A Theorem:
If $R$ is a reduced residue system modulo $n$: $R=\{a\in C \mid \gcd(a,n)=1\}$ Where $C$ is a complete residue system modulo $n$, so:
$$\sum_{a\in R}a=\phi(n)\cdot \frac{n}{2}$$ Where $\sum_{a\in R}a$ is the sum of all elements in $R$.
The Proof:
since R is a reduced residue system mod $n$ so $\mid R\mid = \phi(n)$ Hence we can suppose that $R=\{a_1,a_2,...,a_{\phi(n)}\}$ Thus:
$$S=\sum_{a\in R}a=\sum_{i=1}^{\phi(n)}a_i$$ But since $\gcd(a_i,n)=1 \iff \gcd(n-a_i,n)=1$ so :$$\sum_{i=1}^{\phi(n)}a_i=\sum_{i=1}^{\phi(n)}(n-a_i)$$
$$ \implies2S = \sum_{i=1}^{\phi(n)}a_i+\sum_{i=1}^{\phi(n)}(n-a_i)=\phi(n)\cdot n$$
$$\iff S= \phi(n)\cdot \frac{n}{2}$$
Ok Everything is clear but this transition: $$\gcd(a_i,n)=1 \iff \gcd(n-a_i,n)=1$$
is not clear for me, and even if it is how can this statement implies this ?$$\sum_{i=1}^{\phi(n)}a_i=\sum_{i=1}^{\phi(n)}(n-a_i)$$

*

*Assume $\gcd(a_i,n)=1$. We want to show that $\gcd(n-a_i,n)=1$. $$\gcd(n-a_i,n)\mid -(n-a_i)+n=a_i$$ Now we see that $\gcd(n-a_i,n)$ divides all three $n-a_i,n,a_i$ therefore $\gcd(n-a_i,n)\mid \gcd(a_i,n)=1$. This is only possible if $\gcd(n-a_i,a_i)=1$.


*Assume that $\gcd(n-a_i,n)=1$. We now want to show that $\gcd(a_i, n)=1$. $$\gcd(a_i,n)\mid n-a_i$$ Again, we see that $\gcd(a_i,n)$ divides all three $n-a_i, a_i, n$, in particular $\gcd(a_i,n)\mid\gcd(n-a_i, n)=1$. This is only possible if $\gcd(a_i, n)=1$.
