Skip to content

Commit

Permalink
boom (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas authored Aug 29, 2023
1 parent df64954 commit 5660785
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plonky2x/src/backend/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ where
} else if self.io.field.is_some() {
self.io.field.clone().unwrap().input_variables
} else {
todo!()
vec![]
};
assert_eq!(input_variables.len(), input.buffer.len());

Expand Down
1 change: 1 addition & 0 deletions plonky2x/src/frontend/eth/beacon/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) mod tests {
let client = BeaconClient::new(consensus_rpc);

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

builder.set_beacon_client(client);

let block_root = builder.constant::<Bytes32Variable>(bytes32!(
Expand Down
29 changes: 19 additions & 10 deletions plonky2x/src/frontend/hash/keccak/keccak256.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use core::marker::PhantomData;

use ethers::types::H256;
use ethers::utils::keccak256;
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::{RichField};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::generator::{GeneratedValues, SimpleGenerator};
use plonky2::iop::target::Target;
use plonky2::iop::witness::PartitionWitness;
use plonky2::plonk::circuit_data::CommonCircuitData;
use plonky2::util::serialization::{Buffer, IoResult, Write, Read};
use ethers::utils::keccak256;
use ethers::types::H256;

use plonky2::util::serialization::{Buffer, IoResult, Read, Write};

use crate::frontend::vars::{Variable, ByteVariable, Bytes32Variable, CircuitVariable};
use crate::frontend::vars::{ByteVariable, Bytes32Variable, CircuitVariable, Variable};

#[derive(Debug, Clone)]
pub struct Keccack256Generator<F: RichField + Extendable<D>, const D: usize> {
Expand All @@ -30,7 +29,12 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>

fn dependencies(&self) -> Vec<Target> {
let mut targets: Vec<Target> = Vec::new();
targets.extend(self.input.iter().flat_map(|x| x.targets()).collect::<Vec<Target>>());
targets.extend(
self.input
.iter()
.flat_map(|x| x.targets())
.collect::<Vec<Target>>(),
);
if let Some(length) = self.length {
targets.extend(length.targets());
}
Expand All @@ -51,7 +55,11 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
#[allow(unused_variables)]
fn serialize(&self, dst: &mut Vec<u8>, common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
// Write each input as a target
let input_bytes = self.input.iter().flat_map(|x| x.targets() ).collect::<Vec<Target>>();
let input_bytes = self
.input
.iter()
.flat_map(|x| x.targets())
.collect::<Vec<Target>>();
dst.write_target_vec(&input_bytes)?;

dst.write_target_vec(&self.output.targets())?;
Expand Down Expand Up @@ -80,7 +88,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
let output_targets = src.read_target_vec()?;
let output = Bytes32Variable::from_targets(&input_targets);

let length = src.read_target_vec()
let length = src
.read_target_vec()
.map(|targets| Some(Variable::from_targets(&targets)))
.unwrap_or(None);

Expand All @@ -91,4 +100,4 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
_phantom: PhantomData::<F>,
})
}
}
}
50 changes: 50 additions & 0 deletions plonky2x/src/frontend/hash/keccak/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
//! An implementation of the keccak256 hash functions in a plonky2 circuit
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;

use self::keccak256::Keccack256Generator;
use crate::frontend::vars::Bytes32Variable;
use crate::prelude::{ByteVariable, CircuitBuilder};

pub mod keccak256;

impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
pub fn keccak256(&mut self, bytes: &[ByteVariable]) -> Bytes32Variable {
let generator = Keccack256Generator {
input: bytes.to_vec(),
output: self.init(),
length: None,
_phantom: Default::default(),
};
self.add_simple_generator(&generator);
generator.output
}
}

#[cfg(test)]
mod tests {
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::plonk::config::PoseidonGoldilocksConfig;

use super::*;
use crate::prelude::CircuitBuilder;
use crate::utils::bytes32;

#[test]
fn test_keccak256() {
env_logger::init();

type F = GoldilocksField;
type C = PoseidonGoldilocksConfig;
const D: usize = 2;

let mut builder = CircuitBuilder::<F, D>::new();
let word = builder.constant::<Bytes32Variable>(bytes32!(
"0x0000000000000000000000000000000000000000000000000000000000000000"
));
let hash = builder.keccak256(&word.0 .0);
builder.watch(&hash, "hi");

let circuit = builder.build::<C>();
let input = circuit.input();
let (_, _) = circuit.prove(&input);
}
}

0 comments on commit 5660785

Please sign in to comment.