4比特和8比特S盒的非线性度的计算程序
时间: 2024-03-16 14:43:43 浏览: 51
以下是4比特和8比特S盒非线性度的计算程序:
4比特S盒非线性度计算程序:
```python
import math
# 4-bit S-box
s_box = [0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2]
n = len(s_box)
total_pairs = n * (n-1) / 2
nonlinear_pairs = 0
for i in range(n):
for j in range(i+1, n):
delta_x = i ^ j
delta_y = s_box[i] ^ s_box[j]
hamming_weight_x = bin(delta_x).count('1')
hamming_weight_y = bin(delta_y).count('1')
if hamming_weight_y != hamming_weight_x:
nonlinear_pairs += 1
nonlinear_degree = (2**(n-1) - nonlinear_pairs) / total_pairs
print("Nonlinear degree of 4-bit S-box is:", nonlinear_degree)
```
8比特S盒非线性度计算程序:
```python
import math
# 8-bit S-box
s_box = [0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1, 0x2]
n = len(s_box)
total_pairs = n * (n-1) / 2
nonlinear_pairs = 0
for i in range(n):
for j in range(i+1, n):
delta_x = i ^ j
delta_y = s_box[i] ^ s_box[j]
hamming_weight_x = bin(delta_x).count('1')
hamming_weight_y = bin(delta_y).count('1')
if hamming_weight_y != hamming_weight_x:
nonlinear_pairs += 1
nonlinear_degree = (2**(n-1) - nonlinear_pairs) / total_pairs
print("Nonlinear degree of 8-bit S-box is:", nonlinear_degree)
```
其中,s_box是S盒,n是S盒的比特数,total_pairs是输入差分对的总数,nonlinear_pairs是S盒的非线性对数。这两个程序的实现方法基本相同,只需要更改S盒即可。
阅读全文