# Make a secret or black box function?

**URL:** <https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 26, 2021, 2:16pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017 "2021-03-26T14:16:02Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [April 2, 2021, 1:22am UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/21 "2021-04-02T01:22:42Z")

</div>

isn’t this approach too brittle? `string(Char.(funvec)...)` gives away the function with minor tinkering by a curious student.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [April 2, 2021, 2:56am UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/22 "2021-04-02T02:56:29Z")

</div>

It’s certainly not the best conceivable algorithm.

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 2, 2021, 3:21am UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/23 "2021-04-02T03:21:21Z")

</div>

Note that with the IRTools or the JLD2 solutions (or really anything that gives you a pure Julia function), they’re a `@code_warntype` away from seeing (a partially obfuscated version of) the source of the top-level function call:

```julia
# with IRTools solution from above
julia> @code_warntype g(nothing,2,1)

Body::Float64
1 ─ %1 = Core.apply_type(Base.Val, 2)::Core.Const(Val{2})
│ %2 = (%1)()::Core.Const(Val{2}())
│ %3 = Base.literal_pow(Main.:^, @_3, %2)::Int64
│ %4 = Main.sin(@_4)::Float64
│ %5 = (%3 + %4)::Float64
└── return %5

```

You can fix that by just wrapping the function in a closure, e.g.

```julia
julia> f = (x,y) -> (() -> x^2 + sin(y))();

```

At that point, you could just as well use the built-in Serialization to save the function:

```julia
julia> serialize("secret_function.jls", f);

julia> g = deserialize("secret_function.jls");

julia> @code_warntype g(1,2)

Body::Float64
1 ─ %1 = Serialization. __deserialized_types__.:(var"#14#16")::Core.Const(Serialization. __deserialized_types__.var"#14#16")
│ %2 = Core.typeof(x)::Core.Const(Int64)
│ %3 = Core.typeof(y)::Core.Const(Int64)
│ %4 = Core.apply_type(%1, %2, %3)::Core.Const(Serialization. __deserialized_types__.var"#14#16"{Int64, Int64})
│ (#14 = %new(%4, x, y))
│ %6 = #14::Serialization. __deserialized_types__.var"#14#16"{Int64, Int64}
│ %7 = (%6)()::Float64
└── return %7

julia> g(1,2)
1.9092974268256817

```

The contents of the file is pretty unreadable (though they may see that e.g. `sin` is used in someway, but doubt anyone can easily tell how):

```julia
7JL
43#13#15"XN�D""MLL�#13#�#NMainD#13aREPL[4]�SN�D3,FF9!#self#xy#14�LeF�63#14#16"}~,"}GF~GF",	,
LLL�#14#�#aNMainD#14aREPL[4]�SN�D3,!a#self#�LeF�V$N�D�%�}V$N�D�$N�DVal�V(�V$N�Dliteral_pow$NMainD^(�(�V$N�D�%�~V$NMainDsin(�V$NMainD�(�(�:(����NF�4LineInfoNodeN�DNMainD#14aREPL[4]����NFNN ��������LLLLN�)V$N�Dtypeof%�V$N�Dtypeof%�V$N�D�(�(�(�Y%��(�%�%�%�V(�:(����NF�4,eNMainD#13aREPL[4]���}~#14�NFNN ��������LLLLN�)

```

Although even still, the function is technically reconstructable in Julia if they `@code_warntype` into the deserialized closure, etc… Only way to make this Julia-proof is probably provide a dynamic library for a C compiled function.

---

<div class="post-metadata">

**Author:** ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)\
**Post date:** [April 2, 2021, 6:56am UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/24 "2021-04-02T06:56:29Z")

</div>

AFAIK, Serialisation may not portable enough as the proper recovery cannot be guaranteed from a machine to another right ?

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [April 3, 2021, 2:27am UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/25 "2021-04-03T02:27:45Z")

</div>

I’m not an expert but machine-to-machine should be OK as long as the student’s Julia version is the same or newer-ish than the version that created the file. I say newer"-ish" bc you’re right its good to keep in mind Serializion has no backwards compatibility guarantees and is definitely not meant for long-term data storage, but maybe is fine here, e.g. just checked the file above created on 1.5 is loadable on 1.6.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 3, 2021, 12:23pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/26 "2021-04-03T12:23:26Z")

</div>

