mirror of
https://github.com/arnaucube/schnorr.git
synced 2026-01-12 17:01:29 +01:00
port to plonky2 v0.2.2 to make it compatible with the recursion-framework
This commit is contained in:
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -265,7 +265,8 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "plonky2"
|
name = "plonky2"
|
||||||
version = "0.2.2"
|
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 = [
|
dependencies = [
|
||||||
"ahash",
|
"ahash",
|
||||||
"anyhow",
|
"anyhow",
|
||||||
@@ -289,7 +290,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "plonky2_field"
|
name = "plonky2_field"
|
||||||
version = "0.2.2"
|
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 = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"itertools",
|
"itertools",
|
||||||
@@ -304,7 +306,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "plonky2_maybe_rayon"
|
name = "plonky2_maybe_rayon"
|
||||||
version = "0.2.0"
|
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 = [
|
dependencies = [
|
||||||
"rayon",
|
"rayon",
|
||||||
]
|
]
|
||||||
@@ -312,7 +315,8 @@ dependencies = [
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "plonky2_util"
|
name = "plonky2_util"
|
||||||
version = "0.2.0"
|
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]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[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"
|
anyhow = "1.0.56"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
|
|||||||
@@ -1,18 +1,15 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use plonky2::field::{
|
||||||
|
goldilocks_field::GoldilocksField,
|
||||||
|
types::{Field, PrimeField64},
|
||||||
|
};
|
||||||
use plonky2::iop::{
|
use plonky2::iop::{
|
||||||
generator::{GeneratedValues, SimpleGenerator},
|
generator::{GeneratedValues, SimpleGenerator},
|
||||||
target::Target,
|
target::Target,
|
||||||
witness::{PartitionWitness, Witness, WitnessWrite},
|
witness::{PartitionWitness, Witness, WitnessWrite},
|
||||||
};
|
};
|
||||||
use plonky2::field::{
|
use plonky2::plonk::{circuit_builder::CircuitBuilder, circuit_data::CommonCircuitData};
|
||||||
goldilocks_field::GoldilocksField,
|
|
||||||
types::{Field, PrimeField64},
|
|
||||||
};
|
|
||||||
use plonky2::plonk::{
|
|
||||||
circuit_builder::CircuitBuilder,
|
|
||||||
circuit_data::CommonCircuitData,
|
|
||||||
};
|
|
||||||
use plonky2::util::serialization::{Buffer, IoResult, Read, Write};
|
use plonky2::util::serialization::{Buffer, IoResult, Read, Write};
|
||||||
|
|
||||||
use crate::schnorr::{SchnorrPublicKey, SchnorrSignature};
|
use crate::schnorr::{SchnorrPublicKey, SchnorrSignature};
|
||||||
@@ -36,11 +33,7 @@ impl SimpleGenerator<GoldF, 2> for Mod65537Generator {
|
|||||||
vec![self.a]
|
vec![self.a]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_once(
|
fn run_once(&self, witness: &PartitionWitness<GoldF>, out_buffer: &mut GeneratedValues<GoldF>) {
|
||||||
&self,
|
|
||||||
witness: &PartitionWitness<GoldF>,
|
|
||||||
out_buffer: &mut GeneratedValues<GoldF>,
|
|
||||||
) -> Result<()> {
|
|
||||||
let a = witness.get_target(self.a);
|
let a = witness.get_target(self.a);
|
||||||
let a64 = a.to_canonical_u64();
|
let a64 = a.to_canonical_u64();
|
||||||
let q64 = a64 / 65537;
|
let q64 = a64 / 65537;
|
||||||
@@ -48,11 +41,13 @@ impl SimpleGenerator<GoldF, 2> for Mod65537Generator {
|
|||||||
|
|
||||||
out_buffer.set_target(self.q, GoldF::from_canonical_u64(q64));
|
out_buffer.set_target(self.q, GoldF::from_canonical_u64(q64));
|
||||||
out_buffer.set_target(self.r, GoldF::from_canonical_u64(r64));
|
out_buffer.set_target(self.r, GoldF::from_canonical_u64(r64));
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<GoldF, 2>) -> IoResult<()> {
|
fn serialize(
|
||||||
|
&self,
|
||||||
|
dst: &mut Vec<u8>,
|
||||||
|
common_data: &CommonCircuitData<GoldF, 2>,
|
||||||
|
) -> IoResult<()> {
|
||||||
dst.write_target(self.a)?;
|
dst.write_target(self.a)?;
|
||||||
dst.write_target(self.q)?;
|
dst.write_target(self.q)?;
|
||||||
dst.write_target(self.r)?;
|
dst.write_target(self.r)?;
|
||||||
@@ -61,7 +56,7 @@ impl SimpleGenerator<GoldF, 2> for Mod65537Generator {
|
|||||||
|
|
||||||
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<GoldF, 2>) -> IoResult<Self>
|
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<GoldF, 2>) -> IoResult<Self>
|
||||||
where
|
where
|
||||||
Self: Sized
|
Self: Sized,
|
||||||
{
|
{
|
||||||
let a = src.read_target()?;
|
let a = src.read_target()?;
|
||||||
let q = src.read_target()?;
|
let q = src.read_target()?;
|
||||||
@@ -84,15 +79,12 @@ impl Mod65537Builder {
|
|||||||
// (these first two checks guarantee that a lies in the range [0, p + 65536])
|
// (these first two checks guarantee that a lies in the range [0, p + 65536])
|
||||||
// if q = floor(p / 65537) then r = 0
|
// if q = floor(p / 65537) then r = 0
|
||||||
// (note that p % 65537 == 1 so this is the only possibility)
|
// (note that p % 65537 == 1 so this is the only possibility)
|
||||||
pub(crate) fn mod_65537 (
|
pub(crate) fn mod_65537(builder: &mut CircuitBuilder<GoldF, 2>, a: Target) -> Target {
|
||||||
builder: &mut CircuitBuilder::<GoldF, 2>,
|
|
||||||
a: Target,
|
|
||||||
) -> Target {
|
|
||||||
let q = builder.add_virtual_target();
|
let q = builder.add_virtual_target();
|
||||||
let r = builder.add_virtual_target();
|
let r = builder.add_virtual_target();
|
||||||
|
|
||||||
// the Mod65537Generator will assign values to q and r later
|
// 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
|
// impose four constraints
|
||||||
// 1. a = 65537 * q + r
|
// 1. a = 65537 * q + r
|
||||||
@@ -120,7 +112,7 @@ impl Mod65537Builder {
|
|||||||
builder.connect(prod_temp, zero_temp);
|
builder.connect(prod_temp, zero_temp);
|
||||||
|
|
||||||
// throw in the Generator to tell builder how to compute r
|
// 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
|
r
|
||||||
}
|
}
|
||||||
@@ -129,19 +121,13 @@ impl Mod65537Builder {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::mod65537::Mod65537Builder;
|
use crate::mod65537::Mod65537Builder;
|
||||||
use plonky2::iop::{
|
use plonky2::field::{goldilocks_field::GoldilocksField, types::Field};
|
||||||
target::Target,
|
use plonky2::iop::{target::Target, witness::PartialWitness};
|
||||||
witness::PartialWitness,
|
|
||||||
};
|
|
||||||
use plonky2::plonk::{
|
use plonky2::plonk::{
|
||||||
circuit_builder::CircuitBuilder,
|
circuit_builder::CircuitBuilder,
|
||||||
circuit_data::CircuitConfig,
|
circuit_data::CircuitConfig,
|
||||||
config::{GenericConfig, PoseidonGoldilocksConfig},
|
config::{GenericConfig, PoseidonGoldilocksConfig},
|
||||||
};
|
};
|
||||||
use plonky2::field::{
|
|
||||||
goldilocks_field::GoldilocksField,
|
|
||||||
types::Field,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mod65537() -> () {
|
fn test_mod65537() -> () {
|
||||||
@@ -160,7 +146,8 @@ mod tests {
|
|||||||
.map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x)))
|
.map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let r: Vec<Target> = a.iter()
|
let r: Vec<Target> = a
|
||||||
|
.iter()
|
||||||
.map(|targ| Mod65537Builder::mod_65537(&mut builder, *targ))
|
.map(|targ| Mod65537Builder::mod_65537(&mut builder, *targ))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -168,10 +155,12 @@ mod tests {
|
|||||||
// obviously you don't need this in your own code
|
// obviously you don't need this in your own code
|
||||||
let r_expected64: Vec<u64> = a64.iter().map(|x| x % 65537).collect();
|
let r_expected64: Vec<u64> = a64.iter().map(|x| x % 65537).collect();
|
||||||
println!("Expected residues mod 64: {:?}", r_expected64);
|
println!("Expected residues mod 64: {:?}", r_expected64);
|
||||||
let r_expected: Vec<Target> = r_expected64.iter()
|
let r_expected: Vec<Target> = r_expected64
|
||||||
|
.iter()
|
||||||
.map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x)))
|
.map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x)))
|
||||||
.collect();
|
.collect();
|
||||||
r.iter().zip(r_expected.iter())
|
r.iter()
|
||||||
|
.zip(r_expected.iter())
|
||||||
.for_each(|(x, y)| builder.connect(*x, *y));
|
.for_each(|(x, y)| builder.connect(*x, *y));
|
||||||
|
|
||||||
let mut pw: PartialWitness<F> = PartialWitness::new();
|
let mut pw: PartialWitness<F> = PartialWitness::new();
|
||||||
|
|||||||
@@ -34,8 +34,9 @@ impl MessageTarget {
|
|||||||
|
|
||||||
pub fn set_witness(&self, pw: &mut PartialWitness<GoldF>, msg: &Vec<GoldF>) -> Result<()> {
|
pub fn set_witness(&self, pw: &mut PartialWitness<GoldF>, msg: &Vec<GoldF>) -> Result<()> {
|
||||||
assert!(msg.len() == self.msg.len());
|
assert!(msg.len() == self.msg.len());
|
||||||
self.msg.iter().zip(msg.iter())
|
self.msg.iter().zip(msg.iter()).for_each(|(&t, &x)| {
|
||||||
.for_each(|(&t, &x)| {pw.set_target(t, x);});
|
pw.set_target(t, x);
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -58,8 +59,8 @@ impl SchnorrSignatureTarget {
|
|||||||
pw: &mut PartialWitness<GoldF>,
|
pw: &mut PartialWitness<GoldF>,
|
||||||
sig: &SchnorrSignature,
|
sig: &SchnorrSignature,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
pw.set_target(self.s, GoldilocksField::from_canonical_u64(sig.s))?;
|
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.e, GoldilocksField::from_canonical_u64(sig.e));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,7 +77,7 @@ impl SchnorrPublicKeyTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_witness(&self, pw: &mut PartialWitness<GoldF>, pk: &SchnorrPublicKey) -> Result<()> {
|
pub fn set_witness(&self, pw: &mut PartialWitness<GoldF>, pk: &SchnorrPublicKey) -> Result<()> {
|
||||||
pw.set_target(self.pk, pk.pk)?;
|
pw.set_target(self.pk, pk.pk);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user