Skip to content

Commit

Permalink
add: factorial (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya authored Jun 26, 2024
1 parent 8a423b8 commit 1226080
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"[Fel87]feldman-verifiable-secret-sharing",
"[Sch91]schnorr-discrete-log-proof-of-knowledge",
"[PLO2]plonky2-fibonacci",
"[PLO2]plonky2-factorial",
]
resolver = "2"

Expand Down
7 changes: 7 additions & 0 deletions [PLO2]plonky2-factorial/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
edition = "2021"
name = "plonky2-factorial"
version = "0.1.0"

[dependencies]
plonky2 = { git = "https://github.com/0xPolygonZero/plonky2" }
52 changes: 52 additions & 0 deletions [PLO2]plonky2-factorial/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#[cfg(test)]
mod tests {
use plonky2::{
field::types::Field,
iop::witness::{PartialWitness, WitnessWrite},
plonk::{
circuit_builder::CircuitBuilder,
circuit_data::CircuitConfig,
config::{GenericConfig, PoseidonGoldilocksConfig},
},
};

#[test]
fn plonky2_factorial_example() {
// This constant defines the extension that we will
// use. Almost always it is going to be 2
const D: usize = 2;

// Generate configuration where we are using Golidilock's
// prime field, it's quadratic extension, Poseidon as the
// hashing function for the merkle tree in FRI stage, and
// the same Poseidon as the hasher for Fiat-Shamir (for
// challenge points generation). See `KeccakGoldilocksCo-
// -nfig`) for when to target Ethereum.
type C = PoseidonGoldilocksConfig;

// Get the Field type that we are using
type F = <C as GenericConfig<D>>::F;

let config = CircuitConfig::standard_recursion_config();

let mut builder = CircuitBuilder::<F, D>::new(config);

let initial = builder.add_virtual_target();
let mut cur_target = initial;
for i in 2..101 {
let multiplier = builder.constant(F::from_canonical_u32(i));
let cur_target = builder.mul(cur_target, multiplier);
}

builder.register_public_input(initial);
builder.register_public_input(cur_target);

let mut pw = PartialWitness::new();
pw.set_target(initial, F::ONE);

let data = builder.build::<C>();
let proof = data.prove(pw).unwrap();

core::assert!(data.verify(proof).is_ok());
}
}

0 comments on commit 1226080

Please sign in to comment.