Skip to content

Commit 265fe02

Browse files
committed
pipe ScalarVars through CanonicalLinearRelation and fix type inference
1 parent 526dda8 commit 265fe02

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

src/linear_relation/canonical.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,9 @@ impl<G: PrimeGroup> CanonicalLinearRelation<G> {
274274

275275
// Build the canonical relation
276276
let mut canonical = Self::new();
277-
canonical.num_scalars = (max_scalar_index + 1) as usize;
277+
canonical.scalar_vars = (0..=max_scalar_index as usize)
278+
.map(|i| ScalarVar(i, PhantomData))
279+
.collect();
278280

279281
// Add all group elements to the map
280282
let mut group_var_map = Vec::new();
@@ -334,19 +336,8 @@ impl<G: PrimeGroup, A: Allocator<G = G>> TryFrom<&LinearRelation<G, A>>
334336
));
335337
}
336338

339+
// Process each constraint using the canonical linear relation builder.
337340
let mut builder = CanonicalLinearRelationBuilder::default();
338-
339-
#[cfg(feature = "std")]
340-
let mut scalar_vars = HashSet::<ScalarVar<G>>::new();
341-
#[cfg(not(feature = "std"))]
342-
let mut scalar_vars = HashSet::<ScalarVar<G>>::with_hasher(RandomState::new());
343-
// Cache for deduplicating weighted group elements
344-
#[cfg(feature = "std")]
345-
let mut weighted_group_cache = HashMap::new();
346-
#[cfg(not(feature = "std"))]
347-
let mut weighted_group_cache = HashMap::with_hasher(RandomState::new());
348-
349-
// Process each constraint using the modular helper method
350341
for (lhs, rhs) in iter::zip(&relation.image, &relation.linear_combinations) {
351342
// If any group element in the image is not assigned, return `InvalidInstance`.
352343
let lhs_value = relation.heap.get_element(*lhs)?;
@@ -526,9 +517,14 @@ impl<G: PrimeGroup> CanonicalLinearRelationBuilder<G> {
526517

527518
impl<G: PrimeGroup> Default for CanonicalLinearRelationBuilder<G> {
528519
fn default() -> Self {
520+
#[cfg(feature = "std")]
521+
let weighted_group_cache = HashMap::new();
522+
#[cfg(not(feature = "std"))]
523+
let weighted_group_cache = HashMap::with_hasher(RandomState::new());
524+
529525
Self {
530526
relation: CanonicalLinearRelation::new(),
531-
weighted_group_cache: Default::default(),
527+
weighted_group_cache,
532528
}
533529
}
534530
}

src/linear_relation/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,26 @@ pub struct LinearRelation<G: PrimeGroup, A = Heap<G>> {
172172
pub image: Vec<GroupVar<G>>,
173173
}
174174

175-
impl<G: PrimeGroup, A: Allocator<G = G>> LinearRelation<G, A> {
175+
impl<G: PrimeGroup> LinearRelation<G> {
176176
/// Create a new empty [`LinearRelation`].
177-
pub fn new() -> Self
178-
where
179-
A: Default,
180-
{
177+
pub fn new() -> Self {
181178
Self {
182179
linear_combinations: Vec::new(),
183180
heap: Default::default(),
184181
image: Vec::new(),
185182
}
186183
}
184+
}
185+
186+
impl<G: PrimeGroup, A: Allocator<G = G>> LinearRelation<G, A> {
187+
/// Create a new empty [`LinearRelation`] using the given [`Allocator`].
188+
pub fn new_in(allocator: A) -> Self {
189+
Self {
190+
linear_combinations: Vec::new(),
191+
heap: allocator,
192+
image: Vec::new(),
193+
}
194+
}
187195

188196
/// Adds a new equation to the statement of the form:
189197
/// `lhs = Σ weight_i * (scalar_i * point_i)`.

src/schnorr_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<G: PrimeGroup> SigmaProtocol for CanonicalLinearRelation<G> {
240240
/// # Errors
241241
/// - Returns [`Error::VerificationFailure`] if the byte data is malformed or the length is incorrect.
242242
fn deserialize_response(&self, data: &[u8]) -> Result<Self::Response, Error> {
243-
deserialize_scalars::<G>(data, self.num_scalars).ok_or(Error::VerificationFailure)
243+
deserialize_scalars::<G>(data, self.scalar_vars.len()).ok_or(Error::VerificationFailure)
244244
}
245245

246246
fn instance_label(&self) -> impl AsRef<[u8]> {

src/tests/test_relations.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::linear_relation::{Allocator, CanonicalLinearRelation, LinearRelation,
1313
pub fn discrete_logarithm<G: PrimeGroup, R: rand::RngCore>(
1414
rng: &mut R,
1515
) -> (CanonicalLinearRelation<G>, ScalarMap<G>) {
16-
// TODO(victor/scalarvar): Why is type inference failing here?
1716
let x = G::Scalar::random(rng);
1817
let mut relation = LinearRelation::new();
1918

0 commit comments

Comments
 (0)