Skip to content

Commit 7641f25

Browse files
committed
Add golang support for ripemd160 and bitcoin hashes
1 parent 990ec27 commit 7641f25

File tree

5 files changed

+116
-45
lines changed

5 files changed

+116
-45
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
module github.com/confio/proofs
22

3-
require github.com/gogo/protobuf v1.2.1
3+
require (
4+
github.com/gogo/protobuf v1.2.1
5+
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
6+
)

go.sum

+7
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
22
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
33
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
44
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
5+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
6+
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 h1:p/H982KKEjUnLJkM3tt/LemDnOc1GiZL5FCVlORJ5zo=
7+
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
8+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
9+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
10+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
11+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
512
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

go/ops.go

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
// adds sha512 capability to crypto.SHA512
88
_ "crypto/sha512"
99
fmt "fmt"
10+
// adds ripemd160 capability to crypto.RIPEMD160
11+
_ "golang.org/x/crypto/ripemd160"
1012
)
1113

1214
func WrapLeaf(leaf *LeafOp) *ProofOp {
@@ -103,6 +105,18 @@ func doHash(hashOp HashOp, preimage []byte) ([]byte, error) {
103105
hash := crypto.SHA512.New()
104106
hash.Write(preimage)
105107
return hash.Sum(nil), nil
108+
case HashOp_RIPEMD160:
109+
hash := crypto.RIPEMD160.New()
110+
hash.Write(preimage)
111+
return hash.Sum(nil), nil
112+
case HashOp_BITCOIN:
113+
// ripemd160(sha256(x))
114+
sha := crypto.SHA256.New()
115+
sha.Write(preimage)
116+
tmp := sha.Sum(nil)
117+
hash := crypto.RIPEMD160.New()
118+
hash.Write(tmp)
119+
return hash.Sum(nil), nil
106120
}
107121
return nil, fmt.Errorf("Unsupported hashop: %d", hashOp)
108122
}

go/ops_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,46 @@ func TestInnerOp(t *testing.T) {
175175
}
176176
}
177177

178+
func TestDoHash(t *testing.T) {
179+
cases := map[string]struct {
180+
hashOp HashOp
181+
preimage string
182+
expectedHash string
183+
}{
184+
"sha256": {
185+
hashOp: HashOp_SHA256,
186+
preimage: "food",
187+
// echo -n food | sha256sum
188+
expectedHash: "c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b",
189+
},
190+
"ripemd160": {
191+
hashOp: HashOp_RIPEMD160,
192+
preimage: "food",
193+
// echo -n food | openssl dgst -rmd160 -hex | cut -d' ' -f2
194+
expectedHash: "b1ab9988c7c7c5ec4b2b291adfeeee10e77cdd46",
195+
},
196+
"bitcoin": {
197+
hashOp: HashOp_BITCOIN,
198+
preimage: "food",
199+
// echo -n c1f026582fe6e8cb620d0c85a72fe421ddded756662a8ec00ed4c297ad10676b | xxd -r -p | openssl dgst -rmd160 -hex
200+
expectedHash: "0bcb587dfb4fc10b36d57f2bba1878f139b75d24",
201+
},
202+
}
203+
204+
for name, tc := range cases {
205+
t.Run(name, func(t *testing.T) {
206+
res, err := doHash(tc.hashOp, []byte(tc.preimage))
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
hexRes := hex.EncodeToString(res)
211+
if hexRes != tc.expectedHash {
212+
t.Fatalf("Expected %s got %s", tc.expectedHash, hexRes)
213+
}
214+
})
215+
}
216+
}
217+
178218
func fromHex(data string) []byte {
179219
res, err := hex.DecodeString(data)
180220
if err != nil {

go/proofs.pb.go

+51-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)