1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use bellman::pairing::{Engine,};
use bellman::pairing::ff::{Field, PrimeField};
use bellman::{ConstraintSystem, SynthesisError};
use super::boolean::{Boolean};
use super::num::{Num, AllocatedNum};
use super::Assignment;

/// Takes a sequence of booleans and exposes them as compact
/// public inputs
pub fn pack_into_inputs<E, CS>(
    mut cs: CS,
    bits: &[Boolean]
) -> Result<(), SynthesisError>
    where E: Engine, CS: ConstraintSystem<E>
{
    for (i, bits) in bits.chunks(E::Fr::CAPACITY as usize).enumerate()
    {
        let mut num = Num::<E>::zero();
        let mut coeff = E::Fr::one();
        for bit in bits {
            num = num.add_bool_with_coeff(CS::one(), bit, coeff);

            coeff.double();
        }

        let input = cs.alloc_input(|| format!("input {}", i), || {
            Ok(*num.get_value().get()?)
        })?;

        // num * 1 = input
        cs.enforce(
            || format!("packing constraint {}", i),
            |_| num.lc(E::Fr::one()),
            |lc| lc + CS::one(),
            |lc| lc + input
        );
    }

    Ok(())
}

/// Takes a sequence of booleans and exposes them as compact
/// variables
pub fn pack_into_variables<E, CS>(
    mut cs: CS,
    bits: &[Boolean]
) -> Result<Vec<AllocatedNum<E>>, SynthesisError>
    where E: Engine, CS: ConstraintSystem<E>
{
    let mut result = vec![];
    for (i, bits) in bits.chunks(E::Fr::CAPACITY as usize).enumerate()
    {
        let mut num = Num::<E>::zero();
        let mut coeff = E::Fr::one();
        for bit in bits {
            num = num.add_bool_with_coeff(CS::one(), bit, coeff);

            coeff.double();
        }

        let variable = AllocatedNum::<E>::alloc(
            cs.namespace(|| format!("variable {}", i)),
            || {
                let value = *num.get_value().get()?;

                Ok(value)
            }
        )?;

        // num * 1 = variable
        cs.enforce(
            || format!("packing constraint {}", i),
            |_| num.lc(E::Fr::one()),
            |lc| lc + CS::one(),
            |lc| lc + variable.get_variable()
        );

        result.push(variable);
    }

    Ok(result)
}

pub fn bytes_to_bits(bytes: &[u8]) -> Vec<bool>
{
    bytes.iter()
         .flat_map(|&v| (0..8).rev().map(move |i| (v >> i) & 1 == 1))
         .collect()
}

pub fn bytes_to_bits_le(bytes: &[u8]) -> Vec<bool>
{
    bytes.iter()
         .flat_map(|&v| (0..8).map(move |i| (v >> i) & 1 == 1))
         .collect()
}

pub fn compute_multipacking<E: Engine>(
    bits: &[bool]
) -> Vec<E::Fr>
{
    let mut result = vec![];

    for bits in bits.chunks(E::Fr::CAPACITY as usize)
    {
        let mut cur = E::Fr::zero();
        let mut coeff = E::Fr::one();

        for bit in bits {
            if *bit {
                cur.add_assign(&coeff);
            }

            coeff.double();
        }

        result.push(cur);
    }

    result
}

#[test]
fn test_multipacking() {
    use rand::{SeedableRng, Rng, XorShiftRng};
    use bellman::{ConstraintSystem};
    use bellman::pairing::bls12_381::{Bls12};
    use ::circuit::test::*;
    use super::boolean::{AllocatedBit, Boolean};

    let mut rng = XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);

    for num_bits in 0..1500 {
        let mut cs = TestConstraintSystem::<Bls12>::new();

        let bits: Vec<bool> = (0..num_bits).map(|_| rng.gen()).collect();

        let circuit_bits = bits.iter().enumerate()
                               .map(|(i, &b)| {
                                   Boolean::from(
                                     AllocatedBit::alloc(
                                        cs.namespace(|| format!("bit {}", i)),
                                        Some(b)
                                     ).unwrap()
                                   )
                               })
                               .collect::<Vec<_>>();

        let expected_inputs = compute_multipacking::<Bls12>(&bits);

        pack_into_inputs(cs.namespace(|| "pack"), &circuit_bits).unwrap();

        assert!(cs.is_satisfied());
        assert!(cs.verify(&expected_inputs));
    }
}