curve25519-dalek中field reduce原理分析

2023-10-24 15:30

本文主要是介绍curve25519-dalek中field reduce原理分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

对于Curve25519,其Field域内的module Fp = 2255-19。
对于64位系统:

/// A `FieldElement51` represents an element of the field
/// \\( \mathbb Z / (2\^{255} - 19)\\).
///
/// In the 64-bit implementation, a `FieldElement` is represented in
/// radix \\(2\^{51}\\) as five `u64`s; the coefficients are allowed to
/// grow up to \\(2\^{54}\\) between reductions modulo \\(p\\).
///
/// # Note
///
/// The `curve25519_dalek::field` module provides a type alias
/// `curve25519_dalek::field::FieldElement` to either `FieldElement51`
/// or `FieldElement2625`.
///
/// The backend-specific type `FieldElement51` should not be used
/// outside of the `curve25519_dalek::field` module.
#[derive(Copy, Clone)]
pub struct FieldElement51(pub (crate) [u64; 5]);

src/backend/serial/u64/field.rs中的reduce函数,是将[u64;5] low-reduce成h, h ∈ [ 0 , 2 ∗ p ) , p = 2 255 − 19 h \in [0, 2*p), p=2^{255}-19 h[0,2p),p=225519。具体的原理如下:

1. field reduce原理分析

如要求某整数 u m o d ( 2 255 − 19 ) u\quad mod \quad (2^{255}-19) umod(225519),可将u整数用多项式做如下表示:
u = ∑ i u i 2 51 i x i , 其 中 , u i ∈ N u=\sum_{i}^{}u_i2^{51i}x^i,其中,u_i \in N u=iui251ixiuiN
设置x=1,通过对u多项式求值即可代表域Fp内的值。
如需求一个值:
a ∈ [ 0 , 2 320 − 1 ] m o d ( 2 255 − 19 ) = ? a\in [0, 2^{320}-1] \quad mod \quad(2^{255}-19)=? a[0,23201]mod(225519)=
在这里插入图片描述
a ∈ [ 0 , 2 320 − 1 ] a\in [0, 2^{320}-1] a[0,23201]以上图表示,同时根据 a i a_i ai分别取相应的 b i , c i b_i,c_i bi,ci:
b i = a i &amp; ( 2 &lt; &lt; 51 − 1 ) , c i = a i &gt; &gt; 51 , 其 中 , c i &lt; = 2 13 b_i=a_i \&amp; (2&lt;&lt;51 -1), c_i=a_i &gt;&gt; 51, 其中,c_i&lt;= 2^{13} bi=ai&(2<<511),ci=ai>>51ci<=213
采用parallel carry-out方式进行,对应有:
在这里插入图片描述
以多项式方式表示时,其中的最高项为:
c 4 ∗ 2 255 ∗ x 5 m o d ( 2 255 − 19 ) c_4*2^{255}*x^5 \quad mod \quad (2^{255}-19) c42255x5mod(225519)
∵ 2 255 ∗ x 5 ≡ 19 m o d ( 2 255 − 19 ) \because 2^{255}*x^5 \equiv 19 \quad mod \quad (2^{255}-19) 2255x519mod(225519)
∴ c 4 ∗ 2 255 ∗ x 5 ≡ c 4 ∗ 19 m o d ( 2 255 − 19 ) \therefore c_4*2^{255}*x^5 \equiv c_4*19 \quad mod \quad (2^{255}-19) c42255x5c419mod(225519)
所以上图可演化为:
在这里插入图片描述
∵ c i &lt; = 2 13 \because c_i&lt;= 2^{13} ci<=213
∵ 2 51 + 2 13 &lt; 2 51 + 2 13 ∗ 19 &lt; 2 51.0000000001 \because 2^{51}+2^{13}&lt;2^{51}+2^{13}*19&lt;2^{51.0000000001} 251+213<251+21319<251.0000000001
∵ 2 ( 51.0000000001 ∗ 5 ) &lt; 2 ∗ ( 2 255 − 19 ) = 2 ∗ p , p = 2 255 − 19 \because 2^{(51.0000000001*5)} &lt; 2*(2^{255}-19)=2*p, p =2^{255}-19 2(51.00000000015)<2(225519)=2p,p=225519

sage: (2^51)+(2^13)*19<2^51.0000000001
True
sage: 2^(51.0000000001*5) < 2*(2^255-19)
True

