Skip to content

Commit 659b191

Browse files
committed
Fix blind signature expiration
- expiration now uses a different standard - expiration tests added - start bumped from 2020 to 2026
1 parent f878652 commit 659b191

2 files changed

Lines changed: 96 additions & 24 deletions

File tree

‎src/pyelliptic/eccblind.py‎

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,53 @@
2626

2727

2828
class Expiration(object):
29-
"""Expiration of pubkey"""
29+
"""Expiration of pubkey, encoded as months since epoch (Jan 2026).
30+
31+
Single byte encoding: 0 = Jan 2026, 255 = Apr 2047.
32+
Granularity of one month balances anonymity pool size
33+
against storage requirements for blind signature certificates.
34+
"""
35+
EPOCH_YEAR = 2026
36+
3037
@staticmethod
3138
def deserialize(val):
32-
"""Create an object out of int"""
33-
year = ((val & 0xF0) >> 4) + 2020
34-
month = val & 0x0F
35-
assert month < 12
36-
return Expiration(year, month)
39+
"""Create an object out of a serialized byte value"""
40+
return Expiration(val)
3741

38-
def __init__(self, year, month):
42+
@staticmethod
43+
def from_date(year, month_0indexed):
44+
"""Create from a year and 0-indexed month (0=Jan, 11=Dec)"""
3945
assert isinstance(year, int)
40-
assert year > 2019 and year < 2036
41-
assert isinstance(month, int)
42-
assert month < 12
43-
self.year = year
44-
self.month = month
45-
self.exp = year + month / 12.0
46+
assert isinstance(month_0indexed, int)
47+
assert 0 <= month_0indexed < 12
48+
raw = (year - Expiration.EPOCH_YEAR) * 12 + month_0indexed
49+
assert 0 <= raw <= 255
50+
return Expiration(raw)
51+
52+
def __init__(self, months_since_epoch):
53+
assert isinstance(months_since_epoch, int)
54+
assert 0 <= months_since_epoch <= 255
55+
self.raw = months_since_epoch
56+
57+
@property
58+
def year(self):
59+
"""Calendar year"""
60+
return self.EPOCH_YEAR + self.raw // 12
61+
62+
@property
63+
def month(self):
64+
"""0-indexed month (0=Jan, 11=Dec)"""
65+
return self.raw % 12
4666

4767
def serialize(self):
4868
"""Make int out of object"""
49-
return ((self.year - 2020) << 4) + self.month
69+
return self.raw
5070

5171
def verify(self):
5272
"""Check if the pubkey has expired"""
5373
now = time.gmtime()
54-
return self.exp >= now.tm_year + (now.tm_mon - 1) / 12.0
74+
current = (now.tm_year - self.EPOCH_YEAR) * 12 + (now.tm_mon - 1)
75+
return self.raw >= current
5576

5677

5778
class Value(object):
@@ -209,7 +230,7 @@ def pubkey(self):
209230
self._ec_point_serialize(self.Q))
210231

211232
def __init__(self, curve="secp256k1", pubkey=None, privkey=None,
212-
year=2025, month=11, value=0xFF):
233+
months_since_epoch=None, value=0xFF):
213234
self.ctx = OpenSSL.BN_CTX_new()
214235

215236
# ECC group
@@ -238,14 +259,12 @@ def __init__(self, curve="secp256k1", pubkey=None, privkey=None,
238259
else:
239260
# new keypair
240261
self.d, self.Q = self.ec_gen_keypair()
241-
if not year or not month:
262+
if months_since_epoch is None:
242263
now = time.gmtime()
243-
if now.tm_mon == 12:
244-
self.expiration = Expiration(now.tm_year + 1, 1)
245-
else:
246-
self.expiration = Expiration(now.tm_year, now.tm_mon + 1)
247-
else:
248-
self.expiration = Expiration(year, month)
264+
# one month from now
265+
months_since_epoch = \
266+
(now.tm_year - Expiration.EPOCH_YEAR) * 12 + now.tm_mon
267+
self.expiration = Expiration(months_since_epoch)
249268
self.value = Value(value)
250269

251270
def __del__(self):

‎src/pyelliptic/tests/test_blindsig.py‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
Test for ECC blind signatures
33
"""
44
import os
5+
import time
56
import unittest
67
from hashlib import sha256
78

89
try:
910
from pyelliptic import ECCBlind, ECCBlindChain, OpenSSL
11+
from pyelliptic.eccblind import Expiration
1012
except ImportError:
1113
from pybitmessage.pyelliptic import ECCBlind, ECCBlindChain, OpenSSL
14+
from pybitmessage.pyelliptic.eccblind import Expiration
1215

1316
# pylint: disable=protected-access
1417

@@ -121,9 +124,59 @@ def test_blind_sig_value(self):
121124
verifier_obj = ECCBlind(pubkey=signer_obj.pubkey())
122125
self.assertFalse(verifier_obj.verify(msg, signature, value=8))
123126

127+
def test_expiration_auto_generated(self):
128+
"""Test that auto-generated expiration is one month from now"""
129+
now = time.gmtime()
130+
obj = ECCBlind()
131+
expected = (now.tm_year - Expiration.EPOCH_YEAR) * 12 + now.tm_mon
132+
self.assertEqual(obj.expiration.raw, expected)
133+
self.assertTrue(obj.expiration.verify())
134+
135+
def test_expiration_future(self):
136+
"""Test that a future expiration verifies"""
137+
exp = Expiration(255)
138+
self.assertTrue(exp.verify())
139+
140+
def test_expiration_past(self):
141+
"""Test that a past expiration fails verification"""
142+
exp = Expiration(0) # Jan 2026
143+
self.assertFalse(exp.verify())
144+
145+
def test_expiration_serialize_roundtrip(self):
146+
"""Test expiration serialization/deserialization round-trip"""
147+
for raw in range(256):
148+
exp = Expiration(raw)
149+
serialized = exp.serialize()
150+
self.assertEqual(serialized, raw)
151+
exp2 = Expiration.deserialize(serialized)
152+
self.assertEqual(exp.raw, exp2.raw)
153+
self.assertEqual(exp.year, exp2.year)
154+
self.assertEqual(exp.month, exp2.month)
155+
156+
def test_expiration_from_date(self):
157+
"""Test creating expiration from year and month"""
158+
exp = Expiration.from_date(2026, 0) # Jan 2026
159+
self.assertEqual(exp.raw, 0)
160+
self.assertEqual(exp.year, 2026)
161+
self.assertEqual(exp.month, 0)
162+
163+
exp = Expiration.from_date(2027, 6) # Jul 2027
164+
self.assertEqual(exp.raw, 18)
165+
self.assertEqual(exp.year, 2027)
166+
self.assertEqual(exp.month, 6)
167+
168+
def test_expiration_year_month_properties(self):
169+
"""Test that year/month properties are consistent"""
170+
for raw in range(256):
171+
exp = Expiration(raw)
172+
self.assertEqual(
173+
(exp.year - Expiration.EPOCH_YEAR) * 12 + exp.month, raw)
174+
self.assertGreaterEqual(exp.month, 0)
175+
self.assertLess(exp.month, 12)
176+
124177
def test_blind_sig_expiration(self):
125178
"""Test blind signature expiration checking"""
126-
signer_obj = ECCBlind(year=2020, month=1)
179+
signer_obj = ECCBlind(months_since_epoch=0) # Jan 2026, already expired
127180
point_r = signer_obj.signer_init()
128181
requester_obj = ECCBlind(pubkey=signer_obj.pubkey())
129182
msg = os.urandom(64)

0 commit comments

Comments
 (0)