Go Down

Topic: Difference code arduino-Pic (Read 1 time) previous topic - next topic

enrico88

Jul 16, 2012, 11:39 am Last 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

Quote

delta=0x9e3779b9;


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

PaulS

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 am Last 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.

In case you need some more info:

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdint.html
Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Riva

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?

enrico88

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

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

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

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 pm Last 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

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

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

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

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

enrico88

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

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


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:

Code: [Select]


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

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Go Up