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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! This module contains the definition of the `Scalar` trait, which is used to represent the scalar field used in Proof of SQL.
mod error;
pub use error::ScalarConversionError;
mod mont_scalar;
#[cfg(test)]
mod mont_scalar_test;
use core::{cmp::Ordering, ops::Sub};
pub use mont_scalar::Curve25519Scalar;
pub(crate) use mont_scalar::MontScalar;
mod mont_scalar_from;
#[cfg(test)]
mod mont_scalar_from_test;

#[cfg(any(test, feature = "test"))]
#[cfg(feature = "blitzar")]
mod commitment_utility;
#[cfg(any(test, feature = "test"))]
#[cfg(feature = "blitzar")]
pub use commitment_utility::compute_commitment_for_testing;
use num_bigint::BigInt;

/// A trait for the scalar field used in Proof of SQL.
pub trait Scalar:
    Clone
    + core::fmt::Debug
    + core::fmt::Display
    + PartialEq
    + Default
    + for<'a> From<&'a str>
    + Sync
    + Send
    + num_traits::One
    + core::iter::Sum
    + core::iter::Product
    + Sub<Output = Self>
    + Copy
    + core::ops::MulAssign
    + core::ops::AddAssign
    + num_traits::Zero
    + for<'a> core::convert::From<&'a Self> // Required for `Column` to implement `MultilinearExtension`
    + for<'a> core::convert::From<&'a bool> // Required for `Column` to implement `MultilinearExtension`
    + for<'a> core::convert::From<&'a i16> // Required for `Column` to implement `MultilinearExtension`
    + for<'a> core::convert::From<&'a i32> // Required for `Column` to implement `MultilinearExtension`
    + for<'a> core::convert::From<&'a i64> // Required for `Column` to implement `MultilinearExtension`
    + for<'a> core::convert::From<&'a i128> // Required for `Column` to implement `MultilinearExtension`
    + core::convert::TryInto <bool>
    + core::convert::TryInto <i8>
    + core::convert::TryInto <i16>
    + core::convert::TryInto <i32>
    + core::convert::TryInto <i64>
    + core::convert::TryInto <i128>
    + core::convert::Into<[u64; 4]>
    + core::convert::From<[u64; 4]>
    + core::cmp::Ord
    + core::ops::Neg<Output = Self>
    + num_traits::Zero
    + core::ops::AddAssign
    + ark_serialize::CanonicalSerialize //This enables us to put `Scalar`s on the transcript
    + ark_std::UniformRand //This enables us to get `Scalar`s as challenges from the transcript
    + num_traits::Inv<Output = Option<Self>> // Note: `inv` should return `None` exactly when the element is zero.
    + core::ops::SubAssign
    + super::ref_into::RefInto<[u64; 4]>
    + for<'a> core::convert::From<&'a String>
    + super::encode::VarInt
    + core::convert::From<String>
    + core::convert::From<i128>
    + core::convert::From<i64>
    + core::convert::From<i32>
    + core::convert::From<i16>
    + core::convert::From<bool>
    + core::convert::Into<BigInt>
    + TryFrom<BigInt, Error = ScalarConversionError>
{
    /// The value (p - 1) / 2. This is "mid-point" of the field - the "six" on the clock.
    /// It is the largest signed value that can be represented in the field with the natural embedding.
    const MAX_SIGNED: Self;
    /// The 0 (additive identity) element of the field.
    const ZERO: Self;
    /// The 1 (multiplicative identity) element of the field.
    const ONE: Self;
    /// 1 + 1
    const TWO: Self;
    /// Compare two `Scalar`s as signed numbers.
    fn signed_cmp(&self, other: &Self) -> Ordering {
        match *self - *other {
            x if x.is_zero() => Ordering::Equal,
            x if x > Self::MAX_SIGNED => Ordering::Less,
            _ => Ordering::Greater,
        }
    }
}

macro_rules! scalar_conversion_to_int {
    ($scalar:ty) => {
        impl TryFrom<$scalar> for bool {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i8",
                        value
                    )));
                }
                let val: i128 = sign * abs[0] as i128;
                match val {
                    0 => Ok(false),
                    1 => Ok(true),
                    _ => Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in a bool",
                        value
                    ))),
                }
            }
        }

        impl TryFrom<$scalar> for i8 {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i8",
                        value
                    )));
                }
                let val: i128 = sign * abs[0] as i128;
                val.try_into().map_err(|_| {
                    ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i8",
                        value
                    ))
                })
            }
        }

        impl TryFrom<$scalar> for i16 {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i16",
                        value
                    )));
                }
                let val: i128 = sign * abs[0] as i128;
                val.try_into().map_err(|_| {
                    ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i16",
                        value
                    ))
                })
            }
        }

        impl TryFrom<$scalar> for i32 {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i32",
                        value
                    )));
                }
                let val: i128 = sign * abs[0] as i128;
                val.try_into().map_err(|_| {
                    ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i32",
                        value
                    ))
                })
            }
        }

        impl TryFrom<$scalar> for i64 {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[1] != 0 || abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i64",
                        value
                    )));
                }
                let val: i128 = sign * abs[0] as i128;
                val.try_into().map_err(|_| {
                    ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i64",
                        value
                    ))
                })
            }
        }

        impl TryFrom<$scalar> for i128 {
            type Error = ScalarConversionError;
            fn try_from(value: $scalar) -> Result<Self, Self::Error> {
                let (sign, abs): (i128, [u64; 4]) = if value > <$scalar>::MAX_SIGNED {
                    (-1, (-value).into())
                } else {
                    (1, value.into())
                };
                if abs[2] != 0 || abs[3] != 0 {
                    return Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i128",
                        value
                    )));
                }
                let val: u128 = (abs[1] as u128) << 64 | (abs[0] as u128);
                match (sign, val) {
                    (1, v) if v <= i128::MAX as u128 => Ok(v as i128),
                    (-1, v) if v <= i128::MAX as u128 => Ok(-(v as i128)),
                    (-1, v) if v == i128::MAX as u128 + 1 => Ok(i128::MIN),
                    _ => Err(ScalarConversionError::Overflow(format!(
                        "{} is too large to fit in an i128",
                        value
                    ))),
                }
            }
        }

        impl From<$scalar> for BigInt {
            fn from(value: $scalar) -> Self {
                // Since we wrap around in finite fields anything greater than the max signed value is negative
                let is_negative = value > <$scalar>::MAX_SIGNED;
                let sign = if is_negative {
                    num_bigint::Sign::Minus
                } else {
                    num_bigint::Sign::Plus
                };
                let value_abs: [u64; 4] = (if is_negative { -value } else { value }).into();
                let bits: &[u8] = bytemuck::cast_slice(&value_abs);
                BigInt::from_bytes_le(sign, &bits)
            }
        }
    };
}

pub(crate) use scalar_conversion_to_int;