∴ h 4 = b 4 + c 3 , h 3 = b 3 + c 2 , h 2 = b 2 + c 1 , h 1 = b 1 + c 0 , h 0 = b 0 + c 4 ∗ 19 \therefore h_4=b_4+c_3, h_3=b_3+c_2,h_2=b_2+c_1,h_1=b_1+c_0,h_0=b_0+c_4*19 h4=b4+c3,h3=b3+c2,h2=b2+c1,h1=b1+c0,h0=b0+c419
∴ h = ∑ i = 0 4 h i ∗ 2 ( 51.0000000001 ∗ i ) &lt; 2 ∗ p \therefore h=\sum_{i=0}^{4}h_i*2^{(51.0000000001*i)}&lt;2*p h=i=04hi2(51.0000000001i)<2p
在这里插入图片描述

2. field reduce代码实现

/// Given 64-bit input limbs, reduce to enforce the bound 2^(51 + epsilon).#[inline(always)]fn reduce(mut limbs: [u64; 5]) -> FieldElement51 {const LOW_51_BIT_MASK: u64 = (1u64 << 51) - 1;// Since the input limbs are bounded by 2^64, the biggest// carry-out is bounded by 2^13.//// The biggest carry-in is c4 * 19, resulting in//// 2^51 + 19*2^13 < 2^51.0000000001//// Because we don't need to canonicalize, only to reduce the// limb sizes, it's OK to do a "weak reduction", where we// compute the carry-outs in parallel.let c0 = limbs[0] >> 51;let c1 = limbs[1] >> 51;let c2 = limbs[2] >> 51;let c3 = limbs[3] >> 51;let c4 = limbs[4] >> 51;limbs[0] &= LOW_51_BIT_MASK;limbs[1] &= LOW_51_BIT_MASK;limbs[2] &= LOW_51_BIT_MASK;limbs[3] &= LOW_51_BIT_MASK;limbs[4] &= LOW_51_BIT_MASK;limbs[0] += c4 * 19;limbs[1] += c0;limbs[2] += c1;limbs[3] += c2;limbs[4] += c3;FieldElement51(limbs)}

3. field reduce结果 ∈ [ 0 , 2 ∗ p ) , p = 2 255 − 19 \in [0, 2*p), p=2^{255}-19 [0,2p),p=225519

src/backend/serial/u64/field.rs中的sub,neg等函数都只是调用了reduce函数,将最终的返回结果限定在了 ∈ [ 0 , 2 ∗ p ) , p = 2 255 − 19 \in [0, 2*p), p=2^{255}-19 [0,2p),p=225519,只有调用to_bytes函数,才会将结果限定在 ∈ [ 0 , p ) , p = 2 255 − 19 \in [0, p), p=2^{255}-19 [0,p),p=225519

		// -1 + (2^255 - 19) = 2^255 - 20 = p-1let a: [u8;32] = [0xec, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f];let mut b = FieldElement::from_bytes(&a);println!("zyd-before negate b:{:?}", b);b.conditional_negate(Choice::from(1)); //求倒数,用的是reduce结果,对应结果为:-(p-1) mod p = p+1println!("zyd--b:{:?}", b);println!("zyd--b.to_bytes():{:?}", b.to_bytes()); //to_bytes()函数会对结果进行再次module,(p+1) mod p = 1.

对应输出为:

zyd-before negate b:FieldElement51([2251799813685228, 2251799813685247, 2251799813685247, 2251799813685247, 2251799813685247])
zyd--b:FieldElement51([2251799813685230, 2251799813685247, 2251799813685247, 2251799813685247, 2251799813685247])
zyd--b.to_bytes():[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

4. 一些常量值表示

1)-1对应modulo值为2255-19-1=57896044618658097711785492504343953926634992332820282019728792003956564819948,用FieldElement51表示为:

FieldElement51([2251799813685228, 2251799813685247, 2251799813685247, 2251799813685247, 2251799813685247])
sage: 2251799813685228+2251799813685247*(2^51)+2251799813685247*(2^102)+2251799813685247*(2^153)+2251799813685247*(2^204)==2^255-19-1
True

2)0值对应FieldElement51表示为:

FieldElement51([ 0, 0, 0, 0, 0 ])

3)1值对应FieldElement51表示为:

FieldElement51([ 1, 0, 0, 0, 0 ])

4)16*p值对应FieldElement51表示为:

FieldElement51([ 36028797018963664, 36028797018963952, 36028797018963952, 36028797018963952, 36028797018963952 ])
sage: p=2^255-19
sage: 36028797018963664+36028797018963952*(2^51)+36028797018963952*(2^102)+36028
....: 797018963952*(2^153)+36028797018963952*(2^204)==16*p
True
sage:

