3 L]$@sdZddlmZmZmZmZddlmZGdddZGdddZ e d d d Z ed d$d Z e e d ddZ edd%dZ e e dddZedd&dZe edddZedd'dZe edddZedd(d Ze ed!d"d#Zd S))a Classes and objects to represent prime-field elliptic curves and points on them. Exports the following items: - PrimeCurve() - PrimePoint() - SECP192R1_CURVE - SECP192R1_BASE_POINT - SECP224R1_CURVE - SECP224R1_BASE_POINT - SECP256R1_CURVE - SECP256R1_BASE_POINT - SECP384R1_CURVE - SECP384R1_BASE_POINT - SECP521R1_CURVE - SECP521R1_BASE_POINT The curve constants are all PrimeCurve() objects and the base point constants are all PrimePoint() objects. Some of the following source code is derived from http://webpages.charter.net/curryfans/peter/downloads.html, but has been heavily modified to fit into this projects lint settings. The original project license is listed below: Copyright (c) 2014 Peter Pearson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. )unicode_literalsdivisionabsolute_importprint_function) inverse_modc@s eZdZdZddZddZdS) PrimeCurvezc Elliptic curve over a prime field. Characteristic two field curves are not supported. cCs||_||_||_dS)a The curve of points satisfying y^2 = x^3 + a*x + b (mod p) :param p: The prime number as an integer :param a: The component a as an integer :param b: The component b as an integer N)pab)selfr r r r B/tmp/pip-install-wfra5znf/asn1crypto/asn1crypto/_elliptic_curve.py__init__<szPrimeCurve.__init__cCsB|j|j}|j|j|j}|||j|j|j|jdkS)z~ :param point: A Point object :return: Boolean if the point is on this curve r)yxr r r )r Zpointy2x3r r rcontainsNs zPrimeCurve.containsN)__name__ __module__ __qualname____doc__rrr r r rr6src@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS) PrimePointz1 A point on a prime-field elliptic curve NcCsR||_||_||_||_|jr2|jj|s2td|jrN||jtkrNtddS)a) :param curve: A PrimeCurve object :param x: The x coordinate of the point as an integer :param y: The y coordinate of the point as an integer :param order: The order of the point, as an integer - optional zInvalid EC pointN)curverrorderr ValueErrorINFINITY)r rrrrr r rras zPrimePoint.__init__cCs0|j|jkr(|j|jkr(|j|jkr(dSdSdS)zy :param other: A PrimePoint object :return: 0 if identical, 1 otherwise rrN)rrr)r otherr r r__cmp__~s$zPrimePoint.__cmp__cCs|tkr |S|tkr|S|j|jks(t|j|jkrX|j|j|jjdkrPtS|jS|jj}|j|jt|j|j||}|||j|j|}||j||j|}t|j||S)zq :param other: A PrimePoint object :return: A PrimePoint object r) rrAssertionErrorrrr doublerr)r rr l_ry3r r r__add__s  "zPrimePoint.__add__cCsdd}|}|jr||j}|dkr(tS|tkr4tS|dks@td|}t|j|j|j |j}||d}|}x\|dkr|j}||@dkr||@dkr||}||@dkr||@dkr||}|d}qrW|S)z :param other: An integer to multiple the Point by :return: A PrimePoint object cSs.|dks td}x||kr$d|}qW|dS)Nrr)r )rresultr r r leftmost_bits    z(PrimePoint.__mul__..leftmost_bitrr%r)rrr rrrrr!)r rr'eZe3Z negative_selfir&r r r__mul__s*      zPrimePoint.__mul__cCs||S)z :param other: An integer to multiple the Point by :return: A PrimePoint object r )r rr r r__rmul__s zPrimePoint.__rmul__cCst|jj}|jj}d|j|j|td|j||}||d|j|}||j||j|}t|j||S)zS :return: A PrimePoint object that is twice this point r(r%)rr r rrrr)r r r r"rr#r r rr!s (zPrimePoint.double)N) rrrrrrr$r+r,r!r r r rr\s  + rNl r(l 9{uDjSg9g(Bl +'1t:_|v!a:@ml H<^W]dZ{cxW\Iql 1(i&^#a;l?lF eY8 w-X"PVd/%PP!-l!"X!#BXtJ9!'|%VA-l4~ f&Dv@h!fE0m9_ qlM/l=*8%(?l?@lK`Opq^cv 3,e< 1U]>{|R*ZlB11e %:f=K`wrH7gHK8hklQ~o]l+fUg+<)Z?8O?q!OlQ%x +Ohbi+}s@l~l*'#.TEbc+Z'@=D 1 "(?7N2Z_+|S/1fl dxRjoyU8T( :ss"nZL8k&"_Ul_!uR/sX0 @qaNQNB&JxS8KJEY K%lls)e`gwl X_[nlv|l#l#?VQ(zO%b95~cte1oR{V;LH w>l-rZE]"Sr&Ga9}*Fl#f=xK)H-apY$3^Q n%k{;/K!u{4-{?$Od8V1l3s: l#Pf?QE$XN!85aZU WL9YLhz f$Du13otc!% pMxjRr`l# dp"z\}[z3"nZ;PK# `7roCQr-r-r-r-)r __future__rrrr_intrrrrZSECP192R1_CURVEZSECP192R1_BASE_POINTZSECP224R1_CURVEZSECP224R1_BASE_POINTZSECP256R1_CURVEZSECP256R1_BASE_POINTZSECP384R1_CURVEZSECP384R1_BASE_POINTZSECP521R1_CURVEZSECP521R1_BASE_POINTr r r r/sf &