Pages: [1] 2   Go Down
Author Topic: Difference code arduino-Pic  (Read 1019 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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
« Last Edit: July 16, 2012, 04:41:11 am by enrico88 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
delta=0x9e3779b9;

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    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.
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 16, 2012, 04:50:03 am by bubulindo » Logged

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

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
vs:
Code:
        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 //?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks paul, but the pic now contains:
Code:
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
        v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);

without //
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry, the last version of codes are:

code for arduino:
Code:
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:
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);
}
« Last Edit: July 16, 2012, 05:52:21 am by enrico88 » Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46042
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
unsigned long myInt[1];
A one element array.

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

Code:
         encrypt(myInt,k);
And passed to the function.

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

Code:
    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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have modified the code how you say paul but the result are still different smiley-sad

Arduino:
Code:
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:
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);
}
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3108
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:

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:

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

Logged

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

Pages: [1] 2   Go Up
Jump to: