Go Down

### Topic: Difference code arduino-Pic (Read 3577 times)previous topic - next topic

#### enrico88

##### Jul 16, 2012, 11:39 amLast Edit: Jul 16, 2012, 11:41 am by enrico88 Reason: 1
Hi,I'm a problem with the code of TEA algorithm. I have implemented  the algorithm for arduino mega 2560 and for PIC16f887.

This my code for arduino,It is write with ide Arduino 1.0:

Code: [Select]
`unsigned int myInt[1];unsigned int k [4] = {0,2,3,4};void setup(){    Serial.begin(9600);}void encrypt (unsigned int* v, unsigned int* k) {    unsigned int v0,v1, sum, i;    unsigned int delta;    unsigned int k0, k1, k2, k3;   /* chiave */    delta=0x9e3779b9;    v0=v[0];    v1=v[1];    sum=0;    k0=k[0];    k1=k[1];    k2=k[2];    k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void loop(){                   myInt[0]=4;         Serial.println(myInt[0],DEC);             encrypt(myInt,k);         Serial.println(myInt[0],DEC);         delay(1000);     }`

This my code for PIC16F887,It is write with Mikro C pro for pic (version 1.65):
Code: [Select]
`unsigned int myInt[1];unsigned int k [4] = {0,2,3,4};void encrypt (unsigned int* v, unsigned int* k) {    unsigned int v0,v1, sum, i;    unsigned int k0, k1, k2,k3;    unsigned int delta;    v0=v[0]; v1=v[1]; sum=0;     delta=0x9e3779b9;    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0);// ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2);// ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void main(){UART1_Init(9600);myInt[0]=4;UART1_Write(myInt[0]);encrypt(myInt,k);UART1_Write(myInt[0]);delay_ms(1000);}`

When i execute the codes i have two different coding of the same number...what is the difference?
Please can someone help me.... thanks a lot

#### majenko

#1
##### Jul 16, 2012, 11:46 am
Quote

delta=0x9e3779b9;

Delta is an unsigned int - that number is an unsigned long.  It won't fit.

#### PaulS

#2
##### Jul 16, 2012, 11:47 am
Quote
Hi,I'm a problem with the code of TEA algorithm.

You are assuming that everyone knows what a TEA algorithm is. That is not a valid assumption. At the very least, you should provide a link.

Code: [Select]
`    unsigned int v0,v1, sum, i;    unsigned int delta;    delta=0x9e3779b9;    for (i=0; i < 32; i++) {        sum += delta;`
You are effectively multiplying 0x9e3779b9 by 32. First, 0x9e3779b9 is NOT an int. That value is 2654435769 in decimal, which won't even come close to fitting in an int, signed or unsigned. Multiplying it be 32 and expecting it to fit in an int is unreasonable.

#### bubulindo

#3
##### Jul 16, 2012, 11:47 amLast Edit: Jul 16, 2012, 11:50 am by bubulindo Reason: 1
Showing what are the numbers you get with both examples would be great...

The one thing I see that may cause this is the delta variable, you defined it as an unsigned int (approx. 65000) and then place a huge number on it. If the MikroC compiler deals with this in a different way (casting the type instead of truncating it), then it could explain the difference. MikroC may have unsigned int as a 32 bit variable unlike Arduino.

Try defining delta and all other related variables as unsigned long and see if that is the problem.

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html
This... is a hobby.

#### Riva

#4
##### Jul 16, 2012, 11:56 am
Apart from other stuff mentioned you define myInt array as having 1 element
Quote
unsigned int myInt[1];

But in the encrypt routine you try to access 2 elements
Quote
v0=v[0];
v1=v[1];

Of the two examples what one produces the correct answer?
Don't PM me for help as I will ignore it.

#### enrico88

#5
##### Jul 16, 2012, 12:22 pm
Thanks all for answersâ€¦you are very kind!
Sorry the algorithm is described here:  http://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
I have defined delta and all other variables as unsigned longâ€¦but my outputs are still different
The serial output are:

Arduino:
4
1655960582

Mikroc:
4
218

#### PaulS

#6
##### Jul 16, 2012, 12:28 pm
Code: [Select]
`        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);`
vs:
Code: [Select]
`        v0 += ((v1<<4) + k0);// ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2);// ^ (v0 + sum) ^ ((v0>>5) + k3);`
I would not expect those two blocks of code to operate the same. Notice that the PIC code contains //?

#### enrico88

#7
##### Jul 16, 2012, 12:31 pm
thanks paul, but the pic now contains:
Code: [Select]
` v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);`

without //

#### PaulS

#8
##### Jul 16, 2012, 12:32 pm
When you change code without posting the new code, you can hardly expect us to help you debug it.

