o
    §ƒðj-  ã                   @   s˜   d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ dd	lmZ dd
lmZ G dd„ dƒZG dd„ dƒZdS )é    )Úoo)Úsymbols)ÚFiniteFieldÚQQÚRationalFieldÚFF)ÚPoly)Úsolve)Úis_sequence)Úas_inté   )Údivisors)Úpolynomial_congruencec                   @   sœ   e Zd ZdZd!dd„Zd"dd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd S )#ÚEllipticCurvea_  
    Create the following Elliptic Curve over domain.

    `y^{2} + a_{1} x y + a_{3} y = x^{3} + a_{2} x^{2} + a_{4} x + a_{6}`

    The default domain is ``QQ``. If no coefficient ``a1``, ``a2``, ``a3``,
    is given then it creates a curve with the following form:

    `y^{2} = x^{3} + a_{4} x + a_{6}`

    Examples
    ========

    References
    ==========

    .. [1] J. Silverman "A Friendly Introduction to Number Theory" Third Edition
    .. [2] https://mathworld.wolfram.com/EllipticDiscriminant.html
    .. [3] G. Hardy, E. Wright "An Introduction to the Theory of Numbers" Sixth Edition

    r   c                 C   sÚ  |dkrt }nt|ƒ}t|j|||||fƒ\}}}}}|| _|| _|d d|  }d| ||  }	|d d|  }
|d | d| |  || |  ||d   |d  }||	|
|f\| _| _| _| _	|d  | d|	d   d|
d   d| |	 |
  | _
|| _|| _|| _|| _|| _tdƒ\}}}|||| _| _| _t|d | || | |  || |d   |d  ||d  |  || |d   ||d   |d	�| _t| jtƒràd| _d S t| jtƒrëd | _d S d S )
Nr   é   é   é   é   é   é	   zx y z)Údomain)r   r   ÚmapÚconvertÚ_domainÚmodulusÚ_b2Ú_b4Ú_b6Ú_b8Ú_discrimÚ_a1Ú_a2Ú_a3Ú_a4Ú_a6r   ÚxÚyÚzr   Ú_polyÚ
isinstancer   Ú_rankr   )ÚselfÚa4Úa6Úa1Úa2Úa3r   r   Úb2Úb4Úb6Úb8r%   r&   r'   © r5   úž/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/sympy/ntheory/elliptic_curve.pyÚ__init__#   s2    88j

ÿzEllipticCurve.__init__r   c                 C   s   t |||| ƒS ©N©ÚEllipticCurvePoint)r+   r%   r&   r'   r5   r5   r6   Ú__call__?   s   zEllipticCurve.__call__c                 C   s–   t |ƒrt|ƒdkrd}n|d }|d d… \}}nt|tƒr+|j|j|j}}}ntdƒ‚| jdkr:|dkr:dS | j	 
| j|| j|| j|i¡dkS )Nr   r   zInvalid point.r   T)r
   Úlenr)   r:   r%   r&   r'   Ú
ValueErrorÚcharacteristicr(   Úsubs)r+   ÚpointÚz1Úx1Úy1r5   r5   r6   Ú__contains__B   s   
"zEllipticCurve.__contains__c                 C   ó
   | j  ¡ S r8   )r(   Ú__repr__©r+   r5   r5   r6   rF   Q   s   
zEllipticCurve.__repr__c                 C   s�   | j }|dkr	| S |dkrt| jd | jd | jd | jd�S | jd d| j  }| jd  d| j | j  d| j  }td| d	| | jd
�S )a<  
        Return minimal Weierstrass equation.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve

        >>> e1 = EllipticCurve(-10, -20, 0, -1, 1)
        >>> e1.minimal()
        Poly(-x**3 + 13392*x*z**2 + y**2*z + 1080432*z**3, x, y, z, domain='QQ')

        r   r   r   )r/   r   é   é$   éØ   iåÿÿÿiÊÿÿÿ)r   )r>   r   r   r   r   r   )r+   ÚcharÚc4Úc6r5   r5   r6   ÚminimalT   s   $&zEllipticCurve.minimalc                    sj   | j }tƒ }|dkr1t|ƒD ] ‰ | j | jˆ | jdi¡j}t||ƒ}| 	‡ fdd„|D ƒ¡ q|S t
