qfall-math 0.2.1

Mathematical foundations for rapid prototyping of lattice-based cryptography
Documentation
// Copyright 2023 Jan Niklas Siemer
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.

//! This module contains algorithms for sampling according to the uniform distribution.

use crate::{
    error::MathError,
    integer::{PolyOverZ, Z},
    macros::seeded::seedable_function,
    traits::SetCoefficient,
    utils::{index::evaluate_index, sample::uniform::UniformIntegerSampler},
};
use std::fmt::Display;

impl PolyOverZ {
    seedable_function!(
        /// Generates a [`PolyOverZ`] instance of maximum degree `max_degree` and coefficients
        /// chosen uniform at random in `[lower_bound, upper_bound)`.
        ///
        /// The internally used uniform at random chosen bytes are generated
        #[unseeded]
        /// by [`ThreadRng`](rand::rngs::ThreadRng), which uses ChaCha12 and is
        #[seeded]
        /// by a [`StdRng`](rand::rngs::StdRng) seeded with `seed`, which uses ChaCha12 and is
        #[optionally_seeded]
        /// by a [`StdRng`](rand::rngs::StdRng) seeded with `seed` or by [`ThreadRng`](rand::rngs::ThreadRng)
        #[optionally_seeded]
        /// if `seed` is `None`. Both use ChaCha12 and are
        /// considered cryptographically secure.
        ///
        /// Parameters:
        /// - `max_degree`: specifies the length of the polynomial,
        ///   i.e. the number of coefficients
        /// - `lower_bound`: specifies the included lower bound of the
        ///   interval over which is sampled
        /// - `upper_bound`: specifies the excluded upper bound of the
        ///   interval over which is sampled
        #[seeded]
        /// - `seed`: specifies the 256-bit seed of the PRNG used for sampling
        #[optionally_seeded]
        /// - `seed`: specifies an optional 256-bit seed of the PRNG used for sampling.
        #[optionally_seeded]
        ///   If `None` is provided, a fresh [`ThreadRng`](rand::rngs::ThreadRng) is used instead.
        ///
        /// Returns a fresh [`PolyOverZ`] instance of length `max_degree` with coefficients
        /// chosen uniform at random in `[lower_bound, upper_bound)` or a [`MathError`]
        /// if the `max_degree` was smaller than `0` or the provided interval was chosen too small.
        ///
        /// # Examples
        /// ```
        /// use qfall_math::integer::PolyOverZ;
        ///
        #[unseeded]
        /// let sample = PolyOverZ::sample_uniform(3, 17, 26).unwrap();
        #[seeded]
        /// let sample = PolyOverZ::sample_uniform_seeded(3, 17, 26, [42; 32]).unwrap();
        /// ```
        ///
        /// # Errors and Failures
        /// - Returns a [`MathError`] of type [`InvalidInterval`](MathError::InvalidInterval)
        ///   if the given `upper_bound` isn't at least larger than `lower_bound`.
        /// - Returns a [`MathError`] of type [`OutOfBounds`](MathError::OutOfBounds) if
        ///   the `max_degree` is negative or it does not fit into an [`i64`].
        pub(crate) fn sample_uniform(
            max_degree: impl TryInto<i64> + Display,
            lower_bound: impl Into<Z>,
            upper_bound: impl Into<Z>,
            seed: Option<[u8; 32]>,
        ) -> Result<Self, MathError> {
            let max_degree = evaluate_index(max_degree)?;
            let lower_bound: Z = lower_bound.into();
            let upper_bound: Z = upper_bound.into();

            let interval_size = &upper_bound - &lower_bound;
            let mut uis = UniformIntegerSampler::init(&interval_size, seed)?;

            let mut poly_z = PolyOverZ::default();
            for index in 0..=max_degree {
                let sample = uis.sample();
                unsafe { poly_z.set_coeff_unchecked(index, &lower_bound + sample) };
            }
            Ok(poly_z)
        }
    );
}

#[cfg(test)]
mod test_sample_uniform {
    use crate::utils::sample::test_seed;
    use crate::{
        integer::{PolyOverZ, Z},
        integer_mod_q::Modulus,
        traits::GetCoefficient,
    };

    /// Checks whether the boundaries of the interval are kept for small intervals.
    #[test]
    fn boundaries_kept_small() {
        for seeded in [false, true] {
            let lower_bound = Z::from(17);
            let upper_bound = Z::from(32);
            let poly_z = PolyOverZ::sample_uniform_optionally_seeded(
                32,
                &lower_bound,
                &upper_bound,
                seeded.then(test_seed),
            )
            .unwrap();

            for i in 0..32 {
                let sample = poly_z.get_coeff(i).unwrap();
                assert!(lower_bound <= sample);
                assert!(sample < upper_bound);
            }
        }
    }

