I use MBED-TLS on STM32F103 device. STM 32F103 device has little SRAM memory (20 Kbytes).
I would like to calculate the ram used by mbedtls_rsa_context
How to do this?
Is it :
sizeof(mbedtls_rsa_context) + 13 * sizeof(mbedtls_mpi ) + mbedtls_mpi_size (D) + ..... + mbedtls_mpi_size (Vf)
Thanks,
Regards.
Note that the struct mbedtls_rsa_context contains these 13 mbedtls_mpi structs, so if you do sizeof(mbedtls_rsa_context), it already includes the 13 * sizeof(mbedtls_mpi ) part. So, no need to add that part.
As for the RAM that each mbedtls_mpi consumes, as you can see in mbedtls_mpi_grow, the size that is allocated is the number of limbs (x->n) multiplied with chars in limbs (CiL).
If you use mbedtls_mpi_size on every mpi, it will just give you the size in bytes that the big integer uses, without the leading zeros, if there are any, which also consume RAM.
Note that this means accessing internal members of the struct, which is not recommended, however there isn't any public API to get that knowledge.
If you are constrained with SRAM, have you considered using ECDSA keys, as same security strength keys consume less RAM?
Regards
Related
Apologies for the generic question title, I wasn't sure how to phrase it properly (suggestions welcome!)
I'm trying to get my head around some of the code for the Common Mark parser and came across this:
/* Oversize the buffer by 50% to guarantee amortized linear time
* complexity on append operations. */
bufsize_t new_size = target_size + target_size / 2;
new_size += 1;
new_size = (new_size + 7) & ~7;
So given a number, eg 32, it will add (32 / 2) [48], add 1 [49], add 7 [56], finally ANDing that with -8 [56].
Is this a common pattern? Specifically the adding of a number and then ANDing with its complement.
Is anyone able to provide any insight into what this is doing and what advantages, if any, exist?
The (+7) & ~7 part rounds the number up to the first multiple of 8. It works only with powers of 2 (7 is 2^3-1). If you want to round to a multiple of 32 then use 31 instead of 7.
The reason to round the size to a multiple of 8 is probably specific to the algorithm.
It is also possible that the author of the code knows how the memory allocator works. If the allocator uses internally blocks of memory of multiple of 8 bytes, an allocation request of any number of bytes between 1 and 8 uses an entire block. By asking for a block having a size that is multiple of 8 one gets several extra bytes for the same price.
TASK_SIZE is a kernel constant that defines the upper limit of the accessible memory for the code working at the lowest privilege level.
Its value is usually set to 0xc0000000 on systems with less than 1GB of physical memory (all examples included in this article refer to this value). The memory above this limit contains the kernel code .
Is there a way to determine the running kernels TASK_SIZE through c program ??
TASK_SIZE
After a lot of google search and analysis , i got a logic
Assume net virtual address are 4gb and the it is divided in 1:3 ratio.
Rough assumptions:-
Kernel (upper 1 gb): c0000000 -ffffffff
USer space (below 3gb):0-c0000000
than
#define GB *1073741824
unsigned int num;
unsigned int task_size;
task_size=(unsigned)&number+ 1 GB / 1 GB * 1GB;
[the process's stack area will be allocated below the kernel space]
so address of num (in stack)= somewhere around in 3 GB range ex:[3214369612]
now adding 1 GB = 1073741824+3214369612=4288111436
dividing by 1GB=3.993614983 that will be 3 (unsigned int)
now multiplying by 1GB = 3 *1073741824 = 3221225472 i.e. (0xC0000000 in hex)
hence i got the kernel starting address (TASK_SIZE)
I tried it assuming (2:6) ratio also and got correct result.
Is this a fair logic ,Please comment ???
I need your help to know if it's possible to use a bitboard for a connect 5 game. Because I saw a lot of examples for connect 4 games but nothing if the board is very large such as 15x15 or 19x19. I don't understand how can I represent the board with 64bits.
Thanks you for your help
It is possible, as explained in unwind's answer, but by doing that you negate the main advantage of bitboards, which is performing logical operations on the entire data structure in one CPU instruction. Optimally, the size of the bitboard would be the width of your instruction set of the platform.
A possible workaround would be to represent the board as multiple smaller "standard" bitboards (32-bit or 64-bit, depending on your platform) and apply logical operations on each mini-bitboard separately. For instance, a board of 15x15 can be broken into 4 8x8 boards, for example:
typedef uint64_t miniboard_type; // 8x8 board
typedef struct
{
miniboard_type miniboard[4]; // Larger 15x15 board comprising 4 8x8 boards
} bitboard_type;
bitboard_type bitboard;
Correspondigly, I believe that you might also need to adjust the math and logic to account for the edges between the mini-bitboards (and that depends on what you want to do with your main bitboard).
Huh?
Of course it's possible, if by "bitboard" you just mean "a 2D array using one bit per cell".
It's sometimes easier if you e.g. round off so each row (or column) is a whole number of bytes, but you don't have to do that.
Just do something like:
#include <stdint.h>
#include <limits.h>
#define WIDTH 22
#define HEIGHT 17
uint8_t bits[(WIDTH * HEIGHT + CHAR_BIT - 1) / CHAR_BIT];
this declares bits as an array of bytes, with enough bytes to hold a matrix of WIDTH by HEIGHT cells.
With the example's values, it'll be (22 * 17 + 7) / 8 or 47 bytes long (assuming CHAR_BIT is 8).
I have a variable that can take any 3 values. If it can take only 2 values I would have assigned a bool type. But my variable can take 3 values. If I assign a int8_t type I am wasting 6 bits. Though this looks like preemptive optimization, I have millions of instances of this type, which is going to make a huge difference in memory usage.
What datatype should I assign the variable to such that least memory is used overall.
If I do it with enum, will that ensure less memory is used?
In particular what datatype should I use in C, Java, Python & MySQL.
If you really (although I'm not sure it's the case) need this data type, you can use a bitfield. However, this could be constraining, since you can't define a pointer to such type. Wasting a bit:
struct s
{
int n:2; /* 4 states instead of 3 */
};
Here's a bit of math: naively you can describe each element with two bits, so you could pack four elements into one byte and get decent random access. Four elements have 34 = 81 states, so that's a usage of 81 / 256 ≈ 32%. If you want to stay on a byte boundary, you could look for the nearest power of three that fits into 28, which is 35 = 243. In other words, if you one one byte to enumerate all possible states of five consecutive elements, you have a space efficiency of 243 / 256 ≈ 95%.
It makes no sense to do this packing in memory unless you're processing vast amounts of data and cannot fit everything into physical memory and can't partition your algorithm to run on smaller chunks at a time. For efficient computation, you should at the very least use a single byte (uint8_t), or even a machine word (uint8fast_t) to store your data. It's only when you serialize your data to disk and find that your terabytes of data are too expensive for your RAID-50 storage that you may wish to consider a complicated packing scheme. (Though then again you could just pipe your data through gzip, which basically does all that for you.)
Here's a rudimentary decoding algorithm for getting the five elements out of a byte:
unsigned int get_tristate(unsigned char const n, size_t const i)
{
/* Conditions: n in [0, 243)
i in [0, 5)
Returns: the i^th trivalent element encoded in n, in [0, 2).
*/
static unsigned int const powers[] = { 1, 3, 9, 27, 81, 243 };
return (n / powers[i]) % powers[i + 1];
}
I tried zeros(1500*64) but it says "Maximum variable size allowed by the program is exceeded."
But [C,MAXSIZE] = COMPUTER returns MAXSIZE = 2.1475e+009
So why isn't it working? Also, after trying to issue this aommand on the Matlab command line a few times, I tried everything from zeroes(500*64) to zeros(1500*64) to find the maximum allowed, and sometimes it returned "Maximum variable size allowed by the program is exceeded." for 500*64 and sometimes returned "Out of memory." error. What could be the reason for that? This is what the memory command returns:
Maximum possible array: 486 MB (5.094e+008 bytes) * Memory
available for all arrays: 1436 MB (1.506e+009 bytes) ** Memory used
by MATLAB: 353 MB (3.697e+008 bytes) Physical Memory
(RAM): 3070 MB (3.219e+009 bytes)
Limited by contiguous virtual address space available.
** Limited by virtual address space available.
Output of [u,s] = memory
[u, s] = memory
u =
MaxPossibleArrayBytes: 509411328
MemAvailableAllArrays: 1.5057e+009
MemUsedMATLAB: 369819648
s =
VirtualAddressSpace: [1x1 struct]
SystemMemory: [1x1 struct]
PhysicalMemory: [1x1 struct]
How do I calculate my allowed maximum size from this information, both in terms of nuber of elements and total bytes occupied?
The command
x = zeros(1500*64);
attempts to create a square matrix of double precision zeros, 96000 elements per side, requiring 73 gigabytes.
I suspect you want to use
x = zeros(1500,64);
which creates a 1500-by-64 array of double precision zeros, requiring 0.8 megabytes of memory.
When I google for that error message, first hit is a descriptive help page from MathWorks, the developer of MatLab:
Why do I get the error 'Maximum variable size allowed by the program is exceeded.' ?
According to that, you should use the computer command, not memory, to learn the maximum matrix size supported by your edition of MatLab.
For the "Out of Memory" error, take the "Maximum possible array: 486 MB (5.094e+008 bytes)" reported by memory, and divide by the size of an array element (8 bytes for double-precision real values, which is what MatLab uses by default). The reason it's so low is due to address space fragmentation, which is what the memory command is telling you when it talks about "Limited by contiguous address space".