Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Cleanup the hash folder #247

Merged
merged 8 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plonky2x/core/src/backend/circuit/serialization/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use plonky2::util::serialization::{Buffer, GateSerializer, IoResult, Read, Write

use super::registry::{SerializationRegistry, Serializer};
use super::PlonkParameters;
use crate::frontend::hash::bit_operations::XOR3Gate;
use crate::frontend::hash::deprecated::bit_operations::XOR3Gate;
use crate::frontend::num::u32::gates::add_many_u32::U32AddManyGate;
use crate::frontend::num::u32::gates::arithmetic_u32::U32ArithmeticGate;
use crate::frontend::num::u32::gates::comparison::ComparisonGate;
Expand Down
4 changes: 2 additions & 2 deletions plonky2x/core/src/backend/circuit/serialization/hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ use crate::frontend::eth::mpt::generators::LteGenerator;
use crate::frontend::eth::storage::generators::{
EthBlockGenerator, EthLogGenerator, EthStorageKeyGenerator, EthStorageProofHint,
};
use crate::frontend::hash::bit_operations::XOR3Generator;
use crate::frontend::hash::blake2::blake2b_curta::MAX_NUM_CURTA_CHUNKS;
use crate::frontend::hash::blake2::curta::MAX_NUM_CURTA_CHUNKS;
use crate::frontend::hash::deprecated::bit_operations::XOR3Generator;
use crate::frontend::hash::keccak::keccak256::Keccak256Generator;
use crate::frontend::hint::asynchronous::generator::{AsyncHintDataRef, AsyncHintRef};
use crate::frontend::hint::asynchronous::hint::AsyncHint;
Expand Down
42 changes: 1 addition & 41 deletions plonky2x/core/src/backend/wrapper/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,11 @@ impl<L: PlonkParameters<D>, const D: usize> WrappedOutput<L, D> {

#[cfg(test)]
mod tests {
use hex::decode;

use super::*;
use crate::backend::circuit::{DefaultParameters, Groth16WrapperParameters};
use crate::frontend::builder::CircuitBuilder;
use crate::frontend::hash::sha::sha256::sha256;
use crate::utils;

fn to_bits(msg: Vec<u8>) -> Vec<bool> {
let mut res = Vec::new();
for bit in msg {
let char = bit;
for j in 0..8 {
if (char & (1 << (7 - j))) != 0 {
res.push(true);
} else {
res.push(false);
}
}
}
res
}

#[test]
#[cfg_attr(feature = "ci", ignore)]
fn test_wrapper() {
Expand Down Expand Up @@ -311,34 +293,12 @@ mod tests {
dummy_wrapped_proof.save(dummy_path).unwrap();
println!("Saved dummy_circuit");

// Set up the circuit and wrapper.
let msg = b"plonky2";
let msg_bits = to_bits(msg.to_vec());
let expected_digest = "8943a85083f16e93dc92d6af455841daacdae5081aa3125b614a626df15461eb";
let digest_bits = to_bits(decode(expected_digest).unwrap());

// Set up a inner circuit and wrapper.
let mut builder = CircuitBuilder::<DefaultParameters, 2>::new();
let targets = msg_bits
.iter()
.map(|b| builder.api.constant_bool(*b))
.collect::<Vec<_>>();
let msg_hash = sha256(&mut builder.api, &targets);
for _ in 0..5 {
let _msg_hash = sha256(&mut builder.api, &targets);
}

let a = builder.evm_read::<ByteVariable>();
let _ = builder.evm_read::<ByteVariable>();
builder.evm_write(a);

for i in 0..digest_bits.len() {
if digest_bits[i] {
builder.api.assert_one(msg_hash[i].target);
} else {
builder.api.assert_zero(msg_hash[i].target);
}
}

let circuit = builder.build();
let mut input = circuit.input();
input.evm_write::<ByteVariable>(0u8);
Expand Down
6 changes: 6 additions & 0 deletions plonky2x/core/src/frontend/builder/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
};
}

// @audit
pub fn read<V: CircuitVariable>(&mut self) -> V {
self.try_init_field_io();
let variable = self.init::<V>();
Expand All @@ -161,6 +162,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
variable
}

// @audit
pub fn evm_read<V: EvmVariable>(&mut self) -> V {
self.try_init_evm_io();
let nb_bytes = V::nb_bytes::<L, D>();
Expand All @@ -176,6 +178,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
variable
}

// @audit
pub fn proof_read(
&mut self,
child_circuit: &CircuitBuild<L, D>,
Expand All @@ -189,6 +192,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
proof
}

// @audit
pub fn write<V: CircuitVariable>(&mut self, variable: V) {
self.try_init_field_io();
match self.io {
Expand All @@ -197,6 +201,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}
}

// @audit
pub fn evm_write<V: EvmVariable>(&mut self, variable: V) {
self.try_init_evm_io();
let bytes = variable.encode(self);
Expand All @@ -206,6 +211,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}
}

// @audit
pub fn proof_write<V: CircuitVariable>(&mut self, variable: V) {
self.try_init_proof_io();
match self.io {
Expand Down
6 changes: 4 additions & 2 deletions plonky2x/core/src/frontend/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use plonky2::plonk::circuit_data::CircuitConfig;
use tokio::runtime::Runtime;

pub use self::io::CircuitIO;
use super::hash::blake2::blake2b_curta::Blake2bAccelerator;
use super::hash::sha::sha256_curta::Sha256Accelerator;
use super::hash::blake2::curta::Blake2bAccelerator;
use super::hash::sha256::curta::Sha256Accelerator;
use super::hint::HintGenerator;
use super::vars::EvmVariable;
use crate::backend::circuit::{CircuitBuild, DefaultParameters, MockCircuitBuild, PlonkParameters};
Expand Down Expand Up @@ -311,6 +311,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
self.api.inverse(i1.0).into()
}

// @audit
/// If selector is true, yields i1 else yields i2.
pub fn select<V: CircuitVariable>(&mut self, selector: BoolVariable, i1: V, i2: V) -> V {
assert_eq!(i1.targets().len(), i2.targets().len());
Expand Down Expand Up @@ -347,6 +348,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}
result
}
// @end-audit

/// Connects two variables.
pub fn connect<V: CircuitVariable>(&mut self, i1: V, i2: V) {
Expand Down
1 change: 1 addition & 0 deletions plonky2x/core/src/frontend/builder/permutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<L: PlonkParameters<D>, const D: usize, const B: usize> Hint<L, D>
}

impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
// @no-audit-okay
pub fn permute_with_dummy<const B: usize>(
&mut self,
inputs: ArrayVariable<U32Variable, B>,
Expand Down
1 change: 1 addition & 0 deletions plonky2x/core/src/frontend/builder/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
.verify_proof::<P::Config>(proof_with_pis, inner_verifier_data, inner_common_data);
}

// @ audit
pub fn constant_verifier_data<P: PlonkParameters<D, Field = L::Field>>(
&mut self,
data: &CircuitData<P::Field, P::Config, D>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
// Verifies and constrains a STARK proof from Curta's SHA256 gadget.
pub fn constrain_sha256_gadget(
&mut self,
gadget: SHA256BuilderGadget<L::Field, L::CubicParams, D>,
gadget: &SHA256BuilderGadget<L::Field, L::CubicParams, D>,
) {
let mut input_stream = VariableStream::new();

Expand Down
4 changes: 2 additions & 2 deletions plonky2x/core/src/frontend/ecc/ed25519/gadgets/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use plonky2::plonk::circuit_builder::CircuitBuilder as BaseCircuitBuilder;
use plonky2::util::serialization::{Buffer, IoResult};

use crate::frontend::ecc::ed25519::curve::curve_types::{AffinePoint, Curve};
use crate::frontend::hash::bit_operations::util::biguint_to_bits_target;
use crate::frontend::hash::deprecated::bit_operations::util::biguint_to_bits_target;
use crate::frontend::num::biguint::{CircuitBuilderBiguint, WitnessBigUint};
use crate::frontend::num::nonnative::nonnative::{
CircuitBuilderNonNative, NonNativeTarget, ReadNonNativeTarget, WriteNonNativeTarget,
Expand Down Expand Up @@ -408,7 +408,7 @@ mod tests {
use crate::frontend::ecc::ed25519::gadgets::curve::{
AffinePointTarget, CircuitBuilderCurve, CircuitBuilderCurveGadget,
};
use crate::frontend::hash::bit_operations::util::biguint_to_bits_target;
use crate::frontend::hash::deprecated::bit_operations::util::biguint_to_bits_target;
use crate::frontend::num::biguint::CircuitBuilderBiguint;
use crate::prelude::{Bytes32Variable, DefaultBuilder};

Expand Down
2 changes: 1 addition & 1 deletion plonky2x/core/src/frontend/ecc/ed25519/gadgets/eddsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use plonky2::plonk::circuit_builder::CircuitBuilder as BaseCircuitBuilder;
use crate::frontend::ecc::ed25519::curve::curve_types::Curve;
use crate::frontend::ecc::ed25519::field::ed25519_scalar::Ed25519Scalar;
use crate::frontend::ecc::ed25519::gadgets::curve::{AffinePointTarget, CircuitBuilderCurve};
use crate::frontend::hash::sha::sha512::{
use crate::frontend::hash::deprecated::sha512::{
sha512, sha512_variable, CHUNK_BITS_1024, LENGTH_BITS_128,
};
use crate::frontend::num::biguint::BigUintTarget;
Expand Down
37 changes: 0 additions & 37 deletions plonky2x/core/src/frontend/hash/bit_operations/u8.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<L: PlonkParameters<D>, const D: usize> Blake2bAccelerator<L, D> {

impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
/// Pads a BLAKE2B input
pub fn curta_blake2b_pad<const MAX_NUM_CHUNKS: usize>(
fn pad_message_blake2b<const MAX_NUM_CHUNKS: usize>(
&mut self,
message: &[ByteVariable],
) -> Vec<ByteVariable> {
Expand All @@ -59,7 +59,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
message: &[ByteVariable],
message_len: Variable,
) -> Bytes32Variable {
let padded_message = self.curta_blake2b_pad::<MAX_NUM_CHUNKS>(message);
let padded_message = self.pad_message_blake2b::<MAX_NUM_CHUNKS>(message);

let message_target_bytes = padded_message
.iter()
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<L: PlonkParameters<D>, const D: usize> CircuitBuilder<L, D> {
}

/// Verifies a blake2b curta instance
pub fn curta_constrain_blake2b(&mut self, accelerator: &Blake2bAccelerator<L, D>) {
fn curta_constrain_blake2b(&mut self, accelerator: &Blake2bAccelerator<L, D>) {
let mut padded_messages = Vec::new();
let mut msg_lengths = Vec::new();
let mut digests = Vec::new();
Expand Down
3 changes: 1 addition & 2 deletions plonky2x/core/src/frontend/hash/blake2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//! An implementation of the Blake2 hash functions in a plonky2 circuit

pub mod blake2b;
pub mod blake2b_curta;
pub mod curta;
Loading