## Resolve this algorithm?

Scope and XITE Development

Original topic on PlanetZ: https://forums.scopeusers.com/viewtopic.php?t=14275

####

Posted: Sat Nov 06, 2004 1:26 pm
by nitri
Hello, I´m attempt to cofig Logic control, I understand all, but not this algorithm.
Header: F0 00 00 66 10
Host send: <Hdr> 00 (F7) Device Query
Logic Control send: <Hdr> 01 ss ss ss ss ss ss ss ll ll ll ll (F7) Host connection Query
Host must calculate four variables.
Host send : <Hdr> 02 ss ss ss ss ss ss ss rr rr rr rr (F7) Host Connection Reply
ss = Serial number (7 bytes)
ll = Challenge code (4 bytes) Random
rr = Response code (4 bytes)
Algorithm:
l1 to l4 = challenge code bytes 1 to 4
r1 to r4 = response code bytes 1 to 4
r1 = 0x7F & (l1+(l2^0xA)-l4);
r2 = 0x7F & ((l3>>4)^(l1+l4));
r3 = 0x7F & (l4-(l3<<2)^(l1|l2));
r4 = 0x7F & (l2-l3+(0xF0^(l4<<4)));
My doubt;
& = and?
>> = ? I think = bit´s to Right
example l3 = 0x23 '100011' >>4 = 000010 ???
<< = ? I think = bit´s to left
example l3 = 0x23 '100011' <<2 = 001100 ???
| = or?
This operators in Windows calculator?
& = key 'And' It´s correct?
^ = key 'x^y' it´s correct?
| = key 'or' it´s correct?
<< and >> = key '?'
Can you calculate a example for me?
l1 = 77, l2 = 3B, l3 = 23, l4 = 0C
Thanks!

####

Posted: Mon Nov 08, 2004 5:08 am
by Counterparts

> nitri wrote:
> r1 = 0x7F & (l1+(l2^0xA)-l4);
> r2 = 0x7F & ((l3>>4)^(l1+l4));
> r3 = 0x7F & (l4-(l3<<2)^(l1|l2));
> r4 = 0x7F & (l2-l3+(0xF0^(l4<<4)));
> My doubt;
> & = and?

yes, bit-wise and operator
e.g. 10001001 & 10000001 = 10000001

> >> = ? I think = bit´s to Right
> example l3 = 0x23 '100011' >>4 = 000010 ???

yes - right-shift bits
e.g. 11110000 >> 4 is 00001111

> << = ? I think = bit´s to left
> example l3 = 0x23 '100011' <<2 = 001100 ???

yes
edit: one usually deals with 8-bit bytes, so your example might be better expressed as:
00100011 << 2 = 100001100

> | = or?
> ^ = key 'x^y' it´s correct?

bit-wise or & bit-wise exclusive or
or: 11110000 | 00001111 = 11111111
xor: 11110000 ^ 11000011 = 11000011

> This operators in Windows calculator?

no, not all

> Can you calculate a example for me?
> l1 = 77, l2 = 3B, l3 = 23, l4 = 0C

I'll do one line for you:
r3 = 0x7F & (l4-(l3<<2)^(l1|l2));
(btw, it'd make reading MUCH easier if you use a capital 'L', not the lower-case one!)
Compare: L1 & l1
Are all the values you're giving hex? It'd also help to make that clear: write 0x77, not 77
L1 | L2 = 0x77 | 0x3b = 0x3f
L3 << 2 = 0x23 << 2 = 0x8c
At this point, one needs to know whether the exclusive-or takes place before the subtraction - is this dependant upon operator precedence, do you know, or is it left-to-right processing?
i.e. is the sum
r3 = 0x7F & ((l4-(l3<<2))^(l1|l2));
or
r3 = 0x7F & (l4-((l3<<2)^(l1|l2)));
??
IF we're strictly using operator precedence, then in fact '-' has a higher precedence than '^', so the next stage is:
L4 - (L3 << 2) = 0x0c - 0x8c = 0x80 (IF we're using byte-sized arithmetic!)
edit: i.e. 0x80 represents -128
(sorry, got the sum wrong first tiome!)
The next stage is to X-or the two together:
0x80 ^ 0x3f = 0xbf
I think you need to know what the order of calculation in the equations is before you can calculate them properly.
edit: actually, both operator-precedence and left-to-right processing would dictate that you perform the subtraction before the exclusive-or.
Royston

####

Posted: Mon Nov 08, 2004 7:41 am
by nitri
thousand thanks!