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
use crate::{Integer, Bit};
use std::ops::{Shl, ShlAssign, Shr, ShrAssign};

impl Shl<&usize> for Integer {
    type Output = Integer;
    fn shl(self, shifts: &usize) -> Integer {
        shift_left(&self, shifts)
    }
}

impl Shl<&Integer> for Integer {
    type Output = Integer;
    fn shl(self, shifts: &Integer) -> Integer {
        shift_left(&self, &shifts.into())
    }
}

impl Shl<&usize> for &Integer {
    type Output = Integer;
    fn shl(self, shifts: &usize) -> Integer {
        shift_left(&self, shifts)
    }
}

impl Shl<&Integer> for &Integer {
    type Output = Integer;
    fn shl(self, shifts: &Integer) -> Integer {
        shift_left(self, &shifts.into())
    }
}

impl ShlAssign<&usize> for Integer {
    fn shl_assign(&mut self, shifts: &usize) {
        * self = shift_left(self, shifts)
    }
}

impl ShlAssign<&Integer> for Integer {
    fn shl_assign(&mut self, shifts: &Integer) {
        * self = shift_left(self, &shifts.into())
    }
}

fn shift_left(a: &Integer, shifts: &usize) -> Integer {
    Integer([a.0.to_vec(), vec![Bit::Zero; *shifts]].concat())
}

impl Shr<&usize> for Integer {
    type Output = Integer;
    fn shr(self, shifts: &usize) -> Integer {
        shift_right(&self, shifts)
    }
}

impl Shr<&Integer> for Integer {
    type Output = Integer;
    fn shr(self, shifts: &Integer) -> Integer {
        shift_right(&self, &shifts.into())
    }
}

impl Shr<&usize> for &Integer {
    type Output = Integer;
    fn shr(self, shifts: &usize) -> Integer {
        shift_right(&self, shifts)
    }
}

impl Shr<&Integer> for &Integer {
    type Output = Integer;
    fn shr(self, shifts: &Integer) -> Integer {
        shift_right(&self, &shifts.into())
    }
}

impl ShrAssign<&usize> for Integer {
    fn shr_assign(&mut self, shifts: &usize) {
        *self = shift_right(self, shifts)
    }
}

impl ShrAssign<&Integer> for Integer {
    fn shr_assign(&mut self, shifts: &Integer) {
        *self = shift_right(self, &shifts.into())
    }
}

fn shift_right(a: &Integer, shifts: &usize) -> Integer {

    if shifts <= &(a.0.len() - 2) {

        Integer(a.0[0..a.0.len() - shifts].to_vec())

    } else {

        Integer(vec![a.0[0];2])

    }
}