Skip to content

Commit

Permalink
fix biguints serde with toml (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
luffykai authored Dec 16, 2024
1 parent 50c8826 commit 67f3733
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 49 deletions.
121 changes: 114 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ parking_lot = "0.12.2"
tracing = "0.1.40"
bon = "3.2.0"
serde_json = "1.0.117"
serde_with = "3.11.0"
toml = "0.8.14"
lazy_static = "1.5.0"
once_cell = "1.19.0"
Expand Down
1 change: 1 addition & 0 deletions extensions/algebra/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ derive_more = { workspace = true, features = ["from"] }
strum = { workspace = true }
derive-new = { workspace = true }
serde.workspace = true
serde_with = { workspace = true }

[dev-dependencies]
halo2curves-axiom = { workspace = true }
Expand Down
9 changes: 4 additions & 5 deletions extensions/algebra/circuit/src/fp2_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ use openvm_mod_circuit_builder::ExprBuilderConfig;
use openvm_rv32_adapters::Rv32VecHeapAdapterChip;
use openvm_stark_backend::p3_field::PrimeField32;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use strum::EnumCount;

use crate::{
fp2_chip::{Fp2AddSubChip, Fp2MulDivChip},
util::deserialize_vec_biguint_from_str,
};
use crate::fp2_chip::{Fp2AddSubChip, Fp2MulDivChip};

#[serde_as]
#[derive(Clone, Debug, derive_new::new, Serialize, Deserialize)]
pub struct Fp2Extension {
#[serde(deserialize_with = "deserialize_vec_biguint_from_str")]
#[serde_as(as = "Vec<DisplayFromStr>")]
pub supported_modulus: Vec<BigUint>,
}

Expand Down
2 changes: 0 additions & 2 deletions extensions/algebra/circuit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub mod fp2_chip;
pub mod modular_chip;

mod util;

mod fp2;
pub use fp2::*;
mod modular_extension;
Expand Down
13 changes: 6 additions & 7 deletions extensions/algebra/circuit/src/modular_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ use openvm_mod_circuit_builder::ExprBuilderConfig;
use openvm_rv32_adapters::{Rv32IsEqualModAdapterChip, Rv32VecHeapAdapterChip};
use openvm_stark_backend::p3_field::PrimeField32;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use strum::EnumCount;

use crate::{
modular_chip::{
ModularAddSubChip, ModularAddSubCoreChip, ModularIsEqualChip, ModularIsEqualCoreChip,
ModularMulDivChip, ModularMulDivCoreChip,
},
util::deserialize_vec_biguint_from_str,
use crate::modular_chip::{
ModularAddSubChip, ModularAddSubCoreChip, ModularIsEqualChip, ModularIsEqualCoreChip,
ModularMulDivChip, ModularMulDivCoreChip,
};

#[serde_as]
#[derive(Clone, Debug, derive_new::new, Serialize, Deserialize)]
pub struct ModularExtension {
#[serde(deserialize_with = "deserialize_vec_biguint_from_str")]
#[serde_as(as = "Vec<DisplayFromStr>")]
pub supported_modulus: Vec<BigUint>,
}

Expand Down
16 changes: 0 additions & 16 deletions extensions/algebra/circuit/src/util.rs

This file was deleted.

1 change: 1 addition & 0 deletions extensions/ecc/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ once_cell = { workspace = true }
eyre = { workspace = true }
num-integer = { workspace = true }
serde = { workspace = true }
serde_with = { workspace = true }

[dev-dependencies]
openvm-stark-sdk = { workspace = true }
Expand Down
18 changes: 6 additions & 12 deletions extensions/ecc/circuit/src/weierstrass_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,28 @@ use openvm_mod_circuit_builder::ExprBuilderConfig;
use openvm_rv32_adapters::Rv32VecHeapAdapterChip;
use openvm_stark_backend::p3_field::PrimeField32;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use strum::EnumCount;

use super::{EcAddNeChip, EcDoubleChip};

#[serde_as]
#[derive(Clone, Debug, derive_new::new, Serialize, Deserialize)]
pub struct CurveConfig {
/// The coordinate modulus of the curve.
#[serde(deserialize_with = "deserialize_biguint_from_str")]
#[serde_as(as = "DisplayFromStr")]
pub modulus: BigUint,
/// The scalar field modulus of the curve.
#[serde(deserialize_with = "deserialize_biguint_from_str")]
#[serde_as(as = "DisplayFromStr")]
pub scalar: BigUint,
/// The coefficient a of y^2 = x^3 + ax + b.
#[serde(deserialize_with = "deserialize_biguint_from_str")]
#[serde_as(as = "DisplayFromStr")]
pub a: BigUint,
/// The coefficient b of y^2 = x^3 + ax + b.
#[serde(deserialize_with = "deserialize_biguint_from_str")]
#[serde_as(as = "DisplayFromStr")]
pub b: BigUint,
}

fn deserialize_biguint_from_str<'de, D>(deserializer: D) -> Result<BigUint, D::Error>
where
D: serde::Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}

pub static SECP256K1_CONFIG: Lazy<CurveConfig> = Lazy::new(|| CurveConfig {
modulus: SECP256K1_MODULUS.clone(),
scalar: SECP256K1_ORDER.clone(),
Expand Down

0 comments on commit 67f3733

Please sign in to comment.