o
    e{                     @   sx  d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ G dd de jZG d	d
 d
e jZG dd de jZG dd de jZG dd de jZeddddd dd dd dpig ZeD ]ZejejejfddZeedej e qlG dd de jZedddd d d!d d"d dpg ZeD ]Zejejejfd#dZeedej e qG d$d% d%e jZedd&d'd(d d)d d*d dpg ZeD ]Zejejejfd+dZeedej e qG d,d- d-e jZG d.d/ d/e jZG d0d1 d1e jZ G d2d3 d3e jZ!G d4d5 d5e jZ"G d6d7 d7e jZ#i fd8d9Z$e%d:kr:d;d Z&e j'd<d= dS dS )>    N)list_test_cases)load_test_vectors)ECC)EccPoint_curvesEccKey)Integerc                   @   s   e Zd Zdd Zdd ZdS )TestEccPointc              
   C   s   t jddj}t jddj}z||  J  ty. } zdt|v s$J W Y d }~nd }~ww z||7 }J  tyP } zdt|v sEJ W Y d }~d S d }~ww )NP-256curveP-384Fznot on the same curve)r   generatepointQ
ValueErrorstr)selfp1p2e r   H/usr/lib/python3/dist-packages/Cryptodome/SelfTest/PublicKey/test_ECC.pytest_mix+   s    zTestEccPoint.test_mixc                 C   s&   t jddddd}| t|d d S )Nr
   l   }	&_n i9]}!5$7P%^-& l   -
k%j&*/n;[jH**7 
i/v{-l   ]W][@iMmlQ;i?XU,p )r   dpoint_xpoint_ya  EccKey(curve='NIST P-256', point_x=20573031766139722500939782666697015100983491952082159880539639074939225934381, point_y=108863130203210779921520632367477406025152638284581252625277850513266505911389, d=75467964919405407085864614198393977741148485328036093939970922195112333446269))r   	constructassertEqualrepr)r   r   r   r   r   	test_repr<   s   zTestEccPoint.test_reprN)__name__
__module____qualname__r   r   r   r   r   r   r	   )   s    r	   c                   @   sl   e Zd ZdZeddZeddZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestEccPoint_NIST_P256zOTests defined in section 4.3 of https://www.nsa.gov/ia/_files/nist-routines.pdfl   ,;zQCl:a*N<xn.h&#/_"$^ l   V"`,1kWypYLG+-pJ|?W@ l   kkdg\[W9r*9.B,LI5/f:hcXUl   Cdc'2gs1E+.-'a!Tc                 C   (   t dd}|| j | || j d S Nr   r   setpointSr   r   pointWr   r   r   test_setO      
zTestEccPoint_NIST_P256.test_setc                 C   F   | j  }| || j  || j | || j | | j | j d S Nr(   copyr   r'   pointTassertNotEqualr)   r   r   r   	test_copyT   
   
z TestEccPoint_NIST_P256.test_copyc                 C   (   | j  }| j | }| || j   d S r.   r(   r   point_at_infinityr   negSsumr   r   r   test_negate[      
z"TestEccPoint_NIST_P256.test_negatec                 C      d}d}| j | j }| |j| | |j| | }| j | }| || j  || j  }| || j  || }| || d S Nl   ~(iM)E=kdHpit3&EpmRrl   dBhE@	hFTVL.E$udK].X r(   r1   r   xyr7   r   pointRxpointRypointRpair   r   r   test_addition`      

z$TestEccPoint_NIST_P256.test_additionc                 C      d}d}| j  }|| j7 }| |j| | |j| | }| j  }||7 }| || j  | }|| j 7 }| || j  | }||7 }| || d S r>   r(   r0   r1   r   r@   rA   r7   rB   r   r   r   test_inplace_additionv       



z,TestEccPoint_NIST_P256.test_inplace_additionc                 C      d}d}| j  }|  | |j| | |j| | j  }| }|  | || | j  }||7 }| |j| | |j| d S )Nl   '6?0T.<63GwG:]Hsivl   P6e#` {yO8^g^ w{9lJ@z r(   r0   doubler   r@   rA   r7   rB   r   r   r   test_doubling      


