# SolAgent-FHE

AI-powered FHE (Fully Homomorphic Encryption) smart contract generator for [Fhenix CoFHE](https://www.fhenix.io/).

Generate privacy-preserving Solidity contracts from natural language descriptions. The AI understands FHE patterns, encrypted types, access control, and compiles contracts automatically using Hardhat.

## Installation

```bash
pip install solagent-fhe
```

**Requirements:** Node.js 18+ and the [cofhe-hardhat-starter](https://github.com/FhenixProtocol/cofhe-hardhat-starter) project for compilation.

## Quick Start

### CLI

```bash
# Generate a contract from description
solagent-fhe "Create a private voting contract with encrypted vote counts"

# Use a predefined task
solagent-fhe --task-key voting

# Run benchmark (all 9 contract types)
solagent-fhe --benchmark

# List available tasks
solagent-fhe --list-tasks

# Use Groq instead of DeepSeek
solagent-fhe --provider groq "Create a sealed-bid auction"
```

### Python API

```python
import asyncio
from solagent_fhe import generate, benchmark, FHE_TASKS

async def main():
    # Generate a single contract
    result = await generate(
        task="Create a private token with encrypted balances",
        contract_name="MyToken",
        provider="deepseek",  # or "groq"
    )

    if result['compiled']:
        print(f"Contract generated: {result['name']}.sol")
        print(f"Refine rounds: {result['rounds']}")
        print(result['code'])

    # Run full benchmark
    results = await benchmark(provider="deepseek")

asyncio.run(main())
```

## Environment Variables

| Variable | Required | Description |
|----------|----------|-------------|
| `DEEPSEEK_API_KEY` | Yes* | DeepSeek API key for code generation |
| `GROQ_API_KEY` | Yes* | Groq API key (alternative provider) |
| `SOLAGENT_HARDHAT_DIR` | No | Path to cofhe-hardhat-starter (auto-detected) |

*At least one LLM provider key required.

## Supported Contract Types

| Key | Contract | Description |
|-----|----------|-------------|
| `voting` | ConfidentialVoting | Encrypted vote counts, admin-controlled |
| `auction` | SealedBidAuction | Hidden bids, FHE comparison for winner |
| `token` | PrivateToken | ERC20-like with encrypted balances |
| `lottery` | BlindLottery | FHE random number for winner selection |
| `threshold` | SecretThreshold | Encrypted threshold comparison |
| `payroll` | ConfidentialPayroll | Hidden salaries, batch payments |
| `dex` | PrivateDEX | Encrypted swap amounts and balances |
| `dao` | PrivateDAO | Encrypted voting power and tallies |
| `vault` | FHESavingsVault | Encrypted deposits with interest |

## How It Works

```
Description ──→ [Code Agent] ──→ Solidity ──→ [Hardhat Compile]
                  (DeepSeek)        │              │
                                    │         ✓ Success → Done
                                    │              │
                                    │         ✗ Errors
                                    │              │
                                    └──→ [Refine Agent] ──→ Fix ──→ Recompile
                                          (Groq/DeepSeek)     (up to 10 rounds)
```

**Benchmark: 5/5 contracts compile successfully (100%)**

## FHE Patterns

The generator understands all Fhenix CoFHE patterns:

- **Encrypted types:** `euint8`–`euint256`, `ebool`, `eaddress`
- **Operations:** `FHE.add`, `FHE.sub`, `FHE.select`, `FHE.gte`, etc.
- **Access control:** `FHE.allowThis()`, `FHE.allow(ct, addr)`
- **Decryption:** Async `FHE.decrypt()` + `getDecryptResultSafe()`
- **Random:** `FHE.randomEuint32()` for on-chain randomness
- **Input:** `InEuint64` for client-encrypted values

## License

MIT
