<s> """<STR_LIT>""" <EOL> from . encoding import double_sha256 <EOL> def merkle ( hashes , hash_f = double_sha256 ) : <EOL> """<STR_LIT>""" <EOL> while len ( hashes ) > <NUM_LIT:1> : <EOL> hashes = merkle_pair ( hashes , hash_f ) <EOL> return hashes [ <NUM_LIT:0> ] <EOL> def merkle_pair ( hashes , hash_f ) : <EOL> """<STR_LIT>""" <EOL> if len ( hashes ) % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> hashes = list ( hashes ) <EOL> hashes . append ( hashes [ - <NUM_LIT:1> ] ) <EOL> l = [ ] <EOL> for i in range ( <NUM_LIT:0> , len ( hashes ) , <NUM_LIT:2> ) : <EOL> l . append ( hash_f ( hashes [ i ] + hashes [ i + <NUM_LIT:1> ] ) ) <EOL> return l <EOL> def test_merkle ( ) : <EOL> from . encoding import h2b <EOL> def to_bin ( s ) : <EOL> return bytes ( reversed ( h2b ( s ) ) ) <EOL> s1 = to_bin ( "<STR_LIT>" ) <EOL> assert merkle ( [ s1 ] , double_sha256 ) == s1 <EOL> mr = to_bin ( "<STR_LIT>" ) <EOL> s1 = to_bin ( "<STR_LIT>" ) <EOL> s2 = to_bin ( "<STR_LIT>" ) <EOL> assert merkle ( [ s1 , s2 ] , double_sha256 ) == mr <EOL> mr = to_bin ( "<STR_LIT>" ) <EOL> s1 = to_bin ( "<STR_LIT>" ) <EOL> s2 = to_bin ( "<STR_LIT>" ) <EOL> s3 = to_bin ( "<STR_LIT>" ) <EOL> assert merkle ( [ s1 , s2 , s3 ] , double_sha256 ) == mr <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_merkle ( ) </s>
