以人为本

Core developer of Mixin Network. Passionate about security and privacy.

Bitcoin Block Hash As a Random Number

Jul 02, 2020

Yesterday I announced a simple tournament to credit my followers with some cryptos, they need to favorite my status updates and the next day I will pick the winner by the last Bitcoin block hash before 12:00 midnight. Lots of people joined but still not many enough to match that large hash number, so I need to do a basic modulo operation.

Bitcoin hash

The qualified Bitcoin block is 637196 according to the rules, so I need to manipulate its hash. Golang is my primary language recently, it’s a shame that no direct native big number support in Golang, so I made this computation by a few lines of Go code.

hash := "0000000000000000000ec2e8aee4ab4141e13edff2bf20482ebd82b18baaef9a"
num, ok := new(big.Int).SetString(hash, 16)
if !ok {
	panic("no way!")
}
mod := new(big.Int).Mod(num, big.NewInt(256))
fmt.Println(mod.Int64())

=> 154

Then I wonder what is a better way to do this without writing any code.

JavaScript in Firefox Console gives out incorrect result, expected.

0x0000000000000000000ec2e8aee4ab4141e13edff2bf20482ebd82b18baaef9a%256 
=> 0

Bash is savage!

echo $((0x0000000000000000000ec2e8aee4ab4141e13edff2bf20482ebd82b18baaef9a%256))
=> 154

Ruby irb is always good at script and numbers.

0x0000000000000000000ec2e8aee4ab4141e13edff2bf20482ebd82b18baaef9a%256
=> 154

Python will never disappoint me on number operations neither.

0x0000000000000000000ec2e8aee4ab4141e13edff2bf20482ebd82b18baaef9a%256
=> 154

All I need to do is send some cryptos to the 154th person in this huge list.

OhMy XIN likes

About the Author

Core developer of Mixin Network. Passionate about security and privacy. Strive to formulate elegant code, simple design and friendly machine.

25566 @ Mixin Messenger

[email protected]