> [@mschauer](#):
>
> One way is to approximate the function they should approximate yourself with a different basis/package as they are using and give them the approximation as target.

This gives me an idea: approximate with (tensored) Chebyshev polynomials, drop high-degree cross terms (eg like Smolyak/sparse bases), then emit code with hardcoded coefficients evaluating a polynomial, summing in random order.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [April 6, 2021, 5:58pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/27 "2021-04-06T17:58:26Z")

</div>

@PeterSimon  
I really liked your suggestion. Even without the obfuscation, most students won’t know how to get at the function definition.

I tried this approach (without using the obfuscation middle-man function). I just tried saving and then reloading the function. But unfortunately, it didn’t work:

```julia
julia> @load "Devious_Function.jld2"
┌ Warning: type Main.#f does not exist in workspace; reconstructing
└ @ JLD2 ~/.julia/packages/JLD2/qncOK/src/data/reconstructing_datatypes.jl:358
1-element Array{Symbol,1}:
 :f

julia> f(rand(14))
ERROR: MethodError: objects of type JLD2.ReconstructedTypes.var"##Main.#f#253" are not callable

```

I defined the function with a random vector that was previous defined:

```julia
f(x)=-exp(-sum(1.0.-abs.(x.-x0))^2/100)

```

`x0` was defined from `x0=rand(14)` before the function definition. (And subtracting 1 was just a way of obfuscating a bit…) I wondered if defining the function with a predefined constant vector was a problem…but trying another function with an explicit definition (no previously defined constants) didn’t help.

So I guess I don’t know how to save and then recall a function and have it still be callable.

Anyone have any hints?

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [April 6, 2021, 9:07pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/28 "2021-04-06T21:07:03Z")

</div>

You can’t save and then recall a function type to a JLD2 file, unfortunately. You can skip the obfuscation part of my procedure, but you will need to put the function definition inside a string, as I did. I tried doing it this way (without the obfuscation part) first, but the string contents were visible when the file was opened in an editor (even after turning on compression for the JLD2 file), hence the need for the obfuscation function.

@marius311 showed a better approach in his post. It’s probably what you’re looking for. If that isn’t clear to you, ask and one of us responders will provide another explicit example.

---

<div class="post-metadata">

**Author:** ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)\
**Post date:** [April 9, 2021, 1:17pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/29 "2021-04-09T13:17:58Z")

</div>

@marius311  
I’ve been playing around with your idea a bit. I was under the impression, apparently incorrectly, that I could just read in this `secret_function.jls` file in another session of julia and that the saved function would still be defined, but it seems that the function that I read in (via deserialization) is only defined _in the same session_. It is still making a reference to the orginal, somehow. (I was hoping to share the `.jls` file only with the students and let them load it in…).

```julia
julia> using Serialization

julia> f(x) = -exp(-5*sum((x.-1.0.+[0.24197 0.15722 0.20821 0.86427 0.6547 0.85627 0.58583 0.90464 0.23778 0.38811 0.0803 0.31662 0.57585 0.71083][[2;6;13;8;10;11;7;3;4;1;5;12;14;9]]).^2))
f (generic function with 1 method)

julia> serialize("secret_function.jls",f)

julia> g = deserialize("secret_function.jls")
f (generic function with 1 method)

julia> g(rand(14))
-1.6990329706087957e-5

julia> exit()
➜ ~/.julia/dev julia #start new session of julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using Serialization

julia> g = deserialize("secret_function.jls")
ERROR: UndefVarError: #f not defined
Stacktrace:
...

```

Am I misunderstanding something about your suggestion?

After I posted, I had one more idea—maybe the closure matters? So I tried the closure before serializing and that works! Yah. Thank you.

```julia
julia> f = x -> (() -> -exp(-5*sum((x.-1.0.+[0.24197 0.15722 0.20821 0.86427 0.6547 0.85627 0.58583 0.90464 0.23778 0.38811 0.0803 0.31662 0.57585 0.71083][[2;6;13;8;10;11;7;3;4;1;5;12;14;9]]).^2)))()
#5 (generic function with 1 method)

julia> serialize("secret_function.jls",f)

julia> exit()
➜ ~/.julia/dev julia           
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.0 (2021-03-24)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> using Serialization

julia> f = deserialize("secret_function.jls")
#5 (generic function with 1 method)

julia> f(rand(14))
-4.564053334390881e-5
```

[Previous page](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017.md?page=1)
