Created
January 9, 2018 12:22
-
-
Save jachiang/5e3c56fb7d6d186fd83f58932937dc93 to your computer and use it in GitHub Desktop.
Plotting script for Programming Blockchain ch2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # coding: utf-8 | |
| # In[81]: | |
| #EC Addition Plot | |
| import math | |
| import numpy as np | |
| class EC: | |
| def __init__(self, a, b, c, d): | |
| self.a = a | |
| self.b = b | |
| self.c = c | |
| self.d = d | |
| def yspace (self, xspace): | |
| ypos=[] | |
| yneg=[] | |
| xSpace=[] | |
| for x in xspace: | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp >= 0: | |
| ypos += [math.sqrt(exp)] | |
| yneg += [-math.sqrt(exp)] | |
| xSpace += [x] | |
| return [xSpace, ypos, yneg] | |
| def y (self, x): | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp >= 0: | |
| ypos = math.sqrt(exp) | |
| return ypos | |
| if exp < 0: | |
| return None | |
| def tangent (self, x): | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp > 0: | |
| ypos = math.sqrt(exp) | |
| m =(3 * x**2 + self.a) / (2 * ypos) | |
| return m | |
| if exp == 0: | |
| m = 'infinity' | |
| return m | |
| return m | |
| if exp < 0: | |
| return None | |
| def add (self, x1, x2, y1_sign, y2_sign): | |
| #this requires numpy | |
| y1 = self.y(x1) * y1_sign | |
| y2 = self.y(x2) * y2_sign | |
| if y1 != None and y2 != None: | |
| if y1 == y2: | |
| if y1 == 'infinity': | |
| return "Infinity, no intersection" | |
| else: | |
| #tangent dy/dx=(3x2+a)/(2y) | |
| #perhaps c, instead of a | |
| m = (3*x1 **2 + self.c) / (2*y1) | |
| #x3=s2-x1-x2 | |
| x3 = m**2 - 2*x1 | |
| #y3=-(s(x-x1)+y1)=s(x1-x3)-y1 | |
| y3 = m *(x1-x3)-y1 | |
| return [x3,y3] | |
| else: | |
| m = (y2-y1)/(x2-x1) | |
| print(m) | |
| #x3=s2-x1-x2 | |
| x3 = m**2 - x1 - x2 | |
| #y3=-(s(x-x1)+y1)=s(x1-x3)-y1 | |
| y3 = m *(x1-x3)-y1 | |
| return [x3,y3] | |
| else: | |
| return "point or points not on EC" | |
| #define EC for BTC | |
| secp256k1 = EC(1,0,0,7) | |
| #define two EC points | |
| x1 = -1 | |
| y1_sign = -1 | |
| y1 = secp256k1.y(x1) * y1_sign | |
| x2 = -1 | |
| y2_sign = -1 | |
| y2 = secp256k1.y(x2) * y2_sign | |
| #calculate result | |
| xy3 = secp256k1.add(x1,x2,y1_sign,y2_sign) | |
| x3 = xy3[0] | |
| y3 = xy3[1] | |
| print(xy3[1]) | |
| print(secp256k1.y(x3)) | |
| #calculate plot x range | |
| xmin = min([x1,x2,x3]) *1.5 | |
| xmax = max([x1,x2,x3]) *1.5 | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-5,xmax,3000) | |
| ypos=secp256k1.yspace(x)[1] | |
| yneg=secp256k1.yspace(x)[2] | |
| xplot = secp256k1.yspace(x)[0] | |
| ySpacePos = go.Scatter( | |
| x=xplot, | |
| y=ypos, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| ySpaceNeg = go.Scatter( | |
| x=xplot, | |
| y=yneg, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| x1x2Line = go.Scatter( | |
| x=[x1,x2,x3], | |
| y=[y1,y2,-y3], | |
| mode='lines', | |
| name='P1, P2, P3', | |
| marker=dict(color='rgb(0, 0, 0)') | |
| ) | |
| x3Line = go.Scatter( | |
| x=[x3,x3], | |
| y=[y3,-y3], | |
| mode='lines', | |
| name='P3-Vertical', | |
| marker=dict(color='rgb(0, 0, 0)') | |
| ) | |
| Points = go.Scatter( | |
| x = [x1,x2,x3], | |
| y = [y1,y2,y3], | |
| mode='markers+text', | |
| name='Markers and Text', | |
| text=['P1=P2', '', 'P1+P2'], | |
| textposition='right', | |
| marker = dict( | |
| size = 10, | |
| color = 'rgba(100, 182, 193, .9)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| #fig = go.Figure(data=[ySpacePos,ySpaceNeg,Points,x1x2Line,x3Line], layout=layout) | |
| fig = go.Figure(data=[ySpacePos,ySpaceNeg], layout=layout) | |
| py.offline.plot(fig, filename='ECADDplot.html') | |
| # In[77]: | |
| #EC Plot | |
| #Plotting EC Curve | |
| import math | |
| import numpy as np | |
| class EC: | |
| def __init__(self, a, b, c, d): | |
| self.a = a | |
| self.b = b | |
| self.c = c | |
| self.d = d | |
| def yspace (self, xspace): | |
| ypos=[] | |
| yneg=[] | |
| xSpace=[] | |
| for x in xspace: | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp >= 0: | |
| ypos += [math.sqrt(exp)] | |
| yneg += [-math.sqrt(exp)] | |
| xSpace += [x] | |
| return [xSpace, ypos, yneg] | |
| def y (self, x): | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp >= 0: | |
| ypos = math.sqrt(exp) | |
| return ypos | |
| if exp < 0: | |
| return None | |
| def tangent (self, x): | |
| exp = self.a*x**3 + self.b*x**2 + self.c*x + self.d | |
| if exp > 0: | |
| ypos = math.sqrt(exp) | |
| m =(3 * x**2 + self.a) / (2 * ypos) | |
| return m | |
| if exp == 0: | |
| m = 'infinity' | |
| return m | |
| return m | |
| if exp < 0: | |
| return None | |
| def add (self, x1, x2, y1_sign, y2_sign): | |
| #this requires numpy | |
| y1 = self.y(x1) * y1_sign | |
| y2 = self.y(x2) * y2_sign | |
| if y1 != None and y2 != None: | |
| if y1 == y2: | |
| if y1 == 'infinity': | |
| return "Infinity, no intersection" | |
| else: | |
| #tangent dy/dx=(3x2+a)/(2y) | |
| m = (3*x1 **2 + self.a) / (2*y1) | |
| #x3=s2-x1-x2 | |
| x3 = m**2 - 2*x1 | |
| #y3=-(s(x-x1)+y1)=s(x1-x3)-y1 | |
| y3 = m *(x1-x3)-y1 | |
| return [x3,y3] | |
| else: | |
| m = (y2-y1)/(x2-x1) | |
| #x3=s2-x1-x2 | |
| x3 = m**2 - x1 - x2 | |
| #y3=-(s(x-x1)+y1)=s(x1-x3)-y1 | |
| y3 = m *(x1-x3)-y1 | |
| return [x3,y3] | |
| else: | |
| return "point or points not on EC" | |
| #define EC for BTC | |
| #ECInstance = EC(1,0,0,7) | |
| #disjoint example | |
| #ECInstance = EC(1,0,-4,0) | |
| #Another example | |
| ECInstance = EC(1,0,-3,3) | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-5,14,3000) | |
| ypos=ECInstance.yspace(x)[1] | |
| yneg=ECInstance.yspace(x)[2] | |
| xplot = ECInstance.yspace(x)[0] | |
| ySpacePos = go.Scatter( | |
| x=xplot, | |
| y=ypos, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| ySpaceNeg = go.Scatter( | |
| x=xplot, | |
| y=yneg, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| fig = go.Figure(data=[ySpacePos,ySpaceNeg], layout=layout) | |
| py.offline.plot(fig, filename='ECplot.html') | |
| # In[78]: | |
| #Linear Plot | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-7,7,3000) | |
| m = 0.7 | |
| b = 4 | |
| y = x * a + b | |
| yData = go.Scatter( | |
| x=x, | |
| y=y, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| fig = go.Figure(data=[yData], layout=layout) | |
| py.offline.plot(fig, filename='linear.html') | |
| # In[57]: | |
| #Quadratic Plot | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-10,10,3000) | |
| m = 1 | |
| a = 1 | |
| b = 0 | |
| c = 3 | |
| y = x**2 * a + x *b + c | |
| yData = go.Scatter( | |
| x=x, | |
| y=y, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| fig = go.Figure(data=[yData], layout=layout) | |
| py.offline.plot(fig, filename='quadratic.html') | |
| # In[ ]: | |
| #Quadratic Plot | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-10,10,3000) | |
| a = 1 | |
| b = 0 | |
| c = 0 | |
| y = x**2 * a + x *b + c | |
| yData = go.Scatter( | |
| x=x, | |
| y=y, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| fig = go.Figure(data=[yData], layout=layout) | |
| py.offline.plot(fig, filename='quadratic.html') | |
| # In[60]: | |
| #Cubic Plot | |
| #Plot code below | |
| import plotly.offline as py #no inline possible apparenly | |
| import plotly.graph_objs as go | |
| import numpy as np | |
| import pandas as pd | |
| import scipy | |
| #define the range for the plot | |
| x = np.linspace(-10,10,3000) | |
| a = 1 | |
| b = 2 | |
| c = 1 | |
| d = 200 | |
| y = x**3 * a + x**2 *b + x * c + d | |
| yData = go.Scatter( | |
| x=x, | |
| y=y, | |
| mode='lines', | |
| name='f(x)', | |
| marker=dict( | |
| color='rgb(220, 20, 60)' | |
| ) | |
| ) | |
| layout = go.Layout(showlegend=False) | |
| fig = go.Figure(data=[yData], layout=layout) | |
| py.offline.plot(fig, filename='cubic.html') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment