1use crate::alloc::string::ToString;
12use crate::math_util::{ct_util::*, *};
13use crate::prelude::*;
14
15macro_rules! implement_public_unsigned_mi {
16 ($t:ty,$bits:literal) => {
17 implement_public_mi!($t, $bits, <$t>::max_val());
18 impl ModNumeric for $t {
19 #[cfg_attr(feature = "use_attributes", in_hacspec)]
21 fn sub_mod(self, rhs: Self, n: Self) -> Self {
22 let mut tmp = self;
23 while tmp < rhs {
24 tmp += n;
25 }
26 (tmp - rhs) % n
27 }
28 #[cfg_attr(feature = "use_attributes", in_hacspec)]
30 fn add_mod(self, rhs: Self, n: Self) -> Self {
31 (self + rhs) % n
32 }
33 #[cfg_attr(feature = "use_attributes", in_hacspec)]
35 fn mul_mod(self, rhs: Self, n: Self) -> Self {
36 (self * rhs) % n
37 }
38 #[cfg_attr(feature = "use_attributes", in_hacspec)]
40 fn pow_mod(self, _exp: Self, _n: Self) -> Self {
41 unimplemented!();
42 }
43 #[cfg_attr(feature = "use_attributes", in_hacspec)]
45 fn modulo(self, n: Self) -> Self {
46 self % n
47 }
48 #[cfg_attr(feature = "use_attributes", in_hacspec)]
50 fn signed_modulo(self, n: Self) -> Self {
51 self.modulo(n)
52 }
53 #[cfg_attr(feature = "use_attributes", in_hacspec)]
55 fn absolute(self) -> Self {
56 self
57 }
58 }
59 };
60}
61
62macro_rules! implement_public_signed_mi {
63 ($t:ty,$bits:literal) => {
64 implement_public_mi!($t, $bits, -1);
65 impl ModNumeric for $t {
66 #[cfg_attr(feature = "use_attributes", in_hacspec)]
68 fn sub_mod(self, rhs: Self, n: Self) -> Self {
69 (self - rhs).signed_modulo(n)
70 }
71 #[cfg_attr(feature = "use_attributes", in_hacspec)]
73 fn add_mod(self, rhs: Self, n: Self) -> Self {
74 (self + rhs).signed_modulo(n)
75 }
76 #[cfg_attr(feature = "use_attributes", in_hacspec)]
78 fn mul_mod(self, rhs: Self, n: Self) -> Self {
79 (self * rhs).signed_modulo(n)
80 }
81 #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
83 fn pow_mod(self, exp: Self, n: Self) -> Self {
84 let r_big = BigInt::from(self).modpow(&BigInt::from(exp), &BigInt::from(n));
85 debug_assert!(r_big <= BigInt::from(Self::max_val()));
86 let r_string = r_big.to_string();
87 r_string.parse().unwrap()
88 }
89 #[cfg_attr(feature = "use_attributes", in_hacspec)]
91 fn modulo(self, n: Self) -> Self {
92 self % n
93 }
94 #[cfg_attr(feature = "use_attributes", in_hacspec)]
96 fn signed_modulo(self, n: Self) -> Self {
97 let mut ret = self.modulo(n);
98 while ret.less_than(Self::ZERO()) {
99 ret = ret + n;
100 }
101 ret
102 }
103 #[cfg_attr(feature = "use_attributes", in_hacspec)]
105 fn absolute(self) -> Self {
106 self.abs()
107 }
108 }
109 };
110}
111
112macro_rules! implement_public_mi {
114 ($t:ty,$bits:literal,$true_val:expr) => {
115 impl NumericCopy for $t {}
116 impl Integer for $t {
117 const NUM_BITS: usize = $bits;
118 #[inline]
119 #[cfg_attr(feature = "use_attributes", in_hacspec)]
120 fn ZERO() -> Self {
121 0
122 }
123 #[inline]
124 #[cfg_attr(feature = "use_attributes", in_hacspec)]
125 fn ONE() -> Self {
126 1
127 }
128 #[inline]
129 #[cfg_attr(feature = "use_attributes", in_hacspec)]
130 fn TWO() -> Self {
131 2
132 }
133
134 #[inline]
135 #[cfg_attr(feature = "use_attributes", in_hacspec)]
136 fn from_literal(val: u128) -> Self {
137 val as $t
138 }
139
140 #[inline]
141 #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
142 fn from_hex_string(s: &String) -> Self {
143 <$t>::from_str_radix(s.trim_start_matches("0x"), 16).unwrap()
144 }
145
146 #[inline]
148 #[cfg_attr(feature = "use_attributes", in_hacspec)]
149 fn get_bit(self, i: usize) -> Self {
150 (self >> i) & Self::ONE()
151 }
152
153 #[inline]
156 #[cfg_attr(feature = "use_attributes", in_hacspec)]
157 fn set_bit(self, b: Self, i: usize) -> Self {
158 debug_assert!(b.clone().equal(Self::ONE()) || b.clone().equal(Self::ZERO()));
159 let tmp1 = Self::from_literal(!(1 << i));
160 let tmp2 = b << i;
161 (self & tmp1) | tmp2
162 }
163
164 #[inline]
166 #[cfg_attr(feature = "use_attributes", in_hacspec)]
167 fn set(self, pos: usize, y: Self, yi: usize) -> Self {
168 let b = y.get_bit(yi);
169 self.set_bit(b, pos)
170 }
171
172 #[cfg_attr(feature = "use_attributes", in_hacspec)]
173 fn rotate_left(self, n: usize) -> Self {
174 assert!(n < Self::NUM_BITS);
176 (self.clone() << n) | (self >> ((-(n as i32) as usize) & (Self::NUM_BITS - 1)))
177 }
178
179 #[cfg_attr(feature = "use_attributes", in_hacspec)]
180 fn rotate_right(self, n: usize) -> Self {
181 assert!(n < Self::NUM_BITS);
183 (self.clone() >> n) | (self << ((-(n as i32) as usize) & (Self::NUM_BITS - 1)))
184 }
185 }
186 impl Numeric for $t {
187 #[cfg_attr(feature = "use_attributes", in_hacspec)]
189 fn max_val() -> Self {
190 <$t>::max_value()
191 }
192
193 #[cfg_attr(feature = "use_attributes", in_hacspec)]
194 fn wrap_add(self, rhs: Self) -> Self {
195 self.wrapping_add(rhs)
196 }
197 #[cfg_attr(feature = "use_attributes", in_hacspec)]
198 fn wrap_sub(self, rhs: Self) -> Self {
199 self.wrapping_sub(rhs)
200 }
201 #[cfg_attr(feature = "use_attributes", in_hacspec)]
202 fn wrap_mul(self, rhs: Self) -> Self {
203 self.wrapping_mul(rhs)
204 }
205 #[cfg_attr(feature = "use_attributes", in_hacspec)]
206 fn wrap_div(self, rhs: Self) -> Self {
207 self.wrapping_div(rhs)
208 }
209
210 #[cfg_attr(feature = "use_attributes", in_hacspec)]
212 fn exp(self, exp: u32) -> Self {
213 self.pow(exp)
214 }
215 #[cfg_attr(feature = "use_attributes", in_hacspec)]
218 fn pow_self(self, _exp: Self) -> Self {
219 unimplemented!();
220 }
221 #[cfg_attr(feature = "use_attributes", in_hacspec)]
223 fn divide(self, rhs: Self) -> Self {
224 self / rhs
225 }
226 #[cfg_attr(feature = "use_attributes", in_hacspec)]
228 fn inv(self, n: Self) -> Self {
229 extended_euclid_invert(self, n, false)
230 }
231
232 #[cfg_attr(feature = "use_attributes", in_hacspec)]
234 fn equal(self, other: Self) -> bool {
235 self == other
236 }
237 #[cfg_attr(feature = "use_attributes", in_hacspec)]
238 fn greater_than(self, other: Self) -> bool {
239 self > other
240 }
241 #[cfg_attr(feature = "use_attributes", in_hacspec)]
242 fn greater_than_or_equal(self, other: Self) -> bool {
243 self >= other
244 }
245 #[cfg_attr(feature = "use_attributes", in_hacspec)]
246 fn less_than(self, other: Self) -> bool {
247 self < other
248 }
249 #[cfg_attr(feature = "use_attributes", in_hacspec)]
250 fn less_than_or_equal(self, other: Self) -> bool {
251 self <= other
252 }
253
254 #[cfg_attr(feature = "use_attributes", in_hacspec)]
256 fn not_equal_bm(self, other: Self) -> Self {
257 if self != other {
258 $true_val
259 } else {
260 <$t>::default()
261 }
262 }
263 #[cfg_attr(feature = "use_attributes", in_hacspec)]
264 fn equal_bm(self, other: Self) -> Self {
265 if self == other {
266 $true_val
267 } else {
268 <$t>::default()
269 }
270 }
271 #[cfg_attr(feature = "use_attributes", in_hacspec)]
272 fn greater_than_bm(self, other: Self) -> Self {
273 if self > other {
274 $true_val
275 } else {
276 <$t>::default()
277 }
278 }
279 #[cfg_attr(feature = "use_attributes", in_hacspec)]
280 fn greater_than_or_equal_bm(self, other: Self) -> Self {
281 if self >= other {
282 $true_val
283 } else {
284 <$t>::default()
285 }
286 }
287 #[cfg_attr(feature = "use_attributes", in_hacspec)]
288 fn less_than_bm(self, other: Self) -> Self {
289 if self < other {
290 $true_val
291 } else {
292 <$t>::default()
293 }
294 }
295 #[cfg_attr(feature = "use_attributes", in_hacspec)]
296 fn less_than_or_equal_bm(self, other: Self) -> Self {
297 if self <= other {
298 $true_val
299 } else {
300 <$t>::default()
301 }
302 }
303 }
304 };
305}
306
307implement_public_unsigned_mi!(u8, 8);
308implement_public_unsigned_mi!(u16, 16);
309implement_public_unsigned_mi!(u32, 32);
310implement_public_unsigned_mi!(u64, 64);
311implement_public_unsigned_mi!(u128, 128);
312
313implement_public_signed_mi!(i8, 8);
314implement_public_signed_mi!(i16, 16);
315implement_public_signed_mi!(i32, 32);
316implement_public_signed_mi!(i64, 64);
317implement_public_signed_mi!(i128, 128);
318
319macro_rules! implement_secret_unsigned_mi {
322 ($t:ident,$base:ty,$bits:literal) => {
323 implement_secret_mi!($t, $base, $bits);
324 impl ModNumeric for $t {
325 #[cfg_attr(feature = "use_attributes", in_hacspec)]
327 fn sub_mod(self, rhs: Self, n: Self) -> Self {
328 (self - rhs).modulo(n)
329 }
330 #[cfg_attr(feature = "use_attributes", in_hacspec)]
332 fn add_mod(self, rhs: Self, n: Self) -> Self {
333 (self + rhs).modulo(n)
334 }
335 #[cfg_attr(feature = "use_attributes", in_hacspec)]
337 fn mul_mod(self, rhs: Self, n: Self) -> Self {
338 (self * rhs).modulo(n)
339 }
340 #[cfg_attr(feature = "use_attributes", in_hacspec)]
343 fn pow_mod(self, _exp: Self, _n: Self) -> Self {
344 unimplemented!();
345 }
346 #[cfg_attr(feature = "use_attributes", in_hacspec)]
348 fn modulo(self, n: Self) -> Self {
349 ct_div(self, n).1
350 }
351 #[cfg_attr(feature = "use_attributes", in_hacspec)]
353 fn signed_modulo(self, n: Self) -> Self {
354 self.modulo(n)
355 }
356 #[cfg_attr(feature = "use_attributes", in_hacspec)]
358 fn absolute(self) -> Self {
359 self
360 }
361 }
362 };
363}
364
365macro_rules! implement_secret_signed_mi {
366 ($t:ident,$base:ty,$bits:literal) => {
367 implement_secret_mi!($t, $base, $bits);
368 impl ModNumeric for $t {
369 #[cfg_attr(feature = "use_attributes", in_hacspec)]
371 fn sub_mod(self, rhs: Self, n: Self) -> Self {
372 (self - rhs).signed_modulo(n)
373 }
374 #[cfg_attr(feature = "use_attributes", in_hacspec)]
376 fn add_mod(self, rhs: Self, n: Self) -> Self {
377 (self + rhs).signed_modulo(n)
378 }
379 #[cfg_attr(feature = "use_attributes", in_hacspec)]
381 fn mul_mod(self, rhs: Self, n: Self) -> Self {
382 (self * rhs).signed_modulo(n)
383 }
384 #[cfg_attr(feature = "use_attributes", in_hacspec)]
387 fn pow_mod(self, _exp: Self, _n: Self) -> Self {
388 unimplemented!();
389 }
390 #[cfg_attr(feature = "use_attributes", in_hacspec)]
392 fn modulo(self, n: Self) -> Self {
393 ct_div(self, n).1
394 }
395 #[cfg_attr(feature = "use_attributes", in_hacspec)]
398 fn signed_modulo(self, n: Self) -> Self {
399 let mut ret = self.modulo(n);
400 while ret.less_than(Self::ZERO()) {
401 ret = ret + n;
402 }
403 ret
404 }
405 #[cfg_attr(feature = "use_attributes", in_hacspec)]
408 fn absolute(self) -> Self {
409 Self(self.declassify().abs())
410 }
411 }
412 };
413}
414
415macro_rules! implement_secret_mi {
417 ($t:ident,$base:ty,$bits:literal) => {
418 impl NumericCopy for $t {}
419 impl Integer for $t {
420 const NUM_BITS: usize = $bits;
421
422 #[inline]
423 #[cfg_attr(feature = "use_attributes", in_hacspec)]
424 fn ZERO() -> Self {
425 $t(0)
426 }
427 #[inline]
428 #[cfg_attr(feature = "use_attributes", in_hacspec)]
429 fn ONE() -> Self {
430 $t(1)
431 }
432 #[inline]
433 #[cfg_attr(feature = "use_attributes", in_hacspec)]
434 fn TWO() -> Self {
435 $t(2)
436 }
437
438 #[inline]
439 #[cfg_attr(feature = "use_attributes", in_hacspec)]
440 fn from_literal(val: u128) -> Self {
441 Self::classify(val as $base)
442 }
443
444 #[inline]
445 #[cfg_attr(feature = "use_attributes", unsafe_hacspec)]
446 fn from_hex_string(s: &String) -> Self {
447 Self::classify(<$base>::from_str_radix(s.trim_start_matches("0x"), 16).unwrap())
448 }
449
450 #[inline]
452 #[cfg_attr(feature = "use_attributes", in_hacspec)]
453 fn get_bit(self, i: usize) -> Self {
454 (self >> i) & Self::ONE()
455 }
456
457 #[inline]
460 #[cfg_attr(feature = "use_attributes", in_hacspec)]
461 fn set_bit(self, b: Self, i: usize) -> Self {
462 debug_assert!(b.clone().equal(Self::ONE()) || b.clone().equal(Self::ZERO()));
463 let tmp1 = Self::from_literal(!(1 << i));
464 let tmp2 = b << i;
465 (self & tmp1) | tmp2
466 }
467
468 #[inline]
470 #[cfg_attr(feature = "use_attributes", in_hacspec)]
471 fn set(self, pos: usize, y: Self, yi: usize) -> Self {
472 let b = y.get_bit(yi);
473 self.set_bit(b, pos)
474 }
475
476 #[cfg_attr(feature = "use_attributes", in_hacspec)]
477 fn rotate_left(self, n: usize) -> Self {
478 assert!(n < Self::NUM_BITS);
480 (self.clone() << n) | (self >> ((-(n as i32) as usize) & (Self::NUM_BITS - 1)))
481 }
482
483 #[cfg_attr(feature = "use_attributes", in_hacspec)]
484 fn rotate_right(self, n: usize) -> Self {
485 assert!(n < Self::NUM_BITS);
487 (self.clone() >> n) | (self << ((-(n as i32) as usize) & (Self::NUM_BITS - 1)))
488 }
489 }
490 impl Numeric for $t {
491 #[cfg_attr(feature = "use_attributes", in_hacspec)]
493 fn max_val() -> Self {
494 Self::from(<$base>::max_value())
495 }
496
497 #[cfg_attr(feature = "use_attributes", in_hacspec)]
498 fn wrap_add(self, rhs: Self) -> Self {
499 self + rhs
500 }
501 #[cfg_attr(feature = "use_attributes", in_hacspec)]
502 fn wrap_sub(self, rhs: Self) -> Self {
503 self - rhs
504 }
505 #[cfg_attr(feature = "use_attributes", in_hacspec)]
506 fn wrap_mul(self, rhs: Self) -> Self {
507 self * rhs
508 }
509 #[cfg_attr(feature = "use_attributes", in_hacspec)]
510 fn wrap_div(self, _rhs: Self) -> Self {
511 unimplemented!();
512 }
513
514 #[cfg_attr(feature = "use_attributes", in_hacspec)]
517 fn exp(self, exp: u32) -> Self {
518 let mut s = self;
519 if exp == 0 {
520 return <$t>::from(1 as $base);
521 } else {
522 for _ in 1..exp {
523 s = s * self
524 }
525 }
526 Self::from(s)
527 }
528 #[cfg_attr(feature = "use_attributes", in_hacspec)]
532 fn pow_self(self, _exp: Self) -> Self {
533 unimplemented!();
534 }
535 #[cfg_attr(feature = "use_attributes", in_hacspec)]
537 fn divide(self, rhs: Self) -> Self {
538 ct_div(self, rhs).0
539 }
540 #[cfg_attr(feature = "use_attributes", in_hacspec)]
543 fn inv(self, n: Self) -> Self {
544 extended_euclid_invert(self, n, false)
545 }
546
547 #[cfg_attr(feature = "use_attributes", in_hacspec)]
550 fn equal(self, other: Self) -> bool {
551 self.equal_bm(other).declassify() != 0
552 }
553 #[cfg_attr(feature = "use_attributes", in_hacspec)]
555 fn greater_than(self, other: Self) -> bool {
556 self.greater_than_bm(other).declassify() != 0
557 }
558 #[cfg_attr(feature = "use_attributes", in_hacspec)]
560 fn greater_than_or_equal(self, other: Self) -> bool {
561 self.greater_than_or_equal_bm(other).declassify() != 0
562 }
563 #[cfg_attr(feature = "use_attributes", in_hacspec)]
565 fn less_than(self, other: Self) -> bool {
566 self.less_than_bm(other).declassify() != 0
567 }
568 #[cfg_attr(feature = "use_attributes", in_hacspec)]
570 fn less_than_or_equal(self, other: Self) -> bool {
571 self.less_than_or_equal_bm(other).declassify() != 0
572 }
573
574 #[cfg_attr(feature = "use_attributes", in_hacspec)]
576 fn not_equal_bm(self, other: Self) -> Self {
577 self.comp_ne(other)
578 }
579 #[cfg_attr(feature = "use_attributes", in_hacspec)]
580 fn equal_bm(self, other: Self) -> Self {
581 self.comp_eq(other)
582 }
583 #[cfg_attr(feature = "use_attributes", in_hacspec)]
584 fn greater_than_bm(self, other: Self) -> Self {
585 self.comp_gt(other)
586 }
587 #[cfg_attr(feature = "use_attributes", in_hacspec)]
588 fn greater_than_or_equal_bm(self, other: Self) -> Self {
589 self.comp_gte(other)
590 }
591 #[cfg_attr(feature = "use_attributes", in_hacspec)]
592 fn less_than_bm(self, other: Self) -> Self {
593 self.comp_lt(other)
594 }
595 #[cfg_attr(feature = "use_attributes", in_hacspec)]
596 fn less_than_or_equal_bm(self, other: Self) -> Self {
597 self.comp_lte(other)
598 }
599 }
600 };
601}
602
603implement_secret_unsigned_mi!(U8, u8, 8);
604implement_secret_unsigned_mi!(U16, u16, 16);
605implement_secret_unsigned_mi!(U32, u32, 32);
606implement_secret_unsigned_mi!(U64, u64, 64);
607implement_secret_unsigned_mi!(U128, u128, 128);
608
609implement_secret_signed_mi!(I8, i8, 8);
610implement_secret_signed_mi!(I16, i16, 16);
611implement_secret_signed_mi!(I32, i32, 32);
612implement_secret_signed_mi!(I64, i64, 64);
613implement_secret_signed_mi!(I128, i128, 128);
614
615impl UnsignedPublicInteger for u8 {
616 #[cfg_attr(feature = "use_attributes", in_hacspec)]
617 fn to_le_bytes(self) -> Seq<u8> {
618 let mut x = Seq::new(1);
619 x[0] = self;
620 x
621 }
622 #[cfg_attr(feature = "use_attributes", in_hacspec)]
623 fn to_be_bytes(self) -> Seq<u8> {
624 let mut x = Seq::new(1);
625 x[0] = self;
626 x
627 }
628 #[cfg_attr(feature = "use_attributes", in_hacspec)]
629 fn from_le_bytes(x: &Seq<u8>) -> Self {
630 assert!(x.len() == 1);
631 x[0]
632 }
633 #[cfg_attr(feature = "use_attributes", in_hacspec)]
634 fn from_be_bytes(x: &Seq<u8>) -> Self {
635 assert!(x.len() == 1);
636 x[0]
637 }
638}
639
640impl UnsignedPublicInteger for u16 {
641 #[cfg_attr(feature = "use_attributes", in_hacspec)]
642 fn to_le_bytes(self) -> Seq<u8> {
643 Seq::from_seq(&u16_to_le_bytes(self))
644 }
645 #[cfg_attr(feature = "use_attributes", in_hacspec)]
646 fn to_be_bytes(self) -> Seq<u8> {
647 Seq::from_seq(&u16_to_be_bytes(self))
648 }
649 #[cfg_attr(feature = "use_attributes", in_hacspec)]
650 fn from_le_bytes(x: &Seq<u8>) -> Self {
651 u16_from_le_bytes(u16Word::from_seq(x))
652 }
653 #[cfg_attr(feature = "use_attributes", in_hacspec)]
654 fn from_be_bytes(x: &Seq<u8>) -> Self {
655 u16_from_be_bytes(u16Word::from_seq(x))
656 }
657}
658
659impl UnsignedPublicInteger for u32 {
660 #[cfg_attr(feature = "use_attributes", in_hacspec)]
661 fn to_le_bytes(self) -> Seq<u8> {
662 Seq::from_seq(&u32_to_le_bytes(self))
663 }
664 #[cfg_attr(feature = "use_attributes", in_hacspec)]
665 fn to_be_bytes(self) -> Seq<u8> {
666 Seq::from_seq(&u32_to_be_bytes(self))
667 }
668 #[cfg_attr(feature = "use_attributes", in_hacspec)]
669 fn from_le_bytes(x: &Seq<u8>) -> Self {
670 u32_from_le_bytes(u32Word::from_seq(x))
671 }
672 #[cfg_attr(feature = "use_attributes", in_hacspec)]
673 fn from_be_bytes(x: &Seq<u8>) -> Self {
674 u32_from_be_bytes(u32Word::from_seq(x))
675 }
676}
677
678impl UnsignedPublicInteger for u64 {
679 #[cfg_attr(feature = "use_attributes", in_hacspec)]
680 fn to_le_bytes(self) -> Seq<u8> {
681 Seq::from_seq(&u64_to_le_bytes(self))
682 }
683 #[cfg_attr(feature = "use_attributes", in_hacspec)]
684 fn to_be_bytes(self) -> Seq<u8> {
685 Seq::from_seq(&u64_to_be_bytes(self))
686 }
687 #[cfg_attr(feature = "use_attributes", in_hacspec)]
688 fn from_le_bytes(x: &Seq<u8>) -> Self {
689 u64_from_le_bytes(u64Word::from_seq(x))
690 }
691 #[cfg_attr(feature = "use_attributes", in_hacspec)]
692 fn from_be_bytes(x: &Seq<u8>) -> Self {
693 u64_from_be_bytes(u64Word::from_seq(x))
694 }
695}
696
697impl UnsignedPublicInteger for u128 {
698 #[cfg_attr(feature = "use_attributes", in_hacspec)]
699 fn to_le_bytes(self) -> Seq<u8> {
700 Seq::from_seq(&u128_to_le_bytes(self))
701 }
702 #[cfg_attr(feature = "use_attributes", in_hacspec)]
703 fn to_be_bytes(self) -> Seq<u8> {
704 Seq::from_seq(&u128_to_be_bytes(self))
705 }
706 #[cfg_attr(feature = "use_attributes", in_hacspec)]
707 fn from_le_bytes(x: &Seq<u8>) -> Self {
708 u128_from_le_bytes(u128Word::from_seq(x))
709 }
710 #[cfg_attr(feature = "use_attributes", in_hacspec)]
711 fn from_be_bytes(x: &Seq<u8>) -> Self {
712 u128_from_be_bytes(u128Word::from_seq(x))
713 }
714}
715
716impl UnsignedInteger for U8 {}
717impl UnsignedInteger for U16 {}
718impl UnsignedInteger for U32 {}
719impl UnsignedInteger for U64 {}
720impl UnsignedInteger for U128 {}
721impl UnsignedInteger for u8 {}
722impl UnsignedInteger for u16 {}
723impl UnsignedInteger for u32 {}
724impl UnsignedInteger for u64 {}
725impl UnsignedInteger for u128 {}
726
727impl UnsignedIntegerCopy for U8 {}
728impl UnsignedIntegerCopy for U16 {}
729impl UnsignedIntegerCopy for U32 {}
730impl UnsignedIntegerCopy for U64 {}
731impl UnsignedIntegerCopy for U128 {}
732impl UnsignedIntegerCopy for u8 {}
733impl UnsignedIntegerCopy for u16 {}
734impl UnsignedIntegerCopy for u32 {}
735impl UnsignedIntegerCopy for u64 {}
736impl UnsignedIntegerCopy for u128 {}
737
738impl UnsignedSecretIntegerCopy for U8 {}
739impl UnsignedSecretIntegerCopy for U16 {}
740impl UnsignedSecretIntegerCopy for U32 {}
741impl UnsignedSecretIntegerCopy for U64 {}
742impl UnsignedSecretIntegerCopy for U128 {}
743impl UnsignedPublicIntegerCopy for u8 {}
744impl UnsignedPublicIntegerCopy for u16 {}
745impl UnsignedPublicIntegerCopy for u32 {}
746impl UnsignedPublicIntegerCopy for u64 {}
747impl UnsignedPublicIntegerCopy for u128 {}
748
749impl SignedInteger for I8 {}
750impl SignedInteger for I16 {}
751impl SignedInteger for I32 {}
752impl SignedInteger for I64 {}
753impl SignedInteger for I128 {}
754impl SignedInteger for i8 {}
755impl SignedInteger for i16 {}
756impl SignedInteger for i32 {}
757impl SignedInteger for i64 {}
758impl SignedInteger for i128 {}
759
760impl SignedIntegerCopy for I8 {}
761impl SignedIntegerCopy for I16 {}
762impl SignedIntegerCopy for I32 {}
763impl SignedIntegerCopy for I64 {}
764impl SignedIntegerCopy for I128 {}
765impl SignedIntegerCopy for i8 {}
766impl SignedIntegerCopy for i16 {}
767impl SignedIntegerCopy for i32 {}
768impl SignedIntegerCopy for i64 {}
769impl SignedIntegerCopy for i128 {}
770
771impl SecretIntegerCopy for I8 {
772 type PublicVersionCopy = i8;
773 fn classify(x: Self::PublicVersionCopy) -> Self {
774 Self(x)
775 }
776}
777impl SecretIntegerCopy for I16 {
778 type PublicVersionCopy = i16;
779 fn classify(x: Self::PublicVersionCopy) -> Self {
780 Self(x)
781 }
782}
783impl SecretIntegerCopy for I32 {
784 type PublicVersionCopy = i32;
785 fn classify(x: Self::PublicVersionCopy) -> Self {
786 Self(x)
787 }
788}
789impl SecretIntegerCopy for I64 {
790 type PublicVersionCopy = i64;
791 fn classify(x: Self::PublicVersionCopy) -> Self {
792 Self(x)
793 }
794}
795impl SecretIntegerCopy for I128 {
796 type PublicVersionCopy = i128;
797 fn classify(x: Self::PublicVersionCopy) -> Self {
798 Self(x)
799 }
800}
801
802impl SecretIntegerCopy for U8 {
803 type PublicVersionCopy = u8;
804 fn classify(x: Self::PublicVersionCopy) -> Self {
805 Self(x)
806 }
807}
808impl SecretIntegerCopy for U16 {
809 type PublicVersionCopy = u16;
810 fn classify(x: Self::PublicVersionCopy) -> Self {
811 Self(x)
812 }
813}
814impl SecretIntegerCopy for U32 {
815 type PublicVersionCopy = u32;
816 fn classify(x: Self::PublicVersionCopy) -> Self {
817 Self(x)
818 }
819}
820impl SecretIntegerCopy for U64 {
821 type PublicVersionCopy = u64;
822 fn classify(x: Self::PublicVersionCopy) -> Self {
823 Self(x)
824 }
825}
826impl SecretIntegerCopy for U128 {
827 type PublicVersionCopy = u128;
828 fn classify(x: Self::PublicVersionCopy) -> Self {
829 Self(x)
830 }
831}
832
833impl PublicIntegerCopy for i8 {
834 type SecretVersionCopy = I8;
835}
836impl PublicIntegerCopy for i16 {
837 type SecretVersionCopy = I16;
838}
839impl PublicIntegerCopy for i32 {
840 type SecretVersionCopy = I32;
841}
842impl PublicIntegerCopy for i64 {
843 type SecretVersionCopy = I64;
844}
845impl PublicIntegerCopy for i128 {
846 type SecretVersionCopy = I128;
847}
848
849impl PublicIntegerCopy for u8 {
850 type SecretVersionCopy = U8;
851}
852impl PublicIntegerCopy for u16 {
853 type SecretVersionCopy = U16;
854}
855impl PublicIntegerCopy for u32 {
856 type SecretVersionCopy = u32;
857}
858impl PublicIntegerCopy for u64 {
859 type SecretVersionCopy = I64;
860}
861impl PublicIntegerCopy for u128 {
862 type SecretVersionCopy = I128;
863}
864
865impl PublicInteger for u8 {
866 type SecretVersion = U8;
867}
868impl PublicInteger for u16 {
869 type SecretVersion = U16;
870}
871impl PublicInteger for u32 {
872 type SecretVersion = U32;
873}
874impl PublicInteger for u64 {
875 type SecretVersion = U64;
876}
877impl PublicInteger for u128 {
878 type SecretVersion = U128;
879}
880impl PublicInteger for i8 {
881 type SecretVersion = I8;
882}
883impl PublicInteger for i16 {
884 type SecretVersion = I16;
885}
886impl PublicInteger for i32 {
887 type SecretVersion = I32;
888}
889impl PublicInteger for i64 {
890 type SecretVersion = I64;
891}
892impl PublicInteger for i128 {
893 type SecretVersion = I128;
894}
895
896impl SecretInteger for U8 {
897 type PublicVersion = u8;
898 #[cfg_attr(feature = "use_attributes", in_hacspec)]
899 fn classify(x: Self::PublicVersion) -> Self {
900 U8(x)
901 }
902}
903impl SecretInteger for U16 {
904 type PublicVersion = u16;
905 #[cfg_attr(feature = "use_attributes", in_hacspec)]
906 fn classify(x: Self::PublicVersion) -> Self {
907 U16(x)
908 }
909}
910impl SecretInteger for U32 {
911 type PublicVersion = u32;
912 #[cfg_attr(feature = "use_attributes", in_hacspec)]
913 fn classify(x: Self::PublicVersion) -> Self {
914 U32(x)
915 }
916}
917impl SecretInteger for U64 {
918 type PublicVersion = u64;
919 #[cfg_attr(feature = "use_attributes", in_hacspec)]
920 fn classify(x: Self::PublicVersion) -> Self {
921 U64(x)
922 }
923}
924impl SecretInteger for U128 {
925 type PublicVersion = u128;
926 #[cfg_attr(feature = "use_attributes", in_hacspec)]
927 fn classify(x: Self::PublicVersion) -> Self {
928 U128(x)
929 }
930}
931impl SecretInteger for I8 {
932 type PublicVersion = i8;
933 #[cfg_attr(feature = "use_attributes", in_hacspec)]
934 fn classify(x: Self::PublicVersion) -> Self {
935 I8(x)
936 }
937}
938impl SecretInteger for I16 {
939 type PublicVersion = i16;
940 #[cfg_attr(feature = "use_attributes", in_hacspec)]
941 fn classify(x: Self::PublicVersion) -> Self {
942 I16(x)
943 }
944}
945impl SecretInteger for I32 {
946 type PublicVersion = i32;
947 #[cfg_attr(feature = "use_attributes", in_hacspec)]
948 fn classify(x: Self::PublicVersion) -> Self {
949 I32(x)
950 }
951}
952impl SecretInteger for I64 {
953 type PublicVersion = i64;
954 #[cfg_attr(feature = "use_attributes", in_hacspec)]
955 fn classify(x: Self::PublicVersion) -> Self {
956 I64(x)
957 }
958}
959impl SecretInteger for I128 {
960 type PublicVersion = i128;
961 #[cfg_attr(feature = "use_attributes", in_hacspec)]
962 fn classify(x: Self::PublicVersion) -> Self {
963 I128(x)
964 }
965}
966
967impl UnsignedSecretInteger for U8 {
968 #[cfg_attr(feature = "use_attributes", in_hacspec)]
969 fn to_le_bytes(self) -> Seq<U8> {
970 let mut x = Seq::new(1);
971 x[0] = self;
972 x
973 }
974 #[cfg_attr(feature = "use_attributes", in_hacspec)]
975 fn to_be_bytes(self) -> Seq<U8> {
976 let mut x = Seq::new(1);
977 x[0] = self;
978 x
979 }
980 #[cfg_attr(feature = "use_attributes", in_hacspec)]
981 fn from_le_bytes(x: &Seq<U8>) -> Self {
982 assert!(x.len() == 1);
983 x[0]
984 }
985 #[cfg_attr(feature = "use_attributes", in_hacspec)]
986 fn from_be_bytes(x: &Seq<U8>) -> Self {
987 assert!(x.len() == 1);
988 x[0]
989 }
990}
991
992impl UnsignedSecretInteger for U16 {
993 #[cfg_attr(feature = "use_attributes", in_hacspec)]
994 fn to_le_bytes(self) -> Seq<U8> {
995 Seq::from_seq(&U16_to_le_bytes(self))
996 }
997 #[cfg_attr(feature = "use_attributes", in_hacspec)]
998 fn to_be_bytes(self) -> Seq<U8> {
999 Seq::from_seq(&U16_to_be_bytes(self))
1000 }
1001 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1002 fn from_le_bytes(x: &Seq<U8>) -> Self {
1003 U16_from_le_bytes(U16Word::from_seq(x))
1004 }
1005 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1006 fn from_be_bytes(x: &Seq<U8>) -> Self {
1007 U16_from_be_bytes(U16Word::from_seq(x))
1008 }
1009}
1010
1011impl UnsignedSecretInteger for U32 {
1012 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1013 fn to_le_bytes(self) -> Seq<U8> {
1014 Seq::from_seq(&U32_to_le_bytes(self))
1015 }
1016 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1017 fn to_be_bytes(self) -> Seq<U8> {
1018 Seq::from_seq(&U32_to_be_bytes(self))
1019 }
1020 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1021 fn from_le_bytes(x: &Seq<U8>) -> Self {
1022 U32_from_le_bytes(U32Word::from_seq(x))
1023 }
1024 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1025 fn from_be_bytes(x: &Seq<U8>) -> Self {
1026 U32_from_be_bytes(U32Word::from_seq(x))
1027 }
1028}
1029
1030impl UnsignedSecretInteger for U64 {
1031 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1032 fn to_le_bytes(self) -> Seq<U8> {
1033 Seq::from_seq(&U64_to_le_bytes(self))
1034 }
1035 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1036 fn to_be_bytes(self) -> Seq<U8> {
1037 Seq::from_seq(&U64_to_be_bytes(self))
1038 }
1039 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1040 fn from_le_bytes(x: &Seq<U8>) -> Self {
1041 U64_from_le_bytes(U64Word::from_seq(x))
1042 }
1043 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1044 fn from_be_bytes(x: &Seq<U8>) -> Self {
1045 U64_from_be_bytes(U64Word::from_seq(x))
1046 }
1047}
1048
1049impl UnsignedSecretInteger for U128 {
1050 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1051 fn to_le_bytes(self) -> Seq<U8> {
1052 Seq::from_seq(&U128_to_le_bytes(self))
1053 }
1054 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1055 fn to_be_bytes(self) -> Seq<U8> {
1056 Seq::from_seq(&U128_to_be_bytes(self))
1057 }
1058 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1059 fn from_le_bytes(x: &Seq<U8>) -> Self {
1060 U128_from_le_bytes(U128Word::from_seq(x))
1061 }
1062 #[cfg_attr(feature = "use_attributes", in_hacspec)]
1063 fn from_be_bytes(x: &Seq<U8>) -> Self {
1064 U128_from_be_bytes(U128Word::from_seq(x))
1065 }
1066}