Skip to content

Commit 4e21b4b

Browse files
committed
core: add OpSecp256k1Commitment
1 parent 21937db commit 4e21b4b

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

opentimestamps/core/op.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,6 @@ def _do_op_call(self, msg):
347347
r = sha3.keccak_256(bytes(msg)).digest()
348348
assert len(r) == self.DIGEST_LENGTH
349349
return r
350+
351+
from opentimestamps.core.secp256k1 import OpSecp256k1Commitment
352+

opentimestamps/core/secp256k1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@
99
# modified, propagated, or distributed except according to the terms contained
1010
# in the LICENSE file.
1111

12+
import hashlib
13+
14+
from opentimestamps.core.op import BinaryOp, MsgValueError
15+
16+
@BinaryOp._register_op
17+
class OpSecp256k1Commitment(BinaryOp):
18+
"""Execute the map commit -> [P + sha256(P||commit)G]_x for a given secp256k1 point P"""
19+
TAG = b'\x09'
20+
TAG_NAME = 'secp256k1commitment'
21+
22+
def _do_op_call(self, msg):
23+
hasher = hashlib.sha256()
24+
pt = Point.decode(self[0])
25+
hasher.update(pt.encode())
26+
hasher.update(msg)
27+
tweak = int.from_bytes(hasher.digest(), 'big')
28+
tweak_pt = SECP256K1_GEN.scalar_mul(tweak)
29+
final_pt = pt.add(tweak_pt)
30+
return final_pt.x.to_bytes(32, 'big')
31+
32+
1233
## What follows is a lot of inefficient but explicit secp256k1 math
1334
class Point(object):
1435
inf = True

opentimestamps/tests/core/test_secp256k1.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# modified, propagated, or distributed except according to the terms contained
1010
# in the LICENSE file.
1111

12+
import hashlib
1213
import binascii
1314
import unittest
1415

@@ -20,7 +21,7 @@ def test_point_rt(self):
2021
gen = SECP256K1_GEN
2122
encode = gen.encode()
2223
self.assertEqual(encode, binascii.unhexlify("0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"))
23-
gen2 = Point().decode(encode)
24+
gen2 = Point.decode(encode)
2425
self.assertEqual(gen, gen2)
2526

2627
def test_pinv(self):
@@ -99,3 +100,9 @@ def test_scalar_mul(self):
99100
self.assertEqual(p2.scalar_mul(-1), np2)
100101
self.assertEqual(p1.scalar_mul(3), p3)
101102

103+
def test_op_signtocontract(self):
104+
pt_encode = binascii.unhexlify("0308aec434612f56df3f02c4e678260424415882ebd3efc16d52e3f9c1e39afdb0")
105+
msg = hashlib.sha256("This is andytoshi on 2017-05-16 21:30 UTC".encode()).digest()
106+
result = binascii.unhexlify("d386ef692770fcecad43362cf541858662e4ebe31d3ad04d196f94168897947a")
107+
self.assertEqual(OpSecp256k1Commitment(pt_encode)(msg), result)
108+

0 commit comments

Comments
 (0)