Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ set(INTERPRETER_SOURCES
interpreter/src/builtins/builtin_module_basics.cpp
interpreter/src/builtins/builtin_module_math.cpp
interpreter/src/builtins/builtin_module_assertions.cpp
interpreter/src/builtins/builtin_module_crypto.cpp
interpreter/src/builtins/builtin_module_async.cpp
interpreter/src/builtins/builtin_module_maps.cpp
interpreter/src/builtins/builtin_module_maps_core.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class SecurityBuiltinsCodeGen
llvm::Value *emitBuiltinSecurityTimestamp(std::shared_ptr<SIRInstruction> inst);
llvm::Value *emitBuiltinSecuritySecureRandom(std::shared_ptr<SIRInstruction> inst);
llvm::Value *emitBuiltinSecurityBase64Encode(std::shared_ptr<SIRInstruction> inst);
llvm::Value *emitBuiltinCryptoBlake3Hash(std::shared_ptr<SIRInstruction> inst);
llvm::Value *emitBuiltinCryptoBlake3KeyedHash(std::shared_ptr<SIRInstruction> inst);
};

}} // namespace Sad::LLVM
Expand Down
2 changes: 2 additions & 0 deletions compiler/include/backend/llvm/llvm_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,8 @@ namespace Sad
llvm::Value *emitBuiltinSecurityTimestamp(std::shared_ptr<SIRInstruction> inst) { return secb_->emitBuiltinSecurityTimestamp(inst); } // وقت_الآن
llvm::Value *emitBuiltinSecuritySecureRandom(std::shared_ptr<SIRInstruction> inst) { return secb_->emitBuiltinSecuritySecureRandom(inst); } // عشوائي_آمن
llvm::Value *emitBuiltinSecurityBase64Encode(std::shared_ptr<SIRInstruction> inst) { return secb_->emitBuiltinSecurityBase64Encode(inst); } // ترميز_64
llvm::Value *emitBuiltinCryptoBlake3Hash(std::shared_ptr<SIRInstruction> inst) { return secb_->emitBuiltinCryptoBlake3Hash(inst); } // بلايك3
llvm::Value *emitBuiltinCryptoBlake3KeyedHash(std::shared_ptr<SIRInstruction> inst) { return secb_->emitBuiltinCryptoBlake3KeyedHash(inst); } // هاش_مفتاح

// ================================================================
// التكامل مع C/C++ — FFI Functions (20)
Expand Down
1 change: 1 addition & 0 deletions compiler/include/frontend/builders/builtin_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace Sad
std::optional<BuildResult> buildBuiltinSystem_OsSystem(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands);

std::optional<BuildResult> buildBuiltinSystem_Security(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands);
std::optional<BuildResult> buildBuiltinSystem_Crypto(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands);

std::optional<BuildResult> buildBuiltinCallSimd(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands);

Expand Down
4 changes: 4 additions & 0 deletions compiler/include/frontend/sir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,10 @@ namespace Sad
{
return builtins_->buildBuiltinSystem_Security(funcName, isUserDefinedFunction, argResults, argOperands);
}
std::optional<BuildResult> buildBuiltinSystem_Crypto(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands)
{
return builtins_->buildBuiltinSystem_Crypto(funcName, isUserDefinedFunction, argResults, argOperands);
}