z$TestEccPoint_NIST_P256.test_doublingc                    s   d}d}d} j | } |j|  |j|  j  } j d } ||  t fdd | j  } |j|  |j| t| j  } |j|  |j| d S )N   _,)N$chKf-5lk<Xk#E l   ? nDf>1x066OPKFQl   ATU*-sX)>~|N\}T9%Dbu4ur   c                      
    j d S Nr(   r   r   r   r   <lambda>      
 z=TestEccPoint_NIST_P256.test_scalar_multiply.<locals>.<lambda>)r(   r   r@   rA   r7   assertRaisesr   r   r   r   rC   rD   rE   rF   r   rW   r   test_scalar_multiply   s    



z+TestEccPoint_NIST_P256.test_scalar_multiplyc                 C   N   d}d}d}d}| j | }| j | | j|  }| |j| | |j| d S )NrR   l   T:XuMKt^5~{"/T3G1S l   vbaB>Y5OgEI# d3ZgX l   uR`/.5ES6p* Pr r(   r1   r   r@   rA   r   r   r   rC   rD   trE   r   r   r   test_joing_scalar_multiply      
z1TestEccPoint_NIST_P256.test_joing_scalar_multiplyc                 C   (   |  | j d |  | j d d S )N       r   r(   size_in_bitssize_in_bytesrW   r   r   r   
test_sizes      z!TestEccPoint_NIST_P256.test_sizesNr    r!   r"   __doc__r   r(   r1   r+   r3   r;   rG   rK   rP   r\   ra   ri   r   r   r   r   r#   D   s&    r#   c                   @   p   e Zd ZdZedddZedddZdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestEccPoint_NIST_P384zOTests defined in section 4.4 of https://www.nsa.gov/ia/_files/nist-routines.pdfl   K<L3YUH[Am]8$_-'DM./8;V~Ho\"l   E^PjWvM0M7bKL84\mn&J

"[!66p#y|@z~W~ p384l   Q'"f4OcE-XJ9ija>:}@sMLUl   P}&zIy$GY9Q%?pa(Fv|EO{ c                 C   s*   t ddd}|| j | || j d S )Nr   ro   r&   r)   r   r   r   r+      s   zTestEccPoint_NIST_P384.test_setc                 C   r-   r.   r/   r)   r   r   r   r3      r4   z TestEccPoint_NIST_P384.test_copyc                 C   r5   r.   r6   r8   r   r   r   r;      r<   z"TestEccPoint_NIST_P384.test_negatec                 C   r=   )Nl   ^\mLH\%sz.	_QHp)wB'2h\|Sz9Y07s\\% l   $	rQ4 1ml w{g~OM wr6bLLU;8*$0xrs, r?   rB   r   r   r   rG      rH   z$TestEccPoint_NIST_P384.test_additionc                 C   rI   r>   rJ   rB   r   r   r   _test_inplace_addition  rL   z-TestEccPoint_NIST_P384._test_inplace_additionc                 C   rM   )Nl   LPeroBr0_nCYw~s7xI|Z:Ze*X!T l   ]e^A|\"Rn0WWCs(h}	c5JBNnN=.rN   rB   r   r   r   rP     rQ   z$TestEccPoint_NIST_P384.test_doublingc                    j   d}d}d} j | } |j|  |j|  j  } j d } ||  t fdd d S )N   4.v0Z:z;ODK#Gv)Z5mXCDs:frkIl   R@dV]9"O/'5r}]~AYwHoY4-?~wl   f9;Y?5.BieXcXtqMDb7K6X}1 }Xr   c                      rS   rT   rV   r   rW   r   r   rX   B  rY   z=TestEccPoint_NIST_P384.test_scalar_multiply.<locals>.<lambda>r(   r   r@   rA   r7   rZ   r   r[   r   rW   r   r\   3     


