# AN codes

AN codes[1] are error-correcting code that are used in arithmetic applications. Arithmetic codes were commonly used in computer processors to ensure the accuracy of its arithmetic operations when electronics were more unreliable. Arithmetic codes help the processor to detect when an error is made and correct it. Without these codes, processors would be unreliable since any errors would go undetected. AN codes are arithmetic codes that are named for the integers ${\displaystyle A}$ and ${\displaystyle N}$ that are used to encode and decode the codewords.

These codes differ from most other codes in that they use arithmetic weight to maximize the arithmetic distance between codewords as opposed to the hamming weight and hamming distance. The arithmetic distance between two words is a measure of the number of errors made while computing an arithmetic operation. Using the arithmetic distance is necessary since one error in an arithmetic operation can cause a large hamming distance between the received answer and the correct answer.

## Arithmetic Weight and Distance

The arithmetic weight of an integer ${\displaystyle x}$ in base ${\displaystyle r}$ is defined by

${\displaystyle w(x)=\min\{t|x=\sum _{i=1}^{t}a_{i}r^{n(i)}\}}$[citation needed]

where ${\displaystyle |{a_{i}}|}$< ${\displaystyle r}$, ${\displaystyle n(i)\geq 0}$, and ${\displaystyle r,n(i)\in \mathbb {Z} }$. The arithmetic distance of a word is upper bounded by its hamming weight since any integer can be represented by its standard polynomial form of ${\displaystyle x=\sum _{i=1}^{n}b_{i}r^{i}}$ where the ${\displaystyle b_{i}}$ are the digits in the integer. Removing all the terms where ${\displaystyle b_{i}=0}$ will simulate a ${\displaystyle t}$ equal to its hamming weight. The arithmetic weight will usually be less than the hamming weight since the ${\displaystyle a_{i}}$ are allowed to be negative. For example, the integer ${\displaystyle x=29}$ which is ${\displaystyle 11101}$ in binary has a hamming weight of ${\displaystyle 4}$. This is a quick upper bound on the arithmetic weight since ${\displaystyle x=2^{0}+2^{2}+2^{3}+2^{4}}$. However, since the ${\displaystyle a_{i}}$ can be negative, we can write ${\displaystyle x=2^{5}-2^{1}-2^{0}}$ which makes the arithmetic weight equal to ${\displaystyle 3}$.

The arithmetic distance between two integers is defined by

${\displaystyle d(x,y)=w(x-y)}$[citation needed]

This is one of the primary metrics used when analyzing arithmetic codes.[citation needed]

## AN Codes

AN codes are defined by integers ${\displaystyle A}$ and ${\displaystyle B}$ and are used to encode integers from ${\displaystyle 0}$ to ${\displaystyle B-1}$ such that

${\displaystyle C=\{AN|N\in \mathbb {Z} ,0\leq N}$<${\displaystyle B\}}$

Each choice of ${\displaystyle A}$ will result in a different code, while ${\displaystyle B}$ serves as a limiting factor to ensure useful properties in the distance of the code. If ${\displaystyle B}$ is too large, it could let a codeword with a very small arithmetic weight into the code which will degrade the distance of the entire code. To utilize these codes, before an arithmetic operation is performed on two integers, each integer is multiplied by ${\displaystyle A}$. Let the result of the operation on the codewords be ${\displaystyle R}$. Note that ${\displaystyle R}$ must also be between ${\displaystyle 0}$ to ${\displaystyle B-1}$ for proper decoding. To decode, simply divide ${\displaystyle R/A}$. If ${\displaystyle A}$ is not a factor of ${\displaystyle R}$, then at least one error has occurred and the most likely solution will be the codeword with the least arithmetic distance from ${\displaystyle R}$. As with codes using hamming distance, AN codes can correct up to ${\displaystyle \lfloor {\frac {d-1}{2}}\rfloor }$ errors where ${\displaystyle d}$ is the distance of the code.

