franklin_crypto/plonk/circuit/
tables.rs

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
use crate::bellman::pairing::Engine;

use crate::bellman::pairing::ff::{BitIterator, Field, PrimeField, PrimeFieldRepr};

use crate::bellman::SynthesisError;

use crate::bellman::plonk::better_better_cs::cs::{
    ArithmeticTerm, Coefficient, ConstraintSystem, Gate, GateInternal, LinearCombinationOfTerms, MainGate, MainGateTerm, PlonkConstraintSystemParams, PlonkCsWidth4WithNextStepParams,
    PolynomialInConstraint, PolynomialMultiplicativeTerm, TimeDilation, TrivialAssembly, Variable, Width4MainGateWithDNext,
};

use crate::bellman::plonk::better_better_cs::data_structures::PolyIdentifier;
use crate::bellman::plonk::better_better_cs::lookup_tables::*;

use super::bigint_new::*;
use super::*;
use crate::plonk::circuit::Assignment;

use crate::plonk::circuit::allocated_num::{AllocatedNum, Num};
use crate::plonk::circuit::linear_combination::LinearCombination;
use crate::plonk::circuit::simple_term::Term;

pub use crate::bellman::plonk::better_better_cs::lookup_tables::RANGE_CHECK_SINGLE_APPLICATION_TABLE_NAME;

const DEFAULT_RANGE_TABLE_NAME_PREFIX: &'static str = "Range check table over 3 columns for";

pub fn inscribe_default_range_table_for_bit_width_over_first_three_columns<E: Engine, CS: ConstraintSystem<E>>(cs: &mut CS, width: usize) -> Result<(), SynthesisError> {
    // inscribe_range_table_for_bit_width_over_first_three_columns(cs, width).map(|_| ())
    let over = vec![PolyIdentifier::VariablesPolynomial(0), PolyIdentifier::VariablesPolynomial(1), PolyIdentifier::VariablesPolynomial(2)];
    let table = LookupTableApplication::new_range_table_of_width_3(width, over)?;
    cs.add_table(table)?;

    Ok(())
}

pub fn inscribe_combined_bitwise_ops_and_range_table<E, CS>(cs: &mut CS, width: usize) -> Result<(), SynthesisError>
where
    E: Engine,
    CS: ConstraintSystem<E>,
{
    let over = vec![PolyIdentifier::VariablesPolynomial(0), PolyIdentifier::VariablesPolynomial(1), PolyIdentifier::VariablesPolynomial(2)];
    use crate::plonk::circuit::bigint_new::*;
    let name = BITWISE_LOGICAL_OPS_TABLE_NAME;
    let table = LookupTableApplication::new(name, CombinedBitwiseLogicRangeTable::new(&name, width), over.clone(), None, true);
    cs.add_table(table)?;

    Ok(())
}

pub fn inscribe_range_table_for_bit_width_over_first_three_columns<E: Engine, CS: ConstraintSystem<E>>(cs: &mut CS, width: usize) -> Result<String, SynthesisError> {
    assert!(width > 0);
    let generator = move || format!("{} {} bits over A/B/C", DEFAULT_RANGE_TABLE_NAME_PREFIX, width);
    let name = (&generator)();
    if let Ok(..) = cs.get_table(&name) {
        return Ok(name);
    }

    let table_internal = RangeCheckTableOverOneColumnOfWidth3::new(width);
    let over = vec![PolyIdentifier::VariablesPolynomial(0), PolyIdentifier::VariablesPolynomial(1), PolyIdentifier::VariablesPolynomial(2)];
    let name_generator = Box::new(generator) as Box<dyn Fn() -> String + Send + Sync>;

    let application = LookupTableApplication::new("Range check table", table_internal, over, Some(name_generator), true);

    cs.add_table(application)?;

    Ok(name)
}

pub fn get_name_for_range_table_for_bit_width_over_first_three_columns(width: usize) -> String {
    assert!(width > 0);
    format!("{} {} bits over A/B/C", DEFAULT_RANGE_TABLE_NAME_PREFIX, width)
}