create 2 dimension array in C, and every cell with 512 bit size

Advertisements

I want to create 2d arrays, with dimension of 2 by M, and any cell in the array will have 512 bit. I was think to create something like this:

#define M 1024

typedef struct {
    unsigned char public_key[2][M];
    unsigned char private_key[2][M];
} key_pair

void keygen(key *key_pair){
    srand(time(NULL))
    //
}

But as I can understand, every cell in this array is of size of 256 bit because is type of char, I want 512 bit, I not sure what is the simple idea to solve it.

I want have in public_key and private_key variables, a random value of 512bit in every cell. probably char I not good option, but I not able to understand what is the right way to do it, I think that I’m little a bit confused.

I have the code in Python, in Python its very simple to do it, and i want to do it in C as well.

def keygen():
    SecretKey = np.zeros((2,M), dtype=object)
    PublicKey = np.zeros((2,M), dtype=object)
    for i in range(0,M):
        SecretKey[0][i] = secrets.token_bytes(512/8)
        SecretKey[1][i] = secrets.token_bytes(512/8)
        PublicKey[0][i] = secrets.token_bytes(512/8)
        PublicKey[1][i] = secrets.token_bytes(512/8)
    key = [SecretKey, PublicKey]
    return key

In python its work perfectly

>Solution :

  1. Create a data type (a structure, for example) whose size is 512 bit.
  2. Create an array whose cell is the data type.
#include <limits.h>

#define M 1024

#define CELL_LENGTH_IN_BITS 512

struct cell_s {
    unsigned char data[(CELL_LENGTH_IN_BITS + CHAR_BITS - 1) / CHAR_BITS];
};

struct cell_s array[2][M];

In this example, the size of a cell may not be 512 bit if CHAR_BITS is not 8, but at least 512 bit.

Leave a ReplyCancel reply