    /// Checks whether the boundaries of the interval are kept for large intervals.
    #[test]
    fn boundaries_kept_large() {
        for seeded in [false, true] {
            let lower_bound = Z::from(i64::MIN) - Z::from(u64::MAX);
            let upper_bound = Z::from(i64::MIN);

            let poly_z = PolyOverZ::sample_uniform_optionally_seeded(
                256,
                &lower_bound,
                &upper_bound,
                seeded.then(test_seed),
            )
            .unwrap();

            for i in 0..256 {
                let sample = poly_z.get_coeff(i).unwrap();
                assert!(lower_bound <= sample);
                assert!(sample < upper_bound);
            }
        }
    }

    /// Checks whether the number of coefficients is correct.
    #[test]
    fn nr_coeffs() {
        for seeded in [false, true] {
            let degrees = [1, 3, 7, 15, 32, 120];
            for degree in degrees {
                let res = PolyOverZ::sample_uniform_optionally_seeded(
                    degree,
                    1,
                    15,
                    seeded.then(test_seed),
                )
                .unwrap();

                assert_eq!(degree, res.get_degree());
            }
        }
    }

    /// Checks whether providing an invalid interval results in an error.
    #[test]
    fn invalid_interval() {
        for seeded in [false, true] {
            let lb_0 = Z::from(i64::MIN);
            let lb_1 = Z::from(i64::MIN);
            let lb_2 = Z::ZERO;
            let upper_bound = Z::from(i64::MIN);

            let res_0 = PolyOverZ::sample_uniform_optionally_seeded(
                1,
                &lb_0,
                &upper_bound,
                seeded.then(test_seed),
            );
            let res_1 = PolyOverZ::sample_uniform_optionally_seeded(
                1,
                &lb_1,
                &upper_bound,
                seeded.then(test_seed),
            );
            let res_2 = PolyOverZ::sample_uniform_optionally_seeded(
                1,
                &lb_2,
                &upper_bound,
                seeded.then(test_seed),
            );

            assert!(res_0.is_err());
            assert!(res_1.is_err());
            assert!(res_2.is_err());
        }
    }

    /// Checks whether providing a length smaller than `0` results in an error.
    #[test]
    fn invalid_max_degree() {
        for seeded in [false, true] {
            let lower_bound = Z::from(0);
            let upper_bound = Z::from(15);

            let res_0 = PolyOverZ::sample_uniform_optionally_seeded(
                -1,
                &lower_bound,
                &upper_bound,
                seeded.then(test_seed),
            );
            let res_1 = PolyOverZ::sample_uniform_optionally_seeded(
                i64::MIN,
                &lower_bound,
                &upper_bound,
                seeded.then(test_seed),
            );

            assert!(res_0.is_err());
            assert!(res_1.is_err());
        }
    }

    /// Checks whether `sample_uniform` is available for all types
    /// implementing [`Into<Z>`], i.e. u8, u16, u32, u64, i8, ...
    #[test]
    fn availability() {
        for seeded in [false, true] {
            let modulus = Modulus::from(7);
            let z = Z::from(7);

            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1u64,
                0u16,
                7u8,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1i64,
                0u32,
                7u16,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1u8,
                0u64,
                7u32,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1u16,
                0i8,
                7u64,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1u32,
                0i16,
                7i8,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1i32,
                0i32,
                7i16,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1i16,
                0i64,
                7i32,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1i8,
                &Z::ZERO,
                7i64,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(
                1,
                0u8,
                &modulus,
                seeded.then(test_seed),
            );
            let _ = PolyOverZ::sample_uniform_optionally_seeded(1, 0, &z, seeded.then(test_seed));

            // explicitly checks the existence of the unseeded and the seeded function
            if seeded {
                let _ = PolyOverZ::sample_uniform_seeded(1u64, 0u16, 7u8, test_seed());
            } else {
                let _ = PolyOverZ::sample_uniform(1u64, 0u16, 7u8);
            }
        }
    }

    /// Checks whether the same seed results in the same sample.
    #[test]
    fn same_seed_same_sample() {
        use crate::integer::PolyOverZ;
        let sample_0 = PolyOverZ::sample_uniform_seeded(3, 17, 26, [42; 32]).unwrap();
        let sample_1 = PolyOverZ::sample_uniform_seeded(3, 17, 26, [42; 32]).unwrap();

        assert_eq!(sample_0, sample_1);
    }

    /// Checks whether the seeded variant outputs the known sample for a fixed seed.
    #[test]
    fn known_answer() {
        use crate::integer::PolyOverZ;
        let sample = PolyOverZ::sample_uniform_seeded(3, 17, 26, [42; 32]).unwrap();

        assert_eq!("4  19 20 17 19", sample.to_string());
    }
}