Skip to content

Commit

Permalink
Merge pull request #1 from petertodd/2017-05-18-op-secp256k1commit
Browse files Browse the repository at this point in the history
Make OpSecp256k1Commitment a unary op
  • Loading branch information
apoelstra authored May 20, 2017
2 parents 3539778 + 1d8b6ea commit e3c3f60
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
Binary file modified examples/andytoshi.ots
Binary file not shown.
22 changes: 16 additions & 6 deletions opentimestamps/core/secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,29 @@

import hashlib

from opentimestamps.core.op import BinaryOp, MsgValueError
from opentimestamps.core.op import UnaryOp, MsgValueError

@BinaryOp._register_op
class OpSecp256k1Commitment(BinaryOp):
"""Execute the map commit -> [P + sha256(P||commit)G]_x for a given secp256k1 point P"""
@UnaryOp._register_op
class OpSecp256k1Commitment(UnaryOp):
"""Map (P || commit) -> [P + sha256(P||commit)G]_x for a given secp256k1 point P
This is a unary op rather than a binary op to allow timestamps to also
timestamp the point itself; in the event of an ECC break this might be
relevant. Such a break would not affect the integrity of the commitment,
but knowledge of the underlying key may be interesting in its own right.
"""
TAG = b'\x09'
TAG_NAME = 'secp256k1commitment'

def _do_op_call(self, msg):
if len(msg) < 33:
raise MsgValueError("Missing secp256k1 point")

pt = Point.decode(msg[0:33])

hasher = hashlib.sha256()
pt = Point.decode(self[0])
hasher.update(pt.encode())
hasher.update(msg)
hasher.update(msg[33:])
tweak = int.from_bytes(hasher.digest(), 'big')
tweak_pt = SECP256K1_GEN.scalar_mul(tweak)
final_pt = pt.add(tweak_pt)
Expand Down
2 changes: 1 addition & 1 deletion opentimestamps/tests/core/test_secp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,5 @@ def test_op_signtocontract(self):
pt_encode = binascii.unhexlify("0308aec434612f56df3f02c4e678260424415882ebd3efc16d52e3f9c1e39afdb0")
msg = hashlib.sha256("This is andytoshi on 2017-05-16 21:30 UTC".encode()).digest()
result = binascii.unhexlify("d386ef692770fcecad43362cf541858662e4ebe31d3ad04d196f94168897947a")
self.assertEqual(OpSecp256k1Commitment(pt_encode)(msg), result)
self.assertEqual(OpSecp256k1Commitment()(pt_encode + msg), result)

0 comments on commit e3c3f60

Please sign in to comment.