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

Do not rely on drop() to perform final accumulation step #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions src/circuit/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::uint32::{
UInt32
};

use super::multieq::MultiEq;
use super::multieq::{MultiEq, multi_eq};

/*
2.1. Parameters
Expand Down Expand Up @@ -202,9 +202,7 @@ fn blake2s_compression<E: Engine, CS: ConstraintSystem<E>>(
v[14] = v[14].xor(cs.namespace(|| "third xor"), &UInt32::constant(u32::max_value()))?;
}

{
let mut cs = MultiEq::new(&mut cs);

multi_eq::<_, _, _, Result<(), SynthesisError>>(&mut cs, |cs| {
for i in 0..10 {
let mut cs = cs.namespace(|| format!("round {}", i));

Expand All @@ -220,7 +218,9 @@ fn blake2s_compression<E: Engine, CS: ConstraintSystem<E>>(
mixing_g(cs.namespace(|| "mixing invocation 7"), &mut v, 2, 7, 8, 13, &m[s[12]], &m[s[13]])?;
mixing_g(cs.namespace(|| "mixing invocation 8"), &mut v, 3, 4, 9, 14, &m[s[14]], &m[s[15]])?;
}
}

Ok(())
})?;

for i in 0..8 {
let mut cs = cs.namespace(|| format!("h[{i}] ^ v[{i}] ^ v[{i} + 8]", i=i));
Expand Down
36 changes: 18 additions & 18 deletions src/circuit/multieq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ use bellman::{
Variable
};

pub fn multi_eq<E, CS, F, R>(cs: CS, f: F) -> R
where E: Engine, CS: ConstraintSystem<E>, F: FnOnce(&mut MultiEq<E, CS>) -> R
{
let mut cs = MultiEq {
cs: cs,
ops: 0,
bits_used: 0,
lhs: LinearCombination::zero(),
rhs: LinearCombination::zero()
};
let tmp = f(&mut cs);
if cs.bits_used > 0 {
cs.accumulate();
}

tmp
}

pub struct MultiEq<E: Engine, CS: ConstraintSystem<E>>{
cs: CS,
ops: usize,
Expand All @@ -20,16 +38,6 @@ pub struct MultiEq<E: Engine, CS: ConstraintSystem<E>>{
}

impl<E: Engine, CS: ConstraintSystem<E>> MultiEq<E, CS> {
pub fn new(cs: CS) -> Self {
MultiEq {
cs: cs,
ops: 0,
bits_used: 0,
lhs: LinearCombination::zero(),
rhs: LinearCombination::zero()
}
}

fn accumulate(&mut self)
{
let ops = self.ops;
Expand Down Expand Up @@ -68,14 +76,6 @@ impl<E: Engine, CS: ConstraintSystem<E>> MultiEq<E, CS> {
}
}

impl<E: Engine, CS: ConstraintSystem<E>> Drop for MultiEq<E, CS> {
fn drop(&mut self) {
if self.bits_used > 0 {
self.accumulate();
}
}
}

impl<E: Engine, CS: ConstraintSystem<E>> ConstraintSystem<E> for MultiEq<E, CS>
{
type Root = Self;
Expand Down
Loading