LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
test_random.c 文件参考
#include "lmmp/lmmpn.h"
#include "lmmp/mprand.h"
#include "lmmp/secret.h"
#include "lmmp/version.h"
#include "cases.h"
#include "support/lmmp_test_utils.h"
#include <string.h>
test_random.c 的引用(Include)关系图:

浏览该文件的源代码.

函数

 TEST_CASE ("rng/basic", global_random_reproducible)
 TEST_CASE ("rng/basic", seed_random_reproducible)
 Copyright (C) 2026 HJimmyK (Jericho Knox)
 TEST_CASE ("rng/strong", strong_random)
 TEST_CASE ("rng/strong", strong_rng_extern_reproducible)
void test_random (void)
 Copyright (C) 2026 HJimmyK (Jericho Knox)

函数说明

◆ TEST_CASE() [1/4]

TEST_CASE ( "rng/basic" ,
global_random_reproducible  )

在文件 test_random.c 第 53 行定义.

53 {
54 mp_size_t n = 16;
55 mp_ptr a = alloc_limbs(n);
56 mp_ptr b = alloc_limbs(n);
57
58 lmmp_global_rng_init_(12345, 1);
60 lmmp_global_rng_init_(12345, 1);
63 for (mp_size_t i = 0; i < an; ++i) TEST_CHECK_EQ(a[i], b[i]);
64
65 lmmp_free(a);
66 lmmp_free(b);
67}
#define an
#define bn
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint64_t mp_size_t
定义 lmmp.h:93
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
void lmmp_global_rng_init_(unsigned seed, unsigned seed_type)
Copyright (C) 2026 HJimmyK (Jericho Knox)
mp_size_t lmmp_random_(mp_ptr dst, mp_size_t n)
生成随机大整数(0 - B^n-1 均匀分布)
#define n
#define TEST_CHECK_EQ(a, b)
定义 test.h:39

引用了 an, bn, lmmp_free(), lmmp_global_rng_init_(), lmmp_random_(), n, TEST_CASE , 以及 TEST_CHECK_EQ.

函数调用图:

◆ TEST_CASE() [2/4]

TEST_CASE ( "rng/basic" ,
seed_random_reproducible  )

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

在文件 test_random.c 第 32 行定义.

32 {
33 mp_size_t n = 16;
34 mp_ptr a = alloc_limbs(n);
35 mp_ptr b = alloc_limbs(n);
36
37 // 相同种子和类型应可复现
38 mp_size_t an = lmmp_seed_random_(a, n, 0x123456789ull, 0);
39 mp_size_t bn = lmmp_seed_random_(b, n, 0x123456789ull, 0);
41 for (mp_size_t i = 0; i < an; ++i) TEST_CHECK_EQ(a[i], b[i]);
42
43 // 不同类型应产生不同序列(概率性,但相同实现下应稳定)
44 mp_size_t cn = lmmp_seed_random_(b, n, 0x123456789ull, 1);
45 bool diff = false;
46 for (mp_size_t i = 0; i < an && i < cn; ++i) if (a[i] != b[i]) diff = true;
47 TEST_CHECK_MSG(diff || an != cn, "seed_type changes sequence");
48
49 lmmp_free(a);
50 lmmp_free(b);
51}
mp_size_t lmmp_seed_random_(mp_ptr dst, mp_size_t n, mp_limb_t seed, unsigned seed_type)
生成随机大整数(0 - B^n-1 均匀分布)
#define TEST_CHECK_MSG(cond, msg)
定义 test.h:38

引用了 an, bn, lmmp_free(), lmmp_seed_random_(), n, TEST_CASE, TEST_CHECK_EQ , 以及 TEST_CHECK_MSG.

函数调用图:

◆ TEST_CASE() [3/4]

TEST_CASE ( "rng/strong" ,
strong_random  )

在文件 test_random.c 第 69 行定义.

69 {
70 mp_size_t k = 20;
71 mp_size_t n = 12;
73 TEST_CHECK_MSG(rng != NULL, "strong rng init");
74
75 mp_ptr a = alloc_limbs(k);
76 mp_ptr b = alloc_limbs(k);
79 TEST_CHECK_MSG(an > 0 && bn > 0, "strong random nonzero length");
80 TEST_CHECK_MSG(an <= n && bn <= n, "strong random length bound");
81 bool same = (an == bn);
82 if (same) for (mp_size_t i = 0; i < an; ++i) if (a[i] != b[i]) same = false;
83 TEST_CHECK_MSG(!same, "strong random sequence advances");
84
86 lmmp_free(a);
87 lmmp_free(b);
88}
#define k
lmmp_strong_rng_t * lmmp_strong_rng_init_(mp_size_t k, unsigned seed)
创建k维度强随机数生成器
void lmmp_strong_rng_free_(lmmp_strong_rng_t *rng)
销毁强随机数生成器
mp_size_t lmmp_strong_random_(mp_ptr dst, mp_size_t n, lmmp_strong_rng_t *rng)
生成n维度强随机数(0 - B^n-1 均匀分布)

引用了 an, bn, k, lmmp_free(), lmmp_strong_random_(), lmmp_strong_rng_free_(), lmmp_strong_rng_init_(), n, TEST_CASE , 以及 TEST_CHECK_MSG.

函数调用图:

◆ TEST_CASE() [4/4]

TEST_CASE ( "rng/strong" ,
strong_rng_extern_reproducible  )

在文件 test_random.c 第 90 行定义.

90 {
91 // 回归:扩容时新增状态曾未初始化(realloc 不清零,且初始化循环上界在更新 k 之前取值)
92 const mp_size_t k1 = 8;
93 const mp_size_t k2 = 24;
94
99
100 mp_ptr ra = alloc_limbs(k2);
101 mp_ptr rb = alloc_limbs(k2);
102 mp_size_t na = lmmp_strong_random_(ra, k2, a);
103 mp_size_t nb = lmmp_strong_random_(rb, k2, b);
104 TEST_CHECK_EQ(na, nb);
105 TEST_CHECK_MSG(na > 0, "extern grown rng nonzero output length");
106 bool same = true;
107 for (mp_size_t i = 0; i < na; ++i)
108 if (ra[i] != rb[i]) same = false;
109 TEST_CHECK_MSG(same, "extern growth deterministic");
110
113 lmmp_free(ra);
114 lmmp_free(rb);
115}
void lmmp_strong_rng_extern_(lmmp_strong_rng_t *rng, mp_size_t k)
将rng内部状态拓展至k维度

引用了 lmmp_free(), lmmp_strong_random_(), lmmp_strong_rng_extern_(), lmmp_strong_rng_free_(), lmmp_strong_rng_init_(), TEST_CASE, TEST_CHECK_EQ , 以及 TEST_CHECK_MSG.

函数调用图:

◆ test_random()

void test_random ( void )

Copyright (C) 2026 HJimmyK (Jericho Knox)

This file is part of LMMP.

LMMP is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see https://www.gnu.org/licenses/.

SPDX-License-Identifier: LGPL-3.0-or-later

在文件 test_random.c 第 117 行定义.

117 {
118 TEST_RUN("rng/basic", seed_random_reproducible);
119 TEST_RUN("rng/basic", global_random_reproducible);
120 TEST_RUN("rng/strong", strong_random);
121 TEST_RUN("rng/strong", strong_rng_extern_reproducible);
122}
#define TEST_RUN(category, name)
定义 test.h:36

引用了 test_random() , 以及 TEST_RUN.

被这些函数引用 main() , 以及 test_random().

函数调用图:
这是这个函数的调用关系图: