### Arithmetic Coding

Arithmetic coding is a very interesting form of encoding that can be used in data compression.

In this post, we are going to look at a generalized form of arithmetic coding, as a generalized change of base (or radix).

This type of arithmetic coding works by counting the occurring frequency of each byte in a given message, then it computes the cumulative frequency of each byte from which it creates a polynomial in the base of the length of message, which will result in a large integer as output.

From the above frequency, we will compute the cumulative frequency, which is the sum of the above frequencies for each byte, in ASCIIbetical order:

Before starting the encoding process, let's map each byte to the corresponding cumulative frequency (just for illustration):

Now, the encoding process begins. We have to create a polynomial in base 7 (which is the length of the message that we want to encode). Each term is multiplied correspondingly by the cumulative frequency of the current byte, which is then multiplied by the product of the frequencies of all previously occurred bytes:

The above result represents the lower bound of the encoding. To compress the message even further, we need to calculate the upper bound and pick a number between those that can be compressed well.

A very practical pick would be a number which can be expressed in the base 2. We can't do this, unfortunately, for this particular message, because the next power of two after 332515 (L) is much greater than the upper bound (U). However, for longer messages, the range will tend to be much wider, providing interesting ways for exploitation.

Another reasonable pick would be a number which is a power of ten, so we can remove some trailing zeros.

In this post, we are going to look at a generalized form of arithmetic coding, as a generalized change of base (or radix).

This type of arithmetic coding works by counting the occurring frequency of each byte in a given message, then it computes the cumulative frequency of each byte from which it creates a polynomial in the base of the length of message, which will result in a large integer as output.

## # Encoding

First of all, we have to calculate the occurring frequency of each byte. To illustrate this, let's use the word "DECODED".

This word has the following frequency of occurring bytes:

= { => 1 => 3 => 2 => 1 }

= { => 0 # () => 1 # (f['C'] -> 1) => 4 # (f['C']+f['D'] -> 1+3) => 6 # (f['C']+f['D']+f['E'] -> 1+3+2) }

[ : 1 : 4 : 0 : 6 : 1 : 4 : 1 ]

= 7^6 * ['D'] + 7^5 * ['E'] * ['D'] + 7^4 * ['C'] * ['D'] * ['E'] + 7^3 * ['O'] * ['D'] * ['E'] * ['C'] + 7^2 * ['D'] * ['D'] * ['E'] * ['C'] * ['O'] + 7^1 * ['E'] * ['D'] * ['E'] * ['C'] * ['O'] * ['D'] + 7^0 * ['D'] * ['D'] * ['E'] * ['C'] * ['O'] * ['D'] * ['E']

which gets evaluated to:

= 7^6 * 1 + 7^5 * 4 * 3 + 7^4 * 0 * 6 + 7^3 * 6 * 6 + 7^2 * 1 * 6 + 7^1 * 4 * 18 + 7^0 * 1 * 36

Finally resulting in:

= 332515

= +

where `pf` is the product of the all frequencies of bytes in the message that we want to encode:

= ['D'] * ['E'] * ['C'] * ['O'] * ['D'] * ['E'] * ['D'] = 108

giving us:

= 332515 + 108 = 332623

Now, we have the possibility to return any number >= L and < U.

For this message, it is guaranteed that we can remove two trailing zeros, because 108 (pf) is greater than 10^2. There is only one number in this range with this property, and that is: 332600.

A general formula for removing as many trailing zeros as possible, is:

= int(log() / log(10)) = int((-1) / 10^)

* where int() is equivalent with the floor() function for positive numbers.

For our example, we will get:

= int(log(108) / log(10)); = 2 = int((332623-1) / 10^2) = 3326

In the end, the encoded message is: 3326 * 10^2 (or just 3326e2)

## # Decoding

The decoding process is just the reverse of the encoding process. All we need, is the encoded message and the table of frequencies from the encoding process (f).

= 332600

From the table of frequencies, we will create the cumulative frequency table (cf), as explained in the encoding process, then we will reverse it to create the dictionary table:

= { 0 => 1 => 4 => 6 => }

The next step is to fill the gaps in the dictionary, so the keys will be in increasing order from 0, up to the sum of all frequencies - 1.

The sum of the frequencies is also the base of the encoded polynomial:

= 1 + 3 + 2 + 1 = 7

The filling process starts at 0 and goes up to base-1 (inclusive). When a gap is encountered, it will be filled with the last character encountered, resulting in:

= { 0 => 1 => 2 => 3 => 4 => 5 => 6 => }

Now, we can start the decoding process by iterating from =-1, down to =0:

= int( / ^) = [] = int(( - ^ * []) / [])

Which, for our encoded message, it will get evaluated into:

= int(332600 / 7^6) = 2 = [2] = 'D' = int((332600 - 7^6 * 1) / 3) = 71650

As the process continues, with each iteration it decodes another byte, creating the original message: DECODED

## # Conclusion

The algorithm is, in my opinion, quite interesting, but in practical use, it has some issues, one of which is the speed; as the message gets longer, the polynomial base increases, as well as the polynomial degree, resulting in very large integers.

## # Implementations

As a generalized change of radix:

**Go:**https://github.com/trizen/go-learning/blob/master/arithmetic_coding.go**Perl:**https://github.com/trizen/perl-scripts/blob/master/Encoders/arithmetic_coding.pl

Other implementations:

Practical implementations:

**TAC file compressor:**https://github.com/trizen/perl-scripts/blob/master/Encoders/tac-compressor.pl**TACC file compressor (with GMP):**https://github.com/trizen/perl-scripts/blob/master/Encoders/tacc-compressor.pl