-
Notifications
You must be signed in to change notification settings - Fork 54
/
qr.go
137 lines (116 loc) · 3.07 KB
/
qr.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
/*
* Copyright 2017 XLAB d.o.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package qr
// Zero-knowledge proof of quadratic residousity (implemented for historical reasons)
import (
"math/big"
"fmt"
"github.com/xlab-si/emmy/crypto/common"
"github.com/xlab-si/emmy/crypto/schnorr"
)
// ProveQR demonstrates how the prover can prove that y1^2 is QR.
func ProveQR(y1 *big.Int, group *schnorr.Group) bool {
y := group.Mul(y1, y1)
prover := NewProver(group, y1)
verifier := NewVerifier(y, group)
m := group.P.BitLen()
for i := 0; i < m; i++ {
x := prover.GetProofRandomData()
c := verifier.GetChallenge(x)
z, _ := prover.GetProofData(c)
proved := verifier.Verify(z)
if !proved {
return false
}
}
return true
}
type Prover struct {
Group *schnorr.Group
Y *big.Int
y1 *big.Int
r *big.Int
}
func NewProver(group *schnorr.Group, y1 *big.Int) *Prover {
y := group.Mul(y1, y1)
return &Prover{
Group: group,
Y: y,
y1: y1,
}
}
func (p *Prover) GetProofRandomData() *big.Int {
r := common.GetRandomInt(p.Group.P)
p.r = r
x := p.Group.Exp(r, big.NewInt(2))
return x
}
func (p *Prover) GetProofData(challenge *big.Int) (*big.Int, error) {
if challenge.Cmp(big.NewInt(0)) == 0 {
return p.r, nil
} else if challenge.Cmp(big.NewInt(1)) == 0 {
z := new(big.Int).Mul(p.r, p.y1)
z.Mod(z, p.Group.P)
return z, nil
} else {
err := fmt.Errorf("challenge is not valid")
return nil, err
}
}
type Verifier struct {
Group *schnorr.Group
x *big.Int
y *big.Int
challenge *big.Int
}
func NewVerifier(y *big.Int, group *schnorr.Group) *Verifier {
return &Verifier{
Group: group,
y: y,
}
}
func (v *Verifier) GetChallenge(x *big.Int) *big.Int {
v.x = x
c := common.GetRandomInt(big.NewInt(2)) // 0 or 1
v.challenge = c
return c
}
func (v *Verifier) Verify(z *big.Int) bool {
z2 := new(big.Int).Mul(z, z)
z2.Mod(z2, v.Group.P)
if v.challenge.Cmp(big.NewInt(0)) == 0 {
return z2.Cmp(v.x) == 0
} else {
s := new(big.Int).Mul(v.x, v.y)
s.Mod(s, v.Group.P)
return z2.Cmp(s) == 0
}
}
// isQR accepts integer a and prime p, and returns true if
// a is quadratic residue in Z_p group, false otherwise.
// If p is not a prime, error is returned.
func isQR(a *big.Int, p *big.Int) (bool, error) {
if !p.ProbablyPrime(20) {
return false, fmt.Errorf("p is not a prime")
}
one := big.NewInt(1)
// check whether a^((p-1)/2) is 1 or -1 (Euler's criterion)
pMin1 := new(big.Int).Sub(p, one)
exp := new(big.Int).Div(pMin1, big.NewInt(2)) // exponent
cr := new(big.Int).Exp(a, exp, p)
return cr.Cmp(one) == 0, nil
}