z+TestEccPoint_NIST_P384.test_scalar_multiplyc                 C   r]   )Nrr   l   ]8|$W!&%TxFgqeYTKeM80,YO_l   tdvO*;q/$L]0	}pn<x&"FFe5hYE~"l   q!jB%~^Y7yK\+5{SWxFJR1DwJA6g)4 r^   r_   r   r   r   ra   D  rb   z1TestEccPoint_NIST_P384.test_joing_scalar_multiplyc                 C   rc   )Ni  0   rf   rW   r   r   r   ri   P  rj   z!TestEccPoint_NIST_P384.test_sizesN)r    r!   r"   rl   r   r(   r1   r+   r3   r;   rG   rp   rP   r\   ra   ri   r   r   r   r   rn      s*    rn   c                   @   rm   )TestEccPoint_NIST_P521zOTests defined in section 4.5 of https://www.nsa.gov/ia/_files/nist-routines.pdfl#   A5;LK5N}W,*n<UP%hA=Cb?iOtiL1!@vlv'Wl#   #5kS7?I]*[nnkYi[7.	 d9MT3X=TP5_Icc	np521l#   GJo7 $=?>D^`bXkNo	tG`xw-dg!;I7>S{) wh\.,#l#   cae.tDffI*"a%A&4h#Ww5%D2>n<!}6@fNvF$>8vpc                 C   r$   r%   r&   r)   r   r   r   r+   b  r,   zTestEccPoint_NIST_P521.test_setc                 C   r-   r.   r/   r)   r   r   r   r3   g  r4   z TestEccPoint_NIST_P521.test_copyc                 C   r5   r.   r6   r8   r   r   r   r;   n  r<   z"TestEccPoint_NIST_P521.test_negatec                 C   r=   Nl#   Y[V=xCmuOFl*sg	Np)giD>u{9gh0!cK-Op-^Nl#   U,R&9/@a}#>;cd{cl@E'98kh~a7?!-(O+Sr?   rB   r   r   r   rG   s  rH   z$TestEccPoint_NIST_P521.test_additionc                 C   rI   rx   rJ   rB   r   r   r   rK     rL   z,TestEccPoint_NIST_P521.test_inplace_additionc                 C   rM   )Nl#   k'<J cmtW9T@%T$SD!kdlPgciSW_%3byF`($/rl#   C5APS?( w8OoXx
e\**L3tX@7N&xrZ7}r.8QIarN   rB   r   r   r   rP     rQ   z$TestEccPoint_NIST_P521.test_doublingc                    rq   )N#   \??7gf@YKjrR)P6_r	[<lSfPL3ZM
TUU3TM|K\xl#   QUx2?z=[nWq0 6&7mnktztX;Ia^Rm<9-Te	bFl#   [}5]v3nLIYjzQ`:Gk`O<Y[w `5Q**e7U5qr   c                      rS   rT   rV   r   rW   r   r   rX     rY   z=TestEccPoint_NIST_P521.test_scalar_multiply.<locals>.<lambda>rs   r[   r   rW   r   r\     rt   z+TestEccPoint_NIST_P521.test_scalar_multiplyc                 C   sR   d}d}d}d}| j | }| j | }|| j| 7 }| |j| | |j| d S )Nry   l#   |TB)I%_
#wUV6C9c  1z7bnl<#qI"fu#0	Xajpx8=nMl#   =etEK9JBadZYx
./jVa@3hMhA]_`s}J+dptl#   XM`Wi&GgwH"!e+oeG$,oQ Tk9P>@5{U':c0'=NDH? r^   r_   r   r   r   ra     s   

z1TestEccPoint_NIST_P521.test_joing_scalar_multiplyc                 C   rc   )Ni	  B   rf   rW   r   r   r   ri     rj   z!TestEccPoint_NIST_P521.test_sizesNrk   r   r   r   r   rv   U  s*    rv   c                   @   (   e Zd ZdZed ZeejejdZ	dS )TestEccPoint_PAI_P2569Test vectors from http://point-at-infinity.org/ecc/nisttvp256N
r    r!   r"   rl   r   r   r   GxGypointGr   r   r   r   r|         r|   )	PublicKeyr   zpoint-at-infinity.org-P256.txtz&P-256 tests from point-at-infinity.orgc                 C      t | S r.   intkr   r   r   rX         rX   c                 C   
   t | dS N   r   r@   r   r   r   rX     rY   c                 C   r   r   r   rA   r   r   r   rX     rY   )r   r@   rA   c                 C   *   | j | }| |j| | |j| d S r.   r   r   r@   rA   r   scalarr@   rA   resultr   r   r   new_test     
