Skip to content

Commit 17fdcc4

Browse files
committed
zk/qndleq: Fix challenge in qndleq.
1 parent 9798df7 commit 17fdcc4

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

zk/qndleq/internal_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,24 @@ func TestForgedProofSecParamZero(t *testing.T) {
4949

5050
test.CheckOk(!forged.Verify(g, gx, h, hx, N), "forged proof must be rejected", t)
5151
}
52+
53+
func TestChallenge(t *testing.T) {
54+
g, gx := big.NewInt(4), big.NewInt(16)
55+
h, hx := big.NewInt(9), big.NewInt(81)
56+
gP := big.NewInt(50)
57+
hP := big.NewInt(60)
58+
N := big.NewInt(101)
59+
60+
invalidValues := []*big.Int{
61+
new(big.Int).Neg(g), // Negative
62+
big.NewInt(0), // Zero
63+
new(big.Int).Set(N), // N
64+
new(big.Int).Add(N, N), // bigger than N
65+
}
66+
67+
for _, invalidValue := range invalidValues {
68+
c, err := doChallenge(invalidValue, gx, h, hx, gP, hP, N, 128)
69+
test.CheckIsErr(t, err, "doChallenge must fail")
70+
test.CheckOk(c == nil, "challenge must be nil", t)
71+
}
72+
}

zk/qndleq/qndleq.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,18 @@ func doChallenge(g, gx, h, hx, gP, hP, N *big.Int, secParam uint) (*big.Int, err
127127
return nil, ErrSecParam
128128
}
129129

130+
err := checkBounds(N, g, gx, h, hx, gP, hP)
131+
if err != nil {
132+
return nil, err
133+
}
134+
130135
modulusLenBytes := (N.BitLen() + 7) / 8
131136
nBytes := make([]byte, modulusLenBytes)
132137
cByteLen := (secParam + 7) / 8
133138
cBytes := make([]byte, cByteLen)
134139

135140
H := sha3.NewShake256()
136-
_, err := H.Write(g.FillBytes(nBytes))
141+
_, err = H.Write(g.FillBytes(nBytes))
137142
if err != nil {
138143
return nil, err
139144
}
@@ -171,5 +176,21 @@ func doChallenge(g, gx, h, hx, gP, hP, N *big.Int, secParam uint) (*big.Int, err
171176
return new(big.Int).SetBytes(cBytes), nil
172177
}
173178

174-
// ErrSecParam is returned when the security parameter is less than 128.
175-
var ErrSecParam = errors.New("zk/qndleq: the security parameter must be greater than 128")
179+
// checkBounds returns nil if 0 < x[i] < N for all 0 <= i < len(x);
180+
// otherwise, returns ErrBounds.
181+
func checkBounds(N *big.Int, x ...*big.Int) error {
182+
for _, xi := range x {
183+
if !(0 < xi.Sign() && xi.Cmp(N) < 0) {
184+
return ErrBounds
185+
}
186+
}
187+
188+
return nil
189+
}
190+
191+
var (
192+
// ErrSecParam is returned when the security parameter is less than 128.
193+
ErrSecParam = errors.New("zk/qndleq: the security parameter must be greater than 128")
194+
// ErrBounds is returned when a value is not in the range 0 to N.
195+
ErrBounds = errors.New("zk/qndleq: input must be greater than 0 and less than N")
196+
)

zk/qndleq/qndleq_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ func TestProve(t *testing.T) {
3434
}
3535
}
3636

37+
func TestInvalidStatement(t *testing.T) {
38+
g, gx := big.NewInt(4), big.NewInt(16) // 4^2 == 16 mod 101
39+
h, hx := big.NewInt(9), big.NewInt(81) // 9^2 == 81 mod 101
40+
N := big.NewInt(101)
41+
incorrectX := big.NewInt(3)
42+
43+
p, err := qndleq.Prove(rand.Reader, incorrectX, g, gx, h, hx, N, 128)
44+
test.CheckNoErr(t, err, "an alleged proof must be computed")
45+
isValid := p.Verify(g, gx, h, hx, N)
46+
test.CheckOk(isValid == false, "proof verification must fail", t)
47+
}
48+
3749
func TestSampleQn(t *testing.T) {
3850
const testTimes = 1 << 7
3951
one := big.NewInt(1)

0 commit comments

Comments
 (0)