-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathefield.rs
522 lines (447 loc) · 14.5 KB
/
efield.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! # Extended Finite Field Element Implementation
//!
//! This module provides an implementation of extended finite field elements,
//! which are built on top of base finite fields. It includes structures and
//! traits for defining and working with extended finite fields, as well as
//! implementations of arithmetic operations in these fields.
//!
//! ## Key Components
//!
//! - `IrreduciblePoly` trait: Defines the irreducible polynomial for the field extension.
//! - `ExtendedFieldElement<M, P>` struct: Represents an element in an extended finite field.
//! - Arithmetic operations: Addition, subtraction, multiplication, and division.
//!
//! ## Features
//!
//! - Creation of extended field elements from base field polynomials.
//! - Arithmetic operations in the extended field.
//! - Reduction of elements modulo the irreducible polynomial.
//! - Inverse computation in the extended field.
//! - Conversion between base field elements and extended field elements.
//!
//! ## Usage
//!
//! To use this module, define a base field, an irreducible polynomial, and then
//! create `ExtendedFieldElement` instances:
//!
//! ```
//! use std::str::FromStr;
//! use paste::paste;
//! use num_bigint::BigInt;
//! use lazy_static::lazy_static;
//! use myzkp::define_myzkp_modulus_type;
//! use myzkp::define_extension_field;
//! use myzkp::modules::algebra::ring::Ring;
//! use myzkp::modules::algebra::field::ModulusValue;
//! use myzkp::modules::algebra::field::FiniteFieldElement;
//! use myzkp::modules::algebra::polynomial::Polynomial;
//! use myzkp::modules::algebra::efield::IrreduciblePoly;
//! use myzkp::modules::algebra::efield::ExtendedFieldElement;
//!
//! define_myzkp_modulus_type!(Mod7, "7");
//! define_extension_field!(
//! Ip7,
//! FiniteFieldElement<Mod7>,
//! Polynomial {
//! coef: vec![
//! FiniteFieldElement::<Mod7>::from_value(1),
//! FiniteFieldElement::<Mod7>::from_value(0),
//! FiniteFieldElement::<Mod7>::from_value(1),
//! ],
//! }
//! );
//!
//! let a = ExtendedFieldElement::<Mod7, Ip7>::new(Polynomial {
//! coef: vec![
//! FiniteFieldElement::from_value(2),
//! FiniteFieldElement::from_value(1),
//! ],
//! });
//! ```
//!
//! ## Note
//!
//! This implementation builds upon the base finite field implementation and uses
//! the `Polynomial` struct for representing elements. It is designed to work with
//! various base fields and irreducible polynomials, allowing for flexible creation
//! of field extensions.
use std::fmt;
use std::fmt::Debug;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Sub, SubAssign};
use num_bigint::BigInt;
use num_traits::One;
use num_traits::Zero;
use crate::modules::algebra::field::{Field, FiniteFieldElement, ModulusValue};
use crate::modules::algebra::polynomial::Polynomial;
use crate::modules::algebra::ring::Ring;
pub trait IrreduciblePoly<F: Field>: Debug + Clone + Hash {
fn modulus() -> &'static Polynomial<F>;
}
#[derive(Clone, Debug)]
pub struct ExtendedFieldElement<M: ModulusValue, P: IrreduciblePoly<FiniteFieldElement<M>>> {
pub poly: Polynomial<FiniteFieldElement<M>>,
_phantom: PhantomData<P>,
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>>
ExtendedFieldElement<M, P>
{
pub fn new(poly: Polynomial<FiniteFieldElement<M>>) -> Self {
let result = Self {
poly: poly,
_phantom: PhantomData,
};
result.sanitize()
}
pub fn degree(&self) -> isize {
P::modulus().degree()
}
pub fn from_base_field(value: FiniteFieldElement<M>) -> Self {
Self::new((Polynomial { coef: vec![value] }).reduce()).sanitize()
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Field
for ExtendedFieldElement<M, P>
{
fn inverse(&self) -> Self {
//if self.poly.is_zero() {
// return None; // Zero has no inverse
//}
let mut lm = Polynomial::<FiniteFieldElement<M>>::one();
let mut hm = Polynomial::zero();
let mut low = self.poly.clone();
let mut high = P::modulus().clone();
while !low.is_zero() {
let q = &high / &low;
let r = &high % &low;
let nm = hm - (&lm * &q);
high = low;
hm = lm;
low = r;
lm = nm;
}
//if high.degree() != 0 {
// return None; // Not invertible
//}
Self::new(hm * high.coef[0].inverse())
}
fn div_ref(&self, other: &Self) -> Self {
self.mul_ref(&other.inverse())
}
fn add_m1_ref(&self, _other: &Self) -> Self {
unimplemented!("Not applicable for extended field elements")
}
fn mul_m1_ref(&self, _other: &Self) -> Self {
unimplemented!("Not applicable for extended field elements")
}
fn sub_m1_ref(&self, _other: &Self) -> Self {
unimplemented!("Not applicable for extended field elements")
}
fn pow_m1<V: Into<BigInt>>(&self, _n: V) -> Self {
unimplemented!("Not applicable for extended field elements")
}
fn sanitize(&self) -> Self {
Self {
poly: &self.poly.reduce() % P::modulus(),
_phantom: PhantomData,
}
}
}
impl<M: ModulusValue, P: IrreduciblePoly<FiniteFieldElement<M>>> Hash
for ExtendedFieldElement<M, P>
{
fn hash<H: Hasher>(&self, state: &mut H) {
for v in &self.poly.coef {
v.hash(state);
}
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> PartialEq
for ExtendedFieldElement<M, P>
{
fn eq(&self, other: &Self) -> bool {
self.poly == other.poly
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Eq
for ExtendedFieldElement<M, P>
{
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Zero
for ExtendedFieldElement<M, P>
{
fn zero() -> Self {
ExtendedFieldElement::<M, P>::new(Polynomial::<FiniteFieldElement<M>>::zero())
}
fn is_zero(&self) -> bool {
self.poly.is_zero()
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> One
for ExtendedFieldElement<M, P>
{
fn one() -> Self {
ExtendedFieldElement::<M, P>::new(Polynomial::<FiniteFieldElement<M>>::one())
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Add
for ExtendedFieldElement<M, P>
{
type Output = Self;
fn add(self, other: Self) -> Self {
self.add_ref(&other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> AddAssign
for ExtendedFieldElement<M, P>
{
fn add_assign(&mut self, other: Self) {
self.add_assign_ref(&other)
}
}
impl<'a, M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>>
Add<&'a ExtendedFieldElement<M, P>> for ExtendedFieldElement<M, P>
{
type Output = ExtendedFieldElement<M, P>;
fn add(self, other: &'a ExtendedFieldElement<M, P>) -> ExtendedFieldElement<M, P> {
self.add_ref(other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Sub
for ExtendedFieldElement<M, P>
{
type Output = Self;
fn sub(self, other: Self) -> Self {
self.sub_ref(&other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> SubAssign
for ExtendedFieldElement<M, P>
{
fn sub_assign(&mut self, other: Self) {
self.sub_assign_ref(&other)
}
}
impl<'a, M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>>
Sub<&'a ExtendedFieldElement<M, P>> for ExtendedFieldElement<M, P>
{
type Output = ExtendedFieldElement<M, P>;
fn sub(self, other: &'a ExtendedFieldElement<M, P>) -> ExtendedFieldElement<M, P> {
self.sub_ref(other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Mul
for ExtendedFieldElement<M, P>
{
type Output = Self;
fn mul(self, other: Self) -> Self {
self.mul_ref(&other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> MulAssign
for ExtendedFieldElement<M, P>
{
fn mul_assign(&mut self, other: Self) {
self.mul_assign_ref(&other)
}
}
impl<'a, M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>>
Mul<&'a ExtendedFieldElement<M, P>> for ExtendedFieldElement<M, P>
{
type Output = ExtendedFieldElement<M, P>;
fn mul(self, other: &'a ExtendedFieldElement<M, P>) -> ExtendedFieldElement<M, P> {
self.mul_ref(other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Div
for ExtendedFieldElement<M, P>
{
type Output = Self;
fn div(self, other: Self) -> Self {
self.div_ref(&other)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Neg
for ExtendedFieldElement<M, P>
{
type Output = Self;
fn neg(self) -> Self {
Self::new(-self.poly)
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> Ring
for ExtendedFieldElement<M, P>
{
fn add_ref(&self, other: &Self) -> Self {
Self::new(&self.poly + &other.poly)
}
fn add_assign_ref(&mut self, other: &Self) {
self.poly += &other.poly;
self.sanitize();
}
fn mul_ref(&self, other: &Self) -> Self {
Self::new(&self.poly * &other.poly)
}
fn mul_assign_ref(&mut self, other: &Self) {
self.poly *= &other.poly;
self.sanitize();
}
fn sub_ref(&self, other: &Self) -> Self {
Self::new(&self.poly - &other.poly)
}
fn sub_assign_ref(&mut self, other: &Self) {
self.poly -= &other.poly;
self.sanitize();
}
fn pow<V: Into<BigInt>>(&self, n: V) -> Self {
let mut base = self.clone();
let mut exponent: BigInt = n.into();
let mut result = Self::one();
while exponent > BigInt::zero() {
if &exponent % BigInt::from(2) == BigInt::one() {
result = result.mul_ref(&base);
}
exponent /= 2;
base = base.mul_ref(&base);
}
result
}
fn get_value(&self) -> BigInt {
unimplemented!("Not applicable for extended field elements")
}
fn from_value<V: Into<BigInt>>(value: V) -> Self {
ExtendedFieldElement::<M, P>::new(
Polynomial::<FiniteFieldElement<M>>::one() * FiniteFieldElement::<M>::from_value(value),
)
}
fn random_element(_exclude_elements: &[Self]) -> Self {
unimplemented!("Random element generation for extended field not implemented")
}
}
impl<M: ModulusValue + 'static, P: IrreduciblePoly<FiniteFieldElement<M>>> fmt::Display
for ExtendedFieldElement<M, P>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.poly)
}
}
#[macro_export]
macro_rules! define_extension_field {
($name:ident, $base_field:ty, $modulus:expr) => {
paste! {#[derive(Debug, Clone, PartialEq, Hash)]
pub struct $name;
lazy_static! {
static ref [<MODULUS_ $name>]: Polynomial<$base_field> = $modulus;
}
impl IrreduciblePoly<$base_field> for $name {
fn modulus() -> &'static Polynomial<$base_field> {
&[<MODULUS_ $name>]
}
}
}
};
}
// Test the implementation
#[cfg(test)]
mod tests {
use super::*;
use lazy_static::lazy_static;
use paste::paste;
use std::str::FromStr;
use crate::define_myzkp_modulus_type;
define_myzkp_modulus_type!(Mod2, "2");
define_myzkp_modulus_type!(Mod7, "7");
define_extension_field!(
Ip2,
FiniteFieldElement<Mod2>,
Polynomial {
coef: vec![
FiniteFieldElement::<Mod2>::from_value(1),
FiniteFieldElement::<Mod2>::from_value(1),
FiniteFieldElement::<Mod2>::from_value(1),
],
}
);
define_extension_field!(
Ip7,
FiniteFieldElement<Mod7>,
Polynomial {
coef: vec![
FiniteFieldElement::<Mod7>::from_value(1),
FiniteFieldElement::<Mod7>::from_value(0),
FiniteFieldElement::<Mod7>::from_value(1),
],
}
);
#[test]
fn test_extended_field_operations_mod2() {
// x + 1
let a = ExtendedFieldElement::<Mod2, Ip2>::new(Polynomial {
coef: vec![
FiniteFieldElement::from_value(1),
FiniteFieldElement::from_value(1),
],
});
// x
let b = ExtendedFieldElement::<Mod2, Ip2>::new(Polynomial {
coef: vec![
FiniteFieldElement::from_value(0),
FiniteFieldElement::from_value(1),
],
});
// Addition
let sum = a.add_ref(&b);
assert_eq!(
sum,
ExtendedFieldElement::<Mod2, Ip2>::new(Polynomial {
coef: vec![FiniteFieldElement::from_value(1)],
})
);
// Multiplication
let product = a.mul_ref(&b);
assert_eq!(
product,
ExtendedFieldElement::<Mod2, Ip2>::new(Polynomial {
coef: vec![FiniteFieldElement::from_value(1)],
},)
);
// Inverse
let inv_a = a.inverse();
let product = a.mul_ref(&inv_a);
assert_eq!(
product,
ExtendedFieldElement::<Mod2, Ip2>::from_base_field(FiniteFieldElement::one())
);
assert_eq!(
a.div_ref(&a),
ExtendedFieldElement::<Mod2, Ip2>::from_base_field(FiniteFieldElement::one())
);
}
#[test]
fn test_extended_field_operations_mod7() {
// x + 2
let a = ExtendedFieldElement::<Mod7, Ip7>::new(Polynomial {
coef: vec![
FiniteFieldElement::from_value(2),
FiniteFieldElement::from_value(1),
],
});
// 4x + 6
let b = ExtendedFieldElement::<Mod7, Ip7>::new(Polynomial {
coef: vec![
FiniteFieldElement::from_value(6),
FiniteFieldElement::from_value(4),
],
});
// Inverse
let inv_a = a.inverse();
assert_eq!(inv_a, b);
let product = a.mul_ref(&inv_a);
assert_eq!(
product,
ExtendedFieldElement::<Mod7, Ip7>::from_base_field(FiniteFieldElement::one())
);
}
}