r   ztest_%dc                   @   r{   )TestEccPoint_PAI_P384r}   ro   Nr   r   r   r   r   r     r   r   zpoint-at-infinity.org-P384.txtz&P-384 tests from point-at-infinity.orgc                 C   r   r.   r   r   r   r   r   rX     r   c                 C   r   r   r   r   r   r   r   rX     rY   c                 C   r   r   r   r   r   r   r   rX     rY   c                 C   r   r.   r   r   r   r   r   r     r   c                   @   r{   )TestEccPoint_PAI_P521r}   rw   Nr   r   r   r   r   r     r   r   zpoint-at-infinity.org-P521.txtz&P-521 tests from point-at-infinity.orgc                 C   r   r.   r   r   r   r   r   rX     r   c                 C   r   r   r   r   r   r   r   rX     rY   c                 C   r   r   r   r   r   r   r   rX     rY   c                 C   r   r.   r   r   r   r   r   r     r   c                   @   <   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd ZdS )TestEccKey_P256c                 C   s   t ddd}| |jd | |  | |jjtd j | |jj	td j
 ttd jtd j
}t dd|d}| |jd | |  | |j| t ddd}t ddd}d S )Nr
      r   r   r~   r   r   point	secp256r1
prime256v1)r   r   r   
failUnlesshas_privater   r@   r   r   rA   r   r   )r   keyr   r   r   r   test_private_key  s   z TestEccKey_P256.test_private_keyc                 C   sB   t td jtd j}td|d}| |  | |j| d S )Nr~   r
   r   r   )	r   r   r   r   r   failIfr   r   r   )r   r   r   r   r   r   test_public_key.  s   zTestEccKey_P256.test_public_keyc                 C   6   t ddd}| }| |  | |j|j d S )Nr
      r   r   
public_keyr   r   r   r   r   priv_keypub_keyr   r   r   test_public_key_derived5     z'TestEccKey_P256.test_public_key_derivedc                 C      |  tdd  d S )Nc                   S      t dddS )NzP-257r   r   r   r   r   r   r   rX   =      z4TestEccKey_P256.test_invalid_curve.<locals>.<lambda>rZ   r   rW   r   r   r   test_invalid_curve<     z"TestEccKey_P256.test_invalid_curvec                 C   $   |  tdd  |  tdd  d S )Nc                   S   r   )Nr
   r   r   r   r   r   r   r   rX   @  r   z0TestEccKey_P256.test_invalid_d.<locals>.<lambda>c                   S      t dtd jdS )Nr
   r~   r   r   r   orderr   r   r   r   rX   A  s    r   rW   r   r   r   test_invalid_d?     zTestEccKey_P256.test_invalid_dc                 C      t jddd}t jddd}t jddd}| }| }| }| || | || | || | || | || d S )Nr   r
   r   r      r   r   r   r   r2   r   private_keyprivate_key2private_key3r   public_key2public_key3r   r   r   test_equalityC     zTestEccKey_P256.test_equalityN	r    r!   r"   r   r   r   r   r   r   r   r   r   r   r     s    r   c                   @   r   )TestEccKey_P384c                 C      t d }tddd}| |jd | |  | |jj|j | |jj	|j
 t|j|j
d}tdd|d}| |jd | |  | |j| tddd}tddd}tddd}d S )Nro   r   r   r   r   	secp384r1
prime384v1r   r   r   r   r   r   r   r@   r   rA   r   r   )r   ro   r   r   r   r   r   r   X     z TestEccKey_P384.test_private_keyc                 C   D   t d }t|j|jd}td|d}| |  | |j| d S )Nro   r   r   	r   r   r   r   r   r   r   r   r   )r   ro   r   r   r   r   r   r   m  
   zTestEccKey_P384.test_public_keyc                 C   r   )Nr   r   r   r   r   r   r   r   r   u  r   z'TestEccKey_P384.test_public_key_derivedc                 C   r   )Nc                   S   r   )NzP-385r   r   r   r   r   r   r   rX   }  r   z4TestEccKey_P384.test_invalid_curve.<locals>.<lambda>r   rW   r   r   r   r   |  r   z"TestEccKey_P384.test_invalid_curvec                 C   r   )Nc                   S   r   )Nr   r   r   r   r   r   r   r   rX     r   z0TestEccKey_P384.test_invalid_d.<locals>.<lambda>c                   S   r   )Nr   ro   r   r   r   r   r   r   rX         r   rW   r   r   r   r     r   zTestEccKey_P384.test_invalid_dc                 C   r   )Nr   r   r   r   r   r   r   r   r   r     r   zTestEccKey_P384.test_equalityNr   r   r   r   r   r   V      r   c                   @   r   )TestEccKey_P521c                 C   r   )Nrw   P-521r   r   r   	secp521r1
