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]]
|
||||
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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use plonky2::field::{
|
||||
goldilocks_field::GoldilocksField,
|
||||
types::{Field, PrimeField64},
|
||||
};
|
||||
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::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<GoldF, 2> for Mod65537Generator {
|
||||
vec![self.a]
|
||||
}
|
||||
|
||||
fn run_once(
|
||||
&self,
|
||||
witness: &PartitionWitness<GoldF>,
|
||||
out_buffer: &mut GeneratedValues<GoldF>,
|
||||
) -> Result<()> {
|
||||
fn run_once(&self, witness: &PartitionWitness<GoldF>, out_buffer: &mut GeneratedValues<GoldF>) {
|
||||
let a = witness.get_target(self.a);
|
||||
let a64 = a.to_canonical_u64();
|
||||
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.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.q)?;
|
||||
dst.write_target(self.r)?;
|
||||
@@ -61,12 +56,12 @@ impl SimpleGenerator<GoldF, 2> for Mod65537Generator {
|
||||
|
||||
fn deserialize(src: &mut Buffer, common_data: &CommonCircuitData<GoldF, 2>) -> IoResult<Self>
|
||||
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::<GoldF, 2>,
|
||||
a: Target,
|
||||
) -> Target {
|
||||
pub(crate) fn mod_65537(builder: &mut CircuitBuilder<GoldF, 2>, 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 = <C as GenericConfig<D>>::F;
|
||||
|
||||
@@ -160,7 +146,8 @@ mod tests {
|
||||
.map(|x| builder.constant(GoldilocksField::from_canonical_u64(*x)))
|
||||
.collect();
|
||||
|
||||
let r: Vec<Target> = a.iter()
|
||||
let r: Vec<Target> = a
|
||||
.iter()
|
||||
.map(|targ| Mod65537Builder::mod_65537(&mut builder, *targ))
|
||||
.collect();
|
||||
|
||||
@@ -168,10 +155,12 @@ mod tests {
|
||||
// obviously you don't need this in your own code
|
||||
let r_expected64: Vec<u64> = a64.iter().map(|x| x % 65537).collect();
|
||||
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)))
|
||||
.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<F> = PartialWitness::new();
|
||||
|
||||
@@ -34,8 +34,9 @@ impl MessageTarget {
|
||||
|
||||
pub fn set_witness(&self, pw: &mut PartialWitness<GoldF>, msg: &Vec<GoldF>) -> 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<GoldF>,
|
||||
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<GoldF>, pk: &SchnorrPublicKey) -> Result<()> {
|
||||
pw.set_target(self.pk, pk.pk)?;
|
||||
pw.set_target(self.pk, pk.pk);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user