Hashtron inference: Difference between revisions
m
Removed "Example Code" heading and put all languages at the ssame level and in correct order
(julia example) |
m (Removed "Example Code" heading and put all languages at the ssame level and in correct order) |
||
Line 70:
The inference function should process the input and generate the square root of the input byte.
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Function hash(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
Dim As Ulongint k = &hFFFFFFFF
Line 161 ⟶ 118:
Sleep</syntaxhighlight>
==
<syntaxhighlight lang="go" line>
package main
Line 216 ⟶ 173:
https://go.dev/play/p/AsmOzKWx7jB
==
<syntaxhighlight lang="java" line>
public class Main {
public static long inference(long command, long bits, long[][] program) {
long out = 0;
// Check if the program is empty
if (program.length == 0) {
return out;
}
// Iterate over the bits
for (long j = 0; j < bits; j++) {
long input = (command & 0xFFFFFFFF) | (((long)j & 0xFF) << (long)16);
long ss = program[0][0];
long maxx = program[0][1];
input = hash(input, ss, maxx);
for (long i = 1; i < program.length; i++) {
long s = program[(int)i][0];
long max = program[(int)i][1];
maxx -= max;
input = hash(input, s, maxx);
}
input &= 1;
if (input != 0) {
out |= (long)1 << (long) j;
}
}
return out;
}
public static long hash(long n, long s, long max_val) {
// Mixing stage, mix input with salt using subtraction
long m = (n - s) & 0xFFFFFFFFL;
// Hashing stage, use xor shift with prime coefficients
m ^= (m << 2) & 0xFFFFFFFFL;
m ^= (m << 3) & 0xFFFFFFFFL;
m ^= (m >> 5) & 0xFFFFFFFFL;
m ^= (m >> 7) & 0xFFFFFFFFL;
m ^= (m << 11) & 0xFFFFFFFFL;
m ^= (m << 13) & 0xFFFFFFFFL;
m ^= (m >> 17) & 0xFFFFFFFFL;
m ^= (m << 19) & 0xFFFFFFFFL;
// Mixing stage 2, mix input with salt using addition
m += s;
m &= 0xFFFFFFFFL;
// Modular stage using Lemire's fast alternative to modulo reduction
return ((m * max_val) >>> 32) & 0xFFFFFFFFL;
}
public static void main(String[] args) {
long command = 42;
long[][] program = {{0,2}}; // Example program
long bits = 64;
long result = inference(command, bits, program);
System.out.println(Long.toUnsignedString(result));
}
}</syntaxhighlight>
=={{header|PHP}}==
<syntaxhighlight lang="php" line>
// define hashtron hash
Line 284 ⟶ 303:
};</syntaxhighlight>
==
<syntaxhighlight lang="
def hash(n, s, max_val):
# Mixing stage, mix input with salt using subtraction
m = (n -
# Hashing stage, use xor shift with prime coefficients
m ^= (m << 2) & 0xFFFFFFFF
m ^= (m << 3) & 0xFFFFFFFF
m ^= (m >> 5) & 0xFFFFFFFF
m ^= (m >> 7) & 0xFFFFFFFF
m ^= (m << 11) & 0xFFFFFFFF
m ^= (m << 13) & 0xFFFFFFFF
m ^= (m >> 17) & 0xFFFFFFFF
m ^= (m << 19) & 0xFFFFFFFF
# Mixing stage 2, mix input with salt using addition
m += s
m &= 0xFFFFFFFF
# Modular stage using Lemire's fast alternative to modulo reduction
return ((m * max_val) >> 32) & 0xFFFFFFFF
def inference(command, bits, program):
out = 0
# Check if the program is empty
if
return
# Iterate over the bits
for j in range(bits):
input_val = command | (j <<
ss,
input_val = hash(input_val,
for i in
input_val &= 1
if input_val !=
print(inference(42,64,[[0,2]]))</syntaxhighlight>
=={{header|Julia}}==
|