NodeCrypto 도움말

내장 함수

모든 내장 함수는 소문자 snake_case 이름을 사용한다. 내장 함수에서는 기본 매개변수 값(= 값으로 표기)과 키워드 인자(예: to_int(b, signed=True))를 지원한다. 아래 표의 모든 식은 실제 인터프리터로 실행하여 검증되었다.

분류함수 목록
길이 및 할당len · zeros · fill
구조 조작concat · reverse · blocks · to_ints · from_ints
비트 회전rol · ror
비교ct_equals · compare
정수 변환to_int · to_bytes
문자열 및 16진수 변환from_hex · to_hex · from_utf8 · to_utf8 · str
정수 수학abs · min · max · pow · bit_length
제어 및 실패 처리range · fail

길이 및 할당

len

len(x: bytes | list[T]) → int — bytes 버퍼의 바이트 길이 또는 리스트의 원소 개수를 반환한다.

식결과
len(h"01 02 03")3
len([10, 20])2
len(h"")0

zeros

zeros(n: int) → bytes — n개의 0x00 바이트로 채워진 바이트열을 반환한다. n < 0이면 BYTES_INVALID_ARGUMENT 에러가 발생한다.

식결과
zeros(4)00000000
zeros(-1)에러 BYTES_INVALID_ARGUMENT

fill

fill(value: int, n: int) → bytes — 바이트 값 value(0..255)로 채워진 길이 n의 바이트열을 반환한다. 범위를 벗어나면 BYTES_INVALID_ARGUMENT 에러가 발생한다.

식결과
fill(0xAA, 3)aaaaaa
fill(256, 1)에러 BYTES_INVALID_ARGUMENT

구조 조작

concat

concat(parts: list[bytes]) → bytes — 바이트열 리스트를 순서대로 이어 붙인다. 결과 크기가 16 MiB를 초과하면 BYTES_SIZE_LIMIT 에러가 발생한다.

식결과
concat([h"01", h"02 03"])010203

reverse

reverse(b: bytes) → bytes — b의 바이트 순서를 뒤집는다(엔디안 반전).

식결과
reverse(h"01 02 03")030201

blocks

blocks(b: bytes, size: int) → list[bytes] — b를 앞에서부터 size 바이트 단위로 분할한다. 마지막 조각은 size보다 짧을 수 있다. size ≤ 0이면 BYTES_INVALID_ARGUMENT 에러가 발생한다.

식결과
len(blocks(h"01 02 03 04 05", 2))3
blocks(h"01 02 03 04 05", 2)[1]0304
blocks(h"01 02 03 04 05", 2)[2]05 (짧은 마지막 블록)

to_ints

to_ints(b: bytes) → list[int] — b의 각 바이트를 0..255 범위의 정수 리스트로 변환한다.

식결과
to_ints(h"01 ff")[1]255
len(to_ints(h"01 ff"))2

from_ints

from_ints(xs: list[int]) → bytes — 정수 리스트(0..255)를 바이트열로 패킹한다. 0..255 범위를 벗어난 원소가 있으면 BYTES_INVALID_ARGUMENT 에러가 발생한다.

식결과
from_ints([1, 255])01ff
from_ints([256])에러 BYTES_INVALID_ARGUMENT

비트 회전

rol

rol(b: bytes, n: int) → bytes — b 전체 비트 시퀀스를 왼쪽으로 n비트 회전(Rotate Left)한다.

식결과
rol(h"81", 1)03 (1000 0001 → 0000 0011)
rol(h"01 81", 1)0302

ror

ror(b: bytes, n: int) → bytes — b 전체 비트 시퀀스를 오른쪽으로 n비트 회전(Rotate Right)한다.

식결과
ror(h"01", 1)80

비교

ct_equals

ct_equals(a: bytes, b: bytes) → bool — 상수 시간(Constant-Time) 바이트열 동등 비교를 수행한다.

식결과
ct_equals(h"01 02", h"01 02")True
ct_equals(h"01 02", h"01 03")False

compare

compare(a: bytes, b: bytes) → int — 두 바이트열을 사전순으로 비교하여 -1, 0, 1 중 하나를 반환한다.

식결과
compare(h"01 02", h"01 03")-1
compare(h"01", h"01")0
compare(h"02", h"01 ff")1

