NodeCrypto Help

Syntax

The language follows a clean, restricted subset of Python syntax. Features intentionally omitted or modified from standard Python are summarized in Differences from Python.

Lines and Indentation

Identifiers and Keywords

Literals

TypeSyntax
intDecimal 255, hexadecimal 0xFF, binary 0b1010. Underscore separators (1_000) are allowed. Negative numbers use the unary - operator.
boolTrue, False
bytes (hex)h"87 23 af"
bytes (binary)b"1010 0001"
bytes (UTF-8 text)s"label" (UTF-8 encoded byte sequence)
string"...". Supported escapes: \\, \", \n, \r, \t, \uNNNN. Single-quoted strings are not used.
string (formatted)f"len={n}" (see String Formatting).
list[e1, e2, ...]. All elements must have the same type. An empty list requires an explicit type annotation, such as xs: list[bytes] = [].

Bytes Literals

Unlike Python, b"..." is a binary bit-string literal (0 and 1), not an ASCII byte literal. Use h"..." for hex bytes and s"..." for ASCII/UTF-8 text bytes.
ExpressionResult (Hex)
h"87 23 af"8723af
b"1000 0111"87
s"0123"30313233
s"0123" == h"30313233"True

Adjacent literals of the same kind inside parentheses are concatenated automatically across lines, allowing multi-line keys or test vectors with per-line comments:

KEY = (h"00 11 22 33 44 55 66 77"   # first 8 bytes
       h"88 99 aa bb cc dd ee ff")   # last 8 bytes
o = KEY

Statements

StatementForm
Assignmentx = expr
Typed Assignmentx: int = expr
Augmented Assignmentx += e (also -= *= //= %= &= |= ^= <<= >>=). Equivalent to x = x op e.
Subscript Assignmentb[i] = v, b[a:c] = part, b[o, n] = part, xs[i] = v (see Subscripts).
Tuple Unpackinga, b = f(x), a, b = b, a
Conditionalif / elif / else
Loopfor x in iterable:
Control Flowreturn, break, continue, pass
Call StatementStandalone function call, e.g., fail("CODE", "message").

A variable's static type is fixed at its first assignment. Conditional expressions take the form val1 if cond else val2, where both branches have the same type:

o = "big" if n > 255 else "small"

Functions and Constants

HEADER_LEN = 1

def split_head(x: bytes) -> tuple[bytes, bytes]:
    return x[:HEADER_LEN], x[HEADER_LEN:]

first, rest = split_head(b)
size = len(b)

Indexing and Slicing (Subscripts)

FormTargetResult
b[i]bytesSingle byte as an int in 0..255 (negative indices count from the end).
b[a:c], b[:c], b[a:], b[:]bytesSlice bytes from index a up to c (exclusive).
b[o, n], b[o,]bytesSlice bytes by offset and length (omitting n slices to the end).
xs[i], xs[a:c], xs[o, n]list[T]Element T or sublist list[T].

Unlike Python, out-of-bounds indices or slice ranges do not silently clamp; if 0 ≤ start ≤ end ≤ len does not hold, evaluation fails with BYTES_OUT_OF_RANGE (or SCRIPT_INDEX_OUT_OF_RANGE for lists). In b[o, n], a negative n acts as an end position counted from the back (identical to b[o:n]).

In the table below, a is 00 11 22 33 44 55 66 77 88:

ExpressionResult (Hex)
a[1, 3]112233 (3 bytes starting at offset 1)
a[6,]667788 (from offset 6 to the end)
a[6, -1]6677 (same as a[6:-1])
a[6, -2]66
a[-3, 3]667788 (negative offset)
a[1:4]112233
a[6:]667788
a[6:-1]6677
a[-1]136 (0x88)

Subscript assignment updates bytes or list elements in place. Slice replacement requires the replacement bytes to have the exact same length as the target slice (otherwise fails with BYTES_LENGTH_MISMATCH):

o = a
o[1, 2] = h"ff ee"

for Loops and range

total = 0
for v in b:
    if v == 2:
        continue
    if v == 5:
        break
    total += v

String Formatting (f-strings)

f"..." interpolates expressions into a string. Format specifiers follow [[fill]align][sign][#][0][width][type]:

Value TypeAllowed Format Types
intd (default), x, X, b, o. Sign (+, -, space), #, and zero-pad 0 apply only to int.
strings (width, fill, and alignment).
bytesx (default), X (two hex digits per byte, no separator).
bools (True / False).
ExpressionResult
f"{n:03d}"007
f"{v:02x}"0a
f"{v:08b}"00000101
f"{v:#x}"0xff
f"{v:#x}"-0xff
f"{v:+d}"+3
f"{name:>8}|{key:X}" ab|0AFF
f"{{{n}}}"{7}

Differences from Python

FeaturePythonNodeCrypto Built-in Script
import, class, lambda, comprehensions, try, while, with, global, printSupportedOmitted by design
Attribute access (.) and methodsSupportedNot used; call built-in functions (len(x), to_hex(b))
/, **, floating-point numbersSupportedOmitted (use integer division // and modular pow(b, e, m))
Truthiness coercionImplicit on all typesConditions must be strictly bool (write if x & 0x80 != 0:)
Byte literalsb"\x87\x23"h"87 23" (hex), b"1000 0111" (bits), s"text" (UTF-8)
Slice subscriptsb[1:4] onlyBoth b[1:4] and offset+length b[1, 3]
Out-of-range slicesSilently clampedRaises BYTES_OUT_OF_RANGE
Bitwise ops on bytes (& | ^ ~ << >>)Not definedBuilt-in byte-array bitwise and bit-shift operators
Assignment semanticsReference sharing for listsPure value-copy semantics