# AN codes

**AN codes** 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 <math>A</math> and <math>N</math> 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.

## Contents

## Arithmetic Weight and Distance[edit]

The arithmetic weight of an integer <math>x</math> in base <math>r</math> is defined by

- <math>w(x) = \min \{t | x=\sum_{i=1}^t a_i r^{n(i)}\} </math>

where <math>|{a_i}|</math>< <math>r</math>, <math>n(i) \geq 0</math>, and <math>r, n(i) \in \mathbb{Z}</math>. 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 <math>x=\sum_{i=1}^n b_i r^i</math> where the <math>b_i</math> are the digits in the integer. Removing all the terms where <math>b_i = 0</math> will simulate a <math>t</math> equal to its hamming weight. The arithmetic weight will usually be less than the hamming weight since the <math>a_i</math> are allowed to be negative. For example, the integer <math>x = 29</math> which is <math>11101</math> in binary has a hamming weight of <math>4</math>. This is a quick upper bound on the arithmetic weight since <math>x = 2^0 + 2^2 + 2^3 + 2^4</math>. However, since the <math>a_i</math> can be negative, we can write <math>x = 2^5 - 2^1 - 2^0</math> which makes the arithmetic weight equal to <math>3</math>.

The arithmetic distance between two integers is defined by

- <math>d(x,y) = w(x-y)</math>

This is one of the primary metrics used when analyzing arithmetic codes.

## AN Codes[edit]

AN codes are defined by integers <math>A</math> and <math>B</math> and are used to encode integers from <math>0</math> to <math>B-1</math> such that

- <math>C = \{ AN|N\in \mathbb{Z}, 0 \leq N </math><<math>B \}</math>

Each choice of <math>A</math> will result in a different code, while <math>B</math> serves as a limiting factor to ensure useful properties in the distance of the code. If <math>B</math> 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 <math>A</math>. Let the result of the operation on the codewords be <math>R</math>. Note that <math>R</math> must also be between <math>0</math> to <math>B-1</math> for proper decoding. To decode, simply divide <math>R/A</math>. If <math>A</math> is not a factor of <math>R</math>, then at least one error has occurred and the most likely solution will be the codeword with the least arithmetic distance from <math>R</math>. As with codes using hamming distance, AN codes can correct up to <math>\lfloor \frac{d-1}{2} \rfloor </math> errors where <math>d</math> is the distance of the code.

For example, an AN code with <math>A = 3</math>, the operation of adding <math>15</math> and <math>16</math> will start by encoding both operands. This results in the operation <math>R = 45 + 48 = 93</math>. Then, to find the solution we divide <math>93/3 = 31</math>. As long as <math>B</math>><math>31</math>, this will be a possible operation under the code. Suppose an error occurs in each of the binary representation of the operands such that <math>45 = 101101 \rightarrow 101111</math> and <math>48 = 110000 \rightarrow 110001</math>, then <math>R = 101111 + 110001 = 1100000</math>. Notice that since <math>93 = 1011101</math>, the hamming weight between the received word and the correct solution is <math>5</math> after just <math>2</math> errors. To compute the arithmetic weight, we take <math>1100000 - 1011101 = 11</math> which can be represented as <math>11 = 2^0 + 2^1</math> or <math>11 = 2^2 - 2^0</math>. In either case, the arithmetic distance is <math>2</math> 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 <math>C </math> is a subgroup of <math> \mathbb{Z}/m \mathbb{Z}</math>, where <math>m = AB</math>. The codes are measured in terms of modular distance which is defined in terms of a graph with vertices being the elements of <math> \mathbb{Z}/m \mathbb{Z}</math>. Two vertices <math>x \pmod{m}</math> and <math>x' \pmod{m}</math> are connected iff

- <math>x - x' \equiv \pm c \cdot r^j \pmod{m}</math>

where <math>c,j \in \mathbb{Z}</math> and <math>0 </math><<math> c </math><<math> r</math>, <math>j \geq 0</math>. 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 <math>0</math> which is equal to

- <math>w_m(x) = min\{w(y)|y \in \mathbb{Z}, y \equiv x \pmod{m} \}</math>

In practice, the value of <math>m</math> is typically chosen such that <math>m=r^n -1</math> since most computer arithmetic is computed <math>\mod 2^n-1</math> 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 <math>m=r^n-1</math> also tends to result in codes with larger distances than other codes.

By using modular weight with <math>m=r^n-1</math>, the AN codes will be cyclic code.

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

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

## Mandelbaum-Barrows Codes[edit]

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

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

**theorem**: Let <math>C \subset [r^n - 1]</math> be a cyclic AN code with generator <math>A</math>, and

- <math>B = |C| = (r^n - 1)/A</math>

Then,

- <math>\sum_{x \in C} w_m(x) = n(\lfloor\frac{rB}{r+1}\rfloor - \lfloor\frac{B}{r+1}\rfloor)</math>

**proof**: Assume that each <math>x \in C</math> has a unique cyclic NAF representation which is

- <math>x \equiv \sum_{i=0}^{n-1} c_{i,x}r^i \pmod{r^n -1}</math>

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

- <math>\frac{n}{B-1}(\lfloor\frac{rB}{r+1}\rfloor - \lfloor\frac{B}{r+1}\rfloor)</math>

**proof**: Let <math>x \in C, x \neq 0</math>, then <math>x = AN \pmod{r^n-1}</math> and <math>N</math> is not divisible by <math>B</math>. This implies there <math>\exists j (N \equiv \pm r^j \pmod{B})</math>. Then <math>w_m(x) = w_m(\pm r^j A) = w_m(A)</math>. This proves that <math>C</math> is equidistant since all codewords have the same weight as <math>A</math>. 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).

## See also[edit]

## Source[edit]