Skip to content

Commit

Permalink
Task 4: try bit magic
Browse files Browse the repository at this point in the history
  • Loading branch information
akifoq committed Aug 16, 2023
1 parent 4741d7a commit c814a09
Showing 1 changed file with 135 additions and 61 deletions.
196 changes: 135 additions & 61 deletions contracts/4.fc
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,165 @@
() recv_internal() {
}

builder encrypt_asm(tuple t, slice cs) asm """
// tuple cs
SWAP c7 POP // cs
AGAINBRK:<{
// ... cs
NEWC SWAP // res cs
DUP SBITS 3 RSHIFT#
REPEAT:<{
8 LDU // res c cs
SWAP GETGLOBVAR // res cs c'

ROT STSLICE // cs res
SWAP
}> // res cs
DUP SREFS IFNOTRETALT

LDREFRTOS NIP // ... res cs'
}> // ... res cs'
DROP // ... res

DEPTH DEC REPEAT:<{
STBREFR
}>
""";
builder encrypt_asm(slice cs, int shift) asm """
0x8080808080808080808080808080808080808080808080808080808080808080 constant mask7
0x0101010101010101010101010101010101010101010101010101010101010101 constant mask0
0x2020202020202020202020202020202020202020202020202020202020202020 constant mask5

0x80808080808080808080808080808080808080808080808080808080808080 constant mask7_31
0x01010101010101010101010101010101010101010101010101010101010101 constant mask0_31
0x20202020202020202020202020202020202020202020202020202020202020 constant mask5_31

// b - b
{ mask7 PUSHINT mask5 PUSHINT // x mask7 mask5

s2 PUSH NOT OVER AND // x mask7 mask5 nx5

s3 s3 XCHG2 OR // nx5 mask7 x

DUP NOT s2 PUSH AND // nx5 mask7 x nx7

s1 s2 PUXC OR // nx5 nx7 x x'

// x_gt = x' - (mask0 * 97);
DUP mask0 97 * PUSHINT SUB // nx5 nx7 x x' x_gt

// x_mid = x' - (mask0 * (123 - shift));
OVER mask0 123 @' shift - * PUSHINT SUB // nx5 nx7 x x' x_gt x_mid

// x+ = nx7 & x_gt & (~ x_mid)
s4 s1 PUXC AND OVER NOT AND // nx5 nx7 x x' x_mid x+

// x += (x+ >> 7) * shift;
7 RSHIFT# @' shift MULCONST s1 s3 XCHG ADD

// x_lt = x' - (mask0 * 123);
SWAP mask0 123 * PUSHINT SUB // nx5 nx7 x_mid x x_lt

// x- = nx7 & x_mid & (~ x_lt);
2SWAP AND SWAP NOT AND // nx5 x x-

// x -= (x- >> 7) * (26 - shift);
7 RSHIFT# 26 @' shift - MULCONST SUB // nx5 x

SWAP XOR // x
} : gen-code-32

{ mask7_31 PUSHINT mask5_31 PUSHINT // x mask7 mask5

s2 PUSH NOT OVER AND // x mask7 mask5 nx5

tuple gt_asm(int shift) asm """
// b x -- b
{ <b swap 8 u, b> <s PUSHSLICE } : add-num
s3 s3 XCHG2 OR // nx5 mask7 x

