Skip to content

Commit

Permalink
Merge pull request #179 from o1-labs/feature/range-check-64-develop
Browse files Browse the repository at this point in the history
Activate custom gates in JS and add rangeCheck64 gadget
  • Loading branch information
mitschabaude authored Oct 16, 2023
2 parents b4e9957 + 851d3df commit 827e6a6
Show file tree
Hide file tree
Showing 17 changed files with 94,417 additions and 94,246 deletions.
2,924 changes: 1,462 additions & 1,462 deletions compiled/node_bindings/plonk_wasm.cjs

Large diffs are not rendered by default.

1,016 changes: 508 additions & 508 deletions compiled/node_bindings/plonk_wasm.d.cts

Large diffs are not rendered by default.

Binary file modified compiled/node_bindings/plonk_wasm_bg.wasm
Binary file not shown.
476 changes: 238 additions & 238 deletions compiled/node_bindings/plonk_wasm_bg.wasm.d.ts

Large diffs are not rendered by default.

179,080 changes: 89,592 additions & 89,488 deletions compiled/node_bindings/snarky_js_node.bc.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiled/node_bindings/snarky_js_node.bc.map

Large diffs are not rendered by default.

1,520 changes: 760 additions & 760 deletions compiled/web_bindings/plonk_wasm.d.ts

Large diffs are not rendered by default.

2,916 changes: 1,458 additions & 1,458 deletions compiled/web_bindings/plonk_wasm.js

Large diffs are not rendered by default.

Binary file modified compiled/web_bindings/plonk_wasm_bg.wasm
Binary file not shown.
542 changes: 271 additions & 271 deletions compiled/web_bindings/plonk_wasm_bg.wasm.d.ts

Large diffs are not rendered by default.

38 changes: 19 additions & 19 deletions compiled/web_bindings/snarky_js_web.bc.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions js/snarky-class-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export default [
name: 'field',
type: 'object',
},
{
name: 'gates',
type: 'object',
},
{
name: 'bool',
type: 'object',
Expand Down
89 changes: 49 additions & 40 deletions kimchi/wasm/src/plonk_verifier_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,16 +713,54 @@ macro_rules! impl_verification_key {
}
} */

fn compute_feature_flags(index: &WasmPlonkVerifierIndex) -> FeatureFlags {
let xor = index.evals.xor_comm.is_some();
let range_check0 = index.evals.range_check0_comm.is_some();
let range_check1 = index.evals.range_check1_comm.is_some();
let foreign_field_add = index.evals.foreign_field_add_comm.is_some();
let foreign_field_mul = index.evals.foreign_field_mul_comm.is_some();
let rot = index.evals.rot_comm.is_some();

let lookup = index
.lookup_index.as_ref()
.map_or(false, |li| li.lookup_info.features.patterns.lookup);

// TODO
let runtime_tables = false;

let patterns = LookupPatterns {
xor,
lookup,
range_check: range_check0 || range_check1 || rot,
foreign_field_mul: foreign_field_mul,
};

FeatureFlags {
range_check0,
range_check1,
foreign_field_add,
foreign_field_mul,
xor,
rot,
lookup_features: LookupFeatures {
patterns,
joint_lookup_used: patterns.joint_lookups_used(),
uses_runtime_tables: runtime_tables,
},
}
}

