ark_feanor/
lib.rs

1//! # ark-feanor
2//! 
3//! A bridge between arkworks' finite field types and feanor-math's ring system.
4//! 
5//! This library enables using feanor-math's advanced polynomial and Gröbner basis
6//! algorithms with arkworks' cryptographic field implementations, particularly for
7//! curves like BLS12-381 and BN254.
8//! 
9//! ## Features
10//! 
11//! - **Zero-cost abstraction**: Minimal overhead when wrapping arkworks fields
12//! - **Full ring support**: Implements all necessary feanor-math traits
13//! - **Prime field specialization**: Additional functionality for prime fields including division
14//! - **Common fields**: Pre-configured types for BN254 and BLS12-381
15//! - **Polynomial support**: Create and manipulate polynomials over cryptographic fields
16//! - **Gröbner basis**: Compute Gröbner bases for polynomial systems
17//! 
18//! ## Quick Start
19//! 
20//! ```ignore
21//! use ark_feanor::*;
22//! use ark_bn254::Fr;
23//! 
24//! // Method 1: Use pre-configured field
25//! let field = &*BN254_FR;
26//! let a = field.int_hom().map(5);
27//! let b = field.int_hom().map(3);
28//! let c = field.add(a, b);
29//! 
30//! // Method 2: Create your own wrapper
31//! let my_field = ArkFieldWrapper::<Fr>::new();
32//! let x = my_field.from_int(10);
33//! let y = my_field.from_int(20);
34//! let z = my_field.mul_ref(&x, &y);
35//! ```
36//! 
37//! ## Polynomial Example
38//! 
39//! ```ignore
40//! use ark_feanor::*;
41//! use feanor_math::rings::multivariate::*;
42//! 
43//! // Create polynomial ring BLS12-381_Fr[x, y]
44//! let field = &*BLS12_381_FR;
45//! let poly_ring = MultivariatePolyRingImpl::new(field, 2);
46//! 
47//! // Create polynomials: x² + y² - 1 and xy - 2
48//! let polys = poly_ring.with_wrapped_indeterminates(|[x, y]| {
49//!     vec![
50//!         x.clone().pow(2) + y.clone().pow(2) - 1,
51//!         x * y - 2,
52//!     ]
53//! });
54//! ```
55//! 
56//! ## Gröbner Basis Computation with F4
57//!
58//! ark-feanor implements the F4 algorithm for computing Gröbner bases.
59//! F4 processes S-polynomials in batches via matrix reduction, making it efficient
60//! for dense systems over cryptographic fields.
61//!
62//! ```ignore
63//! use ark_feanor::*;
64//! use ark_feanor::f4::*;
65//!
66//! let field = &*BN254_FR;
67//! let poly_ring = MultivariatePolyRingImpl::new(field, 2);
68//!
69//! let system = vec![/* your polynomials */];
70//!
71//! // Compute Gröbner basis with F4
72//! let gb = f4_simple(&poly_ring, system, DegRevLex);
73//! ```
74//!
75//! ### With Configuration Options
76//!
77//! ```ignore
78//! use ark_feanor::*;
79//! use ark_feanor::f4::*;
80//!
81//! let field = &*BN254_FR;
82//! let poly_ring = MultivariatePolyRingImpl::new(field, 2);
83//!
84//! // Configure with degree limit to avoid explosion
85//! let config = F4Config::new()
86//!     .with_max_degree(10);  // Abort if degrees exceed 10
87//!
88//! let system = vec![/* your polynomials */];
89//!
90//! // Compute with configuration
91//! match f4_configured(&poly_ring, system, DegRevLex, config) {
92//!     Ok(gb) => println!("Computed GB with {} elements", gb.len()),
93//!     Err(GBAborted::DegreeExceeded { max_degree, actual_degree }) => {
94//!         println!("Aborted: degree {} exceeded limit {}", actual_degree, max_degree);
95//!     }
96//!     Err(e) => println!("Error: {:?}", e),
97//! }
98//! ```
99//! 
100//! ## Type Aliases
101//! 
102//! For convenience, this library provides type aliases for common fields:
103//! 
104//! - `BN254ScalarField` / `BN254_FR`: The BN254 scalar field (Fr)
105//! - `BN254BaseField` / `BN254_FQ`: The BN254 base field (Fq)
106//! - `BLS12_381ScalarField` / `BLS12_381_FR`: The BLS12-381 scalar field (Fr)
107//! - `BLS12_381BaseField` / `BLS12_381_FQ`: The BLS12-381 base field (Fq)
108//! 
109//! ## Performance
110//! 
111//! The wrapper is designed to have minimal overhead. Field operations through
112//! `ArkFieldWrapper` should be within 5% of direct arkworks operations.
113//! 
114//! ## Limitations
115//! 
116//! - Extension fields (Fq2, Fq6, Fq12) are not yet optimally supported
117//! - Characteristic extraction only works for `PrimeField` types
118//! - Some advanced feanor-math features may not be available for all field types
119
120#![doc(html_root_url = "https://docs.rs/ark-feanor/0.1.0")]
121#![warn(missing_docs)]
122#![warn(rust_2018_idioms)]
123#![cfg_attr(test, feature(allocator_api))]
124
125// Core modules
126pub mod field_wrapper;
127pub mod prime_field;
128pub mod homomorphisms;
129pub mod conversions;
130pub mod common_fields;
131
132// F4 algorithm for Gröbner bases
133pub mod f4;
134
135// Re-export main types
136pub use field_wrapper::ArkFieldWrapper;
137pub use common_fields::*;
138pub use conversions::{
139    extract_characteristic,
140    biguint_to_field,
141    bigint_to_field,
142    i64_to_field,
143    field_to_biguint,
144    field_to_small_int,
145};
146pub use prime_field::FieldProperties;
147
148// Re-export useful feanor-math types for convenience
149pub use feanor_math::{
150    ring::*,
151    divisibility::*,
152    pid::*,
153    rings::{
154        multivariate::{
155            multivariate_impl::MultivariatePolyRingImpl,
156            MultivariatePolyRingStore,
157            DegRevLex,
158            Lex,
159        },
160        poly::{
161            PolyRingStore,
162            dense_poly::DensePolyRing,
163        },
164    },
165};
166
167// Re-export F4 types
168pub use f4::GBAborted;
169
170/// Version information
171pub const VERSION: &str = env!("CARGO_PKG_VERSION");
172
173/// Library name
174pub const NAME: &str = env!("CARGO_PKG_NAME");
175
176#[cfg(test)]
177mod tests {
178    use super::*;
179    use ark_bn254::Fr;
180    
181    #[test]
182    fn test_library_basics() {
183        // Ensure the library exports work
184        let field = ArkFieldWrapper::<Fr>::new();
185        let a = field.from_int(5);
186        let b = field.from_int(10);
187        let c = field.add_ref(&a, &b);
188        let expected = field.from_int(15);
189        assert!(field.eq_el(&c, &expected));
190    }
191    
192    #[test]
193    fn test_version_info() {
194        assert_eq!(NAME, "ark-feanor");
195        assert!(!VERSION.is_empty());
196    }
197}