Botan 1.10.17
mp_mulop.cpp
Go to the documentation of this file.
1/*
2* Simple O(N^2) Multiplication and Squaring
3* (C) 1999-2008 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/internal/mp_core.h>
9#include <botan/internal/mp_asm.h>
10#include <botan/internal/mp_asmi.h>
11#include <botan/mem_ops.h>
12
13namespace Botan {
14
15extern "C" {
16
17/*
18* Simple O(N^2) Multiplication
19*/
20void bigint_simple_mul(word z[], const word x[], size_t x_size,
21 const word y[], size_t y_size)
22 {
23 const size_t x_size_8 = x_size - (x_size % 8);
24
25 clear_mem(z, x_size + y_size);
26
27 for(size_t i = 0; i != y_size; ++i)
28 {
29 const word y_i = y[i];
30
31 word carry = 0;
32
33 for(size_t j = 0; j != x_size_8; j += 8)
34 carry = word8_madd3(z + i + j, x + j, y_i, carry);
35
36 for(size_t j = x_size_8; j != x_size; ++j)
37 z[i+j] = word_madd3(x[j], y_i, z[i+j], &carry);
38
39 z[x_size+i] = carry;
40 }
41 }
42
43/*
44* Simple O(N^2) Squaring
45*
46* This is exactly the same algorithm as bigint_simple_mul, however
47* because C/C++ compilers suck at alias analysis it is good to have
48* the version where the compiler knows that x == y
49*
50* There is an O(n^1.5) squaring algorithm specified in Handbook of
51* Applied Cryptography, chapter 14
52*
53*/
54void bigint_simple_sqr(word z[], const word x[], size_t x_size)
55 {
56 const size_t x_size_8 = x_size - (x_size % 8);
57
58 clear_mem(z, 2*x_size);
59
60 for(size_t i = 0; i != x_size; ++i)
61 {
62 const word x_i = x[i];
63 word carry = 0;
64
65 for(size_t j = 0; j != x_size_8; j += 8)
66 carry = word8_madd3(z + i + j, x + j, x_i, carry);
67
68 for(size_t j = x_size_8; j != x_size; ++j)
69 z[i+j] = word_madd3(x[j], x_i, z[i+j], &carry);
70
71 z[x_size+i] = carry;
72 }
73 }
74
75}
76
77}
word word8_madd3(word z[8], const word x[8], word y, word carry)
Definition mp_asmi.h:159
void bigint_simple_mul(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition mp_mulop.cpp:20
void bigint_simple_sqr(word z[], const word x[], size_t x_size)
Definition mp_mulop.cpp:54
word word_madd3(word a, word b, word c, word *d)
Definition mp_asm.h:102
void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:32