Skip to main content
Multi-agent crypto validation: scheme analysis → attack simulation → performance benchmarking → compliance validation. Includes SQLite audit logs, structured output, and API deployment.

Setup

# Create environment
python3 -m venv venv && source venv/bin/activate

# Install packages
pip install praisonaiagents praisonai

# Set API key
export OPENAI_API_KEY="your-key"

Create Sample Data

# Create crypto schemes
cat > schemes.json << 'EOF'
[
  {"id": "KYBER-1024", "type": "KEM", "security_level": 5, "key_size": 3168, "ciphertext_size": 1568, "nist_status": "standardized"},
  {"id": "DILITHIUM-5", "type": "signature", "security_level": 5, "public_key_size": 2592, "signature_size": 4595, "nist_status": "standardized"},
  {"id": "SPHINCS+-256f", "type": "signature", "security_level": 5, "public_key_size": 64, "signature_size": 49856, "nist_status": "standardized"}
]
EOF

# Create compliance requirements
cat > compliance.json << 'EOF'
{
  "nist_pqc": {"required_level": 3, "status": "mandatory"},
  "fips_140_3": {"required_level": 2, "status": "recommended"},
  "common_criteria": {"required_eal": "EAL4", "status": "optional"}
}
EOF

Run: Python Code

from praisonaiagents import Agent, Agents, Task, tool
from praisonaiagents import Agent, Agents, Task, tool, db
from pydantic import BaseModel
from typing import List
import json

# Structured output
class CryptoValidationReport(BaseModel):
    scheme_id: str
    scheme_type: str
    security_level: int
    quantum_resistant: bool
    attack_resistance: List[str]
    performance_grade: str  # A, B, C, D, F
    compliance_status: str  # compliant, partial, non_compliant
    recommendations: List[str]

# Database persistence
db_instance = db(database_url="sqlite:///crypto_audit.db")

# Tools
@tool
def get_scheme(scheme_id: str) -> str:
    """Get cryptographic scheme details."""
    with open("schemes.json") as f:
        schemes = json.load(f)
    for s in schemes:
        if s["id"] == scheme_id:
            return json.dumps(s)
    return json.dumps({"error": "Scheme not found"})

@tool
def simulate_attack(scheme_type: str, security_level: int) -> str:
    """Simulate quantum attacks on scheme."""
    attacks = {
        "grover": {"effective": security_level < 3, "mitigation": "double_key_size"},
        "shor": {"effective": False, "mitigation": "not_applicable"},
        "lattice_reduction": {"effective": security_level < 4, "mitigation": "increase_dimension"}
    }
    resistant = not any(a["effective"] for a in attacks.values())
    return json.dumps({"attacks_tested": list(attacks.keys()), "quantum_resistant": resistant, "details": attacks})

@tool
def benchmark_performance(key_size: int, signature_size: int) -> str:
    """Benchmark cryptographic performance."""
    # Simulated benchmarks based on sizes
    keygen_ms = key_size / 100
    sign_ms = signature_size / 500 if signature_size else 0
    verify_ms = sign_ms * 0.5
    
    if keygen_ms < 10 and sign_ms < 50:
        grade = "A"
    elif keygen_ms < 50 and sign_ms < 200:
        grade = "B"
    elif keygen_ms < 100 and sign_ms < 500:
        grade = "C"
    else:
        grade = "D"
    
    return json.dumps({"keygen_ms": round(keygen_ms, 2), "sign_ms": round(sign_ms, 2), "verify_ms": round(verify_ms, 2), "grade": grade})

@tool
def check_compliance(security_level: int, nist_status: str) -> str:
    """Check compliance with standards."""
    with open("compliance.json") as f:
        requirements = json.load(f)
    
    nist_ok = security_level >= requirements["nist_pqc"]["required_level"] and nist_status == "standardized"
    fips_ok = security_level >= requirements["fips_140_3"]["required_level"]
    
    if nist_ok and fips_ok:
        status = "compliant"
    elif nist_ok or fips_ok:
        status = "partial"
    else:
        status = "non_compliant"
    
    return json.dumps({"nist_pqc": nist_ok, "fips_140_3": fips_ok, "overall": status})

# Agents
analyzer = Agent(
    name="CryptoAnalyzer",
    instructions="Analyze cryptographic schemes. Use get_scheme tool.",
    tools=[get_scheme],
    db=db_instance,
    session_id="crypto-validation"
)