dƒ‚)a5  
        Return points of curve over Finite Field.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e2 = EllipticCurve(1, 1, 1, 1, 1, modulus=5)
        >>> e2.points()
        {(0, 2), (1, 4), (2, 0), (2, 2), (3, 0), (3, 1), (4, 0)}

        r   c                 3   s   � | ]}ˆ |fV  qd S r8   r5   )Ú.0Únum©Úir5   r6   Ú	<genexpr>   s   € z'EllipticCurve.points.<locals>.<genexpr>zInfinitely many points)r>   ÚsetÚranger(   r?   r%   r'   Úexprr   Úupdater=   )r+   rK   Úall_ptÚcongruence_eqÚsolr5   rQ   r6   Úpointsk   s   
zEllipticCurve.pointsc                 C   sx   g }| j tkrt| j | j|¡ƒD ]	}| ||f¡ q|S | j | j|| jdi¡j}t	|| j
ƒD ]	}| ||f¡ q0|S )z7Returns points on the curve for the given x-coordinate.r   )r   r   r	   r(   r?   r%   Úappendr'   rV   r   r>   )r+   r%   Úptr&   rY   r5   r5   r6   Úpoints_x„   s   
ýzEllipticCurve.points_xc                 C   sÖ   | j dkr	tdƒ‚t | ¡g}t| j | jd| jdi¡ƒD ]}|j	r*| 
| |dƒ¡ qt| jdd�D ]6}t|d ƒ}|d |krht| j | j|| jdi¡ƒD ]}|j	sTqN| ||ƒ}| ¡ tkrg| || g¡ qNq2|S )al  
        Return torsion points of curve over Rational number.

        Return point objects those are finite order.
        According to Nagell-Lutz theorem, torsion point p(x, y)
        x and y are integers, either y = 0 or y**2 is divisor
        of discriminent. According to Mazur's theorem, there are
        at most 15 points in torsion collection.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e2 = EllipticCurve(-43, 166)
        >>> sorted(e2.torsion_points())
        [(-5, -16), (-5, 16), O, (3, -8), (3, 8), (11, -32), (11, 32)]

        r   z"No torsion point for Finite Field.r   T)Ú	generatorg      à?r   )r>   r=   r:   Úpoint_at_infinityr	   r(   r?   r&   r'   Úis_rationalr\   r   ÚdiscriminantÚintÚorderr   Úextend)r+   ÚlÚxxrR   ÚjÚpr5   r5   r6   Útorsion_points�   s$   
 € 
€zEllipticCurve.torsion_pointsc                 C   rE   )zè
        Return domain characteristic.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e2 = EllipticCurve(-43, 166)
        >>> e2.characteristic
        0

        )r   r>   rG   r5   r5   r6   r>   ´   ó   
zEllipticCurve.characteristicc                 C   s
   t | jƒS )zæ
        Return curve discriminant.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e2 = EllipticCurve(0, 17)
        >>> e2.discriminant
        -124848

        )rc   r   rG   r5   r5   r6   rb   Ä   rk   zEllipticCurve.discriminantc                 C   s
   | j dkS )zE
        Return True if curve discriminant is equal to zero.
        r   )rb   rG   r5   r5   r6   Úis_singularÔ   s   
zEllipticCurve.is_singularc                 C   s*   | j d d| j  }| j |d | j ¡S )zñ
        Return curve j-invariant.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e1 = EllipticCurve(-2, 0, 0, 1, 1)
        >>> e1.j_invariant
        1404928/389

        r   rH   r   )r   r   r   Úto_sympyr   )r+   rL   r5   r5   r6   Új_invariantÛ   s   zEllipticCurve.j_invariantc                 C   s   | j dkr	tdƒ‚t|  ¡ ƒS )zì
        Number of points in Finite field.

        Examples
        ========

        >>> from sympy.ntheory.elliptic_curve import EllipticCurve
        >>> e2 = EllipticCurve(1, 0, modulus=19)
        >>> e2.order
        19

        r   úStill not implemented)r>   ÚNotImplementedErrorr<   r[   rG   r5   r5   r6   rd   ì   s   
zEllipticCurve.orderc                 C   s   | j dur| j S tdƒ‚)zj
        Number of independent points of infinite order.

        For Finite field, it must be 0.
        Nro   )r*   rp   rG   r5   r5   r6   Úrankþ   s   
