www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - merkle reverse

reply aerto <cvsc gmx.com> writes:
This is the bitcoin genesis block merkle root 
4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b 
how i can get it at this format 
3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a 
??

i try it using

string merkle = 
"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";

writeln(merkle.retro.text); and it gives me

b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Apr 05 2018
next sibling parent reply SimonN <eiderdaus gmail.com> writes:
On Thursday, 5 April 2018 at 08:12:38 UTC, aerto wrote:
 This is the bitcoin genesis block merkle root 
 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b how i can get
it at this format 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a
1323a9fb8aa4b1e5e4a ??

 i try it using

 string merkle = 
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";

 writeln(merkle.retro.text); and it gives me

 b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Here's one solution with std.range.chunks. A small downside is that it needs the array allocation in the middle because chunks cannot offer the bi-directional range necessary for retro. import std.range; import std.algorithm; void main() { string merkle = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"; assert (merkle.retro.equal( "b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4")); assert (merkle.chunks(2).array.retro.joiner.equal( "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a")); } -- Simon
Apr 05 2018
next sibling parent drug <drug2004 bk.ru> writes:
05.04.2018 11:57, SimonN пишет:
 
 Here's one solution with std.range.chunks. A small downside is that it 
 needs the array allocation in the middle because chunks cannot offer the 
 bi-directional range necessary for retro.
 
      import std.range;
      import std.algorithm;
 
      void main()
      {
          string merkle =
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";
          assert (merkle.retro.equal(
 "b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4"));
          assert (merkle.chunks(2).array.retro.joiner.equal(
 "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a"));
      }
 
 -- Simon
It's a pity that retro can't work with chunks without array, it would be really nice!
Apr 05 2018
prev sibling parent reply Seb <seb wilzba.ch> writes:
On Thursday, 5 April 2018 at 08:57:11 UTC, SimonN wrote:
 On Thursday, 5 April 2018 at 08:12:38 UTC, aerto wrote:
 This is the bitcoin genesis block merkle root 
 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b how i can get
it at this format 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a
1323a9fb8aa4b1e5e4a ??

 i try it using

 string merkle = 
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";

 writeln(merkle.retro.text); and it gives me

 b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Here's one solution with std.range.chunks. A small downside is that it needs the array allocation in the middle because chunks cannot offer the bi-directional range necessary for retro. import std.range; import std.algorithm; void main() { string merkle = "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"; assert (merkle.retro.equal( "b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4")); assert (merkle.chunks(2).array.retro.joiner.equal( "3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a")); } -- Simon
FYI: The problem isn't chunks, but that strings aren't bi-directional ranges (hello ugly auto-decoding!). Simply disable it with byCodeUnit: --- import std.experimental.all; void main() { "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".byCodeUnit.chunks(2).retro.joiner.writeln; "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b" byCodeUnit.slide(2, 2).retro.joiner.writeln; } --- Oh and the new slide is a generalization of chunks. https://run.dlang.io/is/ggrh14
Apr 05 2018
parent reply SimonN <eiderdaus gmail.com> writes:
On Thursday, 5 April 2018 at 09:07:52 UTC, Seb wrote:
 FYI: The problem isn't chunks, but that strings aren't 
 bi-directional ranges (hello ugly auto-decoding!).
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".byCodeUnit
Thanks! Very appropriate because it's all hex digits anyway. Instead of std.experimental.all, one can also import std.utf. Initially, I wondered whether autodecoding was the issue here, but I dismissed it because the OP's example calls retro immediately on a string, which is supposedly not autodecodable as bi-directional. But I should examine retro's implementation because I remember several Phobos functions having special cases for strings (which is exactly the issue of auto-decoding). -- Simon
Apr 05 2018
parent reply Seb <seb wilzba.ch> writes:
On Thursday, 5 April 2018 at 09:24:31 UTC, SimonN wrote:
 On Thursday, 5 April 2018 at 09:07:52 UTC, Seb wrote:
 FYI: The problem isn't chunks, but that strings aren't 
 bi-directional ranges (hello ugly auto-decoding!).
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b".byCodeUnit
Thanks! Very appropriate because it's all hex digits anyway. Instead of std.experimental.all, one can also import std.utf. Initially, I wondered whether autodecoding was the issue here, but I dismissed it because the OP's example calls retro immediately on a string, which is supposedly not autodecodable as bi-directional. But I should examine retro's implementation because I remember several Phobos functions having special cases for strings (which is exactly the issue of auto-decoding).
Well sorry for my poor words. Strings are bi-directional ranges, but they aren't random-access nor have a length which is typically required by other ranges to forward bidirectionality. chunks requires hasSlicing + hasLength: https://github.com/dlang/phobos/blob/193c61d985a9645014b2161b59ddb8692308e063/std/range/package.d#L7851 slide requires just hasSlicing: https://github.com/dlang/phobos/blob/193c61d985a9645014b2161b59ddb8692308e063/std/range/package.d#L8650 Now why does "aa"[0.. 1] work, but hasSlicing return false? Because it's explicitly excluded: https://github.com/dlang/phobos/blob/193c61d985a9645014b2161b59ddb8692308e063/std/range/primitives.d#L1557 tl;dr: if you want to do anything useful with strings, byCodeUnit is usually your best friend.
Apr 05 2018
parent SimonN <eiderdaus gmail.com> writes:
On Thursday, 5 April 2018 at 09:49:58 UTC, Seb wrote:
 Strings are bi-directional ranges, but they aren't 
 random-access nor have a length
 chunks requires hasSlicing + hasLength:
Okay, thanks for the great references. chunks/slide certainly need the length to decide which, and how many, elements to serve in the final chunk. The crucial part is now that an autodecoded string's final element can be determined in O(1) by looking at up to 4 code units from its un-decoded end, whereas its autodecoded length cannot be found in O(1). -- Simon
Apr 05 2018
prev sibling parent reply Andy Smith <andyrsmith gmail.com> writes:
On Thursday, 5 April 2018 at 08:12:38 UTC, aerto wrote:
 This is the bitcoin genesis block merkle root 
 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b how i can get
it at this format 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a
1323a9fb8aa4b1e5e4a ??

 i try it using

 string merkle = 
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";

 writeln(merkle.retro.text); and it gives me

 b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Yeah..... Angela did some messing around with Shadow Volume algorithms before she got into politics... :-) Cheers, A.
Apr 05 2018
parent aerto <csvc gmx.com> writes:
On Thursday, 5 April 2018 at 14:58:21 UTC, Andy Smith wrote:
 On Thursday, 5 April 2018 at 08:12:38 UTC, aerto wrote:
 This is the bitcoin genesis block merkle root 
 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b how i can get
it at this format 3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a
1323a9fb8aa4b1e5e4a ??

 i try it using

 string merkle = 
 "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b";

 writeln(merkle.retro.text); and it gives me

 b33adedfa7b7212ba77cc2e37667f816f78cb13c88a81523a3f98baab4e1e5a4
Yeah..... Angela did some messing around with Shadow Volume algorithms before she got into politics... :-) Cheers, A.
Is not merkel is merkle, :P
Apr 05 2018