add mul pseudocode

This commit is contained in:
collin 2020-07-17 14:15:22 -07:00
parent 7405a6284a
commit 6f9db500c7
2 changed files with 16 additions and 1 deletions

View File

@ -1,7 +1,7 @@
use snarkos_models::gadgets::utilities::boolean::Boolean; use snarkos_models::gadgets::utilities::boolean::Boolean;
/// Sign extends an array of bits to the desired length. /// Sign extends an array of bits to the desired length.
/// Least significant bit first /// Expects least significant bit first
pub trait SignExtend pub trait SignExtend
where where
Self: std::marker::Sized, Self: std::marker::Sized,

View File

@ -23,10 +23,25 @@ use snarkos_models::{
macro_rules! mul_int_impl { macro_rules! mul_int_impl {
($($gadget: ident)*) => ($( ($($gadget: ident)*) => ($(
/// Bitwise multiplication of two signed integer objects.
impl<F: PrimeField> Mul<F> for $gadget { impl<F: PrimeField> Mul<F> for $gadget {
type ErrorType = SignedIntegerError; type ErrorType = SignedIntegerError;
fn mul<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, Self::ErrorType> { fn mul<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Self, Self::ErrorType> {
// pseudocode:
//
// res = 0;
// for (i, bit) in other.bits.enumerate() {
// shifted_self = self << i;
//
// if bit {
// res += shifted_self;
// }
// }
// return res
// Conditionally select constant result // Conditionally select constant result
let is_constant = Boolean::constant(Self::result_is_constant(&self, &other)); let is_constant = Boolean::constant(Self::result_is_constant(&self, &other));
let allocated_false = Boolean::from(AllocatedBit::alloc(&mut cs.ns(|| "false"), || Ok(false)).unwrap()); let allocated_false = Boolean::from(AllocatedBit::alloc(&mut cs.ns(|| "false"), || Ok(false)).unwrap());