-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathequivalence.go
146 lines (120 loc) · 4.4 KB
/
equivalence.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
package zksigma
import (
"bytes"
"crypto/rand"
"fmt"
"math/big"
)
// EquivalenceProof is an Equivalence Proof. A proof that both A and B both use the
// same scalar, x.
//
// Public: generator points G and H
//
// Prover Verifier
// ====== ========
// know x
// A = xG ; B = xH learns A, B
// selects random u
// T1 = uG
// T2 = uH
// c = HASH(G, H, xG, xH, uG, uH)
// s = u + c * x
//
// T1, T2, s, c ---------------------->
// c ?= HASH(G, H, A, B, T1, T2)
// sG ?= T1 + cA
// sH ?= T2 + cB
type EquivalenceProof struct {
UG ECPoint // uG is the scalar mult of u (random num) with base G
UH ECPoint // uH is the scalar mult of u (random num) with base H
Challenge *big.Int // Challenge is hash sum of challenge commitment
HiddenValue *big.Int // Hidden Value hides the discrete log x that we want to prove equivalence for
}
// NewEquivalenceProof generates an equivalence proof that Result1 is the scalar multiple of base Base1,
// and Result2 is the scalar multiple of base Base2 and that both results are using the same x as discrete log.
func NewEquivalenceProof(
zkpcp ZKPCurveParams, Base1, Result1, Base2, Result2 ECPoint, x *big.Int) (*EquivalenceProof, error) {
modValue := new(big.Int).Mod(x, zkpcp.C.Params().N)
check1 := zkpcp.Mult(Base1, modValue)
if !check1.Equal(Result1) {
return nil, &errorProof{"EquivalenceProve", "Base1 and Result1 are not related by x"}
}
check2 := zkpcp.Mult(Base2, modValue)
if !check2.Equal(Result2) {
return nil, &errorProof{"EquivalenceProve", "Base2 and Result2 are not related by x"}
}
// random number
u, err := rand.Int(rand.Reader, zkpcp.C.Params().N) // random number to hide x later
if err != nil {
return nil, err
}
// uG
uBase1 := zkpcp.Mult(Base1, u)
// uH
uBase2 := zkpcp.Mult(Base2, u)
// HASH(G, H, xG, xH, uG, uH)
Challenge := GenerateChallenge(zkpcp, Base1.Bytes(), Result1.Bytes(),
Base2.Bytes(), Result2.Bytes(),
uBase1.Bytes(), uBase2.Bytes())
// s = u + c * x
HiddenValue := new(big.Int).Add(u, new(big.Int).Mul(Challenge, modValue))
HiddenValue = HiddenValue.Mod(HiddenValue, zkpcp.C.Params().N)
return &EquivalenceProof{
uBase1, // uG
uBase2, // uH
Challenge,
HiddenValue}, nil
}
// Verify checks if EquivalenceProof eqProof is a valid proof that Result1 is
// the scalar multiple of base Base1, and Result2 is the scalar multiple of base
// Base2. Both using the same x as discrete log.
func (eqProof *EquivalenceProof) Verify(
zkpcp ZKPCurveParams, Base1, Result1, Base2, Result2 ECPoint) (bool, error) {
if eqProof == nil {
return false, &errorProof{"EquivalenceVerify", fmt.Sprintf("passed proof is nil")}
}
// Regenerate challenge string
c := GenerateChallenge(zkpcp, Base1.Bytes(), Result1.Bytes(),
Base2.Bytes(), Result2.Bytes(),
eqProof.UG.Bytes(), eqProof.UH.Bytes())
if c.Cmp(eqProof.Challenge) != 0 {
return false, &errorProof{"EquivalenceVerify", fmt.Sprintf("challenge comparison failed. proof: %v calculated: %v",
eqProof.Challenge, c)}
}
// sG ?= uG + cA
sG := zkpcp.Mult(Base1, eqProof.HiddenValue)
cG := zkpcp.Mult(Result1, eqProof.Challenge)
test := zkpcp.Add(eqProof.UG, cG)
if !sG.Equal(test) {
return false, &errorProof{"EquivalenceVerify", "sG comparison did not pass"}
}
// sH ?= uH + cB
sH := zkpcp.Mult(Base2, eqProof.HiddenValue)
cH := zkpcp.Mult(Result2, eqProof.Challenge)
test = zkpcp.Add(eqProof.UH, cH)
if !sH.Equal(test) {
return false, &errorProof{"EquivalenceVerify", "sH comparison did not pass"}
}
// All three checks passed, proof must be correct
return true, nil
}
// Bytes returns a byte slice with a serialized representation of EquivalenceProof proof
func (proof *EquivalenceProof) Bytes() []byte {
var buf bytes.Buffer
WriteECPoint(&buf, proof.UG)
WriteECPoint(&buf, proof.UH)
WriteBigInt(&buf, proof.Challenge)
WriteBigInt(&buf, proof.HiddenValue)
return buf.Bytes()
}
// NewEquivalenceProofFromBytes returns a EquivalenceProof generated from the
// deserialization of byte slice b
func NewEquivalenceProofFromBytes(b []byte) (*EquivalenceProof, error) {
proof := new(EquivalenceProof)
buf := bytes.NewBuffer(b)
proof.UG, _ = ReadECPoint(buf)
proof.UH, _ = ReadECPoint(buf)
proof.Challenge, _ = ReadBigInt(buf)
proof.HiddenValue, _ = ReadBigInt(buf)
return proof, nil
}