Jūs esateŽurnalai / Ernestas Kardzys's blog / Multiple-Precision Modular Arithmetics. Addition

Multiple-Precision Modular Arithmetics. Addition


ParašėErnestas Kardzys - 2009 Kovo 15

I was browsing (ok, lets be honest - Goooogling) the web in order to find some Multiple-Precision Modular Arithmetics algorithms. Our lecturer recomended a very good book - "Handbook of Applied Cryptography". So, I used this book in order to code my algorithms (modular addition, subraction and multiplication) :) So, here is my modular addition algorithm implementation in the C programming language.
// Name: int MultiplePrecisionAddition() // Input:        x        - first integer, size n //                y        - second integer, size n //                n        - number of digits in x, y, w //                base    - modulus // Output:        w        - result, size n, w[n - 1]...w[0] // Description: Calculates (x + y mod base) int MultiplePrecisionAddition(int x[], int y[], int w[], int n, int base) { int i = 0, c = 0, result = 0, temp = 0; for (i = 0; i < n - 1; ++i) { result = x[i] + y[i] + c; w[i] = result % base; result < base ?    c = 0 : c = 1; } w[n - 1] = c; // Converts from w[0]w[1]...w[n - 1] to w[n - 1]...w[0] for (i = 0; i < n / 2; ++i) { temp = w[i]; w[i] = w[n - 1 - i]; w[n - 1 - i] = temp; } return 0; }
Any suggestions how to improve my code? ;)

Skelbti naują komentarą

Šio laukelio turinys bus laikomas privatus ir nerodomas viešai.
  • Web puslapiu adresai ir el. pašto adresai automatiškai tampa nuorodomis.
  • Leidžiamos HTML žymės: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linijos ir paragrafai atskiriami automatiškai
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Daugiau informacijos apie teksto formatavimą

CAPTCHA
Šis klausimas yra skirtas įsitikinti, jog jūs esate žmogus, ir sustabdyti automatinį šlamšto siuntimą.