Archyvas

Įrašo tag'ai: ‘cpp’

Point multiplication in Elliptic Curves

Gegužė 16th, 2009 Ernestas Kardzys Nėra komentarų

I was browsing the web for information about the binary multiplication (also known as “left-to-right multiplication”) in Ellipti curves. I wrote a demonstration of the algorithm. Unfortunately, it’s very brief version, but it might be interesting for someone :) This demonstration generates the right point multiplication sequency in binary multiplication of point.

First – very short theory. Point multiplication is the operation: Q = k∙P. Point multiplication is the combination of point doubling and point addition. The number k is converted into binary form and according to it’s value (0 or 1) the addition is performed or not. The simplest and the oldest is the binary multiplication method (also called “left to right point multiplication”). The algorithm:

Ø Input: A point P and l-bit integer k, where k = (k[l - 1]…k[1]k[0]), k[i] = {0, 1};

Ø Output: k∙P.

Ø Q = 0

Ø For j = l – 1 to 0 do

o Q = [2]Q

o If kj = 1, then Q = Q + P

Ø Return Q

So, we need to get the binary representation of our k. It can be done with & operator in C. The addition must be performed, if k[i] = 1 (condition: number & 1).

So, the demonstration code looks like that:

void sim_bmul(const long number) {
long no = number;
string line;
int p[sizeof(long) * 8];

/* Get binary digits */
int i = 0;
while(no > 0)
{
if (no & 1)
p[i++] = 1;
else
(p[i++] = 0);
no >>= 1;
}

i–;

while(i >= 0) {
/* Doubling emulation */
if (line.size() != 0)
line = line.insert(0, “(2″);
/* End of doubling emulation */

if (p[i] == 1) {
/* Addition simulation */
if (line.size() == 0)
line.insert(line.size(), “(P”);
else
line.insert(line.size(), ” + P”);
}

if (line.size() > 0)
line.insert(line.size(), “)”);

cout << line.c_str() << endl;

i–;
}

cout << line.c_str() << endl;
}

So, if we pass 11 (in binary: 1011) this demonstration should give us the following sentence: (2(2(2(P)) + P) + P).

Doubling emulation looks quite weird, but at the moment I could find a better solution. Well, next time…

Nice ;)

How many C++ programmers does it take to change a light bulb?

Balandis 24th, 2009 Ernestas Kardzys 4 komentarai

How many C++ programmers does it take to change a light bulb?
You’re still thinking procedurally. A properly designed light bulb object would inherit a change method from a generic light bulb class, so all you would have to do is send a light-bulb-change message.

Kategorijos:Džiaukai Raktažodžiai:, ,

GTK žinutės (Message Box) išmetimas

Gegužė 5th, 2008 Ernestas Kardzys Nėra komentarų

WinAPI turi tokią funkciją kaip MessageBox(). GTK bibliotekoje ieškojau analogo ir suradau:

#include <gtk/gtk.h>

// Kodas…

GtkWidget *dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,  GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, “Nenurodytas rezultatų failas!”);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);

makefile pavyzdys

Vasaris 20th, 2008 Ernestas Kardzys 4 komentarai

Jeigu programuojate su C/C++, turbūt dažnai tenka nurodyti daugybę C++ failų g++ programai (kažkas tokio: g++ failas.cpp kitas_failas.cpp desimtas_failas.cpp ir t.t.). Nepatogu.

Problemą padės išspręsti makefile failiukas, kuris įvedus make tvarkingai sukompiliuos programą.

CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=failas.cpp kitas_failas.cpp dar_vienas_failas.cpp paskutinis_failas.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=programos_pavadinimas

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
$(CC) $(CFLAGS) $< -o $@

clean:
rm -rf *o
programos_pavadinimas

SOURCES eilutėje, atskirdami tarpu, išvadinkite savo kompiliuojamus failus, EXECUTABLE eilutėje įrašykite sukompiliuotos programos vardą (komanda: g++ -o programos_vardas). Kai norėsite išvalyti katalogą nuo objektinių failų, įveskite make clean

Visas smagumas atrodo maždaug taip:

$ cd a048/

$ make
g++ -c -Wall a048.cpp -o a048.o
g++ -c -Wall sorting.cpp -o sorting.o
g++ -c -Wall actions.cpp -o actions.o
g++ -c -Wall filesystem.cpp -o filesystem.o
g++  a048.o sorting.o actions.o filesystem.o -o a048

$ ./a048 

$ make clean 

Iš mistikos sferos

Vasaris 12th, 2008 Ernestas Kardzys 6 komentarai

Turime klasės header faile konstruktoriaus/destruktoriaus aprašymą: Sorting() ir ~Sorting(). Klasės cpp faile: Sorting::Sorting() { } ir Sorting::~Sorting() { }

Klausimas: kodėl to niekaip G++ kompiliuoti nenori? Tvirtina, jog:

> g++ gbsort.cpp -Wall
/tmp/ccy8OFFP.o: In function `main’:
gbsort.cpp:(.text+0×200): undefined reference to `Sorting::Sorting()’
gbsort.cpp:(.text+0×244): undefined reference to `Sorting::~Sorting()’
collect2: ld returned 1 exit status

Kaip bebūtų keista, jei aš header faile pakeičiu Sorting() į Sorting() {} ir ~Sorting() į ~Sorting() {} – veikia. Gal kas idėjų turite?

Kategorijos:Programavimas Raktažodžiai:, , , , , , ,