-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommitment.coffee
31 lines (28 loc) · 1.32 KB
/
commitment.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Commitments are a cryptographic method of allowing somebody to "commit" to a
# value without revealing what that value is (confidential). Later in time,
# they admit the value that they committed to and a "decommitment" so third
# parties can verify that they have not tried to change the value they committed
# to initially.
#
# http://en.wikipedia.org/wiki/Commitment_scheme#Coin_flipping
ursa = require 'ursa'
key = require './key'
# Makes a commitment. Returns the pair (commitment, decommitment). As above
# indicates, the commitment should be published immediately. The decommitment
# should be published along with the commited item later in time.
#
# 1. `info` is the value to be commited to. (I choose heads.) *(Buffer)*
exports.make = (info) ->
privKey = key.createPrivate()
sig = privKey.hashAndSign 'sha512', info
[sig, privKey.toPublicPem()]
# Verifies a commitment. Returns true or false.
#
# 1. `cand` is the candidate value admitted by the committer. (He says he chose
# heads.) *(Buffer)*
# 2. `commitment` is the commitment they published. *(Buffer)*
# 3. `decommitment` is the value published with the candidate as proof of
# commitment.
exports.verify = (candidate, commitment, decommitment) ->
pubKey = ursa.createPublicKey decommitment
pubKey.hashAndVerify 'sha512', candidate, commitment