5

I am writing my Master's Thesis in cryptography at the moment. The main part deals with randomness; therefore, I wanted to make a little Tikz picture in the introduction to show an example of randomness. Of course, I could create a random string somewhere else and just import it to LaTeX, but this approach feels unsatisfying. My question: Is there a way to access a cryptographic secure random number generator directly from LaTeX, e.g. within a Tikz picture? I assume my solution using random() from pgf math is not cryptographic secure:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{center}
        \begin{tikzpicture}
            \foreach \row in {0,1,...,7}{
                \foreach \column in {0,1,...,7}{
                    \pgfmathparse{random(0,1)}
                    \ifnum\pgfmathresult=1
                        \draw[fill, black] (\row, \column) rectangle (\row +1, \column + 1);
                    \fi
                }
            }
        \end{tikzpicture}
    \end{center}

\end{document}

Edit: To be more accurate on the secure part: I am searching for a random generator, that is accepted as a standard, e.g. by Nist. For those more interested in the topic, here is a reference.

10
  • 2
    I think for the benefit of the general readership you could define 'cryptographically secure' here. I think it's likely that neither the pgf macro-based implementation nor the engine primitives are going to meet the requirement.
    – Joseph Wright
    Dec 8, 2022 at 9:05
  • Are you familiar with LuaLaTeX and the ways you could access Lua's math.random function?
    – Mico
    Dec 8, 2022 at 9:12
  • @Mico I suspect that's the same, though: is it going to meet the criteria required by someone working on cryptography?
    – Joseph Wright
    Dec 8, 2022 at 9:13
  • 2
    @JosephWright - I agree with you. :-) Lua's reference guide states that the math.random function "uses the xoshiro256** algorithm to produce pseudo-random 64-bit integers, which are the results of calls with argument 0. Other results (ranges and floats) are unbiased extracted from these integers." Hopefully, the OP will know whether this explanation indicates that the resulting pseudo random numbers are 'cryptographically secure'.
    – Mico
    Dec 8, 2022 at 9:17
  • 3
    You can find a secure random generator in Lua here. It's an implementation of ISAAC CSPRNG in Lua. So you should be able to use it with LuaLaTeX. However the randomness depends on the used entropy. There are several more implementations of secure random generators in Lua available via internet. So I would suggest to use LuaLaTeX and try one (or more) of them.
    – cabohah
    Dec 8, 2022 at 9:39

1 Answer 1

5

You can use -shell-escape to make a call to some program on your platform that implements a CSPRNG.

In the following example, I use /dev/urandom via od using a suggestion from Heitor's log, because /dev/urandom on macOS uses Fortuna.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\settorandomnumber}{m}
 {% #1 = macro to be set
  \sys_get_shell:nnN
   { od ~ -vAn ~ -N4 ~ -t ~ u4 ~ < ~ /dev/urandom } % execute
   { \endlinechar=-1 } % ignore endline
   \l_tmpa_tl % store in
  \tl_set:Nx #1 { \fp_eval:n { \l_tmpa_tl / (2^32) } } % normalize
 }

\ExplSyntaxOff

\begin{document}

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\end{document}

The call to od generates a random integer between 0 and 232 − 1 (inclusive), and we can divide to get a number in the interval [0,1).

enter image description here

Here's also a “random bit generator”: we can generate just a one-byte number and check whether it's larger than 127.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\settorandomnumber}{m}
 {% #1 = macro to be set
  \sys_get_shell:nnN
   { od ~ -vAn ~ -N4 ~ -t ~ u4 ~ < ~ /dev/urandom } % execute
   { \endlinechar=-1 } % ignore endline
   \l_tmpa_tl % store in
  \tl_set:Nx #1 { \fp_eval:n { \l_tmpa_tl / (2^32) } } % normalize
 }

\NewDocumentCommand{\settorandombit}{m}
 {% #1 = macro to be set
  \sys_get_shell:nnN
   { od ~ -vAn ~ -N1 ~ -t ~ u4 ~ < ~ /dev/urandom } % execute
   { \endlinechar=-1 } % ignore endline
   \l_tmpa_tl % store in
  \tl_set:Nx #1 { \int_compare:nNnTF { \l_tmpa_tl } > {127} { 1 } { 0 } }

 }

\ExplSyntaxOff

\begin{document}

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandomnumber\pgfmathresult \pgfmathresult

\settorandombit\pgfmathresult \pgfmathresult

\settorandombit\pgfmathresult \pgfmathresult

\settorandombit\pgfmathresult \pgfmathresult

\settorandombit\pgfmathresult \pgfmathresult

\end{document}

enter image description here

6
  • OP wants either 0 or 1 on a random basis.
    – AlexG
    Dec 8, 2022 at 11:05
  • @AlexG I was able to archive getting 0 and 1 by adding the round function inside the floating-point evaluation: \fp_eval:n { round( \l_tmpa_tl / (2^32) ) } - I hope this does not conflict with my initial goal
    – Titanlord
    Dec 8, 2022 at 11:26
  • @Titanlord You can use just one byte instead of four and round.
    – egreg
    Dec 8, 2022 at 11:35
  • @Titanlord : 0.5 always rounds up to 1. So you may get slightly more ones than zeros.
    – AlexG
    Dec 8, 2022 at 11:37
  • 1
    @Titanlord I added the random bit generation
    – egreg
    Dec 8, 2022 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .