Software APIs
random_order.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_BASE_RANDOM_ORDER_H_
6 #define OPENTITAN_SW_DEVICE_LIB_BASE_RANDOM_ORDER_H_
7 
8 #include <stddef.h>
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif // __cplusplus
13 
14 /**
15  * @file
16  * @brief Functions for generating random traversal orders.
17  */
18 
19 /**
20  * Context for a random traversal order.
21  *
22  * A "random traversal order" specifies a random order to walk through some
23  * buffer of length `n`, which is an important building block for
24  * constant-power code. Given `n`, the random order emits integers in the
25  * range `0..m`, where `m` is an implementation-defined, per-random-order
26  * value greater than `n`. The order is guaranteed to visit each integer in
27  * `0..n` at least once, but with some caveats:
28  * - Values greater than `n` may be returned.
29  * - The same value may be returned multiple times.
30  *
31  * Users must be mindful of these constraints when using `random_order_t`.
32  * These caveats are intended to allow for implementation flexibility, such as
33  * intentionally adding decoys to the sequence.
34  */
35 typedef struct random_order {
36  size_t state;
37  size_t max;
39 
40 /**
41  * Constructs a new, randomly-seeded traversal order,
42  * running from `0` to at least `min_len`.
43  *
44  * This function does not take a seed as input; instead, the seed is
45  * extracted, in some manner or another, from the hardware by this function.
46  *
47  * @param ctx The context to initialize.
48  * @param min_len The minimum length this traversal order must visit.
49  */
50 void random_order_init(random_order_t *ctx, size_t min_len);
51 
52 /**
53  * Returns the length of the sequence represented by `ctx`.
54  *
55  * This value may be greater than `min_len` specified in
56  * `random_order_init()`, but the sequence is guaranteed to contain every
57  * integer in `0..min_len`.
58  *
59  * This value represents the number of times `random_order_advance()` may be
60  * called.
61  *
62  * @param ctx The context to query.
63  * @return The length of the sequence.
64  */
65 size_t random_order_len(const random_order_t *ctx);
66 
67 /**
68  * Returns the next element in the sequence represented by `ctx`.
69  *
70  * See `random_order_len()` for discovering how many times this function can
71  * be called.
72  *
73  * @param ctx The context to advance.
74  * @return The next value in the sequence.
75  */
77 
78 #ifdef __cplusplus
79 } // extern "C"
80 #endif // __cplusplus
81 
82 #endif // OPENTITAN_SW_DEVICE_LIB_BASE_RANDOM_ORDER_H_