-
Notifications
You must be signed in to change notification settings - Fork 22
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
Added serialization support for compatibility with latest plonky2 head #3
Open
cf
wants to merge
2
commits into
0xPolygonZero:main
Choose a base branch
from
cf:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
use itertools::Itertools; | ||
use core::marker::PhantomData; | ||
use plonky2::util::serialization::{Buffer, IoResult, Read, Write}; | ||
|
||
use alloc::string::{String, ToString}; | ||
use num::{BigUint, Integer, Zero}; | ||
use plonky2::field::extension::Extendable; | ||
use plonky2::field::types::{PrimeField, PrimeField64}; | ||
|
@@ -13,7 +16,6 @@ use plonky2::plonk::circuit_builder::CircuitBuilder; | |
use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target}; | ||
use plonky2_u32::gadgets::multiple_comparison::list_le_u32_circuit; | ||
use plonky2_u32::witness::{GeneratedValuesU32, WitnessU32}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct BigUintTarget { | ||
pub limbs: Vec<U32Target>, | ||
|
@@ -27,6 +29,15 @@ impl BigUintTarget { | |
pub fn get_limb(&self, i: usize) -> U32Target { | ||
self.limbs[i] | ||
} | ||
pub fn write_to_serializer(&self, dst: &mut Vec<u8>) -> IoResult<()> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: spacing between functions |
||
dst.write_target_vec(&self.limbs.iter().map(|l| l.0).collect_vec()) | ||
} | ||
pub fn deserialize(src: &mut Buffer) -> IoResult<Self> { | ||
let limbs = src.read_target_vec()?; | ||
Ok(Self { | ||
limbs: limbs.into_iter().map(|l| U32Target(l)).collect(), | ||
}) | ||
} | ||
} | ||
|
||
pub trait CircuitBuilderBiguint<F: RichField + Extendable<D>, const D: usize> { | ||
|
@@ -342,6 +353,31 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F> | |
out_buffer.set_biguint_target(&self.div, &div); | ||
out_buffer.set_biguint_target(&self.rem, &rem); | ||
} | ||
|
||
fn id(&self) -> String { | ||
"BigUintDivRemGenerator".to_string() | ||
} | ||
|
||
fn serialize(&self, dst: &mut Vec<u8>) -> IoResult<()> { | ||
self.a.write_to_serializer(dst)?; | ||
self.b.write_to_serializer(dst)?; | ||
self.div.write_to_serializer(dst)?; | ||
self.rem.write_to_serializer(dst) | ||
} | ||
|
||
fn deserialize(src: &mut Buffer) -> IoResult<Self> { | ||
let a = BigUintTarget::deserialize(src)?; | ||
let b = BigUintTarget::deserialize(src)?; | ||
let div = BigUintTarget::deserialize(src)?; | ||
let rem = BigUintTarget::deserialize(src)?; | ||
Ok(Self { | ||
a, | ||
b, | ||
div, | ||
rem, | ||
_phantom: PhantomData, | ||
}) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For all of these (especially your forked repo here), is there a reason for using these specific commits?