forked from winderica/kryptology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zk_r_p.go
158 lines (139 loc) · 3.86 KB
/
zk_r_p.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package zk_r_p
import (
"crypto/rand"
"fmt"
"math/big"
"sync"
"github.com/The9born/kryptology/pkg/tecdsa/2ecdsa/mta/paillier/zk"
"github.com/gtank/merlin"
)
// Nothing needs to be agreed beforehand
type Agreed struct {
}
type Proof struct {
w *big.Int
x [zk.T]*big.Int
a [zk.T]bool
b [zk.T]bool
z [zk.T]*big.Int
}
// N
type Statement = big.Int
type Witness struct {
p *big.Int
q *big.Int
}
func NewWitness(p *big.Int, q *big.Int) *Witness {
return &Witness{p, q}
}
func Prove(tx *merlin.Transcript, witness *Witness, N *Statement) *Proof {
tx.AppendMessage([]byte("N"), N.Bytes()) // Strong Fiat-Shamir
// Step 1: Commit
var w *big.Int
for {
w, _ = rand.Int(rand.Reader, N)
if big.Jacobi(w, N) == -1 {
break
}
}
tx.AppendMessage([]byte("w"), w.Bytes())
// Step 2: Challenge (Fiat-Shamir)
y := [zk.T]*big.Int{}
t := big.NewInt(0)
tt := big.NewInt(0)
for i := 0; i < zk.T; {
if tt.Cmp(N) == -1 {
v := new(big.Int).SetBytes(tx.ExtractBytes([]byte(fmt.Sprintf("y[%d]", i)), zk.N_BITS/8+1))
vv := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), zk.N_BITS+8), big.NewInt(1))
t = new(big.Int).Add(new(big.Int).Lsh(t, zk.N_BITS+8), v)
tt = new(big.Int).Add(new(big.Int).Lsh(tt, zk.N_BITS+8), vv)
} else {
y[i] = new(big.Int).Mod(t, N)
t = new(big.Int).Div(t, N)
tt = new(big.Int).Div(tt, N)
i += 1
}
}
// Step 3: Prove
var wg sync.WaitGroup
wg.Add(zk.T)
x, z := [zk.T]*big.Int{}, [zk.T]*big.Int{}
a, b := [zk.T]bool{}, [zk.T]bool{}
p, q := witness.p, witness.q
phi_N := new(big.Int).Mul(new(big.Int).Sub(p, big.NewInt(1)), new(big.Int).Sub(q, big.NewInt(1)))
index := new(big.Int).ModInverse(N, phi_N)
inv_p_mod_q, inv_q_mod_p := new(big.Int).ModInverse(p, q), new(big.Int).ModInverse(q, p)
for i := 0; i < zk.T; i++ {
go func (i int) {
z[i] = new(big.Int).Exp(y[i], index, N)
for _, ab := range [][]bool{{false, false}, {false, true}, {true, false}, {true, true}} {
y := y[i]
if ab[0] {
y = new(big.Int).Neg(y)
}
if ab[1] {
y = new(big.Int).Mul(y, w)
}
y = new(big.Int).Mod(y, N)
if big.Jacobi(y, p) == 1 && big.Jacobi(y, q) == 1 {
// y has 4 square roots, i.e.,
// (1) CRT(sqrt(y, p), sqrt(y, q))
// (2) CRT(p - sqrt(y, p), sqrt(y, q))
// (3) CRT(sqrt(y, p), q - sqrt(y, q))
// (4) CRT(p - sqrt(y, p), q - sqrt(y, q))
// But only (1) has its own square root
y_sqrt := zk.CRT(new(big.Int).ModSqrt(y, p), new(big.Int).ModSqrt(y, q), p, q, inv_p_mod_q, inv_q_mod_p)
x[i] = zk.CRT(new(big.Int).ModSqrt(y_sqrt, p), new(big.Int).ModSqrt(y_sqrt, q), p, q, inv_p_mod_q, inv_q_mod_p)
a[i] = ab[0]
b[i] = ab[1]
break
}
}
wg.Done()
}(i)
}
wg.Wait()
return &Proof{w, x, a, b, z}
}
func Verify(tx *merlin.Transcript, N *Statement, proof *Proof) bool {
tx.AppendMessage([]byte("N"), N.Bytes()) // Strong Fiat-Shamir
tx.AppendMessage([]byte("w"), proof.w.Bytes())
// Step 2: Challenge (Fiat-Shamir)
y := [zk.T]*big.Int{}
t := big.NewInt(0)
tt := big.NewInt(0)
for i := 0; i < zk.T; {
if tt.Cmp(N) == -1 {
v := new(big.Int).SetBytes(tx.ExtractBytes([]byte(fmt.Sprintf("y[%d]", i)), zk.N_BITS/8+1))
vv := new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), zk.N_BITS+8), big.NewInt(1))
t = new(big.Int).Add(new(big.Int).Lsh(t, zk.N_BITS+8), v)
tt = new(big.Int).Add(new(big.Int).Lsh(tt, zk.N_BITS+8), vv)
} else {
y[i] = new(big.Int).Mod(t, N)
t = new(big.Int).Div(t, N)
tt = new(big.Int).Div(tt, N)
i += 1
}
}
// Step 4: Verify
if N.ProbablyPrime(64) {
return false
}
for i := 0; i < zk.T; i++ {
if new(big.Int).Exp(proof.z[i], N, N).Cmp(y[i]) != 0 {
return false
}
y := y[i]
if proof.a[i] {
y = new(big.Int).Neg(y)
}
if proof.b[i] {
y = new(big.Int).Mul(y, proof.w)
}
y = new(big.Int).Mod(y, N)
if new(big.Int).Exp(proof.x[i], big.NewInt(4), N).Cmp(y) != 0 {
return false
}
}
return true
}