# 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:** 20\
**Page:** 1

<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:** [March 26, 2021, 2:16pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/1 "2021-03-26T14:16:02Z")

</div>

I’d like to give students in my research group a function to optimize…but I’d like to keep the functional form secret, for pedagogical purposes. Is there some way to “compile” a function or obfuscate it somehow so that another user can use it but not have access to the actual code? Or maybe someone has an even better idea to meet the teaching goal…

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [March 26, 2021, 2:18pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/2 "2021-03-26T14:18:17Z")

</div>

Have the function value served by a web page on your server?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [March 26, 2021, 2:19pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/3 "2021-03-26T14:19:47Z")

</div>

Maybe write the function in C and compile it into a shared library and have them call it via `@ccall` (or try StaticCompiler.jl)?

Actually, just providing the C source code is perhaps enough obsfucation 😃

---

<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:** [March 26, 2021, 2:28pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/4 "2021-03-26T14:28:07Z")

</div>

I’d have to remember C…it’s been decades… 😆

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [March 26, 2021, 2:34pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/5 "2021-03-26T14:34:58Z")

</div>

Put the source code in a separate file and tell them not to look at it?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [March 26, 2021, 2:36pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/6 "2021-03-26T14:36:34Z")

</div>

Perhaps [https://github.com/FluxML/IRTools.jl/blob/master/README.md](https://github.com/FluxML/IRTools.jl/blob/master/README.md)

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 26, 2021, 6:17pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/7 "2021-03-26T18:17:17Z")

</div>

Isn’t it enough to manually obfuscate it by changing all symbol names to random names? Perhaps together with some manual code transforms purely for obfuscating purposes. If the obfuscation is only for pedagogical purposes, maybe this is enough?

---

<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 1, 2021, 1:49pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/8 "2021-04-01T13:49:37Z")

</div>

Suppose my function was `f(x,y) = x^2+sin(y)`. Can you use this as an example to tell me how to use IRTools to generate some intermediate representation that is directly callable? I looked at the README for IRTools; I could see how code was turned into some “lower level” julia, but it wasn’t clear to me how to call/use that lower level stuff. In the README, I didn’t fully understand

```julia
julia> f = IRTools.func(ir); # Turn the new IR into a lambda

julia> f(nothing, 10, 5)

```

though that seems to be what I want to do: just cut and paste the output of @code…except that only works if you give arguments to it. Can I not define a function with the intermediate representation? If I gave that to a student, they’d be able to use it but not really understand it (i.e., black box).

---

<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 1, 2021, 1:56pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/9 "2021-04-01T13:56:17Z")

</div>

As a MWE, I did this:

```julia
julia> f(x,y) = x^2 + sin(y)
f (generic function with 2 methods)

julia> ir = @code_ir f(1,1)
1: (%1, %2, %3)
  %4 = Core.apply_type(Base.Val, 2)
  %5 = (%4)()
  %6 = Base.literal_pow(Main.:^, %2, %5)
  %7 = Main.sin(%3)
  %8 = %6 + %7
  return %8

julia> g = IRTools.func(ir)
##254 (generic function with 1 method)

julia> g(nothing,2,1)
4.841470984807897

julia> f(2,1)
4.841470984807897

```

Seems to do the trick. But how would I copy the output of `ir = @code...` into a function definition? And why is `nothing` needed as the first argument in `g`? (Probably these are simple questions, but IRs are outside of my experience…)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 1, 2021, 2:17pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/10 "2021-04-01T14:17:21Z")

</div>

Interesting approach. I don’t know how to call `@code_ir`, but if you use `@code_lowered`, and make some adjustments, it works. The result is hard to understand if the function is complex enough:

```julia
julia> f(x,y) = x^2 + sin(y)
f (generic function with 1 method)

julia> @code_lowered f(1.0,1.0)
CodeInfo(
1 ─ %1 = Core.apply_type(Base.Val, 2)
│ %2 = (%1)()
│ %3 = Base.literal_pow(Main.:^, x, %2)
│ %4 = Main.sin(y)
│ %5 = %3 + %4
└── return %5
)

```

I removed the tabbing and replaced the `%` with `var`, to get:

