(6)Other functions

以下內容源自PBC Library 的 英文manual(Chapter 6)。

本文摘要

六. Other functions

  • 6.1 Random bits
  • 6.2 Custom allocation
  • 6.3 Logging

一,Other functions

Random number generation, memory allocation, logging.

隨機數生成,內存分配,日誌記錄。

6.1. Random bits

The first time PBC is asked to generate a random number, the library will try to open the file /dev/urandom as a source of random bits. If this fails, PBC falls back to a deterministic random number generator (which is of course completely useless for cryptography).

It is possible to change the file used for random bits. Also, explicitly selecting the deterministic random number generator will suppress the warning.

On Windows, by default, PBC uses the Microsoft Crypto API to generate random bits.

第一次要求PBC生成隨機數時,庫將嘗試打開文件/ dev / urandom作爲隨機位的源。如果失敗,PBC將退回到確定性隨機數生成器(對於加密術,它當然完全沒有用)。

可以更改用於隨機位的文件。同樣,顯式選擇確定性隨機數生成器將抑制該警告。

在Windows上,默認情況下,PBC使用Microsoft Crypto API生成隨機位。

void pbc_random_set_file(char *filename)

Sets filename as a source of random bytes. For example, on Linux one might use /dev/random.

將文件名設置爲隨機字節源。例如,在Linux上,一個人可能使用/dev/random作爲文件名;

void pbc_random_set_deterministic(unsigned int seed)

Uses a determinstic random number generator, seeded with seed.

使用一個確定性的隨機數,該生成器帶有種子;

void pbc_random_set_function(void (*fun)(mpz_t, mpz_t, void *), void *data)

Uses given function as a random number generator.

使用給出的函數作爲隨機數生成器;

void pbc_mpz_random(mpz_t z, mpz_t limit)

Selects a random z that is less than limit.

z應當小於limit;

void pbc_mpz_randomb(mpz_t z, unsigned int bits)

Selects a random bits-bit integer z.

選擇一個隨機位位整數z;

6.2. Custom allocation

Like GMP , PBC can be instructed to use custom memory allocation functions. This must be done before any memory allocation is performed, usually at the beginning of a program before any other PBC functions have been called.

Also like GMP , the PBC wrappers around malloc and realloc will print a message on standard error and terminate program execution if the calls fail. Replacements for these functions should act similarly.

However, unlike GMP , PBC does not pass the number of bytes previously allocated along with the pointer in calls to realloc and free.

像GMP一樣,可以指示PBC使用自定義內存分配功能。必須在執行任何內存分配之前完成此操作,通常是在調用任何其他PBC功能之前在程序開始時執行。

與GMP一樣,圍繞malloc和realloc的PBC包裝器將在標準錯誤時顯示一條消息,並在調用失敗時終止程序執行。這些功能的替換應具有相似的作用。

但是,與GMP不同,PBC不會在調用realloc和free時,傳遞先前分配的字節數以及指針。

void pbc_set_memory_functions(void *(*malloc_fn)(size_t), void *(*realloc_fn)(void *, size_t), void(*free_fn)(void *))

Set custom allocation functions. The parameters must be function pointers to drop-in replacements for malloc,realloc and free, except that malloc and realloc should terminate the program on failure: they must not return in
this case.

設置自定義分配功能。參數必須是指向malloc,realloc和free的直接替換的函數指針,除了malloc和realloc應當在失敗時終止程序:在這種情況下,它們不得返回。

6.3. Logging

int pbc_set_msg_to_stderr(int i)

By default error messages are printed to standard error. Call pbc_set_msg_to_stderr(0) to suppress messages.

默認情況下,錯誤消息會打印爲標準錯誤。調用pbc_set_msg_to_stderr(0)禁止顯示消息。

void pbc_die(const char *err, . . .)

Reports error message and exits with code 128.

報告錯誤消息,並且以代碼128退出;

void pbc_info(const char *err, . . .)

Reports informational message.

報告參考消息;

void pbc_warn(const char *err, . . .)

Reports warning message.

報告警告消息;

void pbc_error(const char *err, . . .)

Reports error message.

報告錯誤消息;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章