-
Notifications
You must be signed in to change notification settings - Fork 14
/
security.py
64 lines (62 loc) · 2.45 KB
/
security.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#this file is for running simulations to help properly tune the constants in flying fox. If the constants are optimally tuned, then flying fox is secure against up to 1/2 byzantine.
import numpy
from cdecimal import Decimal
def control_block_trial(total, clique, p):
if numpy.random.poisson(clique)>p*total:
return 1
return 0
def control_block_probability(clique, total=Decimal(78), p=Decimal('0.66666666666666666666666666666666666666666666666666666')):
#how often does the clique, which is a subset of the total, control > p of the block?
a=Decimal(0)
rounds=Decimal(500000)
for i in range(rounds):
a+=control_block_trial(total, clique, p)
#print("a: " +str(a))
return(Decimal(a)/rounds)
def frequency_half_control(x): return Decimal(1)/control_block_probability(x/2, x)
def test():
#We are aiming for a clique of 50% of the bonded validators to be able to verify 4% of the blocks. verification requres 2/3 of all signatures.
j=54
hc=str(frequency_half_control(Decimal(j)))
print(str(j)+" : " +hc)
print("this shows that if there are "+str(j)+" validators on average, then an attacking group with 50% of bonded stake would control 1 block in "+hc+" on average")
def proof_gap():
#gap is given by numpy.random.gamma(1, 50)
import random
def f(): return random.random()*25>24
def g(): return "1" if f() else "0"
def h():
out=""
for i in range(1000):
out+=g()
out=out.split("1")
#print(out)
out=map(lambda x: len(x), out)
return out
#h() =doing=same= gamma()
def gamma():
out=[]
for i in range(25):
out+=[int(numpy.random.gamma(1, 50))]
return out
def fork_time():#how long till fork dies? assuming 50% want the fork to live.
#assuming 50 blocks is too big of a gap to jump.
#this shows that that we should aim for a 4% probability that attackers can control the block. (1/25 = 0.04)
#forks usually die within 200 blocks.
def tg():#probability next gap is big enough to kill
out=0
times=1000
for i in range(times):
if numpy.random.gamma(1, 25)>50: