Skip to content

Commit

Permalink
feat: public inputs in gnark circuit (#576)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas authored Apr 24, 2024
1 parent 27da586 commit efbcc09
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 36 deletions.
1 change: 1 addition & 0 deletions core/src/utils/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn setup_logger() {
.unwrap_or_else(|_| EnvFilter::new(default_filter))
.add_directive("p3_keccak_air=off".parse().unwrap())
.add_directive("p3_fri=off".parse().unwrap())
.add_directive("p3_dft=off".parse().unwrap())
.add_directive("p3_challenger=off".parse().unwrap());
tracing_subscriber::fmt::Subscriber::builder()
.compact()
Expand Down
1 change: 1 addition & 0 deletions prover/scripts/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ pub fn main() {

tracing::info!("sanity check gnark prove");
let proof = Groth16Prover::prove(witness.clone(), args.build_dir.clone().into());

println!("{:?}", proof);
}
70 changes: 70 additions & 0 deletions recursion/circuit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,73 @@ pub mod witness;
pub const SPONGE_SIZE: usize = 3;
pub const DIGEST_SIZE: usize = 1;
pub const RATE: usize = 8;

#[cfg(test)]
mod tests {
use p3_bn254_fr::Bn254Fr;
use p3_field::AbstractField;
use sp1_recursion_compiler::config::OuterConfig;
use sp1_recursion_compiler::constraints::ConstraintCompiler;
use sp1_recursion_compiler::ir::{Builder, Witness};
use sp1_recursion_groth16_ffi::Groth16Prover;

#[test]
fn test_commit() {
let mut builder = Builder::<OuterConfig>::default();
let vkey_hash_bn254 = Bn254Fr::from_canonical_u32(1345237507);
let commited_values_digest_bn254 = Bn254Fr::from_canonical_u32(102);
let vkey_hash = builder.eval(vkey_hash_bn254);
let commited_values_digest = builder.eval(commited_values_digest_bn254);
builder.commit_vkey_hash_circuit(vkey_hash);
builder.commit_commited_values_digest_circuit(commited_values_digest);

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);

let mut witness = Witness::default();
witness.set_vkey_hash(vkey_hash_bn254);
witness.set_commited_values_digest(commited_values_digest_bn254);

Groth16Prover::test::<OuterConfig>(constraints.clone(), witness);
}

#[test]
#[should_panic]
fn test_commit_vkey_fail() {
let mut builder = Builder::<OuterConfig>::default();
let vkey_hash_bn254 = Bn254Fr::from_canonical_u32(1345237507);
let commited_values_digest_bn254 = Bn254Fr::from_canonical_u32(102);
let vkey_hash = builder.eval(vkey_hash_bn254);
let commited_values_digest = builder.eval(commited_values_digest_bn254);
builder.commit_vkey_hash_circuit(vkey_hash);
builder.commit_commited_values_digest_circuit(commited_values_digest);

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);

let mut witness = Witness::default();
witness.set_commited_values_digest(commited_values_digest_bn254);

Groth16Prover::test::<OuterConfig>(constraints.clone(), witness);
}

#[test]
#[should_panic]
fn test_commit_commited_values_digest_fail() {
let mut builder = Builder::<OuterConfig>::default();
let vkey_hash_bn254 = Bn254Fr::from_canonical_u32(1345237507);
let commited_values_digest_bn254 = Bn254Fr::from_canonical_u32(102);
let vkey_hash = builder.eval(vkey_hash_bn254);
let commited_values_digest = builder.eval(commited_values_digest_bn254);
builder.commit_vkey_hash_circuit(vkey_hash);
builder.commit_commited_values_digest_circuit(commited_values_digest);

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);

let mut witness = Witness::default();
witness.set_vkey_hash(vkey_hash_bn254);

Groth16Prover::test::<OuterConfig>(constraints.clone(), witness);
}
}
2 changes: 2 additions & 0 deletions recursion/circuit/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ mod tests {
vars: vec![Bn254Fr::one(), Bn254Fr::two()],
felts: vec![BabyBear::one(), BabyBear::two()],
exts: vec![OuterChallenge::one(), OuterChallenge::two()],
vkey_hash: Bn254Fr::one(),
commited_values_digest: Bn254Fr::one(),
},
);
}
Expand Down
9 changes: 8 additions & 1 deletion recursion/compiler/src/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,17 @@ impl<C: Config + Debug> ConstraintCompiler<C> {
opcode: ConstraintOpcode::WitnessE,
args: vec![vec![a.id()], vec![b.to_string()]],
}),
DslIr::CircuitCommitVkeyHash(a) => constraints.push(Constraint {
opcode: ConstraintOpcode::CommitVkeyHash,
args: vec![vec![a.id()]],
}),
DslIr::CircuitCommitCommitedValuesDigest(a) => constraints.push(Constraint {
opcode: ConstraintOpcode::CommitCommitedValuesDigest,
args: vec![vec![a.id()]],
}),
_ => panic!("unsupported {:?}", instruction),
};
}
println!("number of meta constraints: {}", constraints.len());
constraints
}
}
2 changes: 2 additions & 0 deletions recursion/compiler/src/constraints/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ pub enum ConstraintOpcode {
WitnessV,
WitnessF,
WitnessE,
CommitVkeyHash,
CommitCommitedValuesDigest,
}
9 changes: 9 additions & 0 deletions recursion/compiler/src/ir/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ impl<C: Config> Builder<C> {
self.operations.push(DslIr::Commit(pv_hash.clone()));
}

