sigma-protocols 0.5.1

SIGMA zero-knowledge proof protocols
Documentation
//! Utility functions for sigma protocols

use curve25519_dalek::scalar::Scalar;
use rand::rngs::OsRng;
use rand::TryRngCore;

use crate::error::{Error, Result};

/// Generate a random scalar
pub fn random_scalar() -> Scalar {
    let mut bytes = [0u8; 32];
    OsRng
        .try_fill_bytes(&mut bytes)
        .expect("Failed to generate random bytes");
    Scalar::from_bytes_mod_order(bytes)
}

/// Deserialize scalar from state bytes
pub fn scalar_from_state(state: &[u8]) -> Result<Scalar> {
    if state.len() != 32 {
        return Err(Error::InvalidProof);
    }

    let mut bytes = [0u8; 32];
    bytes.copy_from_slice(state);

    Scalar::from_canonical_bytes(bytes)
        .into_option()
        .ok_or(Error::InvalidScalar)
}