-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement bus constraints for small fields (#2334)
This PR adds a `std::math::extension_field` module to our PIL standard library, which abstracts over which extension field is used. I changed all protocols - most importantly the bus - to use the new abstraction. As a result, we can now have bus constraints on smaller fields like BabyBear, e.g.: ``` $ cargo run pil test_data/asm/block_to_block_with_bus.asm -o output -f --field bb ``` TODOs left to future PRs: - Add M31 to `std::field::KnownField` - Implement witness generation for Bus with Fp4
- Loading branch information
1 parent
9f0285d
commit 315592b
Showing
11 changed files
with
218 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::field::known_field; | ||
use std::field::KnownField; | ||
|
||
use std::array::len; | ||
use std::check::panic; | ||
|
||
/// Whether we need to operate on an extension field (because the base field is too small). | ||
let needs_extension: -> bool = || required_extension_size() > 1; | ||
|
||
/// How many field elements / field extensions are recommended for the current base field. | ||
let required_extension_size: -> int = || match known_field() { | ||
Option::Some(KnownField::Goldilocks) => 2, | ||
Option::Some(KnownField::BN254) => 1, | ||
Option::Some(KnownField::BabyBear) => 4, | ||
Option::Some(KnownField::KoalaBear) => 4, | ||
None => panic("The permutation/lookup argument is not implemented for the current field!") | ||
}; | ||
|
||
/// Wrapper around Fp2 and Fp4 to abstract which extension field is used. | ||
/// Once PIL supports traits, we can remove this type and the functions below. | ||
enum Ext<T> { | ||
Fp2(std::math::fp2::Fp2<T>), | ||
Fp4(std::math::fp4::Fp4<T>) | ||
} | ||
|
||
let<T: Add> add_ext: Ext<T>, Ext<T> -> Ext<T> = |a, b| match (a, b) { | ||
(Ext::Fp2(aa), Ext::Fp2(bb)) => Ext::Fp2(std::math::fp2::add_ext(aa, bb)), | ||
(Ext::Fp4(aa), Ext::Fp4(bb)) => Ext::Fp4(std::math::fp4::add_ext(aa, bb)), | ||
_ => panic("Operands have different types") | ||
}; | ||
|
||
let<T: Sub> sub_ext: Ext<T>, Ext<T> -> Ext<T> = |a, b| match (a, b) { | ||
(Ext::Fp2(aa), Ext::Fp2(bb)) => Ext::Fp2(std::math::fp2::sub_ext(aa, bb)), | ||
(Ext::Fp4(aa), Ext::Fp4(bb)) => Ext::Fp4(std::math::fp4::sub_ext(aa, bb)), | ||
_ => panic("Operands have different types") | ||
}; | ||
|
||
let<T: Add + FromLiteral + Mul> mul_ext: Ext<T>, Ext<T> -> Ext<T> = |a, b| match (a, b) { | ||
(Ext::Fp2(aa), Ext::Fp2(bb)) => Ext::Fp2(std::math::fp2::mul_ext(aa, bb)), | ||
(Ext::Fp4(aa), Ext::Fp4(bb)) => Ext::Fp4(std::math::fp4::mul_ext(aa, bb)), | ||
_ => panic("Operands have different types") | ||
}; | ||
|
||
let eval_ext: Ext<expr> -> Ext<fe> = query |a| match a { | ||
Ext::Fp2(aa) => Ext::Fp2(std::math::fp2::eval_ext(aa)), | ||
Ext::Fp4(aa) => Ext::Fp4(std::math::fp4::eval_ext(aa)), | ||
}; | ||
|
||
let inv_ext: Ext<fe> -> Ext<fe> = query |a| match a { | ||
Ext::Fp2(aa) => Ext::Fp2(std::math::fp2::inv_ext(aa)), | ||
Ext::Fp4(aa) => Ext::Fp4(std::math::fp4::inv_ext(aa)), | ||
}; | ||
|
||
let<T> unpack_ext_array: Ext<T> -> T[] = |a| match a { | ||
Ext::Fp2(aa) => std::math::fp2::unpack_ext_array(aa), | ||
Ext::Fp4(aa) => std::math::fp4::unpack_ext_array(aa), | ||
}; | ||
|
||
let next_ext: Ext<expr> -> Ext<expr> = |a| match a { | ||
Ext::Fp2(aa) => Ext::Fp2(std::math::fp2::next_ext(aa)), | ||
Ext::Fp4(aa) => Ext::Fp4(std::math::fp4::next_ext(aa)), | ||
}; | ||
|
||
let<T: FromLiteral> from_base: T -> Ext<T> = |x| match required_extension_size() { | ||
1 => Ext::Fp2(std::math::fp2::from_base(x)), | ||
2 => Ext::Fp2(std::math::fp2::from_base(x)), | ||
4 => Ext::Fp4(std::math::fp4::from_base(x)), | ||
_ => panic("Expected 1, 2, or 4") | ||
}; | ||
|
||
let<T: FromLiteral> from_array: T[] -> Ext<T> = |arr| match len(arr) { | ||
1 => Ext::Fp2(std::math::fp2::from_array(arr)), | ||
2 => Ext::Fp2(std::math::fp2::from_array(arr)), | ||
4 => Ext::Fp4(std::math::fp4::Fp4::Fp4(arr[0], arr[1], arr[2], arr[3])), | ||
_ => panic("Expected 1, 2, or 4") | ||
}; | ||
|
||
let constrain_eq_ext: Ext<expr>, Ext<expr> -> Constr[] = |a, b| match (a, b) { | ||
(Ext::Fp2(aa), Ext::Fp2(bb)) => std::math::fp2::constrain_eq_ext(aa, bb), | ||
(Ext::Fp4(aa), Ext::Fp4(bb)) => std::math::fp4::constrain_eq_ext(aa, bb), | ||
_ => panic("Operands have different types") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod ff; | ||
mod fp2; | ||
mod fp4; | ||
mod fp4; | ||
mod extension_field; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.