pub fn of_wasm(
max_poly_size: i32,
public_: i32,
prev_challenges: i32,
log_size_of_group: i32,
srs: &$WasmSrs,
evals: &WasmPlonkVerificationEvals,
shifts: &WasmShifts,
lookup_index: Option<WasmLookupVerifierIndex>
index: WasmPlonkVerifierIndex,
) -> (DlogVerifierIndex<GAffine, OpeningProof<GAffine>>, Arc<SRS<GAffine>>) {
let max_poly_size = index.max_poly_size;
let public_ = index.public_;
let prev_challenges = index.prev_challenges;
let log_size_of_group = index.domain.log_size_of_group;
let srs = &index.srs;
let evals = &index.evals;
let shifts = &index.shifts;

/*
let urs_copy = Rc::clone(&*urs);
let urs_copy_outer = Rc::clone(&*urs);
Expand All @@ -734,26 +772,7 @@ macro_rules! impl_verification_key {
let (endo_q, _endo_r) = poly_commitment::srs::endos::<$GOther>();
let domain = Domain::<$F>::new(1 << log_size_of_group).unwrap();

let feature_flags =
FeatureFlags {
range_check0: false,
range_check1: false,
foreign_field_add: false,
foreign_field_mul: false,
rot: false,
xor: false,
lookup_features:
LookupFeatures {
patterns: LookupPatterns {
xor: false,
lookup: false,
range_check: false,
foreign_field_mul: false, },
joint_lookup_used:false,
uses_runtime_tables: false,
},
};

let feature_flags = compute_feature_flags(&index);
let (linearization, powers_of_alpha) = expr_linearization(Some(&feature_flags), true, 3);

let index =
Expand Down Expand Up @@ -809,24 +828,14 @@ macro_rules! impl_verification_key {

linearization,
powers_of_alpha,
lookup_index: lookup_index.map(Into::into),
lookup_index: index.lookup_index.map(Into::into),
};
(index, srs.0.clone())
}

impl From<WasmPlonkVerifierIndex> for DlogVerifierIndex<$G, OpeningProof<$G>> {
fn from(index: WasmPlonkVerifierIndex) -> Self {
of_wasm(
index.max_poly_size,
index.public_,
index.prev_challenges,
index.domain.log_size_of_group,
&index.srs,
&index.evals,
&index.shifts,
index.lookup_index
)
.0
of_wasm(index).0
}
}

Expand Down
3 changes: 2 additions & 1 deletion ocaml/lib/pickles_bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type pickles_rule_js =
; shouldVerify : Boolean.var array Js.prop >
Js.t )
Js.prop
; featureFlags : bool Pickles_types.Plonk_types.Features.t Js.prop
; proofsToVerify :
< isSelf : bool Js.t Js.prop ; tag : Js.Unsafe.any Js.t Js.prop > Js.t
array
Expand Down Expand Up @@ -250,7 +251,7 @@ module Choices = struct
Pickles.Tag.t ) ->
let prevs = prevs ~self in
{ Pickles.Inductive_rule.identifier = Js.to_string rule##.identifier
; feature_flags = Pickles_types.Plonk_types.Features.none_bool
; feature_flags = rule##.featureFlags
; prevs
; main =
(fun { public_input } ->
Expand Down
1 change: 1 addition & 0 deletions ocaml/lib/pickles_bindings.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type pickles_rule_js =
; shouldVerify : Boolean.var array Js.prop >
Js.t )
Js.prop
; featureFlags : bool Pickles_types.Plonk_types.Features.t Js.prop
; proofsToVerify :
< isSelf : bool Js.t Js.prop ; tag : Js.Unsafe.any Js.t Js.prop > Js.t
array
Expand Down
35 changes: 35 additions & 0 deletions ocaml/lib/snarky_bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ module Field' = struct
let to_constant_and_terms x = Field.to_constant_and_terms x
end

module Gates = struct
let range_check0 v0 (v0p0, v0p1, v0p2, v0p3, v0p4, v0p5)
(v0c0, v0c1, v0c2, v0c3, v0c4, v0c5, v0c6, v0c7) compact =
Impl.with_label "range_check0" (fun () ->
Impl.assert_
{ annotation = Some __LOC__
; basic =
Kimchi_backend_common.Plonk_constraint_system.Plonk_constraint.T
(RangeCheck0
{ (* Current row *) v0
; v0p0
; v0p1
; v0p2
; v0p3
; v0p4
; v0p5
; v0c0
; v0c1
; v0c2
; v0c3
; v0c4
; v0c5
; v0c6
; v0c7
; (* Coefficients *)
compact
} )
} )
end

module Bool = struct
let not x = Boolean.not x

Expand Down Expand Up @@ -284,6 +314,11 @@ let snarky =
method toConstantAndTerms = to_constant_and_terms
end

val gates =
object%js
method rangeCheck0 = Gates.range_check0
end

val bool =
object%js
method not = Bool.not
Expand Down
Loading

0 comments on commit 827e6a6

Please sign in to comment.