[erlang-questions] A pointless problem?

Dmitry Kolesnikov dmkolesnikov@REDACTED
Sat Nov 17 18:28:42 CET 2012


Hello,

This looks as endianness problem. You have to keep in-mind that binaries is big-endian by default, which contrasts with C memory. You have to be very careful of C algorithms mapping.

I've made a small fixes to int32list_xxx routines (see bold text):

int32list_from_binary(Bin) ->
	int32list_from_binary(Bin, []).
int32list_from_binary(<<X:32/little, Bin/binary>>, Acc) ->
	int32list_from_binary(Bin, [X|Acc]);
int32list_from_binary(<<>>, Acc) ->
	lists:reverse(Acc).

int32list_to_binary(List) ->	
	list_to_binary([<<X:32/little>> || X <- List]).

Some of tests got passed. Unfortunately, some of tests are failed I hope you can easily validate rest of you code agains endian.  

{test,true,true,{<<"ab043705808c5d57">>,<<"ab043705808c5d57">>}}
{test,false,true,{<<"26e3868b9d66a048">>,<<"d1e78be2c746728a">>}}
{test,false,true,{<<"2a70e36b99941f2d">>,<<"67ed0ea8e8973fc5">>}}
{test,false,true,{<<"a30d3870c0873c23">>,<<"8c3707c01c7fccc4">>}}
{test,true,true,
      {<<"b2601cefb078b772abccba6a">>,<<"b2601cefb078b772abccba6a">>}}
{test,false,true,
      {<<"ddbcb4b88b2c5a268081a8b4">>,<<"579016d143ed6247ac6710dd">>}}
{test,true,true,
      {<<"c0a19f06ebb0d63925aa27f74cc6b2d0">>,
       <<"c0a19f06ebb0d63925aa27f74cc6b2d0">>}}
{test,false,true,
      {<<"bd28749d4fc20b73a79d59a25d55ab27">>,
       <<"01b815fd2e4894d13555da434c9d868a">>}}

Best Regards,
Dmitry


On Nov 17, 2012, at 7:02 PM, Steve Davis wrote:

> I am intrigued by what is arguably a pointless problem. 
> 
> Looking at the tiny encryption algorithm aka XXTEA it seemed easy enough to implement in Erlang. I have an implementation (attached with source and all references inside the module) which encodes and decodes successfully... BUT the cipher text doesn't match the test vectors...
> 
> What makes this pointless is that I should "just make it a NIF" etc. But it's likely that I am misunderstanding some aspect of either Erlang or C, however I am confounded as to what that problem is... why does the attached implementation not generate the same ciphertext as in the test vectors from the C implementation? Can anyone spot my error?
> 
> Thanks,
> /s
> 
> <xxtea.erl.txt>_______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20121117/b2de5298/attachment.htm>


More information about the erlang-questions mailing list