```julia
julia> function g(x,y)
       var1 = Core.apply_type(Base.Val, 2)
       var2 = (var1)()
       var3 = Base.literal_pow(Main.:^, x, var2)
       var4 = Main.sin(y)
       var5 = var3 + var4
            return var5
       end
g (generic function with 1 method)

```

Which is the same function as `f`. Unless your students are used to this and/or your function is very simple, probably that is good enough. I will use it.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 1, 2021, 4:18pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/11 "2021-04-01T16:18:28Z")

</div>

The premise is that if C code is sufficiently obfuscated by its low-level nature, as someone proposed earlier, then even lower-level IR code would be even better obfuscated. I think the approach would be that you supply Julia code that includes some representation of the IR code of your function, and that Julia code would expose the function generated by IRTools.func. I expect you can probably persist that IR code as text or a binary blob. JLD2 maybe?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 1, 2021, 4:24pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/12 "2021-04-01T16:24:42Z")

</div>

> [@Gus\_Hart](#):
>
> And why is `nothing` needed as the first argument in `g` ?

I don’t know, but you could wrap g with another function that calls `g` passing `nothing` as the first argument.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 1, 2021, 4:38pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/13 "2021-04-01T16:38:29Z")

</div>

Is there some way to define a function that evals the `@code_native` output? That is really obfuscated.

```julia
julia> f(x,y) = x + y
f (generic function with 1 method)

julia> @code_native f(1,1)
        .text
; ┌ @ REPL[51]:1 within `f'
; │┌ @ int.jl:87 within `+'
        leaq (%rdi,%rsi), %rax
; │└
        retq
        nopw %cs:(%rax,%rax)
; └

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [April 1, 2021, 4:43pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/14 "2021-04-01T16:43:48Z")

</div>

This might be a stupid question, but does

```julia
(v1.1) pkg> add https://github.com/JuliaLang/Example.jl

```

work if the Github repo is a private repo? My guess is that it does not… 😅

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 1, 2021, 4:46pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/15 "2021-04-01T16:46:20Z")

</div>

If it would, the repo wouldn’t be private 🙂

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 1, 2021, 4:47pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/16 "2021-04-01T16:47:48Z")

</div>

> [@lmiq](#):
>
> Is there some way to define a function that evals the `@code_native` output?

Don’t know about native but I’d naively guess that `@code_llvm` should be possible (don’t ask me how though…). At least there are things like `Base.llvmcall` and such.

---

<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 1, 2021, 6:22pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/17 "2021-04-01T18:22:23Z")

</div>

Here is a simple obfuscation scheme that is probably adequate for students who aren’t very familiar with Julia…

Contents of `save_obfuscated.jl`:

```nohighlight
using JLD2

obfuscate_string(str) = [Int(c) for c in str]

funvec = obfuscate_string(
"""
function obfuscated_function(x,y)
    x^2 + 2x*y + y^2
end
""")

@save "function_archive.jld2" {compress=true} funvec

```

Contents of `load_obfuscated.jl`:

```nohighlight
using JLD2

@load "function_archive.jld2" funvec
include_string(@ __MODULE__ , string(Char.(funvec)...))

```

Sample usage…

First Julia session:

```nohighlight
julia> include("save_obfuscated.jl")

```

A later Julia session run by students for whom you did not supply a copy of `save_obfuscated.jl`:

```nohighlight
julia> include("load_obfuscated.jl")
obfuscated_function (generic function with 1 method)

julia> obfuscated_function(3,4.0)
49.0

```

The file `function_archive.jld2` (which you do supply to your students along with `load_obfuscated.jl`) is not human-readable.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [April 1, 2021, 6:33pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/18 "2021-04-01T18:33:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anon74562486](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anon74562486](https://discourse.julialang.org/u/anon74562486)\
**Post date:** [April 1, 2021, 6:57pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/19 "2021-04-01T18:57:11Z")

</div>

If you have a simple function like f(x, y) = x^2 + sin(y) in a given domain and you want it as a black box you can learn it in a neural network. It is black box because of its complexity.  
Then you can give to your students a function which encapsulate the network.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 1, 2021, 11:49pm UTC](https://discourse.julialang.org/t/make-a-secret-or-black-box-function/58017/20 "2021-04-01T23:49:32Z")

</div>

@code\_native might not be portable enough

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