Synsema docsENES

Bytes, matemática y arrays

bytes-math.syn
-- Doc example: bytes, math, and numeric arrays / linear algebra (all pure).
intent: "doc example: bytes, math and arrays"

print("decode(bytes(\"Hi\")) = " + decode(bytes("Hi")) + ",  norm([3,4]) = " + text(norm(array([3, 4]))))

test "bytes <-> text are inverses; hex encoding"
    let b be bytes("Hi")
    assert_eq(decode(b), "Hi")
    assert_eq(decode(bytes("Hi"), "hex"), "4869")
    assert_eq(decode(bytes("4869", "hex")), "Hi")

test "base58/base32 round-trip; bytes <-> exact integers"
    assert_eq(decode(bytes(decode(bytes("Hi"), "base58"), "base58")), "Hi")
    assert_eq(decode(bytes("Hi"), "base32"), "JBUQ")
    -- bytes_to_int/int_to_bytes: big-endian, exact (protocol/signature integers)
    assert_eq(bytes_to_int(bytes("0400", "hex")), 1024)
    assert_eq(int_to_bytes(1024), bytes("0400", "hex"))
    assert_eq(int_to_bytes(7, 4), bytes("00000007", "hex"))

test "math operators"
    assert_eq(2 ** 10, 1024)
    assert_eq(10 % 3, 1)

test "numeric arrays + linear algebra"
    assert_eq(norm(array([3, 4])), 5)
    assert_eq(dot(array([1, 2, 3]), array([4, 5, 6])), 32)

Bytes

bytes es binario crudo, distinto de text. bytes(...) y decode(...) son inversos:

bytes("Hi")                 -- bytes UTF-8
bytes("4869", "hex")        -- decodificar hex → bytes
bytes("SGk=", "base64")     -- decodificar base64 → bytes
bytes("SGk", "base64url")   -- decodificar base64url (URL-safe -_, padding opcional; JWT/tokens) → bytes
bytes("StV1DL6", "base58")  -- decodificar base58 (alfabeto Bitcoin/Solana) → bytes
bytes("JBSWY3DP", "base32") -- decodificar base32 RFC 4648 (convención Algorand) → bytes
decode(b)                   -- bytes → text (UTF-8 estricto)
decode(b, "hex")            -- bytes → texto hex (también base64 / base64url / base58 / base32)
sha256(x)                   -- digest crudo (bytes); hex con decode(sha256(x), "hex")
bytes_to_int(b)             -- bytes big-endian → entero exacto no-negativo (vacío → 0)
int_to_bytes(n, size?)      -- entero → bytes big-endian: mínimos, o con padding de ceros a size
int_to_bytes_le(n, size)    -- entero → bytes little-endian de exactamente size (structs binarios, p.ej. u32/u64 LE de Solana)

text(b) muestra una repr hex como bytes(48656c6c6f)no decodifica. Un secret nunca se materializa a través de bytes(...). bytes_to_int es exacto a cualquier ancho (un valor de 32 bytes jamás pasa por float), que es lo que piden los enteros de firmas/protocolos — mirá 38-blockchain.

Matemática

Operadores + - / % ; la división es siempre float*. Constantes: pi, tau, e, inf, nan. La torre numérica también tiene decimal (1.50d, exacto) y complex.

Arrays numéricos y álgebra lineal

array([...]) construye un array numérico; + - / * son elemento a elemento (con broadcasting). Álgebra lineal (2D, vía faer):

norm(array([3, 4]))                          -- 5
dot(array([1, 2, 3]), array([4, 5, 6]))      -- 32
matmul(a, b)                                  -- producto matricial
solve(A, b)   det(A)   inv(A)   eig(A)   svd(A)