-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhashlock.fs
29 lines (26 loc) · 1.44 KB
/
hashlock.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%When I run tests, in test_chalang.erl, string "test" appended to the code. That way the test macro gets run.
%I don't want the test macro to run every time I load this library.
%"binary" is the command to load a binary onto the stack.
%The next number 32, tells binary how big the binary will be. Next is a base64 encoded binary.
%"hash" replaces the the binary on the top of the stack with the hash of that binary.
%Then we load a 12 byte binary.
%Then we check if they are equal with the "==" comparison operation.
%comparison doesn't drop the 2 things it compares off the stack because duplication is expensive.
%duplication has to be expensive because we are measuring gas, and using up memory is expensive
%I don't want to have to duplicate things before every comparison to save them from getting deleted.
%Here is how forth operations are documented:
( A B -- A B C )
% This starts with 2 things on the stack, and adds a third on top.
%Next we do a swap, which trades the top 2 elements
( A B C -- A C B )
%Then we drop the top element
( A C B -- A C )
%Then we swap
( A C -- C A )
%Then we drop again
( C A -- C )
%And the only thing left on the stack is the result of the comparison. So if the hash of the 32 byte binary is equal to the 12 byte binary, then it returns [<<1:32>>], otherwise it returns [<<0:32>>]
macro test
binary 32 qfPbi+pbLu+SN+VICd2kPwwhj4DQ0yijP1tM//8zSOY= hash
binary 32 Q+s3EEGaJrywFpYUNhG2Sv9X5/H+7H6xHZKVmehJUMw= == swap drop swap drop
;