Skip to content

Commit eb8deca

Browse files
committed
Added open method to return message and verity to return bool
1 parent 9aa841f commit eb8deca

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

fcs/fcs.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
import hashlib
44
import secrets
5-
from typing import Any, Callable, Generic, Optional, Tuple, TypeVar
5+
from typing import Any, Callable, Generic, Optional, TypeVar
66

77
from BitVector import BitVector
88
import bchlib
@@ -124,9 +124,9 @@ def commit(self, witness: K,
124124
self._extractor(witness))
125125
return commitment
126126

127-
def _verify_raw(self, commitment: 'Commitment',
128-
candidate: BitVector) -> Tuple[bool, bytes]:
129-
"""See verify."""
127+
def _open_raw(self, commitment: 'Commitment',
128+
candidate: BitVector) -> Optional[bytes]:
129+
"""See open."""
130130
codeword_cand = (commitment.auxiliar.reverse()
131131
^ candidate.reverse()).reverse()
132132
codeword_cand_bytes = bytes.fromhex(
@@ -140,20 +140,36 @@ def _verify_raw(self, commitment: 'Commitment',
140140
hashlib.sha256(msg_cand).digest()
141141
)
142142
# use & for constant time and (no shortcut)
143-
return (msg_match & (bitflips != -1), msg_cand)
143+
is_valid = msg_match & (bitflips != -1)
144+
return bytes(msg_cand) if is_valid else None
145+
146+
def open(self, commitment: 'Commitment',
147+
candidate: K) -> Optional[bytes]:
148+
"""Tries to opens the commitment with the candidate.
149+
150+
Args:
151+
commitment: Commitment to open.
152+
candidate: Candidate to open with.
153+
154+
Returns:
155+
The committed message if the candidate is close enough to the
156+
commitment or None otherwise.
157+
"""
158+
return self._open_raw(commitment, self._extractor(candidate))
144159

145160
def verify(self, commitment: 'Commitment',
146-
candidate: K) -> Tuple[bool, bytes]:
161+
candidate: K) -> bool:
147162
"""Verifies the given candidate against the commitment.
148163
149164
Args:
150-
commitment:
151-
candidate:
165+
commitment: Commitment to verify against.
166+
candidate: Candidate to verify.
152167
153168
Returns:
154-
True if the candidate matches the commitment, false otherwise.
169+
True if the candidate is close enough to the commitment, False
170+
otherwise.
155171
"""
156-
return self._verify_raw(commitment, self._extractor(candidate))
172+
return self.open(commitment, candidate) is not None
157173

158174

159175
class Commitment(object):

fcs/test_fcs.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ def setUp(self):
2424
self.commitment = self.cs.commit(self.witness)
2525

2626
def test_unaltered_witness(self):
27-
valid, _key = self.cs.verify(self.commitment, self.witness)
27+
valid = self.cs.verify(self.commitment, self.witness)
2828
self.assertTrue(valid)
2929

3030
def test_altered_tolerable(self):
3131
witness_mod = random_flip(self.witness, self.threshold)
32-
valid, _key = self.cs.verify(self.commitment, witness_mod)
32+
valid = self.cs.verify(self.commitment, witness_mod)
3333
self.assertTrue(valid)
3434

3535
def test_altered_intolerable(self):
3636
witness_mod = random_flip(self.witness, self.threshold + 1)
37-
valid, _key = self.cs.verify(self.commitment, witness_mod)
37+
valid = self.cs.verify(self.commitment, witness_mod)
3838
self.assertFalse(valid)
3939

4040

@@ -47,17 +47,17 @@ def setUp(self):
4747
self.commitment = self.cs.commit(self.witness)
4848

4949
def test_unaltered_witness(self):
50-
valid, _key = self.cs.verify(self.commitment, self.witness)
50+
valid = self.cs.verify(self.commitment, self.witness)
5151
self.assertTrue(valid)
5252

5353
def test_altered_tolerable(self):
5454
witness_mod = random_flip(self.witness, self.threshold)
55-
valid, _key = self.cs.verify(self.commitment, witness_mod)
55+
valid = self.cs.verify(self.commitment, witness_mod)
5656
self.assertTrue(valid)
5757

5858
def test_altered_intolerable(self):
5959
witness_mod = random_flip(self.witness, self.threshold+1)
60-
valid, _key = self.cs.verify(self.commitment, witness_mod)
60+
valid = self.cs.verify(self.commitment, witness_mod)
6161
self.assertFalse(valid)
6262

6363

@@ -78,19 +78,16 @@ def setUp(self):
7878
self.commitment = self.cs.commit(self.witness, message=self.message)
7979

8080
def test_unaltered_witness(self):
81-
valid, msg = self.cs.verify(self.commitment, self.witness)
82-
self.assertTrue(valid)
81+
msg = self.cs.open(self.commitment, self.witness)
8382
self.assertEqual(msg, self.message)
8483

8584
def test_altered_tolerable(self):
86-
valid, msg = self.cs.verify(self.commitment, 2) # one bit changed
87-
self.assertTrue(valid)
85+
msg = self.cs.open(self.commitment, 2) # one bit changed
8886
self.assertEqual(msg, self.message)
8987

9088
def test_altered_intolerable(self):
91-
valid, msg = self.cs.verify(self.commitment, 4) # three bits changed
92-
self.assertFalse(valid)
93-
self.assertNotEqual(msg, self.message)
89+
msg = self.cs.open(self.commitment, 4) # three bits changed
90+
self.assertEqual(msg, None)
9491

9592

9693
if __name__ == '__main__':

0 commit comments

Comments
 (0)