From 5e9022ff6bd28cecc5a958463cfbc3d2eb1ac983 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 10 Oct 2024 15:40:10 +0200 Subject: [PATCH] port to plonky2 v0.2.2 to make it compatible with the recursion-framework --- Cargo.lock | 12 +++++--- Cargo.toml | 3 +- src/mod65537.rs | 71 ++++++++++++++++++------------------------- src/schnorr_prover.rs | 11 ++++--- 4 files changed, 46 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b41887c..01e4fed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -265,7 +265,8 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "plonky2" version = "0.2.2" -source = "git+https://github.com/mir-protocol/plonky2#41dc325e61ab8d4c0491e68e667c35a4e8173ffa" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85f26b090b989aebdeaf6a4eed748c1fbcabf67e7273a22e4e0c877b63846d0f" dependencies = [ "ahash", "anyhow", @@ -289,7 +290,8 @@ dependencies = [ [[package]] name = "plonky2_field" version = "0.2.2" -source = "git+https://github.com/mir-protocol/plonky2#41dc325e61ab8d4c0491e68e667c35a4e8173ffa" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a1dca60ad900d81b1fe2df3d0b88d43345988e2935e6709176e96573f4bcf5d" dependencies = [ "anyhow", "itertools", @@ -304,7 +306,8 @@ dependencies = [ [[package]] name = "plonky2_maybe_rayon" version = "0.2.0" -source = "git+https://github.com/mir-protocol/plonky2#41dc325e61ab8d4c0491e68e667c35a4e8173ffa" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ff44a90aaca13e10e7ddf8fab815ba1b404c3f7c3ca82aaf11c46beabaa923" dependencies = [ "rayon", ] @@ -312,7 +315,8 @@ dependencies = [ [[package]] name = "plonky2_util" version = "0.2.0" -source = "git+https://github.com/mir-protocol/plonky2#41dc325e61ab8d4c0491e68e667c35a4e8173ffa" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b16136f5f3019c1e83035af76cccddd56d789a5e2933306270185c3f99f12259" [[package]] name = "ppv-lite86" diff --git a/Cargo.toml b/Cargo.toml index 3f087af..ef5c9b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -plonky2 = { git = "https://github.com/mir-protocol/plonky2" } +# plonky2 = { git = "https://github.com/mir-protocol/plonky2" } +plonky2 = "0.2.2" anyhow = "1.0.56" rand = "0.8.5" diff --git a/src/mod65537.rs b/src/mod65537.rs index 56a4117..0915eae 100644 --- a/src/mod65537.rs +++ b/src/mod65537.rs @@ -1,18 +1,15 @@ use anyhow::Result; -use plonky2::iop::{ - generator::{GeneratedValues, SimpleGenerator}, - target::Target, - witness::{PartitionWitness, Witness, WitnessWrite}, -}; use plonky2::field::{ goldilocks_field::GoldilocksField, types::{Field, PrimeField64}, }; -use plonky2::plonk::{ - circuit_builder::CircuitBuilder, - circuit_data::CommonCircuitData, +use plonky2::iop::{ + generator::{GeneratedValues, SimpleGenerator}, + target::Target, + witness::{PartitionWitness, Witness, WitnessWrite}, }; +use plonky2::plonk::{circuit_builder::CircuitBuilder, circuit_data::CommonCircuitData}; use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; use crate::schnorr::{SchnorrPublicKey, SchnorrSignature}; @@ -36,11 +33,7 @@ impl SimpleGenerator for Mod65537Generator { vec![self.a] } - fn run_once( - &self, - witness: &PartitionWitness, - out_buffer: &mut GeneratedValues, - ) -> Result<()> { + fn run_once(&self, witness: &PartitionWitness, out_buffer: &mut GeneratedValues) { let a = witness.get_target(self.a); let a64 = a.to_canonical_u64(); let q64 = a64 / 65537; @@ -48,11 +41,13 @@ impl SimpleGenerator for Mod65537Generator { out_buffer.set_target(self.q, GoldF::from_canonical_u64(q64)); out_buffer.set_target(self.r, GoldF::from_canonical_u64(r64)); - - Ok(()) } - fn serialize(&self, dst: &mut Vec, common_data: &CommonCircuitData) -> IoResult<()> { + fn serialize( + &self, + dst: &mut Vec, + common_data: &CommonCircuitData, + ) -> IoResult<()> { dst.write_target(self.a)?; dst.write_target(self.q)?; dst.write_target(self.r)?; @@ -61,12 +56,12 @@ impl SimpleGenerator for Mod65537Generator { fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData) -> IoResult where - Self: Sized + Self: Sized, { - let a = src.read_target()?; - let q = src.read_target()?; - let r = src.read_target()?; - Ok(Self { a, q, r }) + let a = src.read_target()?; + let q = src.read_target()?; + let r = src.read_target()?; + Ok(Self { a, q, r }) } } @@ -84,15 +79,12 @@ impl Mod65537Builder { // (these first two checks guarantee that a lies in the range [0, p + 65536]) // if q = floor(p / 65537) then r = 0 // (note that p % 65537 == 1 so this is the only possibility) - pub(crate) fn mod_65537 ( - builder: &mut CircuitBuilder::, - a: Target, - ) -> Target { + pub(crate) fn mod_65537(builder: &mut CircuitBuilder, a: Target) -> Target { let q = builder.add_virtual_target(); let r = builder.add_virtual_target(); // the Mod65537Generator will assign values to q and r later - builder.add_simple_generator( Mod65537Generator { a, q, r } ); + builder.add_simple_generator(Mod65537Generator { a, q, r }); // impose four constraints // 1. a = 65537 * q + r @@ -120,7 +112,7 @@ impl Mod65537Builder { builder.connect(prod_temp, zero_temp); // throw in the Generator to tell builder how to compute r - builder.add_simple_generator( Mod65537Generator {a, q, r} ); + builder.add_simple_generator(Mod65537Generator { a, q, r }); r } @@ -129,24 +121,18 @@ impl Mod65537Builder { #[cfg(test)] mod tests { use crate::mod65537::Mod65537Builder; - use plonky2::iop::{ - target::Target, - witness::PartialWitness, - }; + use plonky2::field::{goldilocks_field::GoldilocksField, types::Field}; + use plonky2::iop::{target::Target, witness::PartialWitness}; use plonky2::plonk::{ circuit_builder::CircuitBuilder, circuit_data::CircuitConfig, config::{GenericConfig, PoseidonGoldilocksConfig}, }; - use plonky2::field::{ - goldilocks_field::GoldilocksField, - types::Field, - }; #[test] fn test_mod65537() -> () { const D: usize = 2; - const p: u64 = 18446744069414584321; // the Goldilocks prime + const p: u64 = 18446744069414584321; // the Goldilocks prime type C = PoseidonGoldilocksConfig; type F = >::F; @@ -160,7 +146,8 @@ mod tests { .map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x))) .collect(); - let r: Vec = a.iter() + let r: Vec = a + .iter() .map(|targ| Mod65537Builder::mod_65537(&mut builder, *targ)) .collect(); @@ -168,16 +155,18 @@ mod tests { // obviously you don't need this in your own code let r_expected64: Vec = a64.iter().map(|x| x % 65537).collect(); println!("Expected residues mod 64: {:?}", r_expected64); - let r_expected: Vec = r_expected64.iter() + let r_expected: Vec = r_expected64 + .iter() .map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x))) .collect(); - r.iter().zip(r_expected.iter()) + r.iter() + .zip(r_expected.iter()) .for_each(|(x, y)| builder.connect(*x, *y)); - + let mut pw: PartialWitness = PartialWitness::new(); let data = builder.build::(); let proof = data.prove(pw).unwrap(); () } -} \ No newline at end of file +} diff --git a/src/schnorr_prover.rs b/src/schnorr_prover.rs index 44e4c31..00f98bb 100644 --- a/src/schnorr_prover.rs +++ b/src/schnorr_prover.rs @@ -34,8 +34,9 @@ impl MessageTarget { pub fn set_witness(&self, pw: &mut PartialWitness, msg: &Vec) -> Result<()> { assert!(msg.len() == self.msg.len()); - self.msg.iter().zip(msg.iter()) - .for_each(|(&t, &x)| {pw.set_target(t, x);}); + self.msg.iter().zip(msg.iter()).for_each(|(&t, &x)| { + pw.set_target(t, x); + }); Ok(()) } @@ -58,8 +59,8 @@ impl SchnorrSignatureTarget { pw: &mut PartialWitness, sig: &SchnorrSignature, ) -> Result<()> { - pw.set_target(self.s, GoldilocksField::from_canonical_u64(sig.s))?; - pw.set_target(self.e, GoldilocksField::from_canonical_u64(sig.e))?; + pw.set_target(self.s, GoldilocksField::from_canonical_u64(sig.s)); + pw.set_target(self.e, GoldilocksField::from_canonical_u64(sig.e)); Ok(()) } } @@ -76,7 +77,7 @@ impl SchnorrPublicKeyTarget { } pub fn set_witness(&self, pw: &mut PartialWitness, pk: &SchnorrPublicKey) -> Result<()> { - pw.set_target(self.pk, pk.pk)?; + pw.set_target(self.pk, pk.pk); Ok(()) } }