[API-NEXT,PATCHv5,2/7] api: random: add ability to generate repeatable random data

Message ID 1480900520-21989-2-git-send-email-bill.fischofer@linaro.org
State New
Headers show

Commit Message

Bill Fischofer Dec. 5, 2016, 1:15 a.m.
Add the new API odp_random_seeded_data() to allow generation of repeatable
deterministic random data from a user-specified seed value that is
updated by each call. Sequences of calls using the same initial seed value
will generate the same pseudo-random sequence.

The caller may specify the kind of data that is to be generated however
the only required kind that need be supported is ODP_RANDOM_BASIC.
Attempts to generate repeatable ODP_RANDOM_TRUE are contradictory and
should be failed.

Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>

---
 include/odp/api/spec/random.h | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

-- 
2.7.4

Comments

Savolainen, Petri (Nokia - FI/Espoo) Dec. 5, 2016, 3:02 p.m. | #1
> diff --git a/include/odp/api/spec/random.h b/include/odp/api/spec/random.h

> index e732c3a..83ef84c 100644

> --- a/include/odp/api/spec/random.h

> +++ b/include/odp/api/spec/random.h

> @@ -59,6 +59,28 @@ typedef enum {

>  int32_t odp_random_data(uint8_t *buf, uint32_t len, odp_random_kind_t

> kind);

> 

>  /**

> + * Generate repeatable random byte data

> + *

> + * For testing purposes it is often useful to generate "random" sequences

> + * that are repeatable. This is accomplished by supplying a seed value

> that

> + * is used for pseudo-random data generation. The caller provides

> + *

> + * @param[out]    buf  Output buffer

> + * @param         len  Length of output buffer in bytes

> + * @param         kind Specifies the type of random data required.

> Request

> + *                     will fail if the implementation is unable to

> provide

> + *                     repeatable random of the requested type. This is

> + *                     always true for true random and may be true for

> + *                     cryptographic random.

> + * @param[in,out] seed Seed value to use

> + *

> + * @return Number of bytes written

> + * @retval <0 on failure

> + */

> +int32_t odp_random_seeded_data(uint8_t *buf, uint32_t len,

> +			       odp_random_kind_t kind, uint32_t *seed);

> +


I think it's better to name this explicitly pseudo random (or deterministic random), so that people do not by mistake use it instead of the "true" random API call. Also "kind" is not needed here. Seed could be larger or even implementation defined size. There are larger than 32 bit pseudo random functions available  https://linux.die.net/man/3/drand48_r.


/* Uses some bits of init value to initialize the seed */
void odp_random_seed(odp_random_seed_t *seed, uint64_t init_value);

/* Uses a seed to produce a deterministic, pseudo random sequence of data */
int32_t odp_random_pseudo(uint8_t *buf, uint32_t len, odp_random_seed_t *seed);
 

-Petri
Bill Fischofer Dec. 5, 2016, 3:17 p.m. | #2
On Mon, Dec 5, 2016 at 9:02 AM, Savolainen, Petri (Nokia - FI/Espoo)
<petri.savolainen@nokia-bell-labs.com> wrote:
>

>> diff --git a/include/odp/api/spec/random.h b/include/odp/api/spec/random.h

>> index e732c3a..83ef84c 100644

>> --- a/include/odp/api/spec/random.h

>> +++ b/include/odp/api/spec/random.h

>> @@ -59,6 +59,28 @@ typedef enum {

>>  int32_t odp_random_data(uint8_t *buf, uint32_t len, odp_random_kind_t

>> kind);

>>

>>  /**

>> + * Generate repeatable random byte data

>> + *

>> + * For testing purposes it is often useful to generate "random" sequences

>> + * that are repeatable. This is accomplished by supplying a seed value

>> that

>> + * is used for pseudo-random data generation. The caller provides

>> + *

>> + * @param[out]    buf  Output buffer

>> + * @param         len  Length of output buffer in bytes

>> + * @param         kind Specifies the type of random data required.

>> Request

>> + *                     will fail if the implementation is unable to

>> provide

>> + *                     repeatable random of the requested type. This is

>> + *                     always true for true random and may be true for

>> + *                     cryptographic random.

>> + * @param[in,out] seed Seed value to use

>> + *

>> + * @return Number of bytes written

>> + * @retval <0 on failure

>> + */

>> +int32_t odp_random_seeded_data(uint8_t *buf, uint32_t len,

>> +                            odp_random_kind_t kind, uint32_t *seed);

>> +

>

> I think it's better to name this explicitly pseudo random (or deterministic random), so that people do not by mistake use it instead of the "true" random API call. Also "kind" is not needed here. Seed could be larger or even implementation defined size. There are larger than 32 bit pseudo random functions available  https://linux.die.net/man/3/drand48_r.


All non-true random is deterministic since it is the result of
computation, so that's not the best distinction. The issue here is
controlled repeatability, which is what having an explicit seed
parameter is trying to communicate. Perhaps
odp_random_repeatable_data() might be clearer?

Kind is still needed here because there is still a statistical
distinction between basic and cryptographic-quality random data. It's
also consistent. The only kind which cannot be supplied in a
repeatable manner is true random, which by definition is not
repeatable.

>

>

> /* Uses some bits of init value to initialize the seed */

> void odp_random_seed(odp_random_seed_t *seed, uint64_t init_value);

>

> /* Uses a seed to produce a deterministic, pseudo random sequence of data */

> int32_t odp_random_pseudo(uint8_t *buf, uint32_t len, odp_random_seed_t *seed);

>

>

> -Petri

>

>

Patch

diff --git a/include/odp/api/spec/random.h b/include/odp/api/spec/random.h
index e732c3a..83ef84c 100644
--- a/include/odp/api/spec/random.h
+++ b/include/odp/api/spec/random.h
@@ -59,6 +59,28 @@  typedef enum {
 int32_t odp_random_data(uint8_t *buf, uint32_t len, odp_random_kind_t kind);
 
 /**
+ * Generate repeatable random byte data
+ *
+ * For testing purposes it is often useful to generate "random" sequences
+ * that are repeatable. This is accomplished by supplying a seed value that
+ * is used for pseudo-random data generation. The caller provides
+ *
+ * @param[out]    buf  Output buffer
+ * @param         len  Length of output buffer in bytes
+ * @param         kind Specifies the type of random data required. Request
+ *                     will fail if the implementation is unable to provide
+ *                     repeatable random of the requested type. This is
+ *                     always true for true random and may be true for
+ *                     cryptographic random.
+ * @param[in,out] seed Seed value to use
+ *
+ * @return Number of bytes written
+ * @retval <0 on failure
+ */
+int32_t odp_random_seeded_data(uint8_t *buf, uint32_t len,
+			       odp_random_kind_t kind, uint32_t *seed);
+
+/**
  * @}
  */