在C中创建一个二维数组,每个单元格大小为512位。

huangapple go评论62阅读模式
英文:

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

问题

以下是翻译好的内容:

我想要创建2维数组,维度为2乘以M,数组中的任何单元格都将有512位。我考虑创建类似于以下代码的结构:

#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))
    //
}

但我理解,数组中的每个单元格的大小是256位,因为它是char类型,我希望有512位,我不确定如何解决这个问题的简单方法。

我想在public_key和private_key变量中,每个单元格都有512位的随机值。可能char不是一个好的选择,但我不明白正确的做法是什么,我想我有点困惑。

我在Python中有这段代码,在Python中做这很简单,我也想在C中做到。

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

在Python中,它运行得很好。

英文:

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

答案1

得分: 1

  1. 创建一个大小为512位的数据类型(例如一个结构体)。

  2. 创建一个数组,其单元格是该数据类型。

#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];

在这个示例中,如果CHAR_BITS不是8,单元格的大小可能不是512位,但至少是512位。

英文:
  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 &lt;limits.h&gt;

#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.

huangapple
  • 本文由 发表于 2023年3月7日 21:05:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定