std::optional<BuildResult> buildBuiltinSystem_FFI(const std::string &funcName, bool isUserDefinedFunction, std::vector<BuildResult> &argResults, std::vector<SIROperand> &argOperands)
{
Expand Down
6 changes: 6 additions & 0 deletions compiler/include/frontend/sir_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,12 @@ namespace Sad
BUILTIN_SECURITY_SECURE_RANDOM, ///< عشوائي_آمن / Secure random number
BUILTIN_SECURITY_BASE64_ENCODE, ///< ترميز_64 / Base64 encode

// ==========================================
// 12ب. وحدة تشفير — Crypto module builtins (BLAKE3)
// ==========================================
BUILTIN_CRYPTO_BLAKE3_HASH, ///< بلايك3 / BLAKE3 hash - hex string
BUILTIN_CRYPTO_BLAKE3_KEYED_HASH, ///< هاش_مفتاح / BLAKE3 keyed hash (MAC) - hex string

// ==========================================
// 13. التكامل مع C/C++ — FFI Functions (15)
// ==========================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,42 @@ namespace Sad { namespace LLVM {



llvm::Value *SecurityBuiltinsCodeGen::emitBuiltinCryptoBlake3Hash(std::shared_ptr<SIRInstruction> inst)
{
if (!inst || inst->operands.empty())
return nullptr;
llvm::Value *val = cg_.resolveOperand(inst->operands[0]);
if (!val)
return nullptr;
// Call runtime sad_blake3_hash(const char*) -> char* (BLAKE3 hex string,
// matching the interpreter — interpreter/src/builtins/builtin_module_crypto.cpp)
llvm::Type *i8Ptr = llvm::Type::getInt8Ty(*cg_.context_)->getPointerTo();
llvm::FunctionType *ft = llvm::FunctionType::get(i8Ptr, {i8Ptr}, false);
llvm::FunctionCallee fn = cg_.module_->getOrInsertFunction("sad_blake3_hash", ft);
llvm::Value *result = cg_.builder_->CreateCall(fn, {val}, "blake3.ret");
if (inst->result.has_value())
cg_.context_info_.namedValues[inst->result->name] = result;
return result;
}



llvm::Value *SecurityBuiltinsCodeGen::emitBuiltinCryptoBlake3KeyedHash(std::shared_ptr<SIRInstruction> inst)
{
if (!inst || inst->operands.size() < 2)
return nullptr;
llvm::Value *data = cg_.resolveOperand(inst->operands[0]);
llvm::Value *key = cg_.resolveOperand(inst->operands[1]);
if (!data || !key)
return nullptr;
// Call runtime sad_blake3_keyed_hash(const char*, const char*) -> char*
llvm::Type *i8Ptr = llvm::Type::getInt8Ty(*cg_.context_)->getPointerTo();
llvm::FunctionType *ft = llvm::FunctionType::get(i8Ptr, {i8Ptr, i8Ptr}, false);
llvm::FunctionCallee fn = cg_.module_->getOrInsertFunction("sad_blake3_keyed_hash", ft);
llvm::Value *result = cg_.builder_->CreateCall(fn, {data, key}, "blake3_keyed.ret");
if (inst->result.has_value())
cg_.context_info_.namedValues[inst->result->name] = result;
return result;
}

}} // namespace Sad::LLVM
4 changes: 4 additions & 0 deletions compiler/src/backend/llvm/builders/core/instr_core_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ namespace Sad
return cg_.emitBuiltinSecuritySecureRandom(inst);
case SIROpcode::BUILTIN_SECURITY_BASE64_ENCODE:
return cg_.emitBuiltinSecurityBase64Encode(inst);
case SIROpcode::BUILTIN_CRYPTO_BLAKE3_HASH:
return cg_.emitBuiltinCryptoBlake3Hash(inst);
case SIROpcode::BUILTIN_CRYPTO_BLAKE3_KEYED_HASH:
return cg_.emitBuiltinCryptoBlake3KeyedHash(inst);

// ===== FFI (20) =====
case SIROpcode::FFI_PRINTF:
Expand Down
7 changes: 7 additions & 0 deletions compiler/src/backend/llvm/llvm_codegen_instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace Sad
namespace Nio = Sad::Builtins::Names::CompilerIo;
namespace Nm = Sad::Builtins::Names::Math;
namespace Na = Sad::Builtins::Names::Assertions;
namespace Ncr = Sad::Builtins::Names::Crypto;
namespace Nar = Sad::Builtins::Names::Arrays;
namespace Nmap = Sad::Builtins::Names::Maps;
namespace Nasync = Sad::Builtins::Names::AsyncAdvanced;
Expand Down Expand Up @@ -205,6 +206,12 @@ namespace Sad
case SIROpcode::BUILTIN_SECURITY_DECRYPT: return std::string(Na::DECRYPT); // فك_تشفير (runtime مستضاف)
case SIROpcode::BUILTIN_SECURITY_SANITIZE: return std::string(Na::SANITIZE); // نظف (runtime مستضاف)
case SIROpcode::BUILTIN_SECURITY_BASE64_ENCODE: return std::string(Na::BASE64_ENCODE); // ترميز_64 (runtime مستضاف)
// (AR) نفس بوّابة عائلة الأمن المستضافة أعلاه — بلايك3/هاش_مفتاح
// يستدعيان sad_blake3_* من نفس runtime المستضاف المضمَّن.
// (EN) Same hosted-runtime gate as the security family above —
// BLAKE3 functions call into the same embedded runtime.
case SIROpcode::BUILTIN_CRYPTO_BLAKE3_HASH: return std::string(Ncr::BLAKE3_HASH); // بلايك3 (runtime مستضاف)
case SIROpcode::BUILTIN_CRYPTO_BLAKE3_KEYED_HASH: return std::string(Ncr::BLAKE3_KEYED_HASH); // هاش_مفتاح (runtime مستضاف)
default: return std::string();
}
}
Expand Down
85 changes: 85 additions & 0 deletions compiler/src/frontend/builders/builtins_crypto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// ============================================================================
// builtins_crypto.cpp
// (AR) دوال وحدة تشفير: بلايك3 (BLAKE3 hash)، هاش_مفتاح (BLAKE3 keyed hash)
// (EN) Crypto module builtins: BLAKE3 hash, BLAKE3 keyed hash (MAC)
// ============================================================================

