|
| 1 | +# Pseudo-Random Number Generators |
| 2 | + |
| 3 | +In the following example, we will explore the PRNGs available in `ChaoticEncryption.jl`. The API documentation for `ChaoticEncryption.jl` is available [here](https://saransh-cpp.github.io/ChaoticEncryption.jl). |
| 4 | + |
| 5 | +Let us start by adding in the julia packages we will be needing - |
| 6 | + |
| 7 | +```jldoctest prng |
| 8 | +# install TestImages for this tutorial |
| 9 | +# julia> using Pkg |
| 10 | +# julia> Pkg.add("TestImages") |
| 11 | +# install ChaoticEncryption.jl if you haven't already! |
| 12 | +# julia> Pkg.add("ChaoticEncryption") |
| 13 | +
|
| 14 | +julia> using TestImages, ChaoticEncryption |
| 15 | +
|
| 16 | +``` |
| 17 | + |
| 18 | +## PRNGs in ChaoticEncryption.jl |
| 19 | + |
| 20 | +Currently, `ChaoticEncryption.jl` includes 2 PRNGs, which are- |
| 21 | +1. Logistic Map |
| 22 | +2. Lorenz System of Differential Equations |
| 23 | + |
| 24 | +We will be adding more of them soon! If you stumble upon an interesting PRNG, feel free to create an issue or a pull request for the same! |
| 25 | + |
| 26 | +## Logistic Map |
| 27 | + |
| 28 | +As per the [documentation](https://saransh-cpp.github.io/ChaoticEncryption.jl/dev/apidocs/prngs/#Logistic-map), `logistic_key` generates a vectors of pseudo-random numbers using the Logistic Map. |
| 29 | + |
| 30 | +The function uses the following equation to generate pseudo-random numbers - |
| 31 | + |
| 32 | +```math |
| 33 | +x_{n+1} = r * x_{n} * (1 - x_{n}) |
| 34 | +``` |
| 35 | + |
| 36 | +The function takes in the following arguments - |
| 37 | + |
| 38 | +- `x_init::Float64`: Initial value of x. x ϵ (0, 1). |
| 39 | +- `r::Float64`: A constant value. Values > 4 usually results in pseudo-random numbers. |
| 40 | +- `num_keys::Int64`: Number of keys to be generated. |
| 41 | +- `scaling_factor::Float64=10.0^16`: Factor to be multiplied to the generated value of pseudo-random |
| 42 | + number. Ideally, the factor should be > upper_bound. |
| 43 | +- `upper_bound::Float64=256.0`: Upper bound of keys (not included). Use 256 for encrypting images |
| 44 | + as the RGB values of a pixel varies from 0 to 255. |
| 45 | + |
| 46 | +And returns the following `Vector` - |
| 47 | +- `keys::Vector{Int64}:`: Generated pseudo-random keys. |
| 48 | + |
| 49 | +## Using logistic_key |
| 50 | + |
| 51 | +After going through the documentation, let us use the function `logistic_key` with the following aarguments - |
| 52 | +- x_init = 0.01 |
| 53 | +- r = 3.97 |
| 54 | +- num_keys = 20 |
| 55 | + |
| 56 | +```jldoctest prng |
| 57 | +julia> keys = logistic_key(0.01, 3.97, 20) |
| 58 | +20-element Vector{Int64}: |
| 59 | + 0 |
| 60 | + 44 |
| 61 | + 7 |
| 62 | + 26 |
| 63 | + 14 |
| 64 | + 224 |
| 65 | + 16 |
| 66 | + 250 |
| 67 | + 162 |
| 68 | + 211 |
| 69 | + 200 |
| 70 | + 217 |
| 71 | + 97 |
| 72 | + 132 |
| 73 | + 134 |
| 74 | + 100 |
| 75 | + 135 |
| 76 | + 232 |
| 77 | + 122 |
| 78 | + 102 |
| 79 | +``` |
| 80 | + |
| 81 | +This returns a 1 dimensional `Vector` of pseudo-random `Int64` elements ranging from 0 - 255 (as the RGB values of an image range from 0 - 255)! |
| 82 | + |
| 83 | +## Generating pseudo-random keys for an image |
| 84 | + |
| 85 | +Now we can try to generate a pseudo-random key for each pixel in a given image. Let us load an image using the `TestImages` package for this! |
| 86 | + |
| 87 | +```julia |
| 88 | +julia> img = testimage("cameraman"); |
| 89 | + |
| 90 | +julia> height, width = size(img) |
| 91 | +(512, 512) |
| 92 | +``` |
| 93 | + |
| 94 | +The image can be viewed using [ImageView.jl](https://github.com/JuliaImages/ImageView.jl) - |
| 95 | + |
| 96 | +```julia |
| 97 | +julia> using ImageView |
| 98 | + |
| 99 | +julia> imshow(img) |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +Generating a key for each pixel in the image |
| 105 | + |
| 106 | +```julia |
| 107 | +julia> keys = logistic_key(0.01, 3.67, height * width) |
| 108 | +262144-element Vector{Int64}: |
| 109 | + 0 |
| 110 | + 68 |
| 111 | + 135 |
| 112 | + 20 |
| 113 | + 13 |
| 114 | + 140 |
| 115 | + 197 |
| 116 | + 182 |
| 117 | + 248 |
| 118 | + 229 |
| 119 | + ⋮ |
| 120 | + 168 |
| 121 | + 182 |
| 122 | + 77 |
| 123 | + 83 |
| 124 | + 74 |
| 125 | + 176 |
| 126 | + 27 |
| 127 | + 251 |
| 128 | + 206 |
| 129 | +``` |
| 130 | + |
| 131 | +We can now use these keys to encrypt the image! Encryption and decryption will be covered in another example :) |
| 132 | + |
| 133 | +## Lorenz System of Differential Equations |
| 134 | + |
| 135 | +As per the [documentation](https://saransh-cpp.github.io/ChaoticEncryption.jl/dev/apidocs/prngs/#Lorenz-system-of-differential-equations), `lorenz_key` generates 3 vectors of pseudo-random numbers using Lorenz system of differential equations. |
| 136 | + |
| 137 | +The function uses the following system of differential equations to generate pseudo-random numbers - |
| 138 | + |
| 139 | +```math |
| 140 | +\frac{dx}{dt} = α * (y - x) |
| 141 | +``` |
| 142 | +```math |
| 143 | +\frac{dy}{dt} = x * (ρ - z) - y |
| 144 | +``` |
| 145 | +```math |
| 146 | +\frac{dz}{dt} = x * y - β * z |
| 147 | +``` |
| 148 | + |
| 149 | +The function takes in the following arguments - |
| 150 | + |
| 151 | +- `x_init::Float64`: Initial value of x. |
| 152 | +- `y_init::Float64`: Initial value of y. |
| 153 | +- `z_init::Float64:` Initial value of z. |
| 154 | +- `num_keys::Int64`: Number of keys (in a single list) to be generated. |
| 155 | +- `α::Float64`: Constant associated with Lorenz system of differential equations. |
| 156 | +- `ρ::Float64`: Constant associated with Lorenz system of differential equations. |
| 157 | +- `β::Float64`: Constant associated with Lorenz system of differential equations. |
| 158 | +- `scaling_factor::Float64=10.0^16`: Factor to be multiplied to the generated value of pseudo-random |
| 159 | + number. Ideally, the factor should be > upper_bound. |
| 160 | +- `upper_bound::Float64=256.0`: Upper bound of keys (not included). Use 256 for encrypting images |
| 161 | + as the RGB values of a pixel varies from 0 to 255. |
| 162 | + |
| 163 | +And returns the following `Vector`s |
| 164 | + |
| 165 | +- `x::Vector{Int64}`: Generated pseudo-random keys corresponding to x values. |
| 166 | +- `y::Vector{Int64}`: Generated pseudo-random keys corresponding to y values. |
| 167 | +- `z::Vector{Int64}`: Generated pseudo-random keys corresponding to z values. |
| 168 | + |
| 169 | +## Using lorenz_key |
| 170 | + |
| 171 | +After going through the documentation, let us use the function `lorenz_key` with the following aarguments - |
| 172 | +- x_init = 0.01 |
| 173 | +- y_init = 0.02 |
| 174 | +- z_init = 0.03 |
| 175 | +- num_keys = 20 |
| 176 | + |
| 177 | +You can play with other arguments as well! |
| 178 | + |
| 179 | +```jldoctest prng |
| 180 | +julia> keys = lorenz_key(0.01, 0.02, 0.03, 20) |
| 181 | +([0, 0, 256, 24, 129, 42, 54, 134, 43, 179, 85, 19, 24, 44, 71, 210, 238, 152, 22, 27], [0, 0, 240, 55, 25, 163, 89, 243, 123, 5, 197, 64, 227, 54, 188, 226, 154, 134, 64, 69], [0, 0, 80, 227, 178, 204, 89, 33, 144, 139, 105, 208, 108, 155, 61, 254, 57, 102, 149, 47]) |
| 182 | +``` |
| 183 | + |
| 184 | +## Generating pseudo-random keys for an image |
| 185 | + |
| 186 | +Now we can try to generate a pseudo-random key for each pixel in a given image. Let us load an image using the `TestImages` package for this! |
| 187 | + |
| 188 | +```julia |
| 189 | +julia> img = testimage("cameraman"); |
| 190 | + |
| 191 | +julia> height, width = size(img) |
| 192 | +(512, 512) |
| 193 | +``` |
| 194 | + |
| 195 | +Generating a key for each pixel in the image |
| 196 | + |
| 197 | +```julia |
| 198 | +julia> x, y, z = lorenz_key(0.01, 0.02, 0.03, height * width) |
| 199 | +([0, 0, 256, 24, 129, 42, 54, 134, 43, 179 … 46, 94, 18, 206, 68, 98, 72, 10, 248, 136], [0, 0, 240, 55, 25, 163, 89, 243, 123, 5 … 4, 112, 116, 100, 108, 92, 236, 80, 152, 144], [0, 0, 80, 227, 178, 204, 89, 33, 144, 139 … 128, 48, 176, 128, 176, 72, 168, 32, 208, 112]) |
| 200 | +``` |
| 201 | + |
| 202 | +`lorenz_key` returns a `Tuple` with each element being an `Vector{Int64}`. Thus, it returns a variable of the type `Tuple{Vector{Int64}, Vector{Int64}, Vector{Int64}}`. |
| 203 | + |
| 204 | +```julia |
| 205 | +julia> x |
| 206 | +262144-element Vector{Int64}: |
| 207 | + 0 |
| 208 | + 0 |
| 209 | + 256 |
| 210 | + 24 |
| 211 | + 129 |
| 212 | + 42 |
| 213 | + 54 |
| 214 | + 134 |
| 215 | + 43 |
| 216 | + 179 |
| 217 | + ⋮ |
| 218 | + 94 |
| 219 | + 18 |
| 220 | + 206 |
| 221 | + 68 |
| 222 | + 98 |
| 223 | + 72 |
| 224 | + 10 |
| 225 | + 248 |
| 226 | + 136 |
| 227 | +``` |
| 228 | + |
| 229 | +A notebook version of this tutorial is available [here](https://github.com/Saransh-cpp/ChaoticEncryption.jl/blob/master/examples/PseudoRandomNumberGenerators.ipynb). Don't forget to star [`ChaoticEncryption.jl`](https://saransh-cpp.github.io/ChaoticEncryption.jl) :) |
| 230 | + |
| 231 | +The complete API documentation is available [here](https://saransh-cpp.github.io/ChaoticEncryption.jl). |
0 commit comments