Skip to content

Commit ddac3b2

Browse files
committed
Migrate existing code
1 parent 9ae7796 commit ddac3b2

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

cmd/cli/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "fmt"
4+
5+
6+
func main() {
7+
fmt.Printf("Hello %s\n", "World")
8+
}

pkg/hash/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package hash
2+
3+
import "fmt"
4+
5+
// ErrSampleNonce is returned if the random nonce can't be sampled.
6+
var ErrSampleNonce = fmt.Errorf("unable to sample random nonce")

pkg/hash/hash.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package hash
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/sha256"
6+
"io"
7+
)
8+
9+
// Commitment is an instance of a commitment.
10+
type Commitment struct {
11+
hash [32]byte
12+
nonce [32]byte
13+
}
14+
15+
// NewCommitment creates a new instance of a commitment.
16+
func NewCommitment(hash [32]byte, nonce [32]byte) *Commitment {
17+
return &Commitment{
18+
hash: hash,
19+
nonce: nonce,
20+
}
21+
}
22+
23+
// Commit creates a commitment that commits to arbitrary data.
24+
// Returns an error if the nonce generation fails.
25+
func Commit(data ...[]byte) (*Commitment, error) {
26+
var nonce [32]byte
27+
_, err := io.ReadFull(rand.Reader, nonce[:])
28+
if err != nil {
29+
return nil, ErrSampleNonce
30+
}
31+
32+
hash := generateCommitment(nonce, data...)
33+
34+
pair := NewCommitment(hash, nonce)
35+
36+
return pair, nil
37+
}
38+
39+
// Verify verifies the correctness of a commitment to data.
40+
func Verify(pair *Commitment, data ...[]byte) bool {
41+
hash := generateCommitment(pair.nonce, data...)
42+
43+
return hash == pair.hash
44+
}
45+
46+
// generateCommitment creates the commitment by hashing the data and nonce
47+
// using SHA-256.
48+
func generateCommitment(nonce [32]byte, data ...[]byte) [32]byte {
49+
var bz []byte
50+
51+
// Data.
52+
for _, d := range data {
53+
bz = append(bz, d...)
54+
}
55+
56+
// Nonce.
57+
bz = append(bz, nonce[:]...)
58+
59+
return sha256.Sum256(bz)
60+
}

pkg/hash/hash_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package hash_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/primefactor-io/commitment/pkg/hash"
7+
)
8+
9+
func TestCommitVerify(t *testing.T) {
10+
t.Parallel()
11+
12+
t.Run("Commit / Verify - Valid (single-data)", func(t *testing.T) {
13+
t.Parallel()
14+
15+
data := []byte("Hello World")
16+
17+
comm, _ := hash.Commit(data)
18+
isValid := hash.Verify(comm, data)
19+
20+
if isValid != true {
21+
t.Error("Commitment verification failed")
22+
}
23+
})
24+
25+
t.Run("Commit / Verify - Valid (multi-data)", func(t *testing.T) {
26+
t.Parallel()
27+
28+
data1 := []byte("Hello")
29+
data2 := []byte("World")
30+
31+
comm, _ := hash.Commit(data1, data2)
32+
isValid := hash.Verify(comm, data1, data2)
33+
34+
if isValid != true {
35+
t.Error("Commitment verification failed")
36+
}
37+
})
38+
39+
t.Run("Commit / Verify - Invalid (single-data)", func(t *testing.T) {
40+
t.Parallel()
41+
42+
data1 := []byte("Hello World")
43+
data2 := []byte("Hello, World")
44+
45+
comm, _ := hash.Commit(data1)
46+
isValid := hash.Verify(comm, data2)
47+
48+
if isValid != false {
49+
t.Error("Commitment verification failed")
50+
}
51+
})
52+
53+
t.Run("Commit / Verify - Invalid (multi-data)", func(t *testing.T) {
54+
t.Parallel()
55+
56+
data1 := []byte("Hello")
57+
data2 := []byte("World")
58+
data3 := []byte(", World")
59+
60+
comm, _ := hash.Commit(data1, data2)
61+
isValid := hash.Verify(comm, data1, data3)
62+
63+
if isValid != false {
64+
t.Error("Commitment verification failed")
65+
}
66+
})
67+
}

0 commit comments

Comments
 (0)