#### enrico88

#9
##### Jul 16, 2012, 12:37 pmLast Edit: Jul 16, 2012, 12:52 pm by enrico88 Reason: 1
sorry, the last version of codes are:

code for arduino:
Code: [Select]
`unsigned long myInt[1];unsigned long k [4] = {0,2,3,4};void setup(){    Serial.begin(9600);}void encrypt (unsigned long* v, unsigned long* k) {    unsigned long v0,v1, sum, i;    unsigned long delta;    unsigned long k0, k1, k2, k3;   /* chiave */    delta=0x9e3779b9;    v0=v[0];    v1=v[1];    sum=0;    k0=k[0];    k1=k[1];    k2=k[2];    k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void loop(){                   myInt[0]=4;         Serial.println(myInt[0],DEC);             encrypt(myInt,k);         Serial.println(myInt[0],DEC);         delay(1000);     }`

code for PIc
Code: [Select]
`unsigned long myInt[1];unsigned long k [4] = {0,2,3,4};void encrypt (unsigned long* v, unsigned long* k) {    unsigned long v0,v1, sum, i;    unsigned long k0, k1, k2,k3;    unsigned long delta;    v0=v[0]; v1=v[1]; sum=0;    delta=0x9e3779b9;;    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void main(){UART1_Init(9600);myInt[0]=4;UART1_Write(myInt[0]);encrypt(myInt,k);UART1_Write(myInt[0]);delay_ms(1000);}`

#### majenko

#10
##### Jul 16, 2012, 12:40 pm
Are you having a laugh?

Arduino:
Quote

delta=0x9e3779b9;

Pic:
Quote

delta=12;

I can see no reason for you to have two different bits of code.  Copy and paste from one to the other and make the functions exactly the same - even down to the comments.

#### PaulS

#11
##### Jul 16, 2012, 12:44 pm
Code: [Select]
`unsigned long myInt[1];`
A one element array.

Code: [Select]
`         myInt[0]=4;`
Properly initialized.

Code: [Select]
`         encrypt(myInt,k);`
And passed to the function.

Code: [Select]
`void encrypt (unsigned long* v, unsigned long* k) {`
Where it gets referenced as v.

Code: [Select]
`    v0=v[0];    v1=v[1];`
v1 was just assigned some random crap, outside the bounds of the array passed in.

Nothing else that happens after this point is correct, since you are referencing garbage.

I fail to see how that works on the PIC.

#### enrico88

#12
##### Jul 16, 2012, 12:57 pm
sorry you are right!!i have modified delta..now are the same

Thanks paul i will try with an array of two elements.

#### enrico88

#13
##### Jul 16, 2012, 01:09 pm
I have modified the code how you say paul but the result are still different

Arduino:
Code: [Select]
`unsigned long myInt[2];unsigned long k [4] = {0,2,3,4};void setup(){    Serial.begin(9600);}void encrypt (unsigned long* v, unsigned long* k) {    unsigned long v0,v1, sum, i;    unsigned long delta;    unsigned long k0, k1, k2, k3;   /* chiave */    delta=0x9e3779b9;    v0=v[0];    v1=v[1];    sum=0;    k0=k[0];    k1=k[1];    k2=k[2];    k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void loop(){                   myInt[0]=4;         myInt[1]=1;         Serial.println(myInt[0],DEC);         Serial.println(myInt[1],DEC);         encrypt(myInt,k);         Serial.println(myInt[0],DEC);         Serial.println(myInt[1],DEC);         delay(1000);     } `

Pic 16f887:
Code: [Select]
`unsigned long myInt[2];unsigned long k [4] = {0,2,3,4};void encrypt (unsigned long* v, unsigned long* k) {    unsigned long v0,v1, sum, i;    unsigned long k0, k1, k2,k3;    unsigned long delta;    v0=v[0]; v1=v[1]; sum=0;    delta=0x9e3779b9;;    k0=k[0]; k1=k[1]; k2=k[2]; k3=k[3];    for (i=0; i < 32; i++) {        sum += delta;        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);    }    v[0]=v0;    v[1]=v1;}void main(){UART1_Init(9600);myInt[0]=4;myInt[1]=1;UART1_Write(myInt[0]);UART1_Write(myInt[1]);encrypt(myInt,k);UART1_Write(myInt[0]);UART1_Write(myInt[1]);delay_ms(1000);}`

#### bubulindo

#14
##### Jul 16, 2012, 01:22 pm
We have no idea how the PIC transmits the data. So did you try the UART1_Write() with a long in it ?

Code: [Select]
`unsigned long lala = 1234567UL;UART1_Write(lala);`

`if (result == RIGHT_RESULT)     Serial.println("Eureka!");`