ark_nonnative_field/lib.rs
1//! This library provides the non-native field gadget for the `arkworks` constraint-writing platform.
2//! The non-native field gadget can be used as a standard `FieldVar`, given
3//! reasonable non-native gadget parameters.
4//!
5//! This file contains the implementation of three structs:
6//!
7//! - `NonNativeFieldParams` specifies the constraint prime field (called `BaseField`),
8//! the simulated prime field (called `TargetField`), and internal parameters
9//! searched by the Python script (see `README.md`).
10//! - `NonNativeFieldVar` implements the `FieldVar` for simulating `TargetField`
11//! arithmetic within `BaseField`.
12//! - `NonNativeFieldMulResultVar` is an intermediate representations of the
13//! result of multiplication, which is hidden from the `FieldVar` interface
14//! and is left for advanced users who want better performance.
15//!
16//! The Python script mentioned above can be found in the subdirectory `scripts`.
17
18#![cfg_attr(not(feature = "std"), no_std)]
19#![deny(
20 warnings,
21 unused,
22 future_incompatible,
23 nonstandard_style,
24 rust_2018_idioms,
25 missing_docs
26)]
27#![allow(
28 clippy::redundant_closure_call,
29 clippy::enum_glob_use,
30 clippy::missing_errors_doc,
31 clippy::cast_possible_truncation,
32 clippy::unseparated_literal_suffix
33)]
34#![forbid(unsafe_code)]
35
36#[macro_use]
37extern crate ark_r1cs_std;
38
39use ark_std::fmt::Debug;
40
41/// example parameters of non-native field gadget
42///
43/// Sample parameters for non-native field gadgets
44/// - `BaseField`: the constraint field
45/// - `TargetField`: the field being simulated
46/// - `num_limbs`: how many limbs are used
47/// - `bits_per_limb`: the size of the limbs
48///
49pub mod params;
50/// a submodule for reducing the representations
51#[doc(hidden)]
52pub mod reduce;
53
54/// a macro for computing ceil(log2(x)) for a field element x
55#[doc(hidden)]
56#[macro_export]
57macro_rules! overhead {
58 ($x:expr) => {{
59 use ark_ff::BigInteger;
60 let num = $x;
61 let num_bits = num.into_repr().to_bits_be();
62 let mut skipped_bits = 0;
63 for b in num_bits.iter() {
64 if *b == false {
65 skipped_bits += 1;
66 } else {
67 break;
68 }
69 }
70
71 let mut is_power_of_2 = true;
72 for b in num_bits.iter().skip(skipped_bits + 1) {
73 if *b == true {
74 is_power_of_2 = false;
75 }
76 }
77
78 if is_power_of_2 {
79 num_bits.len() - skipped_bits
80 } else {
81 num_bits.len() - skipped_bits + 1
82 }
83 }};
84}
85
86/// Parameters for a specific `NonNativeFieldVar` instantiation
87#[derive(Clone, Debug)]
88pub struct NonNativeFieldParams {
89 /// The number of limbs (`BaseField` elements) used to represent a `TargetField` element. Highest limb first.
90 pub num_limbs: usize,
91
92 /// The number of bits of the limb
93 pub bits_per_limb: usize,
94}
95
96mod allocated_nonnative_field_var;
97pub use allocated_nonnative_field_var::*;
98
99mod nonnative_field_var;
100pub use nonnative_field_var::*;
101
102mod allocated_nonnative_field_mul_result_var;
103pub use allocated_nonnative_field_mul_result_var::*;
104
105mod nonnative_field_mul_result_var;
106pub use nonnative_field_mul_result_var::*;