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

Improve memory allocation when generating expression identifier #115

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions halo2_proofs/src/circuit/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,45 @@ impl<F: Field> Mul<F> for Value<&Assigned<F>> {
}
}

impl<F: PartialEq> PartialEq<Value<F>> for Value<F> {
fn eq(&self, other: &Value<F>) -> bool {
if let Some((a, b)) = self.inner.as_ref().zip(other.inner.as_ref()) {
a.eq(b)
} else {
self.inner.is_none() == other.inner.is_none()
}
}
}
impl<F: PartialEq> Eq for Value<F> {}

impl<F: PartialOrd> PartialOrd<Value<F>> for Value<F> {
fn partial_cmp(&self, other: &Value<F>) -> Option<std::cmp::Ordering> {
match (self.inner.as_ref(), other.inner.as_ref()) {
(None, None) => Some(std::cmp::Ordering::Equal),
(None, Some(_)) => Some(std::cmp::Ordering::Less),
(Some(_), None) => Some(std::cmp::Ordering::Greater),
adria0 marked this conversation as resolved.
Show resolved Hide resolved
(Some(a), Some(b)) => a.partial_cmp(b),
}
}
}

impl<F: PartialOrd> Ord for Value<F> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.partial_cmp(other).unwrap()
}
}

impl<V, E> Value<Result<V, E>> {
/// Transposes an `Value` of a [`Result`] into a [`Result`] of an `Value`.
pub fn transpose(self) -> Result<Value<V>, E> {
match self.inner {
Some(Ok(x)) => Ok(Value::known(x)),
Some(Err(e)) => Err(e),
None => Ok(Value::unknown()),
}
}
}

adria0 marked this conversation as resolved.
Show resolved Hide resolved
impl<V> Value<V> {
/// Returns the field element corresponding to this value.
pub fn to_field<F: Field>(&self) -> Value<Assigned<F>>
Expand Down
57 changes: 43 additions & 14 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,40 +925,69 @@ impl<F: Field> Expression<F> {
}
}

/// Identifier for this expression. Expressions with identical identifiers
/// do the same calculation (but the expressions don't need to be exactly equal
/// in how they are composed e.g. `1 + 2` and `2 + 1` can have the same identifier).
pub fn identifier(&self) -> String {
fn write_identifier<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
match self {
Expression::Constant(scalar) => format!("{:?}", scalar),
Expression::Selector(selector) => format!("selector[{}]", selector.0),
Expression::Constant(scalar) => write!(writer, "{:?}", scalar),
Expression::Selector(selector) => write!(writer, "selector[{}]", selector.0),
Expression::Fixed(query) => {
format!("fixed[{}][{}]", query.column_index, query.rotation.0)
write!(
writer,
"fixed[{}][{}]",
query.column_index, query.rotation.0
)
}
Expression::Advice(query) => {
format!("advice[{}][{}]", query.column_index, query.rotation.0)
write!(
writer,
"advice[{}][{}]",
query.column_index, query.rotation.0
)
}
Expression::Instance(query) => {
format!("instance[{}][{}]", query.column_index, query.rotation.0)
write!(
writer,
"instance[{}][{}]",
query.column_index, query.rotation.0
)
}
Expression::Challenge(challenge) => {
format!("challenge[{}]", challenge.index())
write!(writer, "challenge[{}]", challenge.index())
}
Expression::Negated(a) => {
format!("(-{})", a.identifier())
writer.write_all(b"(-")?;
a.write_identifier(writer)?;
writer.write_all(b")")
}
Expression::Sum(a, b) => {
format!("({}+{})", a.identifier(), b.identifier())
writer.write_all(b"(")?;
a.write_identifier(writer)?;
writer.write_all(b"+")?;
b.write_identifier(writer)?;
writer.write_all(b")")
}
Expression::Product(a, b) => {
format!("({}*{})", a.identifier(), b.identifier())
writer.write_all(b"(")?;
a.write_identifier(writer)?;
writer.write_all(b"*")?;
b.write_identifier(writer)?;
writer.write_all(b")")
}
Expression::Scaled(a, f) => {
format!("{}*{:?}", a.identifier(), f)
a.write_identifier(writer)?;
write!(writer, "*{:?}", f)
}
}
}

/// Identifier for this expression. Expressions with identical identifiers
/// do the same calculation (but the expressions don't need to be exactly equal
/// in how they are composed e.g. `1 + 2` and `2 + 1` can have the same identifier).
pub fn identifier(&self) -> String {
let mut cursor = std::io::Cursor::new(Vec::new());
self.write_identifier(&mut cursor).unwrap();
String::from_utf8(cursor.into_inner()).unwrap()
}

/// Compute the degree of this polynomial
pub fn degree(&self) -> usize {
match self {
Expand Down