From c7cef447b883cb9ae5e7232d8c6e4df294a2fa18 Mon Sep 17 00:00:00 2001 From: AsyncKurisu <1750981157@qq.com> Date: Mon, 24 Nov 2025 16:43:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E6=95=B0=E9=80=BB=E8=BE=91v2=EF=BC=8C=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4WAYS=3D4=EF=BC=8C6=EF=BC=8C8=EF=BC=8C8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/arm64crypto/randombytes_ctrdrbg.c | 107 ++++++++++++++++--- 1 file changed, 90 insertions(+), 17 deletions(-) diff --git a/src/common/arm64crypto/randombytes_ctrdrbg.c b/src/common/arm64crypto/randombytes_ctrdrbg.c index b5ab52f..a775f9d 100644 --- a/src/common/arm64crypto/randombytes_ctrdrbg.c +++ b/src/common/arm64crypto/randombytes_ctrdrbg.c @@ -129,6 +129,31 @@ add_to_V_optimized(unsigned char V[], int incr) vst1q_u8(V, vV); } +// 动态确定最优WAYS值 +static int +determine_optimal_ways(unsigned long long data_size) +{ + // 根据数据大小选择最优的WAYS值 + // 这些阈值可以通过实际测试优化 + + // 小数据块: 使用4路并行 + if (data_size < 256) { + return 4; + } + // 中等数据块: 使用6路并行 + else if (data_size < 1024) { + return 6; + } + // 大数据块: 使用8路并行 + else if (data_size < 4096) { + return 8; + } + // 超大数据块: 使用10路并行,但不超过12 + else { + return 8; + } +} + // 优化7: 改进DRBG更新函数,减少内存操作 static void AES256_CTR_DRBG_Update_Optimized(unsigned char *provided_data, @@ -230,10 +255,7 @@ randombytes_init_arm64crypto_optimized(unsigned char *entropy_input, DRBG_ctx.reseed_counter = 1; } -// 优化9: 提高WAYS值以利用更宽的向量寄存器 -#define WAYS_OPTIMIZED 8 // 增加到8,利用更宽的向量化 - -// 优化10: 改进主随机数生成函数,使用更大的WAYS值和更好的向量化 +// 优化9: 动态选择WAYS值的主随机数生成函数 int randombytes_arm64crypto_optimized(unsigned char *x, unsigned long long xlen) { @@ -247,14 +269,18 @@ randombytes_arm64crypto_optimized(unsigned char *x, unsigned long long xlen) vsubkeys[j] = vld1q_u8(subkeys[j]); } - // 处理大块数据(使用优化后的WAYS值) - if (xlen >= WAYS_OPTIMIZED * 16) { - uint8x16_t vV_array[WAYS_OPTIMIZED]; + // 根据数据大小动态确定最优的WAYS值 + int ways = determine_optimal_ways(xlen); + + // 处理大块数据(使用动态确定的WAYS值) + if (xlen >= ways * 16) { + // 使用动态分配的数组来适应不同的WAYS值 + uint8x16_t vV_array[12]; // 最多支持12路并行 uint8x16_t vV = vld1q_u8(DRBG_ctx.V); // 初始化计数器值 vV_array[0] = vV; - for (int j = 1; j < WAYS_OPTIMIZED; j++) { + for (int j = 1; j < ways; j++) { uint64x2_t vV64 = vreinterpretq_u64_u8(vV); uint64x2_t inc = vdupq_n_u64(j); vV64 = vaddq_u64(vV64, inc); @@ -262,27 +288,27 @@ randombytes_arm64crypto_optimized(unsigned char *x, unsigned long long xlen) } // 处理大块数据 - while (xlen >= WAYS_OPTIMIZED * 16) { + while (xlen >= ways * 16) { // 批量AES加密 - AES256_ECB_XWAYS_OPTIMIZED(WAYS_OPTIMIZED, vsubkeys, vV_array, x); + AES256_ECB_XWAYS_OPTIMIZED(ways, vsubkeys, vV_array, x); // 更新计数器值 - uint64x2_t vV64 = vreinterpretq_u64_u8(vV_array[WAYS_OPTIMIZED - 1]); - uint64x2_t inc = vdupq_n_u64(WAYS_OPTIMIZED); + uint64x2_t vV64 = vreinterpretq_u64_u8(vV_array[ways - 1]); + uint64x2_t inc = vdupq_n_u64(ways); vV64 = vaddq_u64(vV64, inc); - for (int j = 0; j < WAYS_OPTIMIZED; j++) { + for (int j = 0; j < ways; j++) { uint64x2_t current = vreinterpretq_u64_u8(vV_array[j]); current = vaddq_u64(current, inc); vV_array[j] = vreinterpretq_u8_u64(current); } - x += WAYS_OPTIMIZED * 16; - xlen -= WAYS_OPTIMIZED * 16; + x += ways * 16; + xlen -= ways * 16; } // 更新V为最后一个计数器值 - vV = vV_array[WAYS_OPTIMIZED - 1]; + vV = vV_array[ways - 1]; vst1q_u8(DRBG_ctx.V, vV); } @@ -313,6 +339,53 @@ randombytes_arm64crypto_optimized(unsigned char *x, unsigned long long xlen) return RNG_SUCCESS; } +// // 高级版本:带有自适应学习能力的随机数生成函数 +// int +// randombytes_arm64crypto_adaptive(unsigned char *x, unsigned long long xlen) +// { +// // 静态变量用于记录历史性能数据 +// static unsigned long long total_bytes_processed = 0; +// static unsigned long long total_time_used = 0; // 假设有时间测量机制 + +// uint8_t subkeys[15][16]; +// uint8x16_t vsubkeys[15]; + +// // 预先计算子密钥 +// AES256_key_schedule(subkeys, DRBG_ctx.Key); +// for (int j = 0; j < 15; j++) { +// vsubkeys[j] = vld1q_u8(subkeys[j]); +// } + +// // 基于历史性能数据自适应选择WAYS值 +// int ways; +// if (total_bytes_processed > 1024 * 1024) { // 如果已经处理了1MB以上数据 +// // 基于历史平均性能选择最优WAYS +// // 这里简化为基于历史平均值的选择,实际中可以更复杂 +// unsigned long long avg_bytes_per_time = total_bytes_processed / (total_time_used ? total_time_used : 1); + +// if (avg_bytes_per_time > 1000) { // 假设阈值 +// ways = (xlen > 4096) ? 12 : 8; // 高性能情况下使用更高并行度 +// } else { +// ways = (xlen > 1024) ? 8 : 6; // 普通情况 +// } +// } else { +// // 初始阶段使用基本规则 +// ways = determine_optimal_ways(xlen); +// } + +// // 确保不超过最大支持的并行度 +// ways = (ways > 12) ? 12 : ways; + +// // 这里开始实际的处理,与前面函数类似,但使用动态确定的ways值 +// // ... (实现与randombytes_arm64crypto_optimized类似) + +// // 更新历史统计 +// total_bytes_processed += xlen; +// // total_time_used += elapsed_time; // 需要实际测量时间 + +// return RNG_SUCCESS; +// } + // 包装函数 #ifdef RANDOMBYTES_ARM64CRYPTO int @@ -330,4 +403,4 @@ randombytes_init(unsigned char *entropy_input, unsigned char *personalization_st { randombytes_init_arm64crypto_optimized(entropy_input, personalization_string, security_strength); } -#endif \ No newline at end of file +#endif