zEllipticCurve.rankN)r   r   r   r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r7   r;   rD   rF   rN   r[   r^   rj   Úpropertyr>   rb   rl   rn   rd   rq   r5   r5   r5   r6   r      s,    

$




r   c                   @   sd   e Zd Z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 )r:   a  
    Point of Elliptic Curve

    Examples
    ========

    >>> from sympy.ntheory.elliptic_curve import EllipticCurve
    >>> e1 = EllipticCurve(-17, 16)
    >>> p1 = e1(0, -4, 1)
    >>> p2 = e1(1, 0)
    >>> p1 + p2
    (15, -56)
    >>> e3 = EllipticCurve(-1, 9)
    >>> e3(1, -3) * 3
    (664/169, 17811/2197)
    >>> (e3(1, -3) * 3).order()
    oo
    >>> e2 = EllipticCurve(-2, 0, 0, 1, 1)
    >>> p = e2(-1,1)
    >>> q = e2(0, -1)
    >>> p+q
    (4, 8)
    >>> p-q
    (1, 0)
    >>> 3*p-5*q
    (328/361, -2800/6859)
    c                 C   s   t ddd| ƒS ©Nr   r   r9   )Úcurver5   r5   r6   r`   '  s   z$EllipticCurvePoint.point_at_infinityc                 C   sN   |j j}||ƒ| _||ƒ| _||ƒ| _|| _| jj | _ | j | ¡s%tdƒ‚d S )Nz%The curve does not contain this point)r   r   r%   r&   r'   Ú_curverD   r=   )r+   r%   r&   r'   rx   Údomr5   r5   r6   r7   +  s   



ÿzEllipticCurvePoint.__init__c                 C   sp  | j dkr|S |j dkr| S | j| j  | j| j  }}|j|j  |j|j  }}| jj}| jj}| jj}| jj}	| jj}
||krU|| ||  }|| ||  ||  }nC|| dkra|  	| j¡S d|d  d| |  |	 ||  || | d|   }|d  |	|  d|
  ||  || | d|   }|d ||  | | | }||  | | | }|  ||d¡S )Nr   r   r   r   )
r'   r%   r&   ry   r    r!   r"   r#   r$   r`   )r+   ri   rB   rC   Úx2Úy2r.   r/   r0   r,   r-   ÚslopeÚyintÚx3Úy3r5   r5   r6   Ú__add__5  s*   

86zEllipticCurvePoint.__add__c                 C   s    | j | j| jf|j |j|jfk S r8   )r%   r&   r'   ©r+   Úotherr5   r5   r6   Ú__lt__M  s    zEllipticCurvePoint.__lt__c                 C   sd   t |ƒ}|  | j¡}|dkr|S |dk r|  |  S | }|r0|d@ r&|| }|dL }|| }|s|S rw   )r   r`   ry   )r+   ÚnÚrri   r5   r5   r6   Ú__mul__P  s   üzEllipticCurvePoint.__mul__c                 C   s   | | S r8   r5   )r+   r…   r5   r5   r6   Ú__rmul___  s   zEllipticCurvePoint.__rmul__c                 C   s.   t | j| j | jj| j  | jj | j| jƒS r8   )r:   r%   r&   ry   r    r"   r'   rG   r5   r5   r6   Ú__neg__b  s   .zEllipticCurvePoint.__neg__c                 C   sX   | j dkrdS | jj}zd | | j¡| | j¡¡W S  ty#   Y nw d | j| j¡S )Nr   ÚOz({}, {}))r'   ry   r   Úformatrm   r%   r&   Ú	TypeError)r+   rz   r5   r5   r6   rF   e  s   
ÿzEllipticCurvePoint.__repr__c                 C   s   |   | ¡S r8   )r�   r‚   r5   r5   r6   Ú__sub__o  s   zEllipticCurvePoint.__sub__c                 C   s  | j dkrdS | jdkrdS | d }|j| j krdS d}| jtkrSt|jƒ|jkrQt|jƒ|jkrQ| | }|d7 }|j dkrA|S t|jƒ|jkrQt|jƒ|jks2tS |jj|jkr„|jj|jkr„| | }|d7 }|dkrotS |j dkrv|S |jj|jkr„|jj|jksatS )z5
        Return point order n where nP = 0.

        r   r   r   r   é   )r'   r&   r   r   rc   r%   r   Ú	numerator)r+   ri   rR   r5   r5   r6   rd   r  s2   


 
 ü
úzEllipticCurvePoint.orderN)rr   rs   rt   ru   Ústaticmethodr`   r7   r�   r„   r‡   rˆ   r‰   rF   r�   rd   r5   r5   r5   r6   r:   
  s    


r:   N)Úsympy.core.numbersr   Úsympy.core.symbolr   Úsympy.polys.domainsr   r   r   r   Úsympy.polys.polytoolsr   Úsympy.solvers.solversr	   Úsympy.utilities.iterablesr
   Úsympy.utilities.miscr   Úfactor_r   Úresidue_ntheoryr   r   r:   r5   r5   r5   r6   Ú<module>   s     