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

feat: blake2b plonky2 gadget #81

Merged
merged 48 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
e4d447b
blake2b compress air
kevjue Sep 7, 2023
b4d31e9
Merge branch 'main' into kevjue/blake2b
kevjue Sep 7, 2023
14b31b4
compiled compress function
kevjue Sep 7, 2023
a1ab6b2
incomplete blake2b build gadget and generator hint
kevjue Sep 7, 2023
dccac65
added the blake2b hint generator
kevjue Sep 8, 2023
01f7414
moved CurtaBytes to another file
kevjue Sep 8, 2023
1fb2799
got blake2b hint with empty string working
kevjue Sep 9, 2023
aaa2b42
alot of changes for blake2b_contrain
kevjue Sep 12, 2023
4f9bf78
one row per chunk
kevjue Sep 12, 2023
c7c0be1
doesn't panic anymore
kevjue Sep 13, 2023
c98edba
using the bus
kevjue Sep 14, 2023
911591c
circuit coded up
kevjue Sep 14, 2023
0697b04
circuit and writer compiled
kevjue Sep 14, 2023
55b24cd
added select to U32Instruction
kevjue Sep 14, 2023
621b5bb
changes
kevjue Sep 14, 2023
af2aad6
the correct trace filling is pretty close
kevjue Sep 15, 2023
4447272
fixed failing constraint for h_input column
kevjue Sep 15, 2023
e6cced0
working for small and empty messages
kevjue Sep 15, 2023
f2db853
working for long message
kevjue Sep 15, 2023
fb81b7b
working version
kevjue Sep 17, 2023
9373b81
added the cycle 12
kevjue Sep 18, 2023
4d94be8
refactoring
kevjue Sep 18, 2023
ecf16b1
alot of refactoring
kevjue Sep 19, 2023
74e102d
refactored the writer
kevjue Sep 19, 2023
b7ebf9c
removed comments
kevjue Sep 19, 2023
3e7e8fe
merged main
kevjue Sep 20, 2023
6180f21
changes for the blake2b generator
kevjue Sep 20, 2023
b61b710
got constrain_blake2b_gadget to compile
kevjue Sep 21, 2023
65ead5f
got test to compile
kevjue Sep 21, 2023
3ebfa7d
fixed bug
kevjue Sep 21, 2023
45799ae
fixed bug
kevjue Sep 21, 2023
43478ae
added some debug lines
kevjue Sep 21, 2023
82705cc
fixed a bug
kevjue Sep 21, 2023
a0a53ee
added watcher and fixed a bug
kevjue Sep 21, 2023
5a2ea6a
Merge branch 'main' into kevjue/blake2b
kevjue Sep 21, 2023
a2aa43b
addressed PR comments
kevjue Sep 21, 2023
ff5646c
created Loop struct
kevjue Sep 21, 2023
430f94b
fixed a comment
kevjue Sep 21, 2023
caeab62
Merge branch 'kevjue/blake2b' into kevjue/blake2b_generator
kevjue Sep 21, 2023
578dc34
removed print lines and created utils watcher file
kevjue Sep 21, 2023
69f723c
Merge branch 'kevjue/blake2b_generator' of github.com:succinctlabs/cu…
kevjue Sep 21, 2023
91ae9f8
removed debugging stuff
kevjue Sep 21, 2023
a235ca1
fixed lint errors
kevjue Sep 21, 2023
5a3a42b
added max_num_chunks
kevjue Sep 21, 2023
e82f018
made phantom field in blake2b builder gadget public
kevjue Sep 21, 2023
9581787
merged main
kevjue Sep 21, 2023
7525c2b
moved max_num_chunks to builder gadget
kevjue Sep 22, 2023
96d4330
some changes
kevjue Sep 22, 2023
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
96 changes: 80 additions & 16 deletions curta/src/chip/hash/blake/blake2b/builder_gadget.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::fmt::Debug;
use core::marker::PhantomData;

use plonky2::field::extension::Extendable;
Expand All @@ -6,18 +7,34 @@ use plonky2::iop::target::Target;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use serde::{Deserialize, Serialize};

use super::generator::BLAKE2BHintGenerator;
use super::generator::{BLAKE2BGenerator, BLAKE2BHintGenerator, BLAKE2BStarkData};
use super::{BLAKE2BPublicData, NUM_MIX_ROUNDS};
use crate::chip::hash::CurtaBytes;
use crate::chip::AirParameters;
use crate::math::prelude::CubicParameters;
use crate::plonky2::stark::config::CurtaConfig;
use crate::plonky2::stark::gadget::StarkGadget;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BLAKE2BBuilderGadget<F, E, const D: usize> {
pub padded_message: Vec<Target>,
pub digest: Vec<Target>,
_marker: PhantomData<(F, E)>,
pub struct BLAKE2BBuilderGadget<L: AirParameters + 'static + Clone + Debug + Send + Sync> {
pub padded_messages: Vec<Target>,
pub msg_lengths: Vec<Target>,
pub digests: Vec<Target>,
_phantom: PhantomData<L>,
}

pub trait BLAKE2BBuilder<F: RichField + Extendable<D>, E: CubicParameters<F>, const D: usize> {
impl<L: AirParameters + 'static + Clone + Debug + Send + Sync> BLAKE2BBuilderGadget<L> {
pub fn max_num_chunks(&mut self) -> usize {
L::num_rows() / NUM_MIX_ROUNDS
}
}

