有限域内的平方根求解原理解析及curve25519-dalek中的实现
1. 引言
求解 x x x值,滿足:
x 2 ≡ a ( m o d p ) x^2 \equiv a\quad (mod \quad p) x2≡a(modp)
其中 p p p為odd素數。
需要做兩層判斷:
1)在有限域 p p p內,是否存在平方值為 a a a的解;
2)若存在解,則相應的解 x x x值如何計算?
對于1),可通過Legendre_symbol來判斷 a a a是否為quadratic residue modulo p.
對于2),可通過1917年的Pocklington算法來計算相應的 x x x值。
2. Legendre_symbol
在Montgomery curves 和 Curve25519滿足Montgomery curve特征的magma腳本驗證中有利用Legendre_symbol來判斷判斷B(A+2)、B(A-2)、A2-4 三者至少有一個是 module p的 quadratic residue。
可轉換為求Legendre symbol值來判斷,若該值為-1,則說明不是quadratic residue,若為1,則是quadratic residue。
3. Pocklington算法
若已確定 a a a為quadratic residue,在有限域 p p p內存在平方根解 x x x,可利用Pocklington算法來求解。
Pocklington對不同的有限域值 p p p分了以下三種情況進行處理:
1)若 p = 4 m + 3 , m ∈ N p=4m+3,m\in \mathbb{N} p=4m+3,m∈N,則解為 x ≡ ± a m + 1 x\equiv \pm a^{m+1} x≡±am+1.
2)若 p = 8 m + 5 , m ∈ N p=8m+5,m\in \mathbb{N} p=8m+5,m∈N,分情況為:
- 當 a 2 m + 1 ≡ 1 a^{2m+1}\equiv 1 a2m+1≡1時,則解為 x ≡ ± a m + 1 x\equiv \pm a^{m+1} x≡±am+1.
- 當 a 2 m + 1 ≡ ? 1 a^{2m+1}\equiv -1 a2m+1≡?1,且2為quadratic non-residue所以有 4 2 m + 1 ≡ ? 1 4^{2m+1}\equiv -1 42m+1≡?1,所以有 ( 4 a ) 2 m + 1 ≡ 1 (4a)^{2m+1}\equiv 1 (4a)2m+1≡1。 ? ( ± ( 4 a ) m + 1 ) 2 ≡ 4 a \Rightarrow (\pm(4a)^{m+1})^2\equiv 4a ?(±(4a)m+1)2≡4a,假設 y ≡ ( 4 a ) m + 1 y\equiv(4a)^{m+1} y≡(4a)m+1,則 y 為 y為 y為 y 2 ≡ 4 a y^2\equiv 4a y2≡4a的解。 ? x ≡ ± y / 2 或 者 當 y 為 奇 數 時 , x ≡ ± ( p + y ) / 2 \Rightarrow x\equiv \pm y/2或者當y為奇數時,x\equiv \pm (p+y)/2 ?x≡±y/2或者當y為奇數時,x≡±(p+y)/2.
3)若 p = 8 m + 1 p=8m+1 p=8m+1,假設 D ≡ ? a D\equiv -a D≡?a,轉換為求解 x 2 + D ≡ 0 x^2+D\equiv 0 x2+D≡0。尋找相應的 t 1 和 u 1 t_1和u_1 t1?和u1?值, N = t 1 2 ? D u 1 2 N=t_1^2-Du_1^2 N=t12??Du12?,使得 N N N值為quadratic non-residue,即相應的 L e g e n d r e S y m b o l ( N , p ) LegendreSymbol(N, p) LegendreSymbol(N,p)值應為-1。
進一步假設:
t n = ( t 1 + u 1 D ) n + ( t 1 ? u 1 D ) n 2 , u n = ( t 1 + u 1 D ) n ? ( t 1 ? u 1 D ) n 2 D t_n=\frac{(t_1+u_1\sqrt D)^n+(t_1-u_1\sqrt D)^n}{2}, u_n=\frac{(t_1+u_1\sqrt D)^n-(t_1-u_1\sqrt D)^n}{2\sqrt D} tn?=2(t1?+u1?D?)n+(t1??u1?D?)n?,un?=2D?(t1?+u1?D?)n?(t1??u1?D?)n?
從而使得以下等式均成立:
t m + n = t m t n + D u m u n , u m + n = t m u n + t n u m , t n 2 ? D u n 2 = N n t_{m+n}=t_mt_n+Du_mu_n, u_{m+n}=t_mu_n+t_nu_m, t_n^2-Du_n^2=N^n tm+n?=tm?tn?+Dum?un?,um+n?=tm?un?+tn?um?,tn2??Dun2?=Nn
注意,當 p = 8 m + 1 p=8m+1 p=8m+1成立時,其實 p = 4 m ′ + 1 p=4m'+1 p=4m′+1亦成立,從而有a為quadratic residue,且-a亦即D也為quadratic residue, ? 存 在 x ′ 使 得 x ′ 2 ≡ D 成 立 \Rightarrow 存在x'使得x'^2\equiv D成立 ?存在x′使得x′2≡D成立。具體magma腳本驗證如下:
注意有限域內有 t p ? 1 ≡ 1 t^{p-1}\equiv 1 tp?1≡1,所以有:
t p ≡ t 1 p ≡ t 1 , u p ≡ u 1 p D ( p ? 1 ) / 2 ≡ u 1 t_p\equiv t_1^p\equiv t_1, u_p\equiv u_1^pD^{(p-1)/2}\equiv u_1 tp?≡t1p?≡t1?,up?≡u1p?D(p?1)/2≡u1?
分別取 m = p ? 1 , n = 1 m=p-1,n=1 m=p?1,n=1,套用到上面的等式從而有:
t p ≡ t 1 ≡ t p ? 1 t 1 + D u p ? 1 u 1 , u p ≡ u 1 ≡ t p ? 1 u 1 + t 1 u p ? 1 t_p\equiv t_1\equiv t_{p-1}t_1+Du_{p-1}u_1,u_p\equiv u_1\equiv t_{p-1}u_1+t_1u_{p-1} tp?≡t1?≡tp?1?t1?+Dup?1?u1?,up?≡u1?≡tp?1?u1?+t1?up?1?
以上等式成立的解為 t p ? 1 ≡ 1 , u p ? 1 ≡ 0 t_{p-1}\equiv 1, u_{p-1}\equiv 0 tp?1?≡1,up?1?≡0。
因為 p p p為odd素數,所以有 p ? 1 = 2 r p-1=2r p?1=2r。
分別取 m = r , n = r m=r,n=r m=r,n=r,則有:
0 ≡ u p ? 1 ≡ u 2 r ≡ t r u r + t r u r ≡ 2 t r u r 0\equiv u_{p-1}\equiv u_{2r}\equiv t_ru_r+t_ru_r\equiv 2t_ru_r 0≡up?1?≡u2r?≡tr?ur?+tr?ur?≡2tr?ur?
? t r 或 者 u r 可 整 除 p . \Rightarrow t_r或者u_r可整除p. ?tr?或者ur?可整除p.
假設 u r u_r ur?可整除 p p p,即 u r ≡ 0 u_r\equiv 0 ur?≡0。若r為偶數,則取 r = 2 s , m = s , n = s r=2s, m=s,n=s r=2s,m=s,n=s,從而有 0 ≡ 2 t s u s 0\equiv 2t_su_s 0≡2ts?us?。但是并不是每一個 u i u_i ui?都可整除 p p p,如 u 1 u_1 u1?就不可以。若r為奇數,因 t r 2 ? D u r 2 = N r t_r^2-Du_r^2=N^r tr2??Dur2?=Nr等式成立而同時N要求為quadratic non-residue,所以 t r 2 t_r^2 tr2?也應為quadratic non-residue,從而自相矛盾。所以,這個循環將在特定的 l l l值處停止,使得 t l ≡ 0 t_l\equiv 0 tl?≡0,從而有:
? D u l 2 ≡ N l -Du_l^2\equiv N^l ?Dul2?≡Nl
因為 ? D -D ?D亦為quadratic residue而 N N N為quadratic non-residue,所以 l l l必須為偶數。假設 l = 2 k , m = k , n = k l=2k,m=k,n=k l=2k,m=k,n=k,從而有 0 ≡ t l ≡ t k 2 + D u k 2 0\equiv t_l\equiv t_k^2+Du_k^2 0≡tl?≡tk2?+Duk2?.
∵ x 2 ≡ ? D \because x^2\equiv-D ∵x2≡?D
∴ t k 2 + D u k 2 ≡ t k 2 ? x 2 u k 2 ≡ 0 \therefore t_k^2+Du_k^2\equiv t_k^2-x^2u_k^2\equiv 0 ∴tk2?+Duk2?≡tk2??x2uk2?≡0
∴ t k 2 ≡ x 2 u k 2 \therefore t_k^2\equiv x^2u_k^2 ∴tk2?≡x2uk2?
∴ u k x ≡ ± t k \therefore u_kx\equiv \pm t_k ∴uk?x≡±tk?
∴ x ≡ ± t k u k \therefore x\equiv \pm \frac{t_k}{u_k} ∴x≡±uk?tk??
4. curve25519 p=2255-19域sqrt_ratio_i實現
對于curve25519,其 p = 2 255 ? 19 p=2^{255}-19 p=2255?19,滿足以上第二個條件,即 p = 8 m + 5 , m ∈ N p=8m+5,m\in \mathbb{N} p=8m+5,m∈N。
- 當 a 2 m + 1 ≡ 1 a^{2m+1}\equiv 1 a2m+1≡1時,則解為 x ≡ ± a m + 1 x\equiv \pm a^{m+1} x≡±am+1.
- 當 a 2 m + 1 ≡ ? 1 a^{2m+1}\equiv -1 a2m+1≡?1,且2為quadratic non-residue所以有 4 2 m + 1 ≡ ? 1 4^{2m+1}\equiv -1 42m+1≡?1,所以有 ( 4 a ) 2 m + 1 ≡ 1 (4a)^{2m+1}\equiv 1 (4a)2m+1≡1。 ? ( ± ( 4 a ) m + 1 ) 2 ≡ 4 a \Rightarrow (\pm(4a)^{m+1})^2\equiv 4a ?(±(4a)m+1)2≡4a,假設 y ≡ ( 4 a ) m + 1 y\equiv(4a)^{m+1} y≡(4a)m+1,則 y 為 y為 y為 y 2 ≡ 4 a y^2\equiv 4a y2≡4a的解。 ? x ≡ ± y / 2 或 者 當 y 為 奇 數 時 , x ≡ ± ( p + y ) / 2 \Rightarrow x\equiv \pm y/2或者當y為奇數時,x\equiv \pm (p+y)/2 ?x≡±y/2或者當y為奇數時,x≡±(p+y)/2.
其中 m = ( p ? 5 ) / 8 , m + 1 = ( p + 3 ) / 8 m=(p-5)/8, m+1=(p+3)/8 m=(p?5)/8,m+1=(p+3)/8,同時有限域內任意值 a , 均 有 a p ? 1 ≡ 1 a,均有a^{p-1}\equiv 1 a,均有ap?1≡1。不難理解sqrt_ratio_i函數中的注釋,均只取非負數解:
// Using the same trick as in ed25519 decoding, we merge the// inversion, the square root, and the square test as follows.//// To compute sqrt(α), we can compute β = α^((p+3)/8).// Then β^2 = ±α, so multiplying β by sqrt(-1) if necessary// gives sqrt(α).//// To compute 1/sqrt(α), we observe that// 1/β = α^(p-1 - (p+3)/8) = α^((7p-11)/8)// = α^3 * (α^7)^((p-5)/8).//// We can therefore compute sqrt(u/v) = sqrt(u)/sqrt(v)// by first computing// r = u^((p+3)/8) v^(p-1-(p+3)/8)// = u u^((p-5)/8) v^3 (v^7)^((p-5)/8)// = (uv^3) (uv^7)^((p-5)/8).//// If v is nonzero and u/v is square, then r^2 = ±u/v,// so vr^2 = ±u.// If vr^2 = u, then sqrt(u/v) = r.// If vr^2 = -u, then sqrt(u/v) = r*sqrt(-1).//// If v is zero, r is also zero.// 并通過flipped_sign_sqrt_i 來判斷是否有解。替代 Legendre_symbol判斷。/// - `(Choice(1), +sqrt(u/v)) ` if `v` is nonzero and `u/v` is square;/// - `(Choice(1), zero) ` if `u` is zero;/// - `(Choice(0), zero) ` if `v` is zero and `u` is nonzero;/// - `(Choice(0), +sqrt(i*u/v))` if `u/v` is nonsquare (so `i*u/v` is square).///具體的代碼如下:
/// Given `FieldElements` `u` and `v`, compute either `sqrt(u/v)`/// or `sqrt(i*u/v)` in constant time.////// This function always returns the nonnegative square root.////// # Return////// - `(Choice(1), +sqrt(u/v)) ` if `v` is nonzero and `u/v` is square;/// - `(Choice(1), zero) ` if `u` is zero;/// - `(Choice(0), zero) ` if `v` is zero and `u` is nonzero;/// - `(Choice(0), +sqrt(i*u/v))` if `u/v` is nonsquare (so `i*u/v` is square).///pub fn sqrt_ratio_i(u: &FieldElement, v: &FieldElement) -> (Choice, FieldElement) {// Using the same trick as in ed25519 decoding, we merge the// inversion, the square root, and the square test as follows.//// To compute sqrt(α), we can compute β = α^((p+3)/8).// Then β^2 = ±α, so multiplying β by sqrt(-1) if necessary// gives sqrt(α).//// To compute 1/sqrt(α), we observe that// 1/β = α^(p-1 - (p+3)/8) = α^((7p-11)/8)// = α^3 * (α^7)^((p-5)/8).//// We can therefore compute sqrt(u/v) = sqrt(u)/sqrt(v)// by first computing// r = u^((p+3)/8) v^(p-1-(p+3)/8)// = u u^((p-5)/8) v^3 (v^7)^((p-5)/8)// = (uv^3) (uv^7)^((p-5)/8).//// If v is nonzero and u/v is square, then r^2 = ±u/v,// so vr^2 = ±u.// If vr^2 = u, then sqrt(u/v) = r.// If vr^2 = -u, then sqrt(u/v) = r*sqrt(-1).//// If v is zero, r is also zero.let v3 = &v.square() * v;let v7 = &v3.square() * v;let mut r = &(u * &v3) * &(u * &v7).pow_p58();let check = v * &r.square();let i = &constants::SQRT_M1;let correct_sign_sqrt = check.ct_eq( u);let flipped_sign_sqrt = check.ct_eq( &(-u));let flipped_sign_sqrt_i = check.ct_eq(&(&(-u)*i));let r_prime = &constants::SQRT_M1 * &r;r.conditional_assign(&r_prime, flipped_sign_sqrt | flipped_sign_sqrt_i);// Choose the nonnegative square root.let r_is_negative = r.is_negative();r.conditional_negate(r_is_negative);let was_nonzero_square = correct_sign_sqrt | flipped_sign_sqrt;(was_nonzero_square, r)}參考資料:
[1] https://math.stackexchange.com/questions/3280271/compute-sqrtx-pmod-p-working-on-finite-fields
[2] https://en.wikipedia.org/wiki/Pocklington's_algorithm
[3] https://en.wikipedia.org/wiki/Legendre_symbol
總結
以上是生活随笔為你收集整理的有限域内的平方根求解原理解析及curve25519-dalek中的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用画线插件如何删除线
- 下一篇: DW计时器