prime521v1r   )r   rw   r   r   r   r   r   r     r   z TestEccKey_P521.test_private_keyc                 C   r   )Nrw   r   r   r   )r   rw   r   r   r   r   r   r     r   zTestEccKey_P521.test_public_keyc                 C   r   )Nr   r   r   r   r   r   r   r   r     r   z'TestEccKey_P521.test_public_key_derivedc                 C   r   )Nc                   S   r   )NzP-522r   r   r   r   r   r   r   rX     r   z4TestEccKey_P521.test_invalid_curve.<locals>.<lambda>r   rW   r   r   r   r     r   z"TestEccKey_P521.test_invalid_curvec                 C   r   )Nc                   S   r   )Nr   r   r   r   r   r   r   r   rX     r   z0TestEccKey_P521.test_invalid_d.<locals>.<lambda>c                   S   r   )Nr   rw   r   r   r   r   r   r   rX     r   r   rW   r   r   r   r     r   zTestEccKey_P521.test_invalid_dc                 C   r   )Nr   r   r   r   r   r   r   r   r   r     r   zTestEccKey_P521.test_equalityNr   r   r   r   r   r     r   r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )TestEccModule_P256c                 C   s^   t jdd}| |  | |jttd jtd j	|j
 d t jdd t jdd d S )Nr
   r   r~   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   test_generate  s   z TestEccModule_P256.test_generatec                 C   s   t jddd}| |  | |jtd j t jdtd jtd j	d}| 
|  | |jtd j t jddd t jddd t jddd d S )Nr
   r   r   r~   r   r   r   r   r   )r   r   r   r   r   r   r   Gr   r   r   r   r   r   r   test_construct  s   z!TestEccModule_P256.test_constructc                 C   ^   t ddd}t td jtd jd}| jttjfddi| | jttjfddd| d S )	N
   r   r   r   r~   r   r
      r   dictr   r   r   rZ   r   r   r   r   coordcoordGr   r   r   test_negative_construct      z*TestEccModule_P256.test_negative_constructNr    r!   r"   r   r   r   r   r   r   r   r     s    r   c                   @   r   )TestEccModule_P384c                 C   ^   t d }tjdd}| |  | |jt|j|j	d|j
  tjdd tjdd d S )Nro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r          z TestEccModule_P384.test_generatec                 C      t d }tjddd}| |  | |jt d j tjd|j|j	d}| 
|  | |j|j tjddd tjddd tjddd d S )Nro   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r        z!TestEccModule_P384.test_constructc                 C   r   )	Nr   r   r   ro   r   r   r   r   r   r   r   r   r   r     r   z*TestEccModule_P384.test_negative_constructNr   r   r   r   r   r         r   c                   @   r   )TestEccModule_P521c                 C   r   )Nrw   r   r   r   r   r   r   r   r   r   r   %  r   z TestEccModule_P521.test_generatec                 C   r   )Nrw   r   r   r   r   r   r   r   r   r   r   r   r   0  r   z!TestEccModule_P521.test_constructc                 C   r   )	Nr   r   r   rw   r   r   r   r   r   r   r   r   r   r   @  r   z*TestEccModule_P521.test_negative_constructNr   r   r   r   r   r   #  r   r   c                 C   s   g }|t t7 }|t t7 }|t t7 }|t t7 }|t t7 }|t t7 }|t t7 }|t t7 }|t t	7 }|t t
7 }|t t7 }|t t7 }|t t7 }|S r.   )r   r	   r#   rn   rv   r|   r   r   r   r   r   r   r   r   )configtestsr   r   r   	get_testsH  s   r   __main__c                   C   s   t t S r.   )unittest	TestSuiter   r   r   r   r   rX   Z  r   suite)defaultTest)(r   timeCryptodome.SelfTest.st_commonr   Cryptodome.SelfTest.loaderr   Cryptodome.PublicKeyr   Cryptodome.PublicKey.ECCr   r   r   Cryptodome.Math.Numbersr   TestCaser	   r#   rn   rv   r|   tv_paitvr   r@   rA   r   setattrcountr   r   r   r   r   r   r   r   r   r    r   mainr   r   r   r   <module>   s      <AA&%%
