Multiplication gate on a vector

Hi,
I was curious how does one implement a multiplication gate between two vectors? Here what I mean is suppose one has
(1,2,3,4)*(1,0,0,1) = (1,0,0,4)
Any idea as to how I can do that?

Thanks in advance!

Same as for any other type, which is to say, by providing an impl std::ops::Add for MyMathVector.

So you are saying, I can use something like, std::ops::MulAssign ?

The thing is, I kind of need to use the function mul from bellman/num.rs at main · zkcrypto/bellman · GitHub

I just don't understand the way to write it, I am getting quite a few errors.

Any help will be great.

Thanks in advance!

What errors are you getting from what code?

1 Like

Hi, sorry for being so late.
But here is the code I am trying to implement.

fn multiply<Scalar: PrimeField, CS: ConstraintSystem<Scalar>>(
    mut cs: CS,
    a: &[Boolean], r: &[Boolean]
) -> Result<Vec<Boolean>, SynthesisError> {
    // Flip endianness of each input byte
    let input_1: Vec<_> = a
        .chunks(8)
        .map(|c| c.iter().rev())
        .flatten()
        .cloned()
        .collect();
    let input_2: Vec<_> = r
        .chunks(8)
        .map(|c| c.iter().rev())
        .flatten()
        .cloned()
        .collect();
    let mid = input_1.iter().zip(input_2)
    .map(|(x, y)| &x.mul(&mut cs, &x, &y))
    .collect::<Result<Vec<Scalar>, SynthesisError>>()?;

    let res = mid.into_iter().enumerate().map(|(i, b)| {AllocatedBit::alloc(cs.namespace(|| format!("a bit {}", i)), b)
       })// Convert the AllocatedBits into Booleans 
            .map(|b| b.map(Boolean::from))
            .collect::<Result<Vec<_>, _>>()?;
	
    
        Ok(res
    //Ok(mid
        .chunks(8)
        .map(|c| c.iter().rev())
        .flatten()
        .cloned()
        .collect())
}

The errors I get are

error[E0599]: no method named `mul` found for reference `&Boolean` in the current scope
  --> src/main.rs:84:22
   |
84 |     .map(|(x, y)| &x.mul(&mut cs, &x, &y))
   |                      ^^^ method not found in `&Boolean`

error[E0308]: mismatched types
  --> src/main.rs:87:118
   |
66 | fn multiply<Scalar: PrimeField, CS: ConstraintSystem<Scalar>>(
   |           ------ this type parameter
...
87 |     let res = mid.into_iter().enumerate().map(|(i, b)| {AllocatedBit::alloc(cs.namespace(|| format!("a bit {}", i)), b)
   |                                                                                                                      ^ expected enum `Option`, found type parameter `Scalar`
   |
   = note:        expected enum `Option<bool>`
           found type parameter `Scalar`

error: aborting due to 2 previous errors; 4 warnings emitted

I want to use the public function mul in bellman/num.rs at main · zkcrypto/bellman · GitHub to multiply two vectors such that (3,3,3,3)*(0,1,0,1) = (0,3,0,3)

I just don't actually know how exactly I should write it.

Thanks in advance!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.