# Design of Encoding Algorithm for Underwater Wireless Optical Communication Based on Spinal Code
## 2 Spinal Code Encoding Algorithm  
The Spinal code encoding principle utilizes a hash function and a random number generator (RNG). A series of random numbers can be generated through RNG to generate a continuous sequence of encoded symbols in a sequential encoding manner. Since the symbols mapped by the hash function are entirely independent, if two source messages differ by only one bit before encoding, the transmitted symbols after encoding will also exhibit significant differences. In the Spinal encoding algorithm, there is no strict standard for the length of the hash value, as the hash function used only serves to increase the difference between the encoded blocks of each partition. As a result, Spinal codes possess excellent noise resistance and bit error resilience. In general, a mapping scheme that maps a data sequence of arbitrary length to a fixed-length sequence is called a hash function. The output of a hash function is referred to as a hash value, hash code, or hash state, among other terms. The mapping performance of a hash function is mainly influenced by the output's independence and randomness characteristics. Regardless of the application, it is desirable for a hash function to provide fully independent and randomly distributed outputs for different inputs. The form of the hash function used in rateless Spinal codes is as follows:  
\[h:\left\{0,1\right\}^{v}\times\left\{0,1\right\}^{k}\rightarrow\left\{0,1 \right\}^{v} \tag{1}\]  
where both parameters \(k\) and \(v\) are positive integers. For any input \(\mathrm{s}\in\left\{0,1\right\}^{v}\) and \(m\in\left\{0,1\right\}^{k}\), as well as any random number generated by a random seed, the hash function \(h\) can produce an independent and equally probable binary sequence \(s_{out}\) of length \(v\). For another input \(s^{\prime}\in\left\{0,1\right\}^{v}\) and \(m^{\prime}\in\left\{0,1\right\}^{k}\), where \((s,m)\
eq(s^{\prime},m^{\prime})\), the following holds:  
\[ \begin{array}{l}\mathbb{P}(h(s,m)=s_{out},h(s^{\prime},m^{\prime})=s^{\prime} _{out})\\ =\mathbb{P}(h(s,m)=s_{out})\mathbb{P}(h(s^{\prime},m^{\prime})=s^{\prime}_{ out})=\frac{1}{2^{2v}}\end{array}  \tag{2}\]  
where \(m\) refers to the input data, which are the data to be hashed and can typically be any length of message, text, or file. The hash function generates a fixed-length hash value based on this input data. \(s\) is the salt, a randomly generated value that is usually combined with the original data before hashing to ensure the uniqueness of the generated hash value. \(s_{out}\in\left\{0,1\right\}^{v}\) and \(s^{\prime}_{out}\in\left\{0,1\right\}^{v}\). To ensure that information can be correctly recovered by the receiver in rateless transmission, the length \(v\) of the hash state must be sufficiently large or even infinite, thereby enabling the generation of an infinite number of channel input symbols. However, in practical applications, constrained by computational and storage capabilities, achieving an infinite \(v\) is infeasible. To address this issue, an RNG can be incorporated before directly mapping the hash state to channel input symbols. This RNG uses a shorter hash-generated sequence as a seed to produce a sufficiently long binary random sequence. Subsequently, a channel mapping function is applied to obtain a sufficient number of channel input symbols. The channel input sequence is then transmitted iteratively to the receiver until successful decoding and feedback confirmation are achieved. Each transmission pass consists of symbols from the sequence \(x_{1},\ x_{2},\ldots,\ x_{n/k}\) in their corresponding order. For example, during the first pass, the transmitter sends symbols \(x_{1,1},\ x_{2,1},\ldots,\ x_{n/k,1}\), where \(x_{i,1}\) denotes the first element of the sequence \(x_{i}\). The channel mapper is responsible for converting the \(c\)-bit binary number \(b\) generated by the random number generator (RNG) into a signal \(x_{i,j}\) suitable for channel transmission.