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
use crate::field::{SizedPrimeField};
use crate::fp::Fp;
use crate::extension_towers::fp2;
use crate::extension_towers::fp3;
use crate::representation::ElementRepr;
use crate::traits::ZeroAndOne;
use crate::integers::MaxFieldUint;
use crate::field::PrimeField;

use crate::errors::ApiError;
use super::decode_utils::*;
use crate::field::field_from_modulus;

pub(crate) fn parse_base_field_from_encoding<
    'a,
    FE: ElementRepr,
    >(encoding: &'a [u8]) -> Result<(PrimeField<FE>, usize, MaxFieldUint, &'a [u8]), ApiError>
{
    let ((modulus, modulus_len), rest) = get_base_field_params(&encoding)?;
    let field = field_from_modulus::<FE>(&modulus).map_err(|_| {
        ApiError::InputError("Failed to create prime field from modulus".to_owned())
    })?;
    if rest.len() < modulus_len {
        return Err(ApiError::InputError("Input is not long enough".to_owned()));
    }

    Ok((field, modulus_len, modulus, rest))
}

pub(crate) fn decode_fp<
    'a,
    'b,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    bytes: &'b [u8], 
    field_byte_len: usize,
    base_field: &'a F
) -> Result<(Fp<'a, FE, F>, &'b [u8]), ApiError>
{
    let (x_encoding, rest) = split(bytes, field_byte_len, "Input is not long enough to get Fp element")?;
    let x = Fp::from_be_bytes(base_field, x_encoding, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp element, {}", e))
    })?;

    Ok((x, rest))
}

pub(crate) fn decode_fp_oversized<
    'a,
    'b,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    bytes: &'b [u8], 
    encoding_length: usize,
    base_field: &'a F
) -> Result<(Fp<'a, FE, F>, &'b [u8]), ApiError>
{
    let (encoding, rest) = split(bytes, encoding_length, "Input is not long enough to get Fp element")?;
    let x = Fp::from_be_bytes_with_padding(base_field, encoding, false, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp element, {}", e))
    })?;

    Ok((x, rest))
}

pub(crate) fn decode_fp2<
    'a,
    'b,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    bytes: &'b [u8], 
    field_byte_len: usize,
    extension_field: &'a fp2::Extension2<'a, FE, F>
) -> Result<(fp2::Fp2<'a, FE, F>, &'b [u8]), ApiError>
{
    let (c0_encoding, rest) = split(bytes, field_byte_len, "Input is not long enough to Fp2_c0")?;
    let c0 = Fp::from_be_bytes(extension_field.field, c0_encoding, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp2.c0 element, {}", e))
    })?;

    let (c1_encoding, rest) = split(rest, field_byte_len, "Input is not long enough to get Fp2_c1")?;
    let c1 = Fp::from_be_bytes(extension_field.field, c1_encoding, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp2.c1 element, {}", e))
    })?;

    let mut x = fp2::Fp2::zero(extension_field);
    x.c0 = c0;
    x.c1 = c1;

    Ok((x, rest))
}

pub(crate) fn decode_fp2_oversized<
    'a,
    'b,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    bytes: &'b [u8], 
    encoding_length: usize,
    extension_field: &'a fp2::Extension2<'a, FE, F>
) -> Result<(fp2::Fp2<'a, FE, F>, &'b [u8]), ApiError>
{
    let (c0_encoding, rest) = split(bytes, encoding_length, "Input is not long enough to Fp2_c0")?;
    let c0 = Fp::from_be_bytes_with_padding(extension_field.field, c0_encoding, false, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp.c0 element, {}", e))
    })?;

    let (c1_encoding, rest) = split(rest, encoding_length, "Input is not long enough to get Fp2_c1")?;
    let c1 = Fp::from_be_bytes_with_padding(extension_field.field, c1_encoding, false, true).map_err(|e| {
        ApiError::InputError(format!("Failed to parse Fp2.c1 element, {}", e))
    })?;

    let mut x = fp2::Fp2::zero(extension_field);
    x.c0 = c0;
    x.c1 = c1;

    Ok((x, rest))
}

