Skip to content

Commit

Permalink
Implement MemoSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
porcuquine committed Dec 23, 2023
1 parent 031d3a6 commit 193980d
Show file tree
Hide file tree
Showing 8 changed files with 1,082 additions and 0 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ disallowed-methods = [
{ path = "pasta_curves::Fp", reason = "use pasta_curves::pallas::Base or pasta_curves::vesta::Scalar instead to communicate your intent" },
{ path = "pasta_curves::Fq", reason = "use pasta_curves::pallas::Scalar or pasta_curves::vesta::Base instead to communicate your intent" },
]
allow-dbg-in-tests = true
22 changes: 22 additions & 0 deletions src/circuit/gadgets/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,28 @@ pub(crate) fn div<F: PrimeField, CS: ConstraintSystem<F>>(
Ok(res)
}

pub(crate) fn invert<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
a: &AllocatedNum<F>,
) -> Result<AllocatedNum<F>, SynthesisError> {
let inv = AllocatedNum::alloc(cs.namespace(|| "invert"), || {
let inv = (a.get_value().ok_or(SynthesisError::AssignmentMissing)?).invert();

let inv_opt: Option<_> = inv.into();
inv_opt.ok_or(SynthesisError::DivisionByZero)
})?;

// inv * a = 1
cs.enforce(
|| "inversion",
|lc| lc + inv.get_variable(),
|lc| lc + a.get_variable(),
|lc| lc + CS::one(),
);

Ok(inv)
}

/// Select the nth element of `from`, where `path_bits` represents n, least-significant bit first.
/// The returned result contains the selected element, and constraints are enforced.
/// `from.len()` must be a power of two.
Expand Down
8 changes: 8 additions & 0 deletions src/circuit/gadgets/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ impl<F: LurkField> AllocatedPtr<F> {
&self.hash
}

pub fn get_value<T: Tag>(&self) -> Option<ZPtr<T, F>> {
self.tag.get_value().and_then(|tag| {
self.hash
.get_value()
.map(|hash| ZPtr::from_parts(Tag::from_field(&tag).expect("bad tag"), hash))
})
}

pub fn enforce_equal<CS: ConstraintSystem<F>>(&self, cs: &mut CS, other: &Self) {
// debug_assert_eq!(self.tag.get_value(), other.tag.get_value());
enforce_equal(cs, || "tags equal", &self.tag, &other.tag);
Expand Down
Loading

0 comments on commit 193980d

Please sign in to comment.