정수 변환

to_int

to_int(b: bytes, endian: string = "big", signed: bool = False) → int — b를 정수로 해석한다(signed=True일 때는 2의 보수 기준, 빈 바이트열은 0).

식결과
to_int(h"01 00")256
to_int(h"01 00", "little")1
to_int(h"ff", signed=True)-1
to_int(h"ff")255
to_int(h"")0

to_bytes

to_bytes(n: int, length: int, endian: string = "big", signed: bool = False) → bytes — 정수 n을 정확히 length 바이트로 인코딩한다. 표현 범위를 초과하면 BYTES_INT_OVERFLOW 에러가 발생한다.

식결과
to_bytes(256, 2)0100
to_bytes(256, 2, "little")0001
to_bytes(-1, 1, signed=True)ff
to_bytes(42, 4)0000002a
to_bytes(256, 1)에러 BYTES_INT_OVERFLOW
to_bytes(-1, 1)에러 BYTES_INT_OVERFLOW

문자열 및 16진수 변환

from_hex

from_hex(s: string) → bytes — 16진수 문자열(공백 무시, 0x 접두어 불허, 짝수 자리수)을 디코딩하여 바이트열로 변환한다. 형식이 잘못되면 BYTES_INVALID_HEX 에러가 발생한다.

식결과
from_hex("01 ff")01ff
from_hex("0")에러 BYTES_INVALID_HEX
from_hex("0xff")에러 BYTES_INVALID_HEX

to_hex

to_hex(b: bytes, upper: bool = False) → string — 바이트열을 바이트당 2자리 연속 16진수 문자열로 인코딩한다.

식결과
to_hex(h"0a ff")0aff
to_hex(h"0a ff", upper=True)0AFF

from_utf8

from_utf8(s: string) → bytes — 유니코드 문자열을 UTF-8 바이트열로 인코딩한다.

식결과
from_utf8("AB")4142
from_utf8("가")eab080

to_utf8

to_utf8(b: bytes) → string — UTF-8 바이트열을 엄격하게 디코딩하여 문자열로 변환한다. 유효하지 않은 UTF-8 시퀀스가 있으면 BYTES_INVALID_UTF8 에러가 발생한다.

식결과
to_utf8(h"41 42")AB
to_utf8(h"ff")에러 BYTES_INVALID_UTF8

str

str(n: int) → string — 정수를 10진수 문자열로 변환한다.

식결과
str(-12)-12
"len=" + str(3)len=3

정수 수학

abs

abs(n: int) → int — 절댓값을 반환한다.

식결과
abs(-5)5

min

min(a: int, b: int) → int — 두 정수 중 작은 값을 반환한다.

식결과
min(3, 7)3

max

max(a: int, b: int) → int — 두 정수 중 큰 값을 반환한다.

식결과
max(3, 7)7

pow

pow(base: int, exp: int, mod: int) → int — 모듈러 거듭제곱(baseexp mod m)을 계산한다. exp ≥ 0 및 mod ≥ 1이어야 한다.

식결과
pow(3, 4, 5)1 (81 mod 5)
pow(2, 10, 1000)24 (1024 mod 1000)
pow(2, -1, 5)에러 SCRIPT_INVALID_ARGUMENT

bit_length

bit_length(n: int) → int — abs(n)을 표현하는 데 필요한 최소 비트 수를 반환한다(0은 0 반환).

식결과
bit_length(255)8
bit_length(256)9
bit_length(0)0

제어 및 실패 처리

range

range(n), range(start, stop), range(start, stop, step) — for 반복문 전용 정수 시퀀스 생성기이다(for 반복문과 range 참고).

up = 0
for i in range(5):
    up += i
down = 0
for i in range(10, 0, -3):
    down += i

fail

fail(code: string, message: string) — 스크립트 실행을 즉시 중단하고 지정한 code와 message로 Script 노드를 ERROR 상태로 만든다. 사용자 에러 코드는 예약된 접두어인 SCRIPT_ 또는 BYTES_로 시작할 수 없다(위반 시 SCRIPT_INVALID_ERROR_CODE 에러 발생).

if n == 0:
    fail("ZERO_NOT_ALLOWED", "n must not be 0")
o = 100 // n