For example, an AN code with ${\displaystyle A=3}$, the operation of adding ${\displaystyle 15}$ and ${\displaystyle 16}$ will start by encoding both operands. This results in the operation ${\displaystyle R=45+48=93}$. Then, to find the solution we divide ${\displaystyle 93/3=31}$. As long as ${\displaystyle B}$>${\displaystyle 31}$, this will be a possible operation under the code. Suppose an error occurs in each of the binary representation of the operands such that ${\displaystyle 45=101101\rightarrow 101111}$ and ${\displaystyle 48=110000\rightarrow 110001}$, then ${\displaystyle R=101111+110001=1100000}$. Notice that since ${\displaystyle 93=1011101}$, the hamming weight between the received word and the correct solution is ${\displaystyle 5}$ after just ${\displaystyle 2}$ errors. To compute the arithmetic weight, we take ${\displaystyle 1100000-1011101=11}$ which can be represented as ${\displaystyle 11=2^{0}+2^{1}}$ or ${\displaystyle 11=2^{2}-2^{0}}$. In either case, the arithmetic distance is ${\displaystyle 2}$ as expected since this is the number of errors that were made. To correct this error, an algorithm would be used to compute the nearest codeword to the received word in terms of arithmetic distance. We will not describe the algorithms in detail.

To ensure that the distance of the code will not be too small, we will define modular AN codes. A modular AN code ${\displaystyle C}$ is a subgroup of ${\displaystyle \mathbb {Z} /m\mathbb {Z} }$, where ${\displaystyle m=AB}$. The codes are measured in terms of modular distance which is defined in terms of a graph with vertices being the elements of ${\displaystyle \mathbb {Z} /m\mathbb {Z} }$. Two vertices ${\displaystyle x{\pmod {m}}}$ and ${\displaystyle x'{\pmod {m}}}$ are connected iff

${\displaystyle x-x'\equiv \pm c\cdot r^{j}{\pmod {m}}}$

where ${\displaystyle c,j\in \mathbb {Z} }$ and ${\displaystyle 0}$<${\displaystyle c}$<${\displaystyle r}$, ${\displaystyle j\geq 0}$. Then the modular distance between two words is the length of the shortest path between their nodes in the graph. The modular weight of a word is its distance from ${\displaystyle 0}$ which is equal to

${\displaystyle w_{m}(x)=min\{w(y)|y\in \mathbb {Z} ,y\equiv x{\pmod {m}}\}}$

In practice, the value of ${\displaystyle m}$ is typically chosen such that ${\displaystyle m=r^{n}-1}$ since most computer arithmetic is computed ${\displaystyle \mod 2^{n}-1}$ so there is no additional loss of data due to the code going out of bounds since the computer will also be out of bounds. Choosing ${\displaystyle m=r^{n}-1}$ also tends to result in codes with larger distances than other codes.

By using modular weight with ${\displaystyle m=r^{n}-1}$, the AN codes will be cyclic code.

definition: A cyclic AN code is a code ${\displaystyle C}$ that is a subgroup of ${\displaystyle [r^{n}-1]}$, where ${\displaystyle [r^{n}-1]=\{0,1,2,\dots ,r^{n}-1\}}$.

A cyclic AN code is a principal ideal of the ring ${\displaystyle [r^{n}-1]}$. There are integers ${\displaystyle A}$ and ${\displaystyle B}$ where ${\displaystyle AB=r^{n}-1}$ and ${\displaystyle A,B}$ satisfy the definition of an AN code. Cyclic AN codes are a subset of cyclic codes and have the same properties.

## Mandelbaum-Barrows Codes

The Mandelbaum-Barrows Codes are a type of cyclic AN codes introduced by D. Mandelbaum and J. T. Barrows.[2][3] These codes are created by choosing ${\displaystyle B}$ to be a prime number that does not divide ${\displaystyle r}$ such that ${\displaystyle \mathbb {Z} /B\mathbb {Z} }$ is generated by ${\displaystyle r}$ and ${\displaystyle -1}$, and ${\displaystyle m=r^{n}-1}$. Let ${\displaystyle n}$ be a positive integer where ${\displaystyle r^{n}\equiv 1{\pmod {B}}}$ and ${\displaystyle A=(r^{n}-1)/B}$. For example, choosing ${\displaystyle r=2,B=5,n=4}$, and ${\displaystyle A=(r^{n}-1)/B=3}$ the result will be a Mandelbaum-Barrows Code such that ${\displaystyle C=\{3N|N\in \mathbb {Z} ,0\leq N}$<${\displaystyle 5\}}$ in base ${\displaystyle 2}$.

To analyze the distance of the Mandelbaum-Barrows Codes, we will need the following theorem.

theorem: Let ${\displaystyle C\subset [r^{n}-1]}$ be a cyclic AN code with generator ${\displaystyle A}$, and

${\displaystyle B=|C|=(r^{n}-1)/A}$

Then,

${\displaystyle \sum _{x\in C}w_{m}(x)=n(\lfloor {\frac {rB}{r+1}}\rfloor -\lfloor {\frac {B}{r+1}}\rfloor )}$

proof: Assume that each ${\displaystyle x\in C}$ has a unique cyclic NAF[4] representation which is

${\displaystyle x\equiv \sum _{i=0}^{n-1}c_{i,x}r^{i}{\pmod {r^{n}-1}}}$

We define an ${\displaystyle n\times B}$ matrix with elements ${\displaystyle c_{i,x}}$ where ${\displaystyle 0\leq i\leq n-1}$ and ${\displaystyle x\in C}$. This matrix is essentially a list of all the codewords in ${\displaystyle C}$ where each column is a codeword. Since ${\displaystyle C}$ is cyclic, each column of the matrix has the same number of zeros. We must now calculate ${\displaystyle n|\{x\in C|c_{n-1,x}\neq 0\}|}$, which is ${\displaystyle n}$ times the number of codewords that don't end with a ${\displaystyle 0}$. As a property of being in cyclic NAF, ${\displaystyle c_{n-1,x}\neq 0}$ iff there is a ${\displaystyle y\in \mathbb {Z} }$ with ${\displaystyle y\equiv x{\pmod {r^{n}-1}},{\frac {m}{r+1}}}$<${\displaystyle y\leq {\frac {mr}{r+1}}}$. Since ${\displaystyle x=AN{\pmod {r^{n}-1}}}$ with ${\displaystyle 0\leq N}$<${\displaystyle B}$, then ${\displaystyle {\frac {B}{r+1}}}$<${\displaystyle N\leq {\frac {Br}{r+1}}}$. Then the number of integers that have a zero as their last bit are ${\displaystyle \lfloor {\frac {rB}{r+1}}\rfloor -\lfloor {\frac {B}{r+1}}\rfloor }$. Multiplying this by the ${\displaystyle n}$ characters in the codewords gives us a sum of the weights of the codewords of ${\displaystyle n(\lfloor {\frac {rB}{r+1}}\rfloor -\lfloor {\frac {B}{r+1}}\rfloor )}$ as desired.

We will now use the previous theorem to show that the Mandelbaum-Barrows Codes are equidistant(which means that every pair of codewords have the same distance), with a distance of

${\displaystyle {\frac {n}{B-1}}(\lfloor {\frac {rB}{r+1}}\rfloor -\lfloor {\frac {B}{r+1}}\rfloor )}$

proof: Let ${\displaystyle x\in C,x\neq 0}$, then ${\displaystyle x=AN{\pmod {r^{n}-1}}}$ and ${\displaystyle N}$ is not divisible by ${\displaystyle B}$. This implies there ${\displaystyle \exists j(N\equiv \pm r^{j}{\pmod {B}})}$. Then ${\displaystyle w_{m}(x)=w_{m}(\pm r^{j}A)=w_{m}(A)}$. This proves that ${\displaystyle C}$ is equidistant since all codewords have the same weight as ${\displaystyle A}$. Since all codewords have the same weight, and by the previous theorem we know the total weight of all codewords, the distance of the code is found by dividing the total weight by the number of codewords(excluding 0).