summaryrefslogtreecommitdiff
path: root/gp-scripts/mp_pub
blob: 9b49c132be6645abd2fe3a43d89d75f06108281d (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
\\ From: "Fully private auctions in a constant number of rounds" (2003) by Felix Brandt pages 9-10


\\\\\\\\\\\\
\\ Adapt the following values to your needs
\\\\\\\\\\\\

\\ auction parameter
M = 1
\\ amount of bidders
n = 2^2
\\ amount of possible prices
k = 2^4
\\ randomize bids (change to something static, if you like)
bid = vector(n,i,random(k)+1)
\\bid = vector(n,i,n-i+1)     \\ first bidder wins
\\bid = vector(n,i,i)         \\ last bidder wins
\\bid = vector(n,i,(i+1)%2)   \\ second bidder wins (with ties)

\\ prime finite field setup (result may be ambiguous if your prime is too small, 4*n*k seems to work fine)
\\q = prime(2^12)
\\ 512bit prime:
q = 12513167897862218633350152063959653109080007724899931588313481862015596111526299656550478091592311160908219544364381660940520774223634480285451547911456579
\\ 2048bit prime:
\\q = 31905233907400964621684499856844075173802000556075101303613351426740101897961025481077892281365444367883091980681462491724119317344478120131982416132058173572772607966572720945691237876256074322291459510766147107539260048324345382562673904236506104922357079761457605045674628331006193183908801308817507027556440703972646885207099302085383887085776295396030033300833460743425162726394704256227108175491673135830378272029374848904772902525385997099641162537271298634032011458617811670193865244028195169383991286227040469186123958053863978710424421008752927011390777187889943940479064193231486057910586526439884046593027
\\ 3072bit prime:
\\q = 5175054779340588353586849786144680366505563673837334790820581054294754700842534366479020240016540005621125885927641963390708863183739793208880756653713659686139600715884857385144475261507869935694699816011948585170171332029002674283854825650901258017026965486602158722052719421343475066067509485302858041368266332080773331946039572497794442067057597327877030322029413318847025776818839927761556478107499002213648377029201340152459685610920194363099878398871001275336711869213616313858200583491913270052111910410231060407633125816386053759634073500319223989240814564691163285769745840521560940666058800931070258886096469889796899266014106833050284032035948051974659796051419431527095503586817863043771919051402039741075037010264761045992285666560487072740505566408086913711094879155498223636912657852688296081316652278801546924079650897913388978423388839346058027184069633227966507908979049369500450630036982661231208087459099
g = Mod(2, q)

\\ get generator / primitive element for G_q
\\var = 'x                                                    \\ copy pasta from internet
\\pe=ffgen(minpoly(ffprimroot(ffgen(ffinit(p,1))),var),var)   \\ get primitive element
\\1/(fforder(pe) == p-1)                                      \\ error out, if ord(pe) is wrong
\\g = Mod(eval(Str(pe))^2, p)                                 \\ dirty hack to convert t_FFELEM to t_INT

\\\\\\\\\\\\
\\ PROLOG
\\\\\\\\\\\\

\\ private keys of agents
x = vector(n,i,random(q))
\\ public keyshares of agents
yshares = vector(n,i,g^x[i])
\\ shared public key
y = prod(X=1,n,yshares[X])

\\ first index level = owning agent id (additive share)
\\ second index level = agent id, price id
m = matrix(n,k,a,b,random(q))

\\ index = owning agent id, price id
r = matrix(n,k,i,j,random(q))
\\ bid matrix
b = matrix(n,k,i,j,g^(bid[i]==j))

\\\\\\\\\\\\
\\ ROUND1
\\\\\\\\\\\\

\\ encrypted bids
alpha = matrix(n,k,i,j, b[i,j]*y^r[i,j])
beta  = matrix(n,k,i,j,        g^r[i,j])

\\\\\\\\\\\\
\\ ROUND2
\\\\\\\\\\\\

\\ multiplicative shares
\\ first index level = owning agent id (multiplicative share)
\\ second index level = agent id, price id
GammaPrice = matrix(n,k,a,j, ( prod(h=1,n,prod(d=j,k,alpha[h,d]) * prod(d=j+1,k,alpha[h,d])) / g^(2*M+1) )^(m[a,j]) )
DeltaPrice = matrix(n,k,a,j, ( prod(h=1,n,prod(d=j,k, beta[h,d]) * prod(d=j+1,k, beta[h,d])) )^(m[a,j]) )
GammaWinner = matrix(n,k,a,j, ( GammaPrice[a,j] * prod(h=1,n,prod(d=j+1,k,alpha[h,d]^(2^(h-1)))) ))
DeltaWinner = matrix(n,k,a,j, ( DeltaPrice[a,j] * prod(h=1,n,prod(d=j+1,k, beta[h,d]^(2^(h-1)))) ))

\\\\\\\\\\\\
\\ ROUND3
\\\\\\\\\\\\

\\ multiplicative shares (decryption)
\\ first index level = owning agent id (multiplicative share)
\\ second index level = agent id, price id
PhiPrice = matrix(n,k,a,j, prod(h=1,n,DeltaPrice[h,j])^x[a] )
PhiWinner = matrix(n,k,a,j, prod(h=1,n,DeltaWinner[h,j])^x[a] )

\\\\\\\\\\\\
\\ EPILOG
\\\\\\\\\\\\

\\ winner matrix
vPrice = lift(vector(k,j, prod(i=1,n,GammaPrice[i,j]) / prod(i=1,n,PhiPrice[i,j]) ))
vWinner = vector(k,j, prod(i=1,n,GammaWinner[i,j]) / prod(i=1,n,PhiWinner[i,j]) )

print("bids are: ", bid)

price = -1
for(j=1,k, if(vPrice[j]==1, price=j))

winners = vector(i=1,M,-1)
winp = binary(znlog(vWinner[price],g)/n)
cur = 1;
for(i=1,length(winp), if(winp[length(winp)-i+1]==1,winners[cur]=i;cur=cur+1))
print("Winners are ", winners)
print("And the price is ", price)