Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create bn254 scalar field chip #1902

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/core/executor/src/events/precompiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub enum PrecompileEvent {
Bn254Fp2AddSub(Fp2AddSubEvent),
/// Bn254 quadratic field mul precompile event.
Bn254Fp2Mul(Fp2MulEvent),
/// Bn254 scalar field operation precompile event.
/// (Maybe it can use the FpOpEvent?)
Bn254Fr(FpOpEvent),
/// Bls12-381 curve add precompile event.
Bls12381Add(EllipticCurveAddEvent),
/// Bls12-381 curve double precompile event.
Expand Down Expand Up @@ -126,7 +129,9 @@ impl PrecompileLocalMemory for Vec<(SyscallEvent, PrecompileEvent)> {
PrecompileEvent::U256xU2048Mul(e) => {
iterators.push(e.local_mem_access.iter());
}
PrecompileEvent::Bls12381Fp(e) | PrecompileEvent::Bn254Fp(e) => {
PrecompileEvent::Bls12381Fp(e)
| PrecompileEvent::Bn254Fp(e)
| PrecompileEvent::Bn254Fr(e) => {
iterators.push(e.local_mem_access.iter());
}
PrecompileEvent::Bls12381Fp2AddSub(e) | PrecompileEvent::Bn254Fp2AddSub(e) => {
Expand Down
28 changes: 28 additions & 0 deletions crates/curves/src/weierstrass/bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ pub struct Bn254Parameters;

pub type Bn254 = SwCurve<Bn254Parameters>;

#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
/// Bn254 scalar field parameter
pub struct Bn254ScalarField;
impl FieldParameters for Bn254ScalarField {
const MODULUS: &'static [u8] = &[
1, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129,
129, 182, 69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48,
];
const WITNESS_OFFSET: usize = 1usize << 14;
fn modulus() -> BigUint {
BigUint::from_str_radix(
"21888242871839275222246405745257275088548364400416034343698204186575808495617",
10,
)
.unwrap()
}

}

impl FpOpField for Bn254ScalarField {
const FIELD_TYPE: FieldType = FieldType::Bn254;
}

impl NumLimbs for Bn254ScalarField {
type Limbs = U32;
type Witness = U62;
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
/// Bn254 base field parameter
pub struct Bn254BaseField;
Expand Down