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

refac: upstream latest plonky2 #7

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ ark-std = "0.4.0"
num-bigint = "0.4.3"
num-traits = "0.2"
rand = "0.8.5"
plonky2_ecdsa = { git = "https://github.com/Lagrange-Labs/plonky2-ecdsa", features = [
plonky2_ecdsa = { git = "https://github.com/Lagrange-Labs/plonky2-ecdsa", rev = "5dd47af", features = [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need to update after merging PR Lagrange-Labs/plonky2-ecdsa#3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in commit c37edcd. Thanks!

"parallel",
] }
plonky2_crypto = { git = "https://github.com/Lagrange-Labs/plonky2-crypto" }
Expand Down
57 changes: 47 additions & 10 deletions src/fields/fq_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,64 @@ impl<F: RichField + Extendable<D>, const D: usize> FqTarget<F, D> {
pub fn is_equal(&self, builder: &mut CircuitBuilder<F, D>, rhs: &Self) -> BoolTarget {
let a_limbs = self.target.value.limbs.iter().map(|x| x.0).collect_vec();
let b_limbs = rhs.target.value.limbs.iter().map(|x| x.0).collect_vec();
assert_eq!(a_limbs.len(), b_limbs.len());

let terms = a_limbs
if a_limbs.len() == b_limbs.len() {
self.compare_same_length(builder, &a_limbs, &b_limbs)
} else {
self.compare_different_length(builder, &a_limbs, &b_limbs)
}
}

fn compare_same_length(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I fail to see why the two functions are different - if you give same length inputs to the compare_different_lengths then it should be returning the same no ?
I think the only change required is to move if lhs.len() < rhs.len() to if lhs.len() <= rhs.len().
In case it's equal for &limb in longer.iter().skip(shorter.len()) { will never run so it'll give the same results no ?

TLDR do we really need this new function ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, it could be simplified as you said. Fixed in commit 698cc66, thanks!

&self,
builder: &mut CircuitBuilder<F, D>,
lhs: &Vec<Target>,
rhs: &Vec<Target>,
) -> BoolTarget {
let terms = lhs
.iter()
.zip(b_limbs)
.map(|(&a, b)| builder.is_equal(a, b).target)
.zip(rhs)
.map(|(&a, b)| builder.is_equal(a, *b).target)
.collect_vec();
let is_equal = builder.mul_many(terms);

// is_equal is ensured to be 0 or 1, so we can safely convert it to bool.
BoolTarget::new_unsafe(is_equal)
}

pub fn is_zero(&self, builder: &mut CircuitBuilder<F, D>) -> BoolTarget {
fn compare_different_length(
&self,
builder: &mut CircuitBuilder<F, D>,
lhs: &Vec<Target>,
rhs: &Vec<Target>,
) -> BoolTarget {
let (shorter, longer) = if lhs.len() < rhs.len() {
(&lhs, &rhs)
} else {
(&rhs, &lhs)
};
let mut terms = Vec::with_capacity(longer.len());

// Compare common limbs
for (a, b) in shorter.iter().zip(longer.iter()) {
terms.push(builder.is_equal(*a, *b).target);
}

let zero = builder.zero();
let mut t = builder._true();
for l in self.target.value.limbs.iter() {
let eq = builder.is_equal(zero, l.0);
t = builder.and(eq, t);
// Check if the remaining limbs in the longer one are all zeros
for &limb in longer.iter().skip(shorter.len()) {
terms.push(builder.is_equal(limb, zero).target);
}
t

let is_equal = builder.mul_many(terms);

// is_equal is ensured to be 0 or 1, so we can safely convert it to bool.
BoolTarget::new_unsafe(is_equal)
}

pub fn is_zero(&self, builder: &mut CircuitBuilder<F, D>) -> BoolTarget {
let zero = Self::zero(builder);
self.is_equal(builder, &zero)
}

pub fn constant(builder: &mut CircuitBuilder<F, D>, c: Fq) -> Self {
Expand Down