Difference code arduino-Pic

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:

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):

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

delta=0x9e3779b9;

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

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.

    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.

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.

In case you need some more info:

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html

Apart from other stuff mentioned you define myInt array as having 1 element

unsigned int myInt[1];

But in the encrypt routine you try to access 2 elements

v0=v[0]; v1=v[1];

Of the two examples what one produces the correct answer?

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

        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);

vs:

        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 //?

thanks paul, but the pic now contains:

 v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);

without //

When you change code without posting the new code, you can hardly expect us to help you debug it.

sorry, the last version of codes are:

code for arduino:

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

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);
}

Are you having a laugh?

Arduino:

delta=0x9e3779b9;

Pic:

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.

unsigned long myInt[1];

A one element array.

         myInt[0]=4;

Properly initialized.

         encrypt(myInt,k);

And passed to the function.

void encrypt (unsigned long* v, unsigned long* k) {

Where it gets referenced as v.

    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.

sorry you are right!!i have modified delta..now are the same

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

I have modified the code how you say paul but the result are still different :frowning:

Arduino:

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:

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);
}

We have no idea how the PIC transmits the data. So did you try the UART1_Write() with a long in it ?

unsigned long lala = 1234567UL;

UART1_Write(lala);

What would this return to you on the PIC?

Another thing, is any of the returned values right? If you know the answer and you want to avoid type conversion problems, you can always do something like:

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

I have write the following code for PIC:

unsigned long la;
void main()
{
  ANSEL  = 0;                     // Configure AN pins as digital
  ANSELH = 0;
  C1ON_bit = 0;                   // Disable comparators
  C2ON_bit = 0;
  la=123;
UART1_Init(9600);
delay_ms(100);
UART1_Write(la);
delay_ms(100);
}

I have tried to print on serial a number with three digits (la=123) and it works...for number with four digits (example la=1234) it doesn't work!!! The problem is that the serial read and write only one byte to the time....how can i resolve this problem??thanks to all

I have write the following code for PIC:

Perhaps if you asked your Chevy question at the Chevy shop, not the Ford shop...

Well...

You can always run that code on a PC to confirm the arduino is outputting the right answer and ditch the PIC. :)