这篇关于curve25519-dalek中field reduce原理分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/276103

相关文章

性能分析之MySQL索引实战案例

文章目录 一、前言二、准备三、MySQL索引优化四、MySQL 索引知识回顾五、总结 一、前言 在上一讲性能工具之 JProfiler 简单登录案例分析实战中已经发现SQL没有建立索引问题,本文将一起从代码层去分析为什么没有建立索引? 开源ERP项目地址:https://gitee.com/jishenghua/JSH_ERP 二、准备 打开IDEA找到登录请求资源路径位置

深入探索协同过滤:从原理到推荐模块案例

文章目录 前言一、协同过滤1. 基于用户的协同过滤(UserCF)2. 基于物品的协同过滤(ItemCF)3. 相似度计算方法 二、相似度计算方法1. 欧氏距离2. 皮尔逊相关系数3. 杰卡德相似系数4. 余弦相似度 三、推荐模块案例1.基于文章的协同过滤推荐功能2.基于用户的协同过滤推荐功能 前言     在信息过载的时代,推荐系统成为连接用户与内容的桥梁。本文聚焦于

hdu4407(容斥原理)

题意:给一串数字1,2,......n,两个操作:1、修改第k个数字,2、查询区间[l,r]中与n互质的数之和。 解题思路:咱一看,像线段树,但是如果用线段树做,那么每个区间一定要记录所有的素因子,这样会超内存。然后我就做不来了。后来看了题解,原来是用容斥原理来做的。还记得这道题目吗?求区间[1,r]中与p互质的数的个数,如果不会的话就先去做那题吧。现在这题是求区间[l,r]中与n互质的数的和

SWAP作物生长模型安装教程、数据制备、敏感性分析、气候变化影响、R模型敏感性分析与贝叶斯优化、Fortran源代码分析、气候数据降尺度与变化影响分析

查看原文>>>全流程SWAP农业模型数据制备、敏感性分析及气候变化影响实践技术应用 SWAP模型是由荷兰瓦赫宁根大学开发的先进农作物模型,它综合考虑了土壤-水分-大气以及植被间的相互作用;是一种描述作物生长过程的一种机理性作物生长模型。它不但运用Richard方程,使其能够精确的模拟土壤中水分的运动,而且耦合了WOFOST作物模型使作物的生长描述更为科学。 本文让更多的科研人员和农业工作者

MOLE 2.5 分析分子通道和孔隙

软件介绍 生物大分子通道和孔隙在生物学中发挥着重要作用,例如在分子识别和酶底物特异性方面。 我们介绍了一种名为 MOLE 2.5 的高级软件工具,该工具旨在分析分子通道和孔隙。 与其他可用软件工具的基准测试表明,MOLE 2.5 相比更快、更强大、功能更丰富。作为一项新功能,MOLE 2.5 可以估算已识别通道的物理化学性质。 软件下载 https://pan.quark.cn/s/57

衡石分析平台使用手册-单机安装及启动

单机安装及启动​ 本文讲述如何在单机环境下进行 HENGSHI SENSE 安装的操作过程。 在安装前请确认网络环境,如果是隔离环境,无法连接互联网时,请先按照 离线环境安装依赖的指导进行依赖包的安装,然后按照本文的指导继续操作。如果网络环境可以连接互联网,请直接按照本文的指导进行安装。 准备工作​ 请参考安装环境文档准备安装环境。 配置用户与安装目录。 在操作前请检查您是否有 sud

hdu4407容斥原理

题意: 有一个元素为 1~n 的数列{An},有2种操作(1000次): 1、求某段区间 [a,b] 中与 p 互质的数的和。 2、将数列中某个位置元素的值改变。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.Inpu

hdu4059容斥原理

求1-n中与n互质的数的4次方之和 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWrit

线性因子模型 - 独立分量分析(ICA)篇

序言 线性因子模型是数据分析与机器学习中的一类重要模型,它们通过引入潜变量( latent variables \text{latent variables} latent variables)来更好地表征数据。其中,独立分量分析( ICA \text{ICA} ICA)作为线性因子模型的一种,以其独特的视角和广泛的应用领域而备受关注。 ICA \text{ICA} ICA旨在将观察到的复杂信号

【软考】希尔排序算法分析

目录 1. c代码2. 运行截图3. 运行解析 1. c代码 #include <stdio.h>#include <stdlib.h> void shellSort(int data[], int n){// 划分的数组,例如8个数则为[4, 2, 1]int *delta;int k;// i控制delta的轮次int i;// 临时变量,换值int temp;in