pub(crate) fn decode_fp3<
    'a,
    'b,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    bytes: &'b [u8], 
    field_byte_len: usize,
    extension_field: &'a fp3::Extension3<'a, FE, F>
) -> Result<(fp3::Fp3<'a, FE, F>, &'b [u8]), ApiError>
{
    let (c0_encoding, rest) = split(bytes, field_byte_len, "Input is not long enough to Fp3_c0")?;
    let c0 = Fp::from_be_bytes(extension_field.field, c0_encoding, true).map_err(|_| {
        ApiError::InputError("Failed to parse Fp3 element".to_owned())
    })?;

    let (c1_encoding, rest) = split(rest, field_byte_len, "Input is not long enough to Fp3_c1")?;
    let c1 = Fp::from_be_bytes(extension_field.field, c1_encoding, true).map_err(|_| {
        ApiError::InputError("Failed to parse Fp3 element".to_owned())
    })?;

    let (c2_encoding, rest) = split(rest, field_byte_len, "Input is not long enough to Fp3_c2")?;
    let c2 = Fp::from_be_bytes(extension_field.field, c2_encoding, true).map_err(|_| {
        ApiError::InputError("Failed to parse Fp3element".to_owned())
    })?;

    let mut x = fp3::Fp3::zero(extension_field);
    x.c0 = c0;
    x.c1 = c1;
    x.c2 = c2;

    Ok((x, rest))
}

pub(crate) fn serialize_fp_fixed_len<
    'a,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
(
    encoding_byte_len: usize,
    element: &'a Fp<'a, FE, F>
) -> Result<Vec<u8>, ApiError>
{
    let mut bytes: Vec<u8> = vec![0u8; encoding_byte_len];
    let element = element.into_repr();
    let expected_byte_len = element.as_ref().len() * 8;
    if encoding_byte_len >= expected_byte_len {
        let start = encoding_byte_len - expected_byte_len;
        element.write_be(&mut bytes[start..]).map_err(|_| {
            ApiError::OutputError("Failed to serialize Fp element".to_owned())
        })?;
    } else {
        bytes.truncate(0);
        element.write_be(&mut bytes).map_err(|_| {
            ApiError::OutputError("Failed to serialize Fp element".to_owned())
        })?;
        let trim = expected_byte_len - encoding_byte_len;
        bytes.drain(0..trim);
    }

    debug_assert_eq!(bytes.len(), encoding_byte_len);

    Ok(bytes)
}

pub(crate) fn serialize_fp2_fixed_len<
    'a,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
    (
        encoding_byte_len: usize,
        element: &'a fp2::Fp2<'a, FE, F>
    ) -> Result<Vec<u8>, ApiError>
{
    let mut bytes = Vec::with_capacity(2*encoding_byte_len);
    bytes.extend(serialize_fp_fixed_len(encoding_byte_len, &element.c0)?);
    bytes.extend(serialize_fp_fixed_len(encoding_byte_len, &element.c1)?);

    Ok(bytes)
}

pub(crate) fn serialize_fp3_fixed_len<
    'a,
    FE: ElementRepr,
    F: SizedPrimeField<Repr = FE>
    >
    (
        encoding_byte_len: usize,
        element: &'a fp3::Fp3<'a, FE, F>
    ) -> Result<Vec<u8>, ApiError>
{
    let mut bytes = Vec::with_capacity(3*encoding_byte_len);
    bytes.extend(serialize_fp_fixed_len(encoding_byte_len, &element.c0)?);
    bytes.extend(serialize_fp_fixed_len(encoding_byte_len, &element.c1)?);
    bytes.extend(serialize_fp_fixed_len(encoding_byte_len, &element.c2)?);

    Ok(bytes)
}