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

[Feature] Fixed-size unsigned integer target types #340

Merged
merged 34 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4a18f28
feat(circuit_framework): Impl uint target types and implement zero, o…
Xearty Jun 6, 2024
dcfa6bb
feat(circuit_framework): Implement div for uint targets
Xearty Jun 6, 2024
bbf1ee1
feat(circuit_framework): Implement mul for uint targets
Xearty Jun 6, 2024
33ff88b
feat(circuit_framework): Implement rem for uint targets
Xearty Jun 6, 2024
3555189
feat(circuit_framework): Implement less than or equal to for uint tar…
Xearty Jun 6, 2024
14decf8
refactor(circuit_framework): Refactor uint targets
Xearty Jun 6, 2024
a13b57d
feat(circuit_framework): Implement Uint32Target and rename other Uint…
Xearty Jun 6, 2024
7ab06e7
revert(circuit_framework): Delete Uint32Target type because of shift …
Xearty Jun 6, 2024
54411df
feat(circuit_framework): Implement equality for uint targets
Xearty Jun 6, 2024
0f9901e
feat(circuit_framework): Automatically implement the rest of the comp…
Xearty Jun 6, 2024
2342a3b
feat(circuit_framework): Implement sub for uint targets
Xearty Jun 6, 2024
b3c17ff
fix(circuit_framework): Fix a bug with gte for uint targets
Xearty Jun 6, 2024
62b9e94
feat(circuit_framework): Implement constant for uint targets
Xearty Jun 6, 2024
621a16b
refactor(circuit_framework): Delete unused stuff
Xearty Jun 6, 2024
d92298f
refactor(circuit_framework): Delete unnecesarry qualification
Xearty Jun 6, 2024
b1875b3
feat(circuit_framework): Implement serialization/deserialization for …
Xearty Jun 6, 2024
f76019a
feat(circuit_framework): Implement ssz serialization for Uint64Target
Xearty Jun 6, 2024
c3120e1
feat(circuit_framework): Implement `to_biguint` to uint targets
Xearty Jun 6, 2024
5b46705
refactor(circuit_framework): Refactor `ssz_hash_tree_root` to use pad…
Xearty Jun 6, 2024
04ff69c
feat(circuit_framework): Implement u64/str and u128/str serde
Xearty Jun 8, 2024
b82067b
feat(circuit_framework): Implement le_bits converters for uint targets
Xearty Jun 11, 2024
63b7891
feat(circuit_framework): Implement le_bytes conversions for uint targets
Xearty Jun 11, 2024
91211d6
feat(circuit_framework): Implement biguint truncation for uint targets
Xearty Jun 11, 2024
994b9b4
feat(circuit_framework): Implement `to_be_bits` for uint targets
Xearty Jun 12, 2024
7b4c911
fix(circuit_framework): Fix `ssz_hash_tree_root` for `Uint64Target`
Xearty Jun 12, 2024
1720b69
refactor(circuit_framework): Change the order of parameters of uint t…
Xearty Jun 12, 2024
70f136a
fix(circuit_framework): Assert the uint limbs are valid properly
Xearty Jun 30, 2024
9841e1c
refactor(circuit_framework): Derive the number of limbs from the prim…
Xearty Jun 30, 2024
79ba305
refactor(circuit_framework): Rename uint target definition macro
Xearty Jun 30, 2024
28f993f
refactor(circuits): Make circuits use fixed-size uint targets
Xearty Jun 11, 2024
5b18507
refactor(circuit_framework): Implement `ssz_hash_tree_root` for `Bool…
Xearty Jun 18, 2024
87fffd3
refactor(): Document `get_balance_from_leaf` function
Xearty Jun 30, 2024
05f13d8
refactor(): Delete unused function `biguint_is_equal_non_equal_limbs`
Xearty Jun 30, 2024
ce64884
refactor(): Delete unused biguint utility functions and renaming
Xearty Jun 30, 2024
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: 2 additions & 0 deletions beacon-light-client/plonky2/crates/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions beacon-light-client/plonky2/crates/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition.workspace = true

[dependencies]
circuit_derive = { path = "../circuit_derive" }
plonky2 = { git = "https://github.com/metacraft-labs/plonky2" }
plonky2_crypto = { git = "https://github.com/metacraft-labs/plonky2-crypto" }
starky = { git = "https://github.com/metacraft-labs/plonky2" }
Expand All @@ -12,3 +13,4 @@ itertools = "0.10.5"
deriving_via = "1.6.1"
serde = "1.0.164"
serde-big-array = "0.5.1"
primitive-types = "0.12.2"
4 changes: 4 additions & 0 deletions beacon-light-client/plonky2/crates/circuit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ pub mod array;
pub mod circuit;
pub mod circuit_builder_extensions;
pub mod public_inputs;
pub mod serde;
pub mod serde_circuit_target;
pub mod set_witness;
pub mod ssz_hash_tree_root;
pub mod target_primitive;
pub mod targets;
pub mod to_targets;

pub use add_virtual_target::AddVirtualTarget;
Expand All @@ -20,6 +23,7 @@ pub use public_inputs::{
};
pub use serde_circuit_target::SerdeCircuitTarget;
pub use set_witness::SetWitness;
pub use ssz_hash_tree_root::SSZHashTreeRoot;
pub use target_primitive::TargetPrimitive;
pub use to_targets::ToTargets;

Expand Down
73 changes: 73 additions & 0 deletions beacon-light-client/plonky2/crates/circuit/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
pub mod serde_u64_str {
use core::fmt;

use serde::{de::Visitor, Deserializer, Serializer};

pub fn serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&value.to_string())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
struct Vis;

impl<'de> Visitor<'de> for Vis {
type Value = u64;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.parse().unwrap())
}
}

deserializer.deserialize_str(Vis)
}
}

pub mod serde_u128_str {
use core::fmt;

use serde::{de::Visitor, Deserializer, Serializer};

pub fn serialize<S>(value: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&value.to_string())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
struct Vis;

impl<'de> Visitor<'de> for Vis {
type Value = u128;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.parse().unwrap())
}
}

deserializer.deserialize_str(Vis)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use plonky2::{
plonk::{circuit_data::VerifierCircuitTarget, proof::ProofWithPublicInputsTarget},
util::serialization::{Buffer, IoResult, Read, Write},
};
use plonky2_crypto::biguint::BigUintTarget;
use plonky2_crypto::{biguint::BigUintTarget, u32::arithmetic_u32::U32Target};
use starky::proof::{StarkOpeningSetTarget, StarkProofTarget, StarkProofWithPublicInputsTarget};

use crate::{Circuit, PublicInputsTargetReadable};
Expand Down Expand Up @@ -118,8 +118,24 @@ impl<T: SerdeCircuitTarget + std::fmt::Debug, const N: usize> SerdeCircuitTarget
}
}

impl SerdeCircuitTarget for U32Target {
fn serialize(&self) -> IoResult<Vec<u8>> {
let mut buffer: Vec<u8> = Vec::new();
buffer.write_target(self.0)?;
Ok(buffer)
}

fn deserialize(buffer: &mut Buffer) -> IoResult<Self>
where
Self: Sized,
{
let target = buffer.read_target()?;
Ok(Self(target))
}
}

impl SerdeCircuitTarget for BigUintTarget {
fn serialize(&self) -> plonky2::util::serialization::IoResult<Vec<u8>> {
fn serialize(&self) -> IoResult<Vec<u8>> {
assert_eq!(self.num_limbs(), 2);

let mut buffer: Vec<u8> = Vec::new();
Expand All @@ -128,9 +144,7 @@ impl SerdeCircuitTarget for BigUintTarget {
Ok(buffer)
}

fn deserialize(
buffer: &mut plonky2::util::serialization::Buffer,
) -> plonky2::util::serialization::IoResult<Self>
fn deserialize(buffer: &mut Buffer) -> IoResult<Self>
where
Self: Sized,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use itertools::Itertools;
use plonky2::{
field::extension::Extendable, hash::hash_types::RichField, iop::target::BoolTarget,
plonk::circuit_builder::CircuitBuilder,
};

use crate::targets::uint::Uint64Target;

pub trait SSZHashTreeRoot<F: RichField + Extendable<D>, const D: usize> {
fn ssz_hash_tree_root(self, builder: &mut CircuitBuilder<F, D>) -> [BoolTarget; 256];
}

impl<F: RichField + Extendable<D>, const D: usize> SSZHashTreeRoot<F, D> for Uint64Target {
fn ssz_hash_tree_root(self, builder: &mut CircuitBuilder<F, D>) -> [BoolTarget; 256] {
let _false = builder._false();

self.to_le_bytes(builder)
.into_iter()
.pad_using(256, |_| _false)
.collect_vec()
.try_into()
.unwrap()
}
}

impl<F: RichField + Extendable<D>, const D: usize> SSZHashTreeRoot<F, D> for BoolTarget {
fn ssz_hash_tree_root(self, builder: &mut CircuitBuilder<F, D>) -> [BoolTarget; 256] {
let mut ssz_leaf = [builder._false(); 256];
ssz_leaf[7] = self;
ssz_leaf
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod uint;
Loading