# [auditor] AuditorManager.sol

> AuditorManagerコントラクトは、Encrypted ERCシステムにおける「監査人（Auditor）」の情報管理を行うAbstractコントラクトです。Auditorは、以下の役割を担います。

- URL: https://cardene.dev/books/encrypted-erc/c36bc3
- 本: Encrypted ERC (https://cardene.dev/books/encrypted-erc.md)
- 章: 18 / 32
- 前の章: https://cardene.dev/books/encrypted-erc/e6d243.md
- 次の章: https://cardene.dev/books/encrypted-erc/f55b85.md
- 公開日: 2025-07-07
- 更新日: 2025-07-07
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, smartcontract, avalanch

## 概要

AuditorManagerコントラクトは、Encrypted ERCシステムにおける「監査人（Auditor）」の情報管理を行うAbstractコントラクトです。
Auditorは、以下の役割を担います。

- 規制対応
    - KYC/AMLなどの要件を満たすために取引内容を確認する権限を持つ
- 運用監視
    - 不正や異常な操作が行われていないかをチェックする

AuditorManagerコントラクトを継承したコントラクトはAuditorのアドレスと公開鍵を保持・更新でき、さらにAuditorが設定されている状態でしか実行できない処理を制限することで、Encrypted ERCシステム全体のセキュリティと信頼性を高めます。

## コード

https://github.com/ava-labs/EncryptedERC/blob/885843e3be6523a09a475ac63ecbc81de06910f9/contracts/auditor/AuditorManager.sol

```solidity
// (c) 2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem
pragma solidity 0.8.27;

import {Point} from "../types/Types.sol";
import {ZeroAddress} from "../errors/Errors.sol";

/**
 * @title AuditorManager
 * @notice Abstract contract that manages auditor-related functionality for encrypted ERC operations
 * @dev This contract is responsible for:
 *      1. Storing and managing the auditor's address and public key
 *      2. Providing access control for auditor-related operations
 *      3. Emitting events when auditor information changes
 *
 * The auditor is a crucial component in the encrypted ERC system that:
 * - Ensures compliance with regulatory requirements
 * - Provides oversight for private operations
 */
abstract contract AuditorManager {
    ///////////////////////////////////////////////////
    ///                   State Variables           ///
    ///////////////////////////////////////////////////

    /// @notice The address of the current auditor
    /// @dev This address is used to identify the auditor and for access control
    address public auditor = address(0);

    /// @notice The public key of the current auditor
    /// @dev This is used in zero-knowledge proofs to validate auditor signatures
    ///      The point (0,1) is considered invalid as it's the identity point in the elliptic curve
    Point public auditorPublicKey = Point({x: 0, y: 0});

    ///////////////////////////////////////////////////
    ///                    Events                   ///
    ///////////////////////////////////////////////////

    /**
     * @notice Emitted when the auditor's information is updated
     * @param oldAuditor The previous auditor's address
     * @param newAuditor The new auditor's address
     */
    event AuditorChanged(
        address indexed oldAuditor,
        address indexed newAuditor
    );

    ///////////////////////////////////////////////////
    ///                   Modifiers                 ///
    ///////////////////////////////////////////////////

    /**
     * @notice Ensures that an auditor is properly
     * @dev This modifier checks two conditions:
     *      1. The auditor's public key is valid (not the identity point)
     *      2. The auditor's address is not the zero address
     *
     * Requirements:
     * - Auditor public key must be set (not the identity point)
     * - Auditor address must be set (not zero address)
     */
    modifier onlyIfAuditorSet() {
        require(
            auditorPublicKey.x != 0 && auditorPublicKey.y != 1,
            "Auditor public key not set"
        );
        require(auditor != address(0), "Auditor not set");
        _;
    }

    ///////////////////////////////////////////////////
    ///                   External                  ///
    ///////////////////////////////////////////////////

    /**
     * @notice Checks if the auditor's public key is properly set
     * @return bool True if the auditor's public key is set and valid
     * @dev This function is used to verify if the contract is ready for
     *      operations that require auditor validation
     */
    function isAuditorKeySet() external view returns (bool) {
        return auditorPublicKey.x != 0 && auditorPublicKey.y != 1;
    }

    ///////////////////////////////////////////////////
    ///                   Internal                  ///
    ///////////////////////////////////////////////////

    /**
     * @notice Updates the auditor's information
     * @param newAuditor The address of the new auditor
     * @param publicKey The public key of the new auditor
     * @dev This function:
     *      1. Validates the new auditor's address
     *      2. Updates the auditor's information
     *      3. Emits an event to track the change
     *
     * Requirements:
     * - newAuditor must not be the zero address
     * - publicKey must be a valid point on the elliptic curve
     */
    function _updateAuditor(
        address newAuditor,
        uint256[2] memory publicKey
    ) internal {
        address oldAuditor = auditor;
        // check if the auditor is the zero address
        if (newAuditor == address(0)) {
            revert ZeroAddress();
        }

        auditor = newAuditor;
        auditorPublicKey = Point({x: publicKey[0], y: publicKey[1]});

        emit AuditorChanged(oldAuditor, newAuditor);
    }
}
```

## State

```solidity
address public auditor = address(0);
Point public auditorPublicKey = Point({x: 0, y: 0});
```

* `auditor`
    * Auditorのアドレス。
    * 初期値は `0x000…0`（未設定状態）。
* `auditorPublicKey`
    * Auditorがゼロ知識証明で使用する公開鍵を `Types.Point`（`x`, `y`）構造体で保持。
    * 初期値は `(0,0)`（未設定状態）。

## Events

### `AuditorChanged`

```solidity
event AuditorChanged(
    address indexed oldAuditor,
    address indexed newAuditor
);
```

Auditorアドレスが更新された時に発行されるイベント。

## Modifiers

### `onlyIfAuditorSet`

```solidity
modifier onlyIfAuditorSet() {
    require(
        auditorPublicKey.x != 0 && auditorPublicKey.y != 1,
        "Auditor public key not set"
    );
    require(auditor != address(0), "Auditor not set");
    _;
}
```

Auditor情報（アドレス／公開鍵）が正しくセットされていないと関数の実行を止めます。
公開鍵が `(0,1)` のときを無効とみなします。

:::message
BabyJubJub曲線において、点 $(0,1)$ は「単位元」にあたります。
つまりどんなスカラー倍をしても同じ点のまま変化せず、本来の公開鍵として持つべき「**曲線上の生成点を秘密鍵でスカラー倍した結果**」という性質を全く備えていません。

そのため、公開鍵が $(0,1)$ のままだと以下の問題が起きるため、「未設定／無効なキー」として扱い、`onlyIfAuditorSet` で弾くようにしています。

* 本来の暗号的強度を全く担保できない
* ゼロ知識証明やElGamal暗号での演算が正しく動作しなくなる
:::

## External Functions

### `isAuditorKeySet`

```solidity
function isAuditorKeySet() external view returns (bool) {
    return auditorPublicKey.x != 0 && auditorPublicKey.y != 1;
}
```

Auditorの公開鍵が設定済みを返す関数。
BabyJubJub曲線の単位元 (0,1) を無効キーとして扱い、それ以外なら `true` を返します

## Internal Functions

### `_updateAuditor`

```solidity
function _updateAuditor(
    address newAuditor,
    uint256[2] memory publicKey
) internal {
    address oldAuditor = auditor;
    if (newAuditor == address(0)) {
        revert ZeroAddress();
    }

    auditor = newAuditor;
    auditorPublicKey = Point({x: publicKey[0], y: publicKey[1]});

    emit AuditorChanged(oldAuditor, newAuditor);
}
```

Auditor のアドレスと公開鍵を更新する関数。
`newAuditor` が0アドレスなら `ZeroAddress()` エラーを返します。
`auditor` と `auditorPublicKey` を更新して、`AuditorChanged` イベントを発行します。

**引数**

- `newAuditor` (`address`)
    - 新しく設定するAuditorのアドレス。
- `publicKey` (`uint256[2] memory`)
    - BabyJubJub曲線上のAuditorの公開鍵 [x, y]。
