# TrainingQualityVerifier

### Overview

`TrainingQualityVerifier.circom` is a standalone zk-SNARK circuit that proves a language-model’s training meets or exceeds a required quality threshold **without revealing** raw training samples, model outputs, or proprietary scores.\
It serves as the “first gate” in the ZK-AgentMesh verification pipeline and can also be embedded in any project that needs zero-knowledge assurance of model-training quality.

***

### Purpose

The circuit enables an agent developer to convince a verifier that:

1. **Every individual training sample** scores at least `min_quality_threshold`.
2. **The average quality** across all samples meets the same threshold.
3. The proof ties back to a prior on-chain **training commitment** and **model weights hash**, preventing post-hoc tampering.

All of the above are proven without exposing:

* The underlying data or model responses.
* Exact per-sample scores or private metrics.

***

### Inputs

#### Private Inputs

| Group           | Name                               | Description                                             |
| --------------- | ---------------------------------- | ------------------------------------------------------- |
| Training data   | `training_samples[n_samples × 32]` | One Poseidon-hashed field element per token (flattened) |
| Model outputs   | `model_responses[n_samples × 32]`  | Poseidon-hashed response tokens                         |
| Quality metrics | `quality_scores[n_samples]`        | Integer score 0–1000 per sample                         |
| Integrity       | `training_seed`                    | Salt used during the commitment phase                   |
| Model hash      | `model_weights_hash`               | Poseidon hash of final model weights                    |

#### Public Inputs

| Name                       | Description                          |
| -------------------------- | ------------------------------------ |
| `agent_id`                 | 256-bit unique agent identifier      |
| `min_quality_threshold`    | Minimum acceptable score (0–1000)    |
| `training_commitment_hash` | Commitment published before training |
| `creator_address`          | EVM address of developer             |

#### Public Outputs

| Output                  | Meaning                                                  |
| ----------------------- | -------------------------------------------------------- |
| `quality_verified`      | `1` if all checks pass, otherwise `0`                    |
| `average_quality`       | Mean score (integer field element)                       |
| `training_proof_hash`   | Poseidon hash binding agent, seed, avg. quality, creator |
| `model_integrity_proof` | Poseidon hash binding model weights to seed and agent    |

***

### Circuit Logic

1. **Per-Sample Check**\
   Each `quality_scores[i]` is constrained with `GreaterEqThan` to be ≥ `min_quality_threshold`.
2. **Average Calculation**\
   `AverageCalculator` sums all scores and enforces\
   `average × n_samples == sum`.
3. **Average Threshold**\
   A second `GreaterEqThan` gate ensures `average_quality ≥ min_quality_threshold`.
4. **Conjunction of Conditions**\
   `quality_verified` is the product of
   * the average-threshold flag
   * the cumulative product of all individual flags\
     If any single check fails, the product is zero.
5. **Hash Commitments**
   * `training_proof_hash` = Poseidon(agent\_id, training\_seed, average\_quality, creator\_address)
   * `model_integrity_proof` = Poseidon(model\_weights\_hash, training\_seed, agent\_id)

These hashes are referenced by higher-level circuits or on-chain contracts.

***

### Compilation

```bash
# Requirements: circom 2.x, snarkjs, pot16_final.ptau
circom TrainingQualityVerifier.circom \
       --r1cs --wasm --sym --c

snarkjs groth16 setup TrainingQualityVerifier.r1cs \
                    pot16_final.ptau \
                    tqv_final.zkey

snarkjs zkey export verificationkey tqv_final.zkey \
                                         tqv_verification_key.json
```

***

### Proof Workflow

```bash
# 1. prepare input.json with private + public signals
node TrainingQualityVerifier_js/generate_witness.js \
     TrainingQualityVerifier_js/TrainingQualityVerifier.wasm \
     input.json witness.wtns

# 2. generate proof
snarkjs groth16 prove tqv_final.zkey \
                       witness.wtns \
                       proof.json public.json

# 3. verify locally
snarkjs groth16 verify tqv_verification_key.json \
                       public.json proof.json
```

`public.json` will contain the four public outputs, ready for on-chain submission.

***

### Security&#x20;

* Private inputs never leave the prover’s environment.
* Poseidon hashing maintains circuit efficiency on BN128.
* Tampering with model weights is detectable through `model_integrity_proof`.
* The circuit must be recompiled if `n_samples` or `n_metrics` change.

***

### Status

* Current version: stable for integer quality metrics (0–1000).
* Planned: floating-point support via fixed-point encoding; integration of additional metrics per sample.
