Hey guys, I am working on making Elliptic Curve Cryptography El-Gamal in 8051 MCU using Keil uvision 4. I want to make it 128 bit, so I need to use GMP library. However, to use GMP I usually download it in mingw-get-setup first, so I think it won't run if I just copy the gmp.h to my project in Keil.
My questions are :
1. how can I use gmp in 8051 using Keil?
2. Or should I compile it first in gcc, then just download the hex file to 8051? How to program the register in gcc?
Thanks for your attention :D
Best regards
Yes sure, as I said, it only able to write in integer or hexadecimal, but I unable to write, for example unsigned int a = 0b110101
Of course elementary student knows that every value is stored in binary form in memory, but I can't write in binary form for this micro 8051.
Anyone who plans to implement any big number library would see it as a trivial exercise to also implement an assign function that can work on an ASCII string of arbitrary number base.
So:
void bignum_set(bignum_t *num,uint8_t base,const char *value); void bignum_set_raw(bignum_t *num,uint8_t value,size_t bytes); uint8_t bignum_data[] = {0xf3,0x12,0x73,0x00,0x03,0x4a }; bignum_set(num1,4,"033303210021111111130203302"); bignum_set(num2,16,"fa3032faccca"); bignum_set(num3,2,"011110100100001110111010110010011010101001010101101000101010"); bignum_set_raw(num4,bignum_data,sizeof(bignum_data));
I can't write in binary form for this micro 8051.
And you really believe that to the biggest obstacle you'll likely run into while trying to implement one of the more computationally challenging tasks there is, on one of the most computationally challenged class of micro controllers in the world?
"for example unsigned int a = 0b110101"
That has nothing to do with the 8051.
Nothing, even, to do with Keil's C51 compiler.
It's time to go back to your 'C' textbook and review the basics of the language syntax...
Thanks everyone for your replies :D However I am still unable to write, for example int a = 0b1101010; . so I must write it in hexadecimal, which is a bit confusing to make the multiplication for large number using addition.
I've got another problem while debugging my code, while it goes in to FOR loop or WHILE loop, I need to step until the condition fulfilled, for example : While(a<5000), I need to wait until a reach 5000. Anyone knows how to step out from looping and get the final result directly? THANKS A LOT :D
Is the answer for my question above is by setting a breakpoints?
However I am still unable to write, for example int a = 0b1101010; . so I must write it in hexadecimal (0x...), which is a bit confusing to make the multiplication for large number using addition.
THANKS A LOT :D
Why do you even care about binary? Most people write source code where they assign in decimal for the majority of all values and only use hexadecimal where there is some form of bit mapping that is important. Or maybe octal if playing with things unix access rights.
The important thing here is that whatever number base you care about, you still can't write so large numbers as numbers because the goal with big-number libraries is to operate on numbers that are much, much, much larger than any number the compiler can handle.
That is why it's common to assign input values using some other form, and write a small piece of code that converts the received data into the internal format for the big-number library.
That was why I suggested the use of text strings and a parameter that specified which numeric base the text string was in. You just might also use an array where every byte of the array represents a single digit in the base you decided to use. This would allow you to use anything from base 2 - binary - up to base 256.
In the end, it really is quite simple to perform numeric base conversions - at least for anyone who can write a big-number library.
Yes because you aren't using VHDL/Verilog, you're using C and are thus constrained to how that language functions. You could create a very simple function to parse "0b1101010", if that was critically important to your application.
On the evidence of this thread, I rather doubt that he could.
Okay I have solved the previous problems. Thanks all :D
I am currently working on modular multiplication 128 bit program, when I run it on GCC,it works. But then I moved it to keil, and it generate error like this :
compiling led2.c... LED2.C(14): error C141: syntax error near 'unsigned' LED2.C(14): error C202: 'temp': undefined identifier LED2.C(15): error C141: syntax error near 'unsigned' LED2.C(15): error C202: 'i': undefined identifier LED2.C(59): error C202: 'i': undefined identifier LED2.C(61): error C202: 'i': undefined identifier compiling sub.c... SUB.C(9): error C231: '_sub': redefinition SUB.C(21): error C141: syntax error near 'char' SUB.C(21): error C202: 'i': undefined identifier SUB.C(22): error C141: syntax error near 'unsigned' SUB.C(22): error C202: 'temp': undefined identifier SUB.C(25): error C202: 'i': undefined identifier SUB.C(27): error C202: 'i': undefined identifier SUB.C(29): error C202: 'i': undefined identifier SUB.C(30): error C202: 'temp': undefined identifier SUB.C(31): error C202: 'temp': undefined identifier SUB.C(32): error C202: 'i': undefined identifier SUB.C(35): error C202: 'i': undefined identifier SUB.C(40): error C231: '_sub': redefinition compiling mod.c...
Although in Led.c I have declared :
#include <REG51.H> #include <stdio.h> #include <stdlib.h> #include <math.h> #include "Li.h" #include <string.h> main() { unsigned char a[17],b[17],p[16]; //main function starts unsigned char *c = (char*) malloc(17);; memset(c,0,17); unsigned char temp=0; unsigned char i=2; memset(a,0,17); memset(b,0,17); memset(p,0,16); a[0]=0xB2; a[1]=0x21; b[0]=0x6A; b[1]=0x6; p[0]=0xF0; p[1]=0x1F; for(i=1;i>=0;i--) { if ((b[i] & 128) == 128) { c = add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 64) == 64) { c = add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 32) == 32) { c = add(c,a); c = add(c,c); c = mod(c,p); } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 16) == 16) { c= add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 8) == 8) { c = add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 4) == 4) { c = add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 2) == 2) { c = add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } if ((b[i] & 1) == 1) { c= add(c,a); c = add(c,c); c = mod(c,p); c[3] = 0; } else { c = add(c,c); c = mod(c,p); c[3] = 0; } } free(c); while(1){} }
and in sub.c :
#include <stdio.h> #include <stdlib.h> #include <math.h> #include "Li.h" #include <string.h> #include <REG51.H> unsigned char *sub(unsigned char a[17],unsigned char b[17]) { /*unsigned d[2],temp,i,shift,k; char a[32],b[32],c[32]; a[31] = 0xA; a[30] = 0xD; a[29] = 0x9; b[31] = 0x8; b[30] = 0xF; b[29] = 0x2; */ unsigned char *c = (char*) malloc(17);; memset(c,0,17); //static char c[3]; char i; unsigned char temp = 0; memcpy(c, a, 17); for(i=16; i >= 0 ; i--) { if (c[i] < b[i]) { c[i+1] = c[i+1] - 1; temp = c[i] - b[i]; temp = 255-temp+1; c[i] = 0xFF - temp + 1; } else { c[i] = c[i] - b[i]; } } return c; //free(c); }
Led.c is the main program, and I just testing it to do multiply of 16 bit to make the debugging easier. Function sub run in function mod. The function mod and add have been proved to run perfectly on GCC, and I don't attach them here. Anyone knows : what's the syntax error near unsigned, and why the variable temp and i is undefined in led.c and sub.c although I have defined it?
Thanks a lot for your help!
and what is the error at last line of sub.c ? SUB.C(40): error C231: '_sub': redefinition
the code at that line is :
return c;
put your variable declarations at the top of the block. not after any function calls.
this depends on the level of c. I can't remember the exact details and I haven't got time to research.
that is left as an exercise for you.
Nice code you have.
for(i=1;i>=0;i--)
i is unsigned char.
Start on one and repeat until negative. How often do you see an unsigned character be negative?
Yes actually it works after I put the declaration on top. However, the pointer c I create share the same address as the variable a. Anyone know how to make they have different address?
c : X:0x000000
a : X:0x000000[...]
Thus, when I debug it by putting the cursor to while(1) and run to cursor line, the c have value 0, although it should have different value when I run it on GCC. Is it because c and a have the same address?