Hashtron inference: Difference between revisions

Content added Content deleted
(julia example)
m (Removed "Example Code" heading and put all languages at the ssame level and in correct order)
Line 70: Line 70:
The inference function should process the input and generate the square root of the input byte.
The inference function should process the input and generate the square root of the input byte.


=={{header|FreeBASIC}}==
== Example Code ==
=== {{header|Python}} ===
<syntaxhighlight lang="python" line>
def hash(n, s, max_val):
# Mixing stage, mix input with salt using subtraction
m = (n - s) & 0xFFFFFFFF
# 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 len(program) == 0:
return out
# Iterate over the bits
for j in range(bits):
input_val = command | (j << 16)
ss, maxx = program[0]
input_val = hash(input_val, ss, maxx)
for i in range(1, len(program)):
s, max_val = program[i]
maxx -= max_val
input_val = hash(input_val, s, maxx)
input_val &= 1
if input_val != 0:
out |= 1 << j
return out

print(inference(42,64,[[0,2]]))</syntaxhighlight>

==={{header|FreeBASIC}}===
<syntaxhighlight lang="vbnet">Function hash(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
<syntaxhighlight lang="vbnet">Function hash(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
Dim As Ulongint k = &hFFFFFFFF
Dim As Ulongint k = &hFFFFFFFF
Line 161: Line 118:
Sleep</syntaxhighlight>
Sleep</syntaxhighlight>


=== {{header|Go}} ===
=={{header|Go}}==
<syntaxhighlight lang="go" line>
<syntaxhighlight lang="go" line>
package main
package main
Line 216: Line 173:
https://go.dev/play/p/AsmOzKWx7jB
https://go.dev/play/p/AsmOzKWx7jB


=== {{header|PHP}} ===
=={{header|Java}}==
<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>
<syntaxhighlight lang="php" line>
// define hashtron hash
// define hashtron hash
Line 284: Line 303:
};</syntaxhighlight>
};</syntaxhighlight>


=== {{header|Java}} ===
=={{header|Python}}==
<syntaxhighlight lang="java" line>
<syntaxhighlight lang="python" line>
def hash(n, s, max_val):
public class Main {
# Mixing stage, mix input with salt using subtraction
public static long inference(long command, long bits, long[][] program) {
long out = 0;
m = (n - s) & 0xFFFFFFFF
# 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


// Check if the program is empty
if (program.length == 0) {
return out;
}


def inference(command, bits, program):
// Iterate over the bits
out = 0
for (long j = 0; j < bits; j++) {
# Check if the program is empty
long input = (command & 0xFFFFFFFF) | (((long)j & 0xFF) << (long)16);
long ss = program[0][0];
if len(program) == 0:
long maxx = program[0][1];
return out
# Iterate over the bits
input = hash(input, ss, maxx);
for j in range(bits):
for (long i = 1; i < program.length; i++) {
long s = program[(int)i][0];
input_val = command | (j << 16)
long max = program[(int)i][1];
ss, maxx = program[0]
maxx -= max;
input_val = hash(input_val, ss, maxx)
input = hash(input, s, maxx);
for i in range(1, len(program)):
}
s, max_val = program[i]
input &= 1;
maxx -= max_val
if (input != 0) {
input_val = hash(input_val, s, maxx)
input_val &= 1
out |= (long)1 << (long) j;
}
if input_val != 0:
}
out |= 1 << j
return out;
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;
}


print(inference(42,64,[[0,2]]))</syntaxhighlight>
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|Julia}}==
=={{header|Julia}}==