求大佬拯救,在HLS平台上我的代码LUT使用率过高,并且我的加密与解密函数和密钥的改变无关,求解决方法
#include <hls_stream.h>
#include <stdint.h>
#include <stdbool.h>
#include "ap_int.h"
#include "ap_axi_sdata.h"
#include <ap_fixed.h>
#include "hls_math.h"
#include "math.h"
//#define MaxL 262144 // 最大缓冲区
#define L1 10 // 假设矩阵的大小,L1 x L2
#define L2 10
const double TWO_PI = 2 * M_PI;
double q_global = 0.0; // 全局变量,用于忆阻内部状态
struct AXIS_wLAST
{
uint8_t data;
bool last;
};
// 定义查表的大小
#define TABLE_SIZE 1024
// 预先计算的正弦值表
const double sin_table[TABLE_SIZE] = {
#include "sin_table.txt"
};
// 预先计算的余弦值表
const double cos_table[TABLE_SIZE] = {
#include "cos_table.txt"
};
// 做插值
double interp_lookup(double angle, const double* table) {
#pragma HLS INLINE
// 将角度转换为 0 到 2*pi 范围内
angle = fmod(angle, TWO_PI);
// 计算查找表中的索引
double index = (angle / (TWO_PI)) * (TABLE_SIZE - 1);
int lower_index = static_cast<int>(floor(index));
int upper_index = (lower_index + 1) % TABLE_SIZE;//得到索引值
// 处理边界情况
if (upper_index > TABLE_SIZE) {
upper_index = 1;
}
double weight = index - lower_index;
return (1 - weight) * table[lower_index] + weight * table[upper_index];
}
double sin_lookup(double angle) {
return interp_lookup(angle, sin_table);
}
double cos_lookup(double angle) {
return interp_lookup(angle, cos_table);
}
// 异或函数,输出uint8_t
ap_uint<8> bitxor2(ap_uint<8> X1, ap_uint<8> X2)
{
#pragma HLS INLINE
return X1 ^ X2;
}
// 模加函数
ap_uint<8> mod_add(ap_uint<8> a, ap_uint<8> b)
{
#pragma HLS INLINE
return (a + b) % 256;
}
// 模减函数
ap_uint<8> mod_add_inv(ap_uint<8> a, ap_uint<8> b)
{
#pragma HLS INLINE
return (a - b + 256) % 256;
}
// 参数
const double u = 0.9;
// HLS 中的 f(x) 函数实现
void yx(double Y[3], double dy[3]) {
// double x = Y[0];
// double y = Y[1];
// double z = Y[2];
double W = cos_lookup(Y[2]);
dy[0] = u * Y[0] + W * sin_lookup(Y[0]) + sin_lookup( Y[1]);
dy[1] = u * Y[1] - 2 * sin_lookup(Y[0]) + 2 * sin_lookup( Y[1]);
dy[2] = Y[2] + 0.1 * sin_lookup( Y[0]);
}
// M 函数,输出uint_8
uint8_t M(double locX, double locY, double pixel) {
const double e = 1e-9;
double m = pixel * M_PI * 2;
//基于忆阻的非线性运算
double input[3] = {locX, locY, e * m + q_global};
double tx[3];
// 调用 yx 函数
yx(input, tx);
// 更新内部状态
q_global = tx[2] - m;
//整型浮点型转换
int64_t tx_out[2];
tx_out[0] = static_cast<int64_t>(std::abs(tx[0]) * 1e12) % 256;
tx_out[1] = static_cast<int64_t>(std::abs(tx[1]) * 1e12) % 256;
// 异或融合
return static_cast<uint8_t>(tx_out[0] ^ tx_out[1]);
}
// 加密函数
void encrypt_sequence(uint8_t im[L1][L2],double key1, double key2, double key3, double key4)
{
// 前向扩散阶段
q_global = key1;
uint8_t C_F[L1][L2];
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
//#pragma HLS PIPELINE
if (i == 0 && j == 0)
{
C_F[i][j] = mod_add(im[i][j], M(i,j,key2));
}
else if (j > 0)
{
C_F[i][j] = mod_add(im[i][j], bitxor2(im[i][j - 1], M(i,j,C_F[i][j - 1])));
}
else
{
C_F[i][j] = mod_add(im[i][j], bitxor2(im[i - 1][L2 - 1], M(i,j,C_F[i - 1][L2 - 1])));
}
}
}
// 后向扩散阶段
q_global = key3;
uint8_t C_B[L1][L2];
for (int i = L1 - 1; i >= 0;i--)
{
for (int j = L2 - 1; j >= 0; j--)
{
//#pragma HLS PIPELINE
if (i == L1 -1 && j == L2 -1 )
{
C_B[i][j] = mod_add(C_F[i][j], M(i,j,key4));
}
else if (j < L2 - 1)
{
C_B[i][j] = mod_add(C_F[i][j], bitxor2(C_F[i][j + 1], M(i,j,C_B[i][j + 1])));
}
else
{
C_B[i][j] = mod_add(C_F[i][j], bitxor2(C_F[i + 1][0], M(i,j,C_B[i + 1][0])));
}
}
}
// 输出加密后的结果
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS PIPELINE
im[i][j] = C_B[i][j];
}
}
}
// 解密函数
void decrypt_sequence(uint8_t im[L1][L2], double key1, double key2, double key3, double key4)
{
// 后向扩散阶段
q_global = key3;
uint8_t C_FD[L1][L2];
// 后向扩散
for (int i = L1 - 1; i >= 0; i--)
{
for (int j = L2 - 1; j >= 0; j--)
{
//#pragma HLS PIPELINE
if (i == L1 -1 && j == L2 -1 )
{
C_FD[i][j] = mod_add_inv(im[i][j], M(i,j,key4));
}
else if (j < L2 - 1)
{
C_FD[i][j] = mod_add_inv(im[i][j], bitxor2(C_FD[i][j + 1], M(i,j,im[i][j + 1])));
}
else
{
C_FD[i][j] = mod_add_inv(im[i][j], bitxor2(C_FD[i + 1][0], M(i,j,im[i + 1][0])));
}
}
}
// 前向扩散阶段
q_global = key1;
uint8_t P_D[L1][L2];
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
//#pragma HLS PIPELINE
if (i == 0 && j == 0)
{
P_D[i][j] = mod_add_inv(C_FD[i][j], M(i,j,key2));
}
else if (j > 0)
{
P_D[i][j] = mod_add_inv(C_FD[i][j], bitxor2(P_D[i][j - 1], M(i,j,C_FD[i][j - 1])));
}
else
{
P_D[i][j] = mod_add_inv(C_FD[i][j], bitxor2(P_D[i - 1][L2 - 1], M(i,j,C_FD[i - 1][L2 - 1])));
}
}
}
// 输出解密后的结果
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS PIPELINE
im[i][j] = P_D[i][j];
}
}
}
void fx(hls::stream<AXIS_wLAST> &DATA_INPUT, hls::stream<AXIS_wLAST> &DATA_OUTPUT, double encryption_key,int operation_id)
{
#pragma HLS INTERFACE axis port = DATA_INPUT
#pragma HLS INTERFACE axis port = DATA_OUTPUT
#pragma HLS INTERFACE s_axilite register port = encryption_key bundle = CTRL
#pragma HLS INTERFACE s_axilite register port = operation_id bundle = CTRL
#pragma HLS INTERFACE s_axilite port=return bundle = CTRL
uint8_t pixels[L1][L2];//储存图像
AXIS_wLAST read_input, write_output;
// 读取输入数据并解包
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS LOOP_TRIPCOUNT min = 100
#pragma HLS PIPELINE II = 1
read_input = DATA_INPUT.read();
pixels[i][j] = read_input.data;
}
}
// 执行加密或解密操作
if (operation_id == 1){
encrypt_sequence(pixels,encryption_key, 1.2, 1.5, 2.0);//传输数据和一个动态密钥,三个静态密钥
}
else
{
decrypt_sequence(pixels,encryption_key, 1.2, 1.5, 2.0);
}
// 打包输出数据
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS LOOP_TRIPCOUNT min = 100
#pragma HLS PIPELINE II = 1
write_output.data = pixels[i][j];
write_output.last = (i == L1 - 1 && j == L2 - 1) ? 1 : 0;
DATA_OUTPUT.write(write_output);
}
}
}
////做sin插值
//double sin_lookup(double angle) {
//#pragma HLS INLINE
// // 将角度转换为 0 到 2*pi 范围内
// angle = fmod(angle, 2 * M_PI);
//
// // 计算查找表中的索引
// double index = (angle / (2 * M_PI)) * (TABLE_SIZE - 1) + 1;
// int lower_index = (int)floor(index);
// int upper_index = (int)ceil(index);
//
// // 处理边界情况
// if (upper_index > TABLE_SIZE) {
// upper_index = 1;
// }
//
// // 线性插值
// double lower_val = sin_table[lower_index - 1];
// double upper_val = sin_table[upper_index - 1];
// double weight = index - lower_index;
//
// return (1 - weight) * lower_val + weight * upper_val;
//}
//
////做cos插值
//double cos_lookup(double angle) {
//#pragma HLS INLINE
// // 将角度转换为 0 到 2*pi 范围内
// angle = fmod(angle, 2 * M_PI);
//
// // 计算查找表中的索引
// double index = (angle / (2 * M_PI)) * (TABLE_SIZE - 1) + 1;
// int lower_index = (int)floor(index);
// int upper_index = (int)ceil(index);
//
// // 处理边界情况
// if (upper_index > TABLE_SIZE) {
// upper_index = 1;
// }
//
// // 线性插值
// double lower_val = cos_table[lower_index - 1];
// double upper_val = cos_table[upper_index - 1];
// double weight = index - lower_index;
//
// return (1 - weight) * lower_val + weight * upper_val;
//}
#include <hls_stream.h>
#include <stdint.h>
#include <stdbool.h>
#include "ap_int.h"
#include "ap_axi_sdata.h"
#include <ap_fixed.h>
#include "hls_math.h"
#include "math.h"
//#define MaxL 262144 // 最大缓冲区
#define L1 10 // 假设矩阵的大小,L1 x L2
#define L2 10
const double TWO_PI = 2 * M_PI;
double q_global = 0.0; // 全局变量,用于忆阻内部状态
struct AXIS_wLAST
{
uint8_t data;
bool last;
};
// 定义查表的大小
#define TABLE_SIZE 1024
// 预先计算的正弦值表
const double sin_table[TABLE_SIZE] = {
#include "sin_table.txt"
};
// 预先计算的余弦值表
const double cos_table[TABLE_SIZE] = {
#include "cos_table.txt"
};
// 做插值
double interp_lookup(double angle, const double* table) {
#pragma HLS INLINE
// 将角度转换为 0 到 2*pi 范围内
angle = fmod(angle, TWO_PI);
// 计算查找表中的索引
double index = (angle / (TWO_PI)) * (TABLE_SIZE - 1);
int lower_index = static_cast<int>(floor(index));
int upper_index = (lower_index + 1) % TABLE_SIZE;//得到索引值
// 处理边界情况
if (upper_index > TABLE_SIZE) {
upper_index = 1;
}
double weight = index - lower_index;
return (1 - weight) * table[lower_index] + weight * table[upper_index];
}
double sin_lookup(double angle) {
return interp_lookup(angle, sin_table);
}
double cos_lookup(double angle) {
return interp_lookup(angle, cos_table);
}
// 异或函数,输出uint8_t
ap_uint<8> bitxor2(ap_uint<8> X1, ap_uint<8> X2)
{
#pragma HLS INLINE
return X1 ^ X2;
}
// 模加函数
ap_uint<8> mod_add(ap_uint<8> a, ap_uint<8> b)
{
#pragma HLS INLINE
return (a + b) % 256;
}
// 模减函数
ap_uint<8> mod_add_inv(ap_uint<8> a, ap_uint<8> b)
{
#pragma HLS INLINE
return (a - b + 256) % 256;
}
// 参数
const double u = 0.9;
// HLS 中的 f(x) 函数实现
void yx(double Y[3], double dy[3]) {
// double x = Y[0];
// double y = Y[1];
// double z = Y[2];
double W = cos_lookup(Y[2]);
dy[0] = u * Y[0] + W * sin_lookup(Y[0]) + sin_lookup( Y[1]);
dy[1] = u * Y[1] - 2 * sin_lookup(Y[0]) + 2 * sin_lookup( Y[1]);
dy[2] = Y[2] + 0.1 * sin_lookup( Y[0]);
}
// M 函数,输出uint_8
uint8_t M(double locX, double locY, double pixel) {
const double e = 1e-9;
double m = pixel * M_PI * 2;
//基于忆阻的非线性运算
double input[3] = {locX, locY, e * m + q_global};
double tx[3];
// 调用 yx 函数
yx(input, tx);
// 更新内部状态
q_global = tx[2] - m;
//整型浮点型转换
int64_t tx_out[2];
tx_out[0] = static_cast<int64_t>(std::abs(tx[0]) * 1e12) % 256;
tx_out[1] = static_cast<int64_t>(std::abs(tx[1]) * 1e12) % 256;
// 异或融合
return static_cast<uint8_t>(tx_out[0] ^ tx_out[1]);
}
// 加密函数
void encrypt_sequence(uint8_t im[L1][L2],double key1, double key2, double key3, double key4)
{
// 前向扩散阶段
q_global = key1;
uint8_t C_F[L1][L2];
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
//#pragma HLS PIPELINE
if (i == 0 && j == 0)
{
C_F[i][j] = mod_add(im[i][j], M(i,j,key2));
}
else if (j > 0)
{
C_F[i][j] = mod_add(im[i][j], bitxor2(im[i][j - 1], M(i,j,C_F[i][j - 1])));
}
else
{
C_F[i][j] = mod_add(im[i][j], bitxor2(im[i - 1][L2 - 1], M(i,j,C_F[i - 1][L2 - 1])));
}
}
}
// 后向扩散阶段
q_global = key3;
uint8_t C_B[L1][L2];
for (int i = L1 - 1; i >= 0;i--)
{
for (int j = L2 - 1; j >= 0; j--)
{
//#pragma HLS PIPELINE
if (i == L1 -1 && j == L2 -1 )
{
C_B[i][j] = mod_add(C_F[i][j], M(i,j,key4));
}
else if (j < L2 - 1)
{
C_B[i][j] = mod_add(C_F[i][j], bitxor2(C_F[i][j + 1], M(i,j,C_B[i][j + 1])));
}
else
{
C_B[i][j] = mod_add(C_F[i][j], bitxor2(C_F[i + 1][0], M(i,j,C_B[i + 1][0])));
}
}
}
// 输出加密后的结果
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS PIPELINE
im[i][j] = C_B[i][j];
}
}
}
// 解密函数
void decrypt_sequence(uint8_t im[L1][L2], double key1, double key2, double key3, double key4)
{
// 后向扩散阶段
q_global = key3;
uint8_t C_FD[L1][L2];
// 后向扩散
for (int i = L1 - 1; i >= 0; i--)
{
for (int j = L2 - 1; j >= 0; j--)
{
//#pragma HLS PIPELINE
if (i == L1 -1 && j == L2 -1 )
{
C_FD[i][j] = mod_add_inv(im[i][j], M(i,j,key4));
}
else if (j < L2 - 1)
{
C_FD[i][j] = mod_add_inv(im[i][j], bitxor2(C_FD[i][j + 1], M(i,j,im[i][j + 1])));
}
else
{
C_FD[i][j] = mod_add_inv(im[i][j], bitxor2(C_FD[i + 1][0], M(i,j,im[i + 1][0])));
}
}
}
// 前向扩散阶段
q_global = key1;
uint8_t P_D[L1][L2];
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
//#pragma HLS PIPELINE
if (i == 0 && j == 0)
{
P_D[i][j] = mod_add_inv(C_FD[i][j], M(i,j,key2));
}
else if (j > 0)
{
P_D[i][j] = mod_add_inv(C_FD[i][j], bitxor2(P_D[i][j - 1], M(i,j,C_FD[i][j - 1])));
}
else
{
P_D[i][j] = mod_add_inv(C_FD[i][j], bitxor2(P_D[i - 1][L2 - 1], M(i,j,C_FD[i - 1][L2 - 1])));
}
}
}
// 输出解密后的结果
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS PIPELINE
im[i][j] = P_D[i][j];
}
}
}
void fx(hls::stream<AXIS_wLAST> &DATA_INPUT, hls::stream<AXIS_wLAST> &DATA_OUTPUT, double encryption_key,int operation_id)
{
#pragma HLS INTERFACE axis port = DATA_INPUT
#pragma HLS INTERFACE axis port = DATA_OUTPUT
#pragma HLS INTERFACE s_axilite register port = encryption_key bundle = CTRL
#pragma HLS INTERFACE s_axilite register port = operation_id bundle = CTRL
#pragma HLS INTERFACE s_axilite port=return bundle = CTRL
uint8_t pixels[L1][L2];//储存图像
AXIS_wLAST read_input, write_output;
// 读取输入数据并解包
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS LOOP_TRIPCOUNT min = 100
#pragma HLS PIPELINE II = 1
read_input = DATA_INPUT.read();
pixels[i][j] = read_input.data;
}
}
// 执行加密或解密操作
if (operation_id == 1){
encrypt_sequence(pixels,encryption_key, 1.2, 1.5, 2.0);//传输数据和一个动态密钥,三个静态密钥
}
else
{
decrypt_sequence(pixels,encryption_key, 1.2, 1.5, 2.0);
}
// 打包输出数据
for (int i = 0; i < L1; i++)
{
for (int j = 0; j < L2; j++)
{
#pragma HLS LOOP_TRIPCOUNT min = 100
#pragma HLS PIPELINE II = 1
write_output.data = pixels[i][j];
write_output.last = (i == L1 - 1 && j == L2 - 1) ? 1 : 0;
DATA_OUTPUT.write(write_output);
}
}
}
////做sin插值
//double sin_lookup(double angle) {
//#pragma HLS INLINE
// // 将角度转换为 0 到 2*pi 范围内
// angle = fmod(angle, 2 * M_PI);
//
// // 计算查找表中的索引
// double index = (angle / (2 * M_PI)) * (TABLE_SIZE - 1) + 1;
// int lower_index = (int)floor(index);
// int upper_index = (int)ceil(index);
//
// // 处理边界情况
// if (upper_index > TABLE_SIZE) {
// upper_index = 1;
// }
//
// // 线性插值
// double lower_val = sin_table[lower_index - 1];
// double upper_val = sin_table[upper_index - 1];
// double weight = index - lower_index;
//
// return (1 - weight) * lower_val + weight * upper_val;
//}
//
////做cos插值
//double cos_lookup(double angle) {
//#pragma HLS INLINE
// // 将角度转换为 0 到 2*pi 范围内
// angle = fmod(angle, 2 * M_PI);
//
// // 计算查找表中的索引
// double index = (angle / (2 * M_PI)) * (TABLE_SIZE - 1) + 1;
// int lower_index = (int)floor(index);
// int upper_index = (int)ceil(index);
//
// // 处理边界情况
// if (upper_index > TABLE_SIZE) {
// upper_index = 1;
// }
//
// // 线性插值
// double lower_val = cos_table[lower_index - 1];
// double upper_val = cos_table[upper_index - 1];
// double weight = index - lower_index;
//
// return (1 - weight) * lower_val + weight * upper_val;
//}