Skip to content

Commit

Permalink
Updates to bindings and Scalars for customisable pickles evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
jspada committed Jan 10, 2024
1 parent 19deab1 commit 8985aa0
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 114 deletions.
84 changes: 7 additions & 77 deletions src/lib/crypto/kimchi_bindings/stubs/src/linearization.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use kimchi::{
circuits::{
gate::GateType,
constraints::FeatureFlags,
expr::{Linearization, PolishToken},
expr::Linearization,
lookup::lookups::{LookupFeatures, LookupPatterns},
},
linearization::{constraints_expr, linearization_columns},
};

/// Converts the linearization of the kimchi circuit polynomial into a printable string.
pub fn linearization_strings<F: ark_ff::PrimeField + ark_ff::SquareRootField>(
// omit_custom_gate: bool,
custom_gate_type: Option<&Vec<PolishToken<F>>>,
omit_custom_gate: bool,
uses_custom_gates: bool,
) -> (String, Vec<(String, String)>) {
let features = if uses_custom_gates {
Expand All @@ -38,7 +36,7 @@ pub fn linearization_strings<F: ark_ff::PrimeField + ark_ff::SquareRootField>(
};
let evaluated_cols = linearization_columns::<F>(features.as_ref());
let (linearization, _powers_of_alpha) =
constraints_expr::<F>(/* omit_custom_gate */ false, custom_gate_type, features.as_ref(), true);
constraints_expr::<F>(omit_custom_gate, None, features.as_ref(), true);

let Linearization {
constant_term,
Expand All @@ -58,90 +56,22 @@ pub fn linearization_strings<F: ark_ff::PrimeField + ark_ff::SquareRootField>(
(constant, other_terms)
}

#[ocaml::func]
pub fn fp_linearization_strings_plus() -> (String, Vec<(String, String)>) {
// Define conditional gate in RPN
// w(0) = w(1) * w(3) + (1 - w(3)) * w(2)
use kimchi::circuits::expr::{PolishToken::*, *};
use kimchi::circuits::gate::CurrOrNext::Curr;
let conditional_gate = Some(vec![
Cell(Variable {
col: Column::Index(GateType::ForeignFieldAdd),
row: Curr,
}),
Cell(Variable {
col: Column::Witness(3),
row: Curr,
}),
Dup,
Mul,
Cell(Variable {
col: Column::Witness(3),
row: Curr,
}),
Sub,
Alpha,
Pow(1),
Cell(Variable {
col: Column::Witness(0),
row: Curr,
}),
Cell(Variable {
col: Column::Witness(3),
row: Curr,
}),
Cell(Variable {
col: Column::Witness(1),
row: Curr,
}),
Mul,
Literal(mina_curves::pasta::Fp::from(1u32)),
Cell(Variable {
col: Column::Witness(3),
row: Curr,
}),
Sub,
Cell(Variable {
col: Column::Witness(2),
row: Curr,
}),
Mul,
Add,
Sub,
Mul,
Add,
Mul,
]);

linearization_strings::<mina_curves::pasta::Fp>(conditional_gate.as_ref(), true)
}

#[ocaml::func]
pub fn fq_linearization_strings_plus() -> (String, Vec<(String, String)>) {
linearization_strings::<mina_curves::pasta::Fq>(None, false)
}


#[ocaml::func]
pub fn fp_linearization_strings_minus() -> (String, Vec<(String, String)>) {
// linearization_strings::<mina_curves::pasta::Fp>(true, true)
linearization_strings::<mina_curves::pasta::Fp>(None, true)
linearization_strings::<mina_curves::pasta::Fp>(true, true)
}

#[ocaml::func]
pub fn fq_linearization_strings_minus() -> (String, Vec<(String, String)>) {
// linearization_strings::<mina_curves::pasta::Fq>(true, false)
linearization_strings::<mina_curves::pasta::Fq>(None, false)
linearization_strings::<mina_curves::pasta::Fq>(true, false)
}

#[ocaml::func]
pub fn fp_linearization_strings() -> (String, Vec<(String, String)>) {
// linearization_strings::<mina_curves::pasta::Fp>(false, true)
linearization_strings::<mina_curves::pasta::Fp>(None, true)
linearization_strings::<mina_curves::pasta::Fp>(false, true)
}

#[ocaml::func]
pub fn fq_linearization_strings() -> (String, Vec<(String, String)>) {
// linearization_strings::<mina_curves::pasta::Fq>(false, false)
linearization_strings::<mina_curves::pasta::Fq>(None, false)
linearization_strings::<mina_curves::pasta::Fq>(false, false)
}
2 changes: 1 addition & 1 deletion src/lib/pickles/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ struct
, r.custom_gate_type )
| r :: rules ->
let feature_flags, custom_gate_type = go rules in
(* Note: For now we only support one choice when custom gates are defined *)
(* Note: For now we only support one choice when configurable gates are defined *)
if
Option.is_some custom_gate_type
|| Option.is_some r.custom_gate_type
Expand Down
8 changes: 4 additions & 4 deletions src/lib/pickles/plonk_checks/gen_scalars/gen_scalars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ let () =
output_string
{ocaml|
(* The constraints for overriden gate *)
module TickPlus : S = struct
module TickMinus : S = struct
let constant_term (type a)
({ add = ( + )
; sub = ( - )
Expand Down Expand Up @@ -277,10 +277,10 @@ let () =
a Env.t) =
|ocaml}

external fp_linearization_plus : bool -> string * (string * string) array
= "fp_linearization_strings_plus"
external fp_linearization_minus : bool -> string * (string * string) array
= "fp_linearization_strings_minus"

let fp_constant_term, fp_index_terms = fp_linearization_plus true
let fp_constant_term, fp_index_terms = fp_linearization_minus true

let () = output_string fp_constant_term

Expand Down
20 changes: 1 addition & 19 deletions src/lib/pickles/plonk_checks/scalars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4256,7 +4256,7 @@ module Tock : S = struct
end

(* The constraints for overriden gate *)
module TickPlus : S = struct
module TickMinus : S = struct
let constant_term (type a)
({ add = ( + )
; sub = ( - )
Expand Down Expand Up @@ -6273,24 +6273,6 @@ module TickPlus : S = struct
field
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
+ if_feature
( ForeignFieldAdd
, (fun () ->
cell (var (Index ForeignFieldAdd, Curr))
* ( square (cell (var (Witness 3, Curr)))
- cell (var (Witness 3, Curr))
+ alpha_pow 1
* ( cell (var (Witness 0, Curr))
- ( cell (var (Witness 3, Curr))
* cell (var (Witness 1, Curr))
+ ( field
"0x0000000000000000000000000000000000000000000000000000000000000001"
- cell (var (Witness 3, Curr)) )
* cell (var (Witness 2, Curr)) ) ) ) )
, fun () ->
field
"0x0000000000000000000000000000000000000000000000000000000000000000"
)
+ if_feature
( ForeignFieldMul
, (fun () ->
Expand Down
8 changes: 4 additions & 4 deletions src/lib/pickles/step_verifier.ml
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,8 @@ struct
let index_terms = Plonk_checks.Scalars.Tick.index_terms
end)

module Type1Plus =
Plonk_checks.Make (Shifted_value.Type1) (Scalars.TickPlus)
module Type1Minus =
Plonk_checks.Make (Shifted_value.Type1) (Scalars.TickMinus)
end

let domain_for_compiled (type branches)
Expand Down Expand Up @@ -1035,7 +1035,7 @@ struct
let ft_eval0 : Field.t =
with_label "ft_eval0" (fun () ->
if Option.is_some custom_gate_type then
Plonk_checks.Type1Plus.ft_eval0
Plonk_checks.Type1Minus.ft_eval0
(module Field)
~env ~domain plonk_minimal combined_evals evals1.public_input
else
Expand Down Expand Up @@ -1106,7 +1106,7 @@ struct
let plonk_checks_passed =
with_label "plonk_checks_passed" (fun () ->
if Option.is_some custom_gate_type then
Plonk_checks.Type1Plus.checked
Plonk_checks.Type1Minus.checked
(module Impl)
~env ~shift:shift1 plonk combined_evals
else
Expand Down
11 changes: 6 additions & 5 deletions src/lib/pickles/test/customizable_gate/test_customizable_gate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ open Pickles.Impls.Step

let perform_step_tests = true

let perform_recursive_tests = true
let perform_recursive_tests = false

let perform_step_choices_test = true
let perform_step_choices_test = false

let () = Pickles.Backend.Tick.Keypair.set_urs_info []

Expand Down Expand Up @@ -212,7 +212,7 @@ let () =
test ~step_only:true ~custom_gate_type:None ~valid_witness:true ;
(* Customised as Conditional gate; valid witness *)
(* Note: Requires Cache.Wrap.read_or_generate to have custom_gate_type passed to it *)
test ~step_only:true ~custom_gate_type:conditional_gate ~valid_witness:true ;
(* test ~step_only:true ~custom_gate_type:conditional_gate ~valid_witness:true ; *)

(* Customised as ForeignFieldAdd gate; invalid witness *)
let test_failed =
Expand All @@ -226,7 +226,7 @@ let () =
assert test_failed ;

(* Customised as Conditional gate; invalid witness *)
let test_failed =
(* let test_failed =
try
let _cs =
test ~step_only:true ~custom_gate_type:conditional_gate
Expand All @@ -235,7 +235,8 @@ let () =
false
with _ -> true
in
assert test_failed )
assert test_failed ) *)
)

(* Recursive tests *)
let () =
Expand Down
8 changes: 4 additions & 4 deletions src/lib/pickles/wrap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ module Type1 =
let index_terms = Plonk_checks.Scalars.Tick.index_terms
end)

module Type1Plus =
module Type1Minus =
Plonk_checks.Make
(Shifted_value.Type1)
(struct
let constant_term = Plonk_checks.Scalars.TickPlus.constant_term
let constant_term = Plonk_checks.Scalars.TickMinus.constant_term

let index_terms = Plonk_checks.Scalars.TickPlus.index_terms
let index_terms = Plonk_checks.Scalars.TickMinus.index_terms
end)

let _vector_of_list (type a t)
Expand All @@ -52,7 +52,7 @@ let combined_inner_product (type actual_proofs_verified) ~env ~domain ~ft_eval1
in
let ft_eval0 : Tick.Field.t =
if Option.is_some custom_gate_type then
Type1Plus.ft_eval0
Type1Minus.ft_eval0
(module Tick.Field)
plonk ~env ~domain
(Plonk_types.Evals.to_in_circuit combined_evals)
Expand Down

0 comments on commit 8985aa0

Please sign in to comment.