ark_ff_macros/montgomery/
biginteger.rs1use quote::quote;
2
3pub(super) fn add_with_carry_impl(num_limbs: usize) -> proc_macro2::TokenStream {
4 let mut body = proc_macro2::TokenStream::new();
5 body.extend(quote! {
6 use ark_ff::biginteger::arithmetic::adc_for_add_with_carry as adc;
7 let mut carry = 0;
8 });
9 for i in 0..num_limbs {
10 body.extend(quote! {
11 carry = adc(&mut a.0[#i], b.0[#i], carry);
12 });
13 }
14 body.extend(quote! {
15 carry != 0
16 });
17 quote! {
18 #[inline(always)]
19 fn __add_with_carry(
20 a: &mut B,
21 b: & B,
22 ) -> bool {
23 #body
24 }
25 }
26}
27
28pub(super) fn sub_with_borrow_impl(num_limbs: usize) -> proc_macro2::TokenStream {
29 let mut body = proc_macro2::TokenStream::new();
30 body.extend(quote! {
31 use ark_ff::biginteger::arithmetic::sbb_for_sub_with_borrow as sbb;
32 let mut borrow = 0;
33 });
34 for i in 0..num_limbs {
35 body.extend(quote! {
36 borrow = sbb(&mut a.0[#i], b.0[#i], borrow);
37 });
38 }
39 body.extend(quote! {
40 borrow != 0
41 });
42 quote! {
43 #[inline(always)]
44 fn __sub_with_borrow(
45 a: &mut B,
46 b: & B,
47 ) -> bool {
48 #body
49 }
50 }
51}
52
53pub(super) fn subtract_modulus_impl(
54 modulus: &proc_macro2::TokenStream,
55) -> proc_macro2::TokenStream {
56 quote! {
57 #[inline(always)]
58 fn __subtract_modulus(a: &mut F) {
59 if a.is_geq_modulus() {
60 __sub_with_borrow(&mut a.0, &#modulus);
61 }
62 }
63
64 #[inline(always)]
65 fn __subtract_modulus_with_carry(a: &mut F, carry: bool) {
66 if a.is_geq_modulus() || carry {
67 __sub_with_borrow(&mut a.0, &#modulus);
68 }
69 }
70 }
71}