attacker = Agent(
    name="AttackSimulator",
    instructions="Simulate quantum attacks. Use simulate_attack tool.",
    tools=[simulate_attack]
)

benchmarker = Agent(
    name="PerformanceBenchmarker",
    instructions="Benchmark performance. Use benchmark_performance tool.",
    tools=[benchmark_performance]
)

compliance_checker = Agent(
    name="ComplianceChecker",
    instructions="Verify compliance. Use check_compliance tool.",
    tools=[check_compliance]
)

# Tasks
analyze_task = Task(
    description="Analyze scheme: {scheme_id}",
    agent=analyzer,
    expected_output="Scheme details"
)

attack_task = Task(
    description="Simulate quantum attacks on the scheme",
    agent=attacker,
    expected_output="Attack resistance report"
)

benchmark_task = Task(
    description="Benchmark performance metrics",
    agent=benchmarker,
    expected_output="Performance grade"
)

compliance_task = Task(
    description="Check compliance with standards",
    agent=compliance_checker,
    expected_output="Compliance status",
    output_pydantic=CryptoValidationReport
)

# Run
agents = Agents(
    agents=[analyzer, attacker, benchmarker, compliance_checker],
    tasks=[analyze_task, attack_task, benchmark_task, compliance_task]
)
result = agents.start(scheme_id="KYBER-1024")
print(result)

Run: CLI

# Validate single scheme
praisonai "Validate KYBER-1024 for quantum resistance" --verbose

# With persistence
praisonai "Audit all post-quantum crypto schemes" --memory --user-id security_team

# Compliance check
praisonai "Check NIST PQC compliance for DILITHIUM-5" --verbose

Run: agents.yaml

Create agents.yaml:
framework: praisonai
topic: "post-quantum cryptography validation"
roles:
  analyzer:
    role: Cryptographic Analyst
    goal: Analyze PQC schemes
    backstory: Expert in post-quantum cryptography
    tasks:
      analyze:
        description: |
          Analyze scheme:
          - Algorithm type (KEM, signature)
          - Security level (1-5)
          - Key/signature sizes
          - NIST standardization status
        expected_output: Scheme analysis
        
  attacker:
    role: Attack Simulator
    goal: Test quantum resistance
    backstory: Expert in cryptanalysis
    tasks:
      attack:
        description: |
          Simulate attacks:
          - Grover's algorithm
          - Shor's algorithm
          - Lattice reduction
        expected_output: Attack resistance report
        
  compliance:
    role: Compliance Auditor
    goal: Verify standards compliance
    backstory: Expert in security standards
    tasks:
      audit:
        description: |
          Check compliance:
          - NIST PQC standards
          - FIPS 140-3
          - Common Criteria
        expected_output: Compliance report
Run:
praisonai agents.yaml --verbose

Monitor & Verify

# View audit history
praisonai --history 10 --user-id security_team

# Check metrics
praisonai --metrics

# Export report
praisonai --save crypto_audit_report

Serve API

from praisonaiagents import Agent, tool
import json

@tool
def quick_crypto_check(scheme_type: str, security_level: int, nist_status: str) -> str:
    """Quick cryptographic scheme validation."""
    quantum_safe = security_level >= 3
    compliant = nist_status == "standardized" and security_level >= 3
    
    if quantum_safe and compliant:
        recommendation = "approved_for_production"
    elif quantum_safe:
        recommendation = "pending_standardization"
    else:
        recommendation = "not_recommended"
    
    return json.dumps({
        "quantum_safe": quantum_safe,
        "compliant": compliant,
        "recommendation": recommendation
    })

agent = Agent(
    name="CryptoAPI",
    instructions="Validate cryptographic schemes for quantum resistance.",
    tools=[quick_crypto_check]
)

agent.launch(path="/validate-crypto", port=8000)
Test:
curl -X POST http://localhost:8000/validate-crypto \
  -H "Content-Type: application/json" \
  -d '{"message": "Validate: KEM scheme, security level 5, NIST standardized"}'

Cleanup

rm -f crypto_audit.db schemes.json compliance.json
deactivate

Features Demonstrated

FeatureImplementation
Multi-agentAnalyzer → Attacker → Benchmarker → Compliance
Structured OutputPydantic CryptoValidationReport
Attack SimulationGrover, Shor, lattice attacks
Compliance CheckingNIST PQC, FIPS 140-3
DB PersistenceSQLite via db()
CLI--memory for audit trail
YAML Config3-agent validation pipeline
API Endpointagent.launch()