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

相关文章

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python主动抛出异常的各种用法和场景分析

《Python主动抛出异常的各种用法和场景分析》在Python中,我们不仅可以捕获和处理异常,还可以主动抛出异常,也就是以类的方式自定义错误的类型和提示信息,这在编程中非常有用,下面我将详细解释主动抛... 目录一、为什么要主动抛出异常?二、基本语法:raise关键字基本示例三、raise的多种用法1. 抛

github打不开的问题分析及解决

《github打不开的问题分析及解决》:本文主要介绍github打不开的问题分析及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、找到github.com域名解析的ip地址二、找到github.global.ssl.fastly.net网址解析的ip地址三

Mysql的主从同步/复制的原理分析

《Mysql的主从同步/复制的原理分析》:本文主要介绍Mysql的主从同步/复制的原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录为什么要主从同步?mysql主从同步架构有哪些?Mysql主从复制的原理/整体流程级联复制架构为什么好?Mysql主从复制注意

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

java -jar命令运行 jar包时运行外部依赖jar包的场景分析

《java-jar命令运行jar包时运行外部依赖jar包的场景分析》:本文主要介绍java-jar命令运行jar包时运行外部依赖jar包的场景分析,本文给大家介绍的非常详细,对大家的学习或工作... 目录Java -jar命令运行 jar包时如何运行外部依赖jar包场景:解决:方法一、启动参数添加: -Xb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

apache的commons-pool2原理与使用实践记录

《apache的commons-pool2原理与使用实践记录》ApacheCommonsPool2是一个高效的对象池化框架,通过复用昂贵资源(如数据库连接、线程、网络连接)优化系统性能,这篇文章主... 目录一、核心原理与组件二、使用步骤详解(以数据库连接池为例)三、高级配置与优化四、典型应用场景五、注意事

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.