> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inco.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Private Raffle

> Build a fully private raffle system where guesses and results stay encrypted

# Private Raffle

A confidential raffle system on Solana where participants submit encrypted guesses, and winners are determined through encrypted comparisons — no one can see the winning number or anyone's guess until results are revealed.

## How It Works

The raffle follows a simple "guess the number" game:

1. **Authority creates a raffle** with a ticket price
2. **Players buy tickets** with encrypted guesses (1-100)
3. **Authority draws** by setting an encrypted winning number
4. **Players check** if their guess matches (encrypted comparison)
5. **Winners claim** their prize through on-chain verification

```mermaid theme={null}
sequenceDiagram
    participant Authority
    participant Player
    participant Program
    participant Inco Lightning

    Authority->>Program: Create raffle (ticket price)
    Player->>Player: Encrypt guess (e.g., 42)
    Player->>Program: Buy ticket + encrypted guess
    Program->>Inco Lightning: Store encrypted handle
    
    Note over Authority,Inco Lightning: Raffle closes
    
    Authority->>Authority: Encrypt winning number
    Authority->>Program: Draw winner
    Program->>Inco Lightning: Store winning handle
    
    Player->>Program: Check if won
    Program->>Inco Lightning: e_eq(guess, winning)
    Inco Lightning-->>Program: Encrypted boolean result
    Player->>Player: Decrypt result (won/lost)
    
    Player->>Program: Claim prize
    Program->>Inco Lightning: e_select(won, prize, 0)
    
    Player->>Program: Withdraw (with proof)
    Program->>Program: Verify Ed25519 signature
    Program-->>Player: Transfer SOL prize
```

## Privacy Guarantees

| Data            | Visibility                    |
| --------------- | ----------------------------- |
| Player's guess  | Only the player can see       |
| Winning number  | Only authority knows          |
| Win/loss result | Only the ticket owner sees    |
| Prize amount    | Only the winner sees          |
| Ticket purchase | Public (on-chain transaction) |

## Key Features

* **Encrypted Guesses**: Players submit encrypted numbers, hidden from everyone
* **Encrypted Drawing**: The winning number is never revealed on-chain
* **Private Results**: Only ticket owners can see if they won
* **On-chain Verification**: Winners prove their prize amount with Ed25519 signatures
* **Trustless Payout**: Prize transfers are verified and executed on-chain

<CardGroup cols={2}>
  <Card title="Setup" icon="gear" href="/svm/tutorials/private-raffle/setup">
    Project configuration and dependencies
  </Card>

  <Card title="Program" icon="code" href="/svm/tutorials/private-raffle/program">
    Raffle program instructions and accounts
  </Card>

  <Card title="Client Integration" icon="browser" href="/svm/tutorials/private-raffle/client">
    TypeScript usage and testing
  </Card>
</CardGroup>

## Quick Example

```typescript theme={null}
// Player buys ticket with encrypted guess
const myGuess = 42;
const encryptedGuess = await encryptValue(BigInt(myGuess));

await program.methods
  .buyTicket(hexToBuffer(encryptedGuess))
  .accounts({
    buyer: wallet.publicKey,
    lottery: rafflePda,
    ticket: ticketPda,
    vault: vaultPda,
    // ...
  })
  .rpc();

// Later: check if won (encrypted comparison)
await program.methods.checkWinner().accounts({...}).rpc();

// Decrypt result
const result = await decrypt([isWinnerHandle], { address, signMessage });
const won = result.plaintexts[0] === "1";
console.log("Did I win?", won ? "YES!" : "No");
```

## Source Code

```bash theme={null}
git clone https://github.com/Inco-fhevm/raffle-example-solana.git
```
