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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use byteorder;
use std::fmt;
use std::error::Error;
use std::io::{self, Read, Write};
pub trait ElementRepr:
Sized
+ Copy
+ Clone
+ Eq
+ Ord
+ Send
+ Sync
+ Default
+ fmt::Debug
+ fmt::Display
+ 'static
+ AsRef<[u64]>
+ AsMut<[u64]>
+ From<u64>
{
const NUM_LIMBS: usize;
fn sub_noborrow(&mut self, other: &Self);
fn add_nocarry(&mut self, other: &Self);
fn num_bits(&self) -> u32;
fn is_zero(&self) -> bool;
fn is_odd(&self) -> bool;
fn is_even(&self) -> bool;
fn div2(&mut self);
fn shr(&mut self, amt: u32);
fn mul2(&mut self);
fn shl(&mut self, amt: u32);
fn write_be<W: Write>(&self, mut writer: W) -> io::Result<()> {
use byteorder::{BigEndian, WriteBytesExt};
for digit in self.as_ref().iter().rev() {
writer.write_u64::<BigEndian>(*digit)?;
}
Ok(())
}
fn read_be<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
use byteorder::{BigEndian, ReadBytesExt};
for digit in self.as_mut().iter_mut().rev() {
*digit = reader.read_u64::<BigEndian>()?;
}
Ok(())
}
fn write_le<W: Write>(&self, mut writer: W) -> io::Result<()> {
use byteorder::{LittleEndian, WriteBytesExt};
for digit in self.as_ref().iter() {
writer.write_u64::<LittleEndian>(*digit)?;
}
Ok(())
}
fn read_le<R: Read>(&mut self, mut reader: R) -> io::Result<()> {
use byteorder::{LittleEndian, ReadBytesExt};
for digit in self.as_mut().iter_mut() {
*digit = reader.read_u64::<LittleEndian>()?;
}
Ok(())
}
fn mont_mul_assign(&mut self, other: &Self, modulus: &Self, mont_inv: u64);
fn mont_square(&mut self, modulus: &Self, mont_inv: u64);
fn mont_mul_assign_with_partial_reduction(&mut self, other: &Self, modulus: &Self, mont_inv: u64);
fn mont_square_with_partial_reduction(&mut self, modulus: &Self, mont_inv: u64);
fn into_normal_repr(&self, modulus: &Self, mont_inv: u64) -> Self;
fn reduce(&mut self, modulus: &Self);
}
pub trait IntoWnaf {
fn wnaf(&self, window: u32) -> Vec<i64>;
}
#[derive(Debug)]
pub enum RepresentationDecodingError {
NotInField(String),
}
impl Error for RepresentationDecodingError {
fn description(&self) -> &str {
match *self {
RepresentationDecodingError::NotInField(..) => "not an element of the field",
}
}
}
impl fmt::Display for RepresentationDecodingError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
RepresentationDecodingError::NotInField(ref repr) => {
write!(f, "{} is not an element of the field", repr)
}
}
}
}
pub(crate) fn num_bits(repr: &[u64]) -> u32 {
let mut bits = (64 * repr.len()) as u32;
for limb in repr.iter().rev() {
let limb = *limb;
if limb == 0 {
bits -= 64;
continue;
} else {
bits -= limb.leading_zeros();
break;
}
}
bits
}
pub(crate) fn right_shift_representation(repr: &mut [u64], shift: u64) {
let num_libs = repr.len();
for i in 0..(num_libs - 1) {
repr[i] = (repr[i] >> shift) | (repr[i+1] << (64 - shift));
}
repr[num_libs - 1] = repr[num_libs - 1] >> shift;
}