he_ring/ntt/
dyn_convolution.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

use std::marker::PhantomData;
use std::ops::Deref;

use feanor_math::algorithms::convolution::ConvolutionAlgorithm;
use feanor_math::ring::*;
use feanor_math::seq::VectorView;

///
/// Trait for algorithms that compute convolutions. This mirrors
/// [`feanor_math::algorithms::convolution::ConvolutionAlgorithm`], but
/// is dyn-compatible.
/// 
/// Wrap a `dyn DynConvolutionAlgorithm<R>` in [`DynConvolutionAlgorithmConvolution`]
/// to use it as a [`feanor_math::algorithms::convolution::ConvolutionAlgorithm`].
/// 
pub trait DynConvolutionAlgorithm<R>
    where R: ?Sized + RingBase
{
    fn compute_convolution_dyn(&self, lhs: &[R::Element], rhs: &[R::Element], dst: &mut [R::Element], ring: &R);
    fn supports_ring_dyn(&self, ring: &R) -> bool;
}

impl<R, C> DynConvolutionAlgorithm<R> for C
    where R: ?Sized + RingBase,
        C: ConvolutionAlgorithm<R>
{
    fn compute_convolution_dyn(&self, lhs: &[R::Element], rhs: &[R::Element], dst: &mut [R::Element], ring: &R) {
        self.compute_convolution(lhs, rhs, dst, RingRef::new(ring));
    }

    fn supports_ring_dyn(&self, ring: &R) -> bool {
        self.supports_ring(RingRef::new(ring))
    }
}

///
/// Wraps a [`DynConvolutionAlgorithm`] trait object to use it as a 
/// [`feanor_math::algorithms::convolution::ConvolutionAlgorithm`].
/// 
pub struct DynConvolutionAlgorithmConvolution<R, C = Box<dyn DynConvolutionAlgorithm<R>>>
    where C: Deref,
        C::Target: DynConvolutionAlgorithm<R>,
        R: ?Sized + RingBase
{
    ring: PhantomData<R>,
    conv: C
}

impl<C, R> Clone for DynConvolutionAlgorithmConvolution<R, C>
    where C: Deref + Clone,
        C::Target: DynConvolutionAlgorithm<R>,
        R: ?Sized + RingBase
{
    fn clone(&self) -> Self {
        Self {
            ring: self.ring,
            conv: self.conv.clone()
        }
    }
}

impl<C, R> DynConvolutionAlgorithmConvolution<R, C>
    where C: Deref,
        C::Target: DynConvolutionAlgorithm<R>,
        R: ?Sized + RingBase
{
    pub fn new(conv: C) -> Self {
        Self {
            ring: PhantomData,
            conv: conv
        }
    }
}

impl<C, R> ConvolutionAlgorithm<R> for DynConvolutionAlgorithmConvolution<R, C>
    where C: Deref,
        C::Target: DynConvolutionAlgorithm<R>,
        R: ?Sized + RingBase
{
    fn compute_convolution<S: RingStore<Type = R> + Copy, V1: VectorView<El<S>>, V2: VectorView<El<S>>>(&self, lhs: V1, rhs: V2, dst: &mut [El<S>], ring: S) {
        let copy_lhs = lhs.as_iter().map(|x| ring.clone_el(x)).collect::<Vec<_>>();
        let copy_rhs = rhs.as_iter().map(|x| ring.clone_el(x)).collect::<Vec<_>>();
        self.conv.compute_convolution_dyn(&copy_lhs, &copy_rhs, dst, ring.get_ring());
    }

    fn supports_ring<S: RingStore<Type = R> + Copy>(&self, ring: S) -> bool {
        self.conv.supports_ring_dyn(ring.get_ring())
    }
}

#[cfg(test)]
use feanor_math::primitive_int::StaticRing;
#[cfg(test)]
use feanor_math::rings::zn::zn_64::{Zn, ZnBase};
#[cfg(test)]
use std::alloc::Global;
#[cfg(test)]
use feanor_math::algorithms::convolution::STANDARD_CONVOLUTION;
#[cfg(test)]
use feanor_math::rings::extension::extension_impl::FreeAlgebraImpl;
#[cfg(test)]
use feanor_math::rings::extension::FreeAlgebraStore;
#[cfg(test)]
use feanor_math::assert_el_eq;

#[test]
fn test_dyn_convolution_is_dyn_compatible() {
    #[allow(unused)]
    fn test(_: &dyn DynConvolutionAlgorithm<StaticRing<i64>>) {}
}

#[test]
fn test_dyn_convolution_convolution_use_build_ring() {
    fn do_test(conv: Box<dyn DynConvolutionAlgorithm<ZnBase>>) {
        let base_ring = Zn::new(2);
        let ring = FreeAlgebraImpl::new_with(base_ring, 3, [base_ring.one(), base_ring.one()], "a", Global, DynConvolutionAlgorithmConvolution::<ZnBase>::new(conv));
        assert_el_eq!(&ring, ring.one(), ring.pow(ring.canonical_gen(), 7));
    }
    do_test(Box::new(STANDARD_CONVOLUTION));
}