// shift -- code
{ =: shift
<{ 0
{ tuck add-num swap 1+ } 64 times
add-num
DUP NOT s2 PUSH AND // nx5 mask7 x nx7

0 { tuck @' shift + 26 mod 65 +
add-num swap 1+ } 26 times drop
s1 s2 PUXC OR // nx5 nx7 x x'

91 add-num
92 add-num
93 add-num
94 add-num
95 add-num
96 add-num
// x_gt = x' - (mask0 * 97);
DUP mask0_31 97 * PUSHINT SUB // nx5 nx7 x x' x_gt

0 { tuck @' shift + 26 mod 97 +
add-num swap 1+ } 26 times drop
// x_mid = x' - (mask0 * (123 - shift));
OVER mask0_31 123 @' shift - * PUSHINT SUB // nx5 nx7 x x' x_gt x_mid

123 { tuck add-num swap 1+ } 131 times
add-num
// x+ = nx7 & x_gt & (~ x_mid)
s4 s1 PUXC AND OVER NOT AND // nx5 nx7 x x' x_mid x+

255 PUSHINT TUPLEVAR }>s
} : gen-table
// x += (x+ >> 7) * shift;
7 RSHIFT# @' shift MULCONST s1 s3 XCHG ADD

// x_lt = x' - (mask0 * 123);
SWAP mask0_31 123 * PUSHINT SUB // nx5 nx7 x_mid x x_lt

// x- = nx7 & x_mid & (~ x_lt);
2SWAP AND SWAP NOT AND // nx5 x x-

// x -= (x- >> 7) * (26 - shift);
7 RSHIFT# 26 @' shift - MULCONST SUB // nx5 x

SWAP XOR // x
} : gen-code-31

// b - b
{ // x
DUP 96 GTINT OVER 123 LESSINT AND
IF:<{ // x
-97 @' shift + ADDCONST
26 PUSHINT MOD
97 ADDCONST
}>

DUP 64 GTINT OVER 91 LESSINT AND
IF:<{ // x
-65 @' shift + ADDCONST
26 PUSHINT MOD
65 ADDCONST
}> // x
} : gen-code-1

// b - b
{ // cs
AGAINBRK:<{
// ... cs
NEWC SWAP // res cs

DUP SBITS 1016 EQINT IFNOTRETALT

3 PUSHINT REPEAT:<{
256 LDU SWAP gen-code-32 // res cs x
ROT 256 STU SWAP // res cs
}>

248 LDU SWAP gen-code-31 // res cs x
ROT 248 STU SWAP // res cs

DUP SREFS IFNOTRETALT

LDREFRTOS NIP // ... res cs'
}> // ... res cs'
DUP SBITS IF:<{
// res cs
DUP SBITS 3 RSHIFT#
REPEAT:<{ // res cs
8 LDU SWAP // res cs' x

gen-code-1 // res cs' x'

ROT 8 STU SWAP // res' cs'
}>
}>
DROP // ... res

DEPTH DEC REPEAT:<{
STBREFR
}> ENDC
} : gen-code

variable dict
dictnew dict !

// v x --
{ dict @ 5 udict! drop dict ! } : dict!+

0 { dup dup gen-table swap dict!+ 1+ } 26 times drop
0 { dup =: shift
dup <{ gen-code }>s swap dict!+
1+ } 26 times drop

// shift
dict @ 5 DICTPUSHCONST DICTUGETEXEC
// cs shift
dict @ 5 DICTPUSHCONST DICTUGETJMP
""";

builder encrypt(tuple t, slice cs) inline {
return encrypt_asm(t, cs);
}

tuple generate_tuple(int shift) inline {
return gt_asm(shift);
builder encrypt(slice cs, int shift) inline {
return encrypt_asm(cs, shift);
}

cell cce_asm(int shift, cell text) asm """
// shift text
DUP HASHCU 28907775397853441246312228362702888526552807915760858513294714309537547733684 EQINT
IFJMP:<{ NIP }>

SWAP 26 PUSHINT MOD generate_tuple INLINECALLDICT SWAP // tuple text

CTOS 32 PUSHINT SDSKIPFIRST
encrypt INLINECALLDICT // res
0 PUSHINT NEWC // res 0 b
32 STU STB ENDC
CTOS SWAP
26 PUSHINT MOD
encrypt INLINECALLDICT
""";

cell ccd_asm(int shift, cell text) asm """
Expand Down

0 comments on commit c814a09

Please sign in to comment.