Skip to content

Commit

Permalink
[book] add the toml related stuff to the algebra chapter (#1063)
Browse files Browse the repository at this point in the history
* Add the toml part to the algebra book chapter, also add the BigUint deserializer

* Add a "no-hexadecimal" part
  • Loading branch information
Golovanov399 authored Dec 16, 2024
1 parent 93e4890 commit 2c216cd
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

14 changes: 14 additions & 0 deletions book/src/custom-extensions/algebra.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,17 @@ pub fn main() {
// Note that these assertions would fail, have we provided the `mod_idx` parameters wrongly.
}
```

### Config parameters

For the guest program to build successfully, all used moduli must be declared in the `.toml` config file in the following format:

```toml
[app_vm_config.modular]
supported_modulus = ["115792089237316195423570985008687907853269984665640564039457584007908834671663"]

[app_vm_config.fp2]
supported_modulus = ["115792089237316195423570985008687907853269984665640564039457584007908834671663"]
```

The `supported_modulus` parameter is a list of moduli that the guest program will use. They must be provided in decimal format in the `.toml` file.
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ prettytable-rs = "0.10"
textwrap = "0.16.0"
ctrlc = "3.4.2"
toml = { workspace = true }
num-bigint-dig = { workspace = true, features = ["serde"] }

[dev-dependencies]
openvm-cli-example-test = { path = "example" }
Expand Down
6 changes: 5 additions & 1 deletion extensions/algebra/circuit/src/fp2_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ use openvm_stark_backend::p3_field::PrimeField32;
use serde::{Deserialize, Serialize};
use strum::EnumCount;

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

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

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

mod util;

mod fp2;
pub use fp2::*;
mod modular_extension;
Expand Down
10 changes: 7 additions & 3 deletions extensions/algebra/circuit/src/modular_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ use openvm_stark_backend::p3_field::PrimeField32;
use serde::{Deserialize, Serialize};
use strum::EnumCount;

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

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

Expand Down
16 changes: 16 additions & 0 deletions extensions/algebra/circuit/src/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use num_bigint_dig::BigUint;
use serde::Deserialize;

pub(crate) fn deserialize_vec_biguint_from_str<'de, D>(
deserializer: D,
) -> Result<Vec<BigUint>, D::Error>
where
D: serde::Deserializer<'de>,
{
let v: Vec<String> = Deserialize::deserialize(deserializer)?;
let res = v.into_iter().map(|s| s.parse()).collect::<Vec<_>>();
if res.iter().any(|x| x.is_err()) {
return Err(serde::de::Error::custom("Failed to parse BigUint"));
}
Ok(res.into_iter().map(|x| x.unwrap()).collect())
}

0 comments on commit 2c216cd

Please sign in to comment.