pub trait BLAKE2BBuilder<
F: RichField + Extendable<D>,
const D: usize,
L: AirParameters + 'static + Clone + Debug + Send + Sync,
>
{
type Gadget;

fn init_blake2b(&mut self) -> Self::Gadget;
Expand All @@ -28,18 +45,30 @@ pub trait BLAKE2BBuilder<F: RichField + Extendable<D>, E: CubicParameters<F>, co
message_len: Target,
gadget: &mut Self::Gadget,
) -> CurtaBytes<32>;

fn constrain_blake2b_gadget<
E: CubicParameters<F>,
C: CurtaConfig<D, F = F, FE = F::Extension>,
>(
&mut self,
gadget: Self::Gadget,
);
}

impl<F: RichField + Extendable<D>, E: CubicParameters<F>, const D: usize> BLAKE2BBuilder<F, E, D>
for CircuitBuilder<F, D>
impl<
F: RichField + Extendable<D>,
const D: usize,
L: AirParameters + 'static + Clone + Debug + Send + Sync,
> BLAKE2BBuilder<F, D, L> for CircuitBuilder<F, D>
{
type Gadget = BLAKE2BBuilderGadget<F, E, D>;
type Gadget = BLAKE2BBuilderGadget<L>;

fn init_blake2b(&mut self) -> Self::Gadget {
BLAKE2BBuilderGadget {
padded_message: Vec::new(),
digest: Vec::new(),
_marker: PhantomData,
padded_messages: Vec::new(),
msg_lengths: Vec::new(),
digests: Vec::new(),
_phantom: PhantomData,
}
}

Expand All @@ -49,13 +78,44 @@ impl<F: RichField + Extendable<D>, E: CubicParameters<F>, const D: usize> BLAKE2
message_len: Target,
gadget: &mut Self::Gadget,
) -> CurtaBytes<32> {
gadget.padded_message.extend_from_slice(&padded_message.0);
gadget.padded_messages.extend_from_slice(&padded_message.0);
let digest_bytes = self.add_virtual_target_arr::<32>();
let hint = BLAKE2BHintGenerator::new(&padded_message.0, message_len, digest_bytes);
self.add_simple_generator(hint);
gadget.digest.extend_from_slice(&digest_bytes);
gadget.digests.extend_from_slice(&digest_bytes);
gadget.msg_lengths.push(message_len);
CurtaBytes(digest_bytes)
}

fn constrain_blake2b_gadget<
E: CubicParameters<F>,
C: CurtaConfig<D, F = F, FE = F::Extension>,
>(
&mut self,
gadget: Self::Gadget,
) {
// Allocate public input targets
let public_blake2b_targets = BLAKE2BPublicData::add_virtual::<F, D, L>(self);

let stark_data = BLAKE2BGenerator::<F, E, C, D, L>::stark_data();
let BLAKE2BStarkData { stark, config, .. } = stark_data;

let public_input_target = public_blake2b_targets.public_input_targets(self);

let virtual_proof = self.add_virtual_stark_proof(&stark, &config);
self.verify_stark_proof(&config, &stark, &virtual_proof, &public_input_target);

let blake2b_generator = BLAKE2BGenerator::<F, E, C, D, L> {
padded_messages: gadget.padded_messages,
msg_lens: gadget.msg_lengths,
pub_values_target: public_blake2b_targets,
config,
proof_target: virtual_proof,
_phantom: PhantomData,
};

self.add_simple_generator(blake2b_generator);
}
}

#[cfg(test)]
Expand All @@ -70,13 +130,17 @@ mod tests {

use super::*;
pub use crate::chip::builder::tests::*;
use crate::chip::hash::blake::blake2b::generator::BLAKE2BAirParameters;
use crate::chip::hash::blake::blake2b::BLAKE2BGadget;
use crate::plonky2::stark::config::CurtaPoseidonGoldilocksConfig;

#[test]
fn test_blake_2b_plonky_gadget() {
type F = GoldilocksField;
type E = GoldilocksCubicParameters;
type SC = CurtaPoseidonGoldilocksConfig;
type C = PoseidonGoldilocksConfig;
type L = BLAKE2BAirParameters<F, E>;
const D: usize = 2;

let _ = env_logger::builder().is_test(true).try_init();
Expand All @@ -86,7 +150,7 @@ mod tests {
let config = CircuitConfig::standard_recursion_config();
let mut builder = CircuitBuilder::<F, D>::new(config);

let mut gadget: BLAKE2BBuilderGadget<F, E, D> = builder.init_blake2b();
let mut gadget: BLAKE2BBuilderGadget<L> = builder.init_blake2b();

let msg_target = CurtaBytes(builder.add_virtual_target_arr::<256>());
let msg_length_target = builder.add_virtual_target();
Expand All @@ -102,7 +166,7 @@ mod tests {
builder.connect(*d, *e);
}

//builder.constrain_blake2b_gadget::<C>(gadget);
builder.constrain_blake2b_gadget::<E, SC>(gadget);

let data = builder.build::<C>();
let mut pw = PartialWitness::new();
Expand Down
Loading