pub fn commit_vkey_hash_circuit(&mut self, var: Var<C::N>) {
self.operations.push(DslIr::CircuitCommitVkeyHash(var));
}

pub fn commit_commited_values_digest_circuit(&mut self, var: Var<C::N>) {
self.operations
.push(DslIr::CircuitCommitCommitedValuesDigest(var));
}

pub fn cycle_tracker(&mut self, name: &str) {
self.operations.push(DslIr::CycleTracker(name.to_string()));
}
Expand Down
5 changes: 5 additions & 0 deletions recursion/compiler/src/ir/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ pub enum DslIr<C: Config> {
WitnessFelt(Felt<C::F>, u32),
WitnessExt(Ext<C::F, C::EF>, u32),
Commit(Array<C, Felt<C::F>>),

// Public inputs for circuits.
CircuitCommitVkeyHash(Var<C::N>),
CircuitCommitCommitedValuesDigest(Var<C::N>),

// FRI specific instructions.
FriFold(Var<C::N>, Array<C, FriFoldInput<C>>),
CircuitSelectV(Var<C::N>, Var<C::N>, Var<C::N>, Var<C::N>),
Expand Down
12 changes: 12 additions & 0 deletions recursion/compiler/src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ pub struct Witness<C: Config> {
pub vars: Vec<C::N>,
pub felts: Vec<C::F>,
pub exts: Vec<C::EF>,
pub vkey_hash: C::N,
pub commited_values_digest: C::N,
}

impl<C: Config> Witness<C> {
pub fn set_vkey_hash(&mut self, vkey_hash: C::N) {
self.vkey_hash = vkey_hash;
}

pub fn set_commited_values_digest(&mut self, commited_values_digest: C::N) {
self.commited_values_digest = commited_values_digest
}
}

impl<N: AbstractField> Usize<N> {
Expand Down
6 changes: 6 additions & 0 deletions recursion/core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ where
}

pub fn run(&mut self) {
let early_exit_ts = std::env::var("RECURSION_EARLY_EXIT_TS")
.map_or(usize::MAX, |ts: String| ts.parse().unwrap());
while self.pc < F::from_canonical_u32(self.program.instructions.len() as u32) {
let idx = self.pc.as_canonical_u32() as usize;
let instruction = self.program.instructions[idx].clone();
Expand Down Expand Up @@ -760,6 +762,10 @@ where
self.clk += F::from_canonical_u32(4);
self.timestamp += 1;
self.access = CpuRecord::default();

if self.timestamp >= early_exit_ts {
break;
}
}

// Collect all used memory addresses.
Expand Down
7 changes: 4 additions & 3 deletions recursion/groth16-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct Groth16Proof {
a: [String; 2],
b: [[String; 2]; 2],
c: [String; 2],
public_inputs: [String; 2],
}

impl Groth16Prover {
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Groth16Prover {

// Write witness.
let mut witness_file = tempfile::NamedTempFile::new().unwrap();
let gnark_witness: Groth16Witness = witness.into();
let gnark_witness = Groth16Witness::new(witness);
let serialized = serde_json::to_string(&gnark_witness).unwrap();
witness_file.write_all(serialized.as_bytes()).unwrap();

Expand Down Expand Up @@ -120,7 +121,7 @@ impl Groth16Prover {

// Write witness.
let witness_path = build_dir.join("witness.json");
let gnark_witness: Groth16Witness = witness.into();
let gnark_witness = Groth16Witness::new(witness);
let mut file = File::create(witness_path).unwrap();
let serialized = serde_json::to_string(&gnark_witness).unwrap();
file.write_all(serialized.as_bytes()).unwrap();
Expand Down Expand Up @@ -165,7 +166,7 @@ impl Groth16Prover {

// Write witness.
let mut witness_file = tempfile::NamedTempFile::new().unwrap();
let gnark_witness: Groth16Witness = witness.into();
let gnark_witness = Groth16Witness::new(witness);
let serialized = serde_json::to_string(&gnark_witness).unwrap();
witness_file.write_all(serialized.as_bytes()).unwrap();

Expand Down
11 changes: 9 additions & 2 deletions recursion/groth16-ffi/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ pub struct Groth16Witness {
pub vars: Vec<String>,
pub felts: Vec<String>,
pub exts: Vec<Vec<String>>,
pub vkey_hash: String,
pub commited_values_digest: String,
}

impl<C: Config> From<Witness<C>> for Groth16Witness {
fn from(mut witness: Witness<C>) -> Self {
impl Groth16Witness {
pub fn new<C: Config>(mut witness: Witness<C>) -> Self {
witness.vars.push(C::N::from_canonical_usize(999));
witness.felts.push(C::F::from_canonical_usize(999));
witness.exts.push(C::EF::from_canonical_usize(999));
Expand All @@ -42,6 +44,11 @@ impl<C: Config> From<Witness<C>> for Groth16Witness {
.collect()
})
.collect(),
vkey_hash: witness.vkey_hash.as_canonical_biguint().to_string(),
commited_values_digest: witness
.commited_values_digest
.as_canonical_biguint()
.to_string(),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions recursion/groth16/babybear/babybear.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,9 @@ func InvEHint(_ *big.Int, inputs []*big.Int, results []*big.Int) error {
func (c *Chip) Ext2Felt(in *ExtensionVariable) [4]*Variable {
return in.Value
}

func (c *Chip) Reduce(in *Variable) *Variable {
return &Variable{
Value: c.field.Reduce(in.Value),
}
}
Loading

0 comments on commit efbcc09

Please sign in to comment.