{"id":118,"date":"2019-10-07T11:16:15","date_gmt":"2019-10-07T09:16:15","guid":{"rendered":"http:\/\/blog.himitsu.eu\/?p=118"},"modified":"2020-01-18T11:09:56","modified_gmt":"2020-01-18T10:09:56","slug":"elliptic-curves-and-how-to-use-them-part-three","status":"publish","type":"post","link":"https:\/\/blog.himitsu.eu\/?p=118","title":{"rendered":"Elliptic curves and how to use them (part three)"},"content":{"rendered":"\r\n<p><a href=\"http:\/\/blog.himitsu.eu\/?p=80\">Go to part two<\/a><\/p>\r\n<p>Modern popular processors are able to accurately and efficiently handle integer operations. Therefore, when implementing actions on elliptic curves, it is worth using this property. The type of elliptic curve that is best suited for use in a software solution will be discussed below.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s choose a certain number p, which is a prime number. The bigger this number is the better safety will be assured. For ease of understanding, I suggest choosing number 7 for our next examples.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s create an algebraic structure consisting of a set of numbers {0, p-1} and an operation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> such that for a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b = (a + b) modulo p. We see that the operation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> is well defined &#8211; modulo p always returns an unequivocal result in the range {0, p-1}. Let&#8217;s check now whether this structure is a group. Operation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> is associative: <br \/>(a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b) <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> c = ((a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b) + c) modulo p = (((a + b) modulo p) + c) modulo p = (a + b + c) modulo p = (a + ((b + c) modulo p)) modulo p = (a + (b <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> c)) modulo p = a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> (b <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> c).<\/p>\r\n<p>The neutral element (as in the normal addition) is 0: <br \/>0 <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> a = (0 + a) modulo p = (a) modulo p = a<\/p>\r\n<p>Every element have got also an inverse element: <br \/><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=a%2B_p%5C%3B%5C%3Ba%5E%7B-1%7D\" alt=\"a+_p\\;\\;a^{-1}\" align=\"absmiddle\" \/> = (a + (p &#8211; a)) modulo p = (p) modulo = 0<\/p>\r\n<p>The operation is also commutative: <br \/>a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b = (a + b) modulo p = (b + a) modulo p = b <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> a<\/p>\r\n<p>So the structure ({0, p-1}, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/>) is an abelian group.<\/p>\r\n<p>Let&#8217;s add the second operation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> (a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> b = (a * b) modulo p) to this structure and see if the structure is a field. The first of the three conditions is met above (({0, p-1}, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/>) is a group). Let&#8217;s check if ({1, p-1}, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/>) is also a group.<\/p>\r\n<p>The <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> operation is associative, which can be proved exactly as for <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/>.<br \/>The neutral element (as in normal multiplication) is 1: <br \/>1 <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> a = (1 * a) modulo p = (a) modulo p = a<\/p>\r\n<p>Every element have got also an inverse element: <br \/>1 = <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=a%5C%3B*_p%5C%3B%5C%3B%5C%3Ba%5E%7B-1%7D\" alt=\"a\\;*_p\\;\\;\\;a^{-1}\" align=\"absmiddle\" \/> = (<img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=a%5C%3B*%5C%3Ba%5E%7B-1%7D\" alt=\"a\\;*\\;a^{-1}\" align=\"absmiddle\" \/>) modulo p, so from modulo we have <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=a%5C%3B*%5C%3Ba%5E%7B-1%7D\" alt=\"a\\;*\\;a^{-1}\" align=\"absmiddle\" \/> = l * p + 1 &#8211; it&#8217;s linear diophantine equation , so it has a solution because GCD(a, p) = 1 (greatest common divisor).<\/p>\r\n<p>The operation is also commutative: <br \/>a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> b = (a * b) modulo p = (b * a) modulo p = b <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> a<\/p>\r\n<p>So the structure ({1, p-1}, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/>) is also an abelian group.<\/p>\r\n<p>The last requirement is that operation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> should be distributive over <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/>: (a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b) <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> c = ((a + b) modulo p) <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> c = ((a + b) * c) modulo p = (a * c + b * c) modulo p = (a * c modulo p) <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> (b * c modulo p) = a <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> c <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> b <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> c<\/p>\r\n<p>So we can conclude that the structure Fp = ({0, p-1}, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/>, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/>) is a field.<\/p>\r\n<p>The inverse element of <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/> operation can be calculated using the diophantic equation, but it is easier to do so using the modified Euclid&#8217;s algorithm to calculate the GCD between a and p:<\/p>\r\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">#!\/usr\/bin\/python\n\ndef invers(a, p):\n    #this only work when p is prime!\n    i = p\n    j = a\n    y2 = 0\n    y1 = 1\n    while(j &gt; 0):\n        q = i\/j\n        r = i - (j * q)\n        y = y2 - (y1 * q)\n        i = j\n        j = r\n        y2 = y1\n        y1 = y\n    return (y2 % p)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\r\n<p>A simple implementation of operations in the Fp field could look like below:<\/p>\r\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">#!\/usr\/bin\/python\n\n...\n\nclass FieldElement:\n    def __init__(self, p, value):\n        if(value &gt;= p):\n            raise ValueError(&#039;value %s outside field (p=%s)&#039;%(value, p))\n        self.p = p\n        self.value = value\n        \n    def __str__(self):\n        if self.value &lt; 0:\n            return &quot;-0x%x&quot;%(self.value*(-1))\n        return &quot;0x%x&quot;%self.value\n    \n    def __add__(self, other):\n        if type(other) == int or type(other) == long:\n            return FieldElement(self.p, ((self.value + other) % self.p) )\n        else:\n            return FieldElement(self.p, ((self.value + other.value) % self.p) )\n    \n    def __sub__(self, other):\n        if type(other) == int or type(other) == long:\n            return FieldElement(self.p, ((self.value - other) % self.p) )\n        else:\n            return FieldElement(self.p, ((self.value - other.value) % self.p) )\n    \n    def __mul__(self, other):\n        if type(other) == int or type(other) == long:\n            return FieldElement(self.p, ((self.value * other) % self.p) )\n        else:\n            return FieldElement(self.p, ((self.value * other.value) % self.p) )\n    \n    def __truediv__(self, other):\n        if type(other) == int or type(other) == long:\n            inv = invers(other, self.p)\n        else:\n            inv = invers(other.value, self.p)\n        return FieldElement(self.p, ((self.value * inv) % self.p) )\n    \n    def __eq__(self, other):\n        return (self.value == other.value)\n    \n    def __ne__(self, other):\n        return (self.value != other.value)\n    \n    @staticmethod\n    def factoryMethod(otherFieldElement, value):\n        return FieldElement(otherFieldElement.p, value % otherFieldElement.p)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\r\n<p>Let&#8217;s see how we can use the Fp field to create elliptic curves. At first glance, the EC formula looks identical:<br \/><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=y%5E2%20%3D%20x%5E3%20%5C%3B%2B_%20p%5C%3B%5C%3B%20ax%5C%3B%2B_p%5C%3B%5C%3Bb\" alt=\"y^2 = x^3 \\;+_ p\\;\\; ax\\;+_p\\;\\;b\" align=\"absmiddle\" \/><br \/>however, in this case we have used elements and operations from the field Fp instead of the field of real numbers. The curve here must meet an additional condition: (4 * <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=a%5E3\" alt=\"a^3\" align=\"absmiddle\" \/> + 27 * <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=b%5E2\" alt=\"b^2\" align=\"absmiddle\" \/>) modulo p = 0.<\/p>\r\n<p>The curves above the Fp fields are less intuitive than the curves above the R body, as shown in the following graphs:<br \/><img loading=\"lazy\" class=\"alignnone size-full wp-image-132\" src=\"http:\/\/blog.himitsu.eu\/wp-content\/uploads\/2019\/10\/ec_porownanie_wykresov.png\" alt=\"\" width=\"635\" height=\"358\" srcset=\"https:\/\/blog.himitsu.eu\/wp-content\/uploads\/2019\/10\/ec_porownanie_wykresov.png 635w, https:\/\/blog.himitsu.eu\/wp-content\/uploads\/2019\/10\/ec_porownanie_wykresov-300x169.png 300w\" sizes=\"(max-width: 635px) 100vw, 635px\" \/><\/p>\r\n<p>In general, the points of the EC(Fp) curve appear to be &#8220;randomly&#8221; arranged with some symmetry. Points do not lie at every x position. Usually there are two points on one x position excluding points whose position y = 0 (in the example above it is point (1, 0), which, however, is in symmetry with itself &#8211; theoretically it is also point (1,7)).<\/p>\r\n<p>The operations on the EC(Fp) curve look quite identical to those on the EC(R) curve with the difference that + * opeations are replaced by <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%2B_p\" alt=\"+_p\" align=\"absmiddle\" \/> and <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=*_p\" alt=\"*_p\" align=\"absmiddle\" \/>.<\/p>\r\n<p>And so point negation looks like (the only major difference):<br \/><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=-A%20%3D%20(x_a%2C%5C%3B(-y_a)(mod%20%5C%3Bp))\" alt=\"-A = (x_a,\\;(-y_a)(mod \\;p))\" align=\"absmiddle\" \/><\/p>\r\n<p>Adding two points (<img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A%2BB%3DC\" alt=\"A+B=C\" align=\"absmiddle\" \/>, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A%5Cneq%20B\" alt=\"A\\neq B\" align=\"absmiddle\" \/>, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A%5Cneq%20-B\" alt=\"A\\neq -B\" align=\"absmiddle\" \/>):<\/p>\r\n<ol>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=s%3D(y_a-y_b)%5Cast(x_a-x_b)%5E%7B-1%7D%5C%3B%5C%3B%5C%3B(mod%20p)\" alt=\"s=(y_a-y_b)\\ast(x_a-x_b)^{-1}\\;\\;\\;(mod p)\" align=\"absmiddle\" \/><\/li>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=x_c%3Ds%5E2-x_a-x_b%5C%3B%5C%3B%5C%3B(mod%20p)\" alt=\"x_c=s^2-x_a-x_b\\;\\;\\;(mod p)\" align=\"absmiddle\" \/><\/li>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=y_c%3D-y_a%2Bs(x_a-x_b)%5C%3B%5C%3B%5C%3B(mod%20p)\" alt=\"y_c=-y_a+s(x_a-x_b)\\;\\;\\;(mod p)\" align=\"absmiddle\" \/><\/li>\r\n<\/ol>\r\n<p>Doubling the point (<img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=2A%3DC\" alt=\"2A=C\" align=\"absmiddle\" \/>, <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=%20y_p%5Cneq%200\" alt=\" y_p\\neq 0\" align=\"absmiddle\" \/>):<\/p>\r\n<ol>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=s%3D(3x%5E2_a%2Ba)%7B%5Cast%7D(2y_a)%5E%7B-1%7D%5C%3B%5C%3B%5C%3B(mod%20p)\" alt=\"s=(3x^2_a+a){\\ast}(2y_a)^{-1}\\;\\;\\;(mod p)\" align=\"absmiddle\" \/>\u00a0<\/li>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=x_c%3Ds%5E2-2x_a%5C%3B%5C%3B%5C%3B(mod%20p))\" alt=\"x_c=s^2-2x_a\\;\\;\\;(mod p))\" align=\"absmiddle\" \/><\/li>\r\n<li><img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=y_c%3D-y_a%2Bs(x_a-x_c)%5C%3B%5C%3B%5C%3B(mod%20p)\" alt=\"y_c=-y_a+s(x_a-x_c)\\;\\;\\;(mod p)\" align=\"absmiddle\" \/><\/li>\r\n<\/ol>\r\n<p>Special cases are also identical:<br \/>Doubling <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A%3D(x_a%2C0)\" alt=\"A=(x_a,0)\" align=\"absmiddle\" \/> gives neutral point (point in infinity).<br \/>Adding negation <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A%2B(-A)\" alt=\"A+(-A)\" align=\"absmiddle\" \/> gives neutral point.<br \/>Adding <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A\" alt=\"A\" align=\"absmiddle\" \/> to neutral point gives point <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=A\" alt=\"A\" align=\"absmiddle\" \/>.<\/p>\r\n<p>The last needed element of EC(Fp) arithmetic is point multiple. Most often it is called point multiplication, but this operation is more understood here as exponentiation in the additive group version.<br \/>Traditional exponentiation is the number of multiplications of a given number that we must do to obtain the result of exponentiation. And so if we have <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=3%5E4\" alt=\"3^4\" align=\"absmiddle\" \/> then we must multiply the number three four times: 3 * 3 * 3 * 3 = 81. But what if we have to calculate <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=3%5E%7B113%7D\" alt=\"3^{113}\" align=\"absmiddle\" \/>? Of course, we can do 3 * 3 * &#8230; * 3 &#8211; 112 multiplications (and each multiplication operation costs some computational power), or write <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=3%5E%7B113%7D\" alt=\"3^{113}\" align=\"absmiddle\" \/> as <img class=\"mathtex-equation-editor\" src=\"http:\/\/chart.apis.google.com\/chart?cht=tx&amp;chl=3%20*%20(((((3%20*%20(3%20*%203%20%5E%202)%20%5E%202%20)%20%5E%202)%20%5E%202)%20%5E%202)%20%5E%202\" alt=\"3 * (((((3 * (3 * 3 ^ 2) ^ 2 ) ^ 2) ^ 2) ^ 2) ^ 2\" align=\"absmiddle\" \/>, where we have only 9 multiplications.<\/p>\r\n<p>And so, if we have a certain point on EC and want to multiply it by 117, then instead of 116 additions, we can write this number to binary form 1110101 and moving from the least significant bit follow the steps of a simple algorithm:<\/p>\r\n<ol>\r\n<li>For the start the base point B equals the multiplied point P, and the &#8216;result&#8217; point R equals the neutral point.<\/li>\r\n<li>If the considered bit is one, add point B to point R.<\/li>\r\n<li>If this is not the last bit, double the B point and go to step 2 considering the next bit.<\/li>\r\n<\/ol>\r\n<p>The result of the algorithm is point R. In our example for 117 * P, the algorithm will perform only 5 additions and 6 doubles.<\/p>\r\n<p>Using the previous Fp implementation, we can now also implement EC(Fp) arithmetic:<\/p>\r\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">#!\/usr\/bin\/python\n\nimport copy\n\n...\n\nclass ECPoint:\n    def __init__(self, p, a, b):\n        testec = (4*a*a*a) + (27*b*b)\n        if(testec %p == 0):\n            raise ValueError(&#039;Bad EC&#039;)\n        self.a = FieldElement(p, a)\n        self.b = FieldElement(p, b)\n        self.neutral = True\n    \n    def __str__(self):\n        if self.neutral:\n            return &quot;(neutral)&quot;\n        return &quot;(0x%x,0x%x)&quot;%(self.x.value, self.y.value)\n    \n    @staticmethod\n    def testOnCurve(a, b, x, y):\n        test_left = y*y\n        test_right = x*x*x+a*x + b\n        if test_left.value == test_right.value:\n            return True\n        return False\n    \n    def testOnSelfCurve(self):\n        if self.neutral:\n            return True\n        return ECPoint.testOnCurve(self.a, self.b, self.x, self.y)\n    \n    @staticmethod\n    def factoryMethod(otherECPoint, x, y):\n        if not ECPoint.testOnCurve(otherECPoint.a, otherECPoint.b, x, y):\n            raise ValueError(&#039;Point not on EC&#039;)\n        res = copy.deepcopy(otherECPoint)\n        res.x = x\n        res.y = y\n        res.neutral = False\n        return res\n    \n    def double(self):\n        if self.y.value == 0:\n            return ECPoint(self.a.p, self.a.value, self.b.value) #return neutral\n        s1 = (self.x*self.x*3) + self.a\n        s2 = self.y*2\n        s2 = invers(s2.value, s2.p)\n        s = s1*s2\n        \n        \n        x = (s*s) - (self.x*2)\n        y = s*(self.x - x) - self.y\n        return ECPoint.factoryMethod(self, x, y)\n    \n    def __add__(self, other):\n        if self.neutral:\n            return copy.deepcopy(other)\n        elif other.neutral:\n            return copy.deepcopy(self)\n        elif other.x == self.x and other.y == self.y:\n            return self.double()\n        \n        s1 = self.y - other.y\n        s2 = self.x - other.x\n        s2 = invers(s2.value, s2.p)\n        s = s1*s2\n        \n        x = s*s - self.x - other.x\n        y = s*(self.x - x) - self.y\n        return ECPoint.factoryMethod(self, x, y)\n\n    def __mul__(self, other):\n        if type(other) == FieldElement:\n            other = other.value\n        elif type(other) != int and type(other) != long:\n            raise ValueError(&quot;Can&#039;t multiply by %s&quot;%(type(other)))\n        \n        res = ECPoint(self.a.p, self.a.value, self.b.value) #start with neutral\n        base = copy.deepcopy(self)\n        mask = 1\n        while True:\n            if mask &amp; other:\n                res += base\n            mask &lt;&lt;= 1\n            if mask &gt; other:\n                break\n            base += base\n        return res<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\r\n<p>We must be sure that the implementation we created is correct. The easiest way to verify it is to select a point and perform some operation on it (e.g. doubling it, then adding it to the doubling result) &#8211; the obtained point should be a point that also lies on the curve (satisfies the curve equation). This is a test to verify basic errors, but it is not enough.<br \/>The main verification tool are test vectors. For example, in the document rfc5903 in section 8.1 we have a test vector for a 256 bit curve (from section 3.1). if we take the point &#8216;g&#8217; (from 3.1) and multiply it by &#8216;i&#8217; (from 8.1), we should get &#8216;gix&#8217; and &#8216;giy&#8217; values:<\/p>\r\n<div class=\"code-embed-wrapper\"> <pre class=\"language-python code-embed-pre\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-python code-embed-code\">#!\/usr\/bin\/python\n\n...\n\np = (2**256)-(2**224)+(2**192)+(2**96)-1\ntv = FieldElement(p, 1)\n\nneutral = ECPoint(p, -3, 0x5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B)\n\ngx = FieldElement.factoryMethod(tv, 0x6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296)\ngy = FieldElement.factoryMethod(tv, 0x4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5)\n\ng = ECPoint.factoryMethod(neutral, gx, gy)\n\ni = 0xC88F01F510D9AC3F70A292DAA2316DE544E9AAB8AFE84049C62A9C57862D1433\nr = 0xC6EF9C5D78AE012A011164ACB397CE2088685D8F06BF9BE0B283AB46476BEE53\ngi = g*i\ngr = g*r\nprint &quot;gi(%s)=%s&quot;%(gi.testOnSelfCurve(), gi)\nprint &quot;gr(%s)=%s&quot;%(gr.testOnSelfCurve(), gr)\ngir = gi*r\ngri = gr*i\nprint &quot;gir(%s)=%s&quot;%(gir.testOnSelfCurve(), gir)\nprint &quot;gri(%s)=%s&quot;%(gri.testOnSelfCurve(), gri)<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\r\n<pre>gi(True)=(0xdad0b65394221cf9b051e1feca5787d098dfe637fc90b9ef945d0c3772581180,0x5271a0461cdb8252d61f1c456fa3e59ab1f45b33accf5f58389e0577b8990bb3)<br \/>gr(True)=(0xd12dfb5289c8d4f81208b70270398c342296970a0bccb74c736fc7554494bf63,0x56fbf3ca366cc23e8157854c13c58d6aac23f046ada30f8353e74f33039872ab)<br \/>gir(True)=(0xd6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de,0x522bde0af0d8585b8def9c183b5ae38f50235206a8674ecb5d98edb20eb153a2)<br \/>gri(True)=(0xd6840f6b42f6edafd13116e0e12565202fef8e9ece7dce03812464d04b9442de,0x522bde0af0d8585b8def9c183b5ae38f50235206a8674ecb5d98edb20eb153a2)<\/pre>\r\n<p>And we quickly proved that the presented implementation is correct.<\/p>\r\n<p><br \/>In the next part we will deal with the optimization of this curve arithmetic implementation.<\/p>\r\n<p><a href=\"http:\/\/blog.himitsu.eu\/wp-content\/uploads\/2020\/01\/ec_part3.doc\">ec_part3<\/a> (full code)<\/p>\r\n<p><a href=\"http:\/\/blog.himitsu.eu\/wp-admin\/post.php?post=148\">go to part 4<\/a><\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Go to part two Modern popular processors are able to accurately and efficiently handle integer operations. Therefore, when implementing actions on elliptic curves, it is worth using this property. The type of elliptic curve that is best suited for use in a software solution will be discussed below. Let&#8217;s choose a certain number p, which &hellip; <a href=\"https:\/\/blog.himitsu.eu\/?p=118\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Elliptic curves and how to use them (part three)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/posts\/118"}],"collection":[{"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":19,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.himitsu.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}