#include "sir_builder.h"
#include "builders/builtin_builder.h"
#include "module_nodes.h"
#include "module_resolver.h"
#include "lexer_core.h"
#include "parser_core.h"
#include "pattern_nodes.h"
#include "utf8_utils.h"
#include <stdexcept>
#include <iostream>
#include <optional>

#include "builtin_registry.h"
namespace Bn = Sad::Builtins::Names;

namespace Sad
{
namespace Compiler
{
namespace SIR
{

std::optional<BuildResult> BuiltinBuilder::buildBuiltinSystem_Crypto(
const std::string &funcName,
bool isUserDefinedFunction,
std::vector<BuildResult> &argResults,
std::vector<SIROperand> &argOperands)
{
(void)isUserDefinedFunction;

// 1. بلايك3 / blake3 - هاش BLAKE3 (256 بت، ذاتيّ التنفيذ، مطابق للمفسّر)
if (funcName == Bn::Crypto::BLAKE3_HASH)
{
if (argResults.empty())
{
std::cerr << "[خطأ] دالة بلايك3 تتطلب معامل واحد (النص)" << std::endl;
return BuildResult("", SadTypeKind::String);
}
std::string resultReg = b_.newTempRegister();
SIROperand resultOp = SIROperand::Register(resultReg, SadTypeKind::String);
SIRInstruction inst(SIROpcode::BUILTIN_CRYPTO_BLAKE3_HASH);
inst.result = resultOp;
inst.operands.push_back(argOperands[0]);
if (b_.currentBlock_)
b_.currentBlock_->instructions.push_back(inst);
#ifndef NDEBUG
std::cout << "[DEBUG] builtin " << funcName << "() -> " << resultReg << std::endl;
#endif
return BuildResult(resultReg, SadTypeKind::String);
}

// 2. هاش_مفتاح / keyed_hash - مصادقة رسالة عبر نمط BLAKE3 المُفتاح
if (funcName == Bn::Crypto::BLAKE3_KEYED_HASH)
{
if (argResults.size() < 2)
{
std::cerr << "[خطأ] دالة هاش_مفتاح تتطلب معاملين (النص، المفتاح)" << std::endl;
return BuildResult("", SadTypeKind::String);
}
std::string resultReg = b_.newTempRegister();
SIROperand resultOp = SIROperand::Register(resultReg, SadTypeKind::String);
SIRInstruction inst(SIROpcode::BUILTIN_CRYPTO_BLAKE3_KEYED_HASH);
inst.result = resultOp;
inst.operands.push_back(argOperands[0]);
inst.operands.push_back(argOperands[1]);
if (b_.currentBlock_)
b_.currentBlock_->instructions.push_back(inst);
#ifndef NDEBUG
std::cout << "[DEBUG] builtin " << funcName << "() -> " << resultReg << std::endl;
#endif
return BuildResult(resultReg, SadTypeKind::String);
}

return std::nullopt;
}

} // namespace SIR
} // namespace Compiler
} // namespace Sad
4 changes: 4 additions & 0 deletions compiler/src/frontend/builders/builtins_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,10 @@ namespace Sad
auto securityResult = buildBuiltinSystem_Security(funcName, isUserDefinedFunction, argResults, argOperands);
if (securityResult) return securityResult;

// Crypto module functions (BLAKE3 hash, keyed hash)
auto cryptoResult = buildBuiltinSystem_Crypto(funcName, isUserDefinedFunction, argResults, argOperands);
if (cryptoResult) return cryptoResult;

// FFI functions (printf, malloc, fopen, etc.)
auto ffiResult = buildBuiltinSystem_FFI(funcName, isUserDefinedFunction, argResults, argOperands);
if (ffiResult) return ffiResult;
Expand Down
Loading
Loading