Bladeren bron

begin setup for VCR mul calc

narodnik 5 jaren geleden
bovenliggende
commit
1ef6264de9
3 gewijzigde bestanden met toevoegingen van 155 en 4 verwijderingen
  1. 90 3
      proofs/mint2.psm
  2. 12 0
      src/jubjub.rs
  3. 53 1
      src/mint2.rs

+ 90 - 3
proofs/mint2.psm

@@ -1,5 +1,9 @@
 constant edwards_d 0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1
 constant edwards_d 0x2a9318e74bfa2b48f5fd9207e6bd7fd4292d7f6d37579d2601065fd6d6343eb1
 constant one 0x0000000000000000000000000000000000000000000000000000000000000001
 constant one 0x0000000000000000000000000000000000000000000000000000000000000001
+constant G_VCR_u 0x6800f4fa0f001cfc7ff6826ad58004b4d1d8da41af03744e3bce3b7793664337
+constant G_VCR_v 0x6d81d3a9cb45dedbe6fb2a6e1e22ab50ad46f1b0473b803b3caefab9380b6a8b
+constant G_VCV_u 0x273f910d9ecc1615d8618ed1d15fef4e9472c89ac043042d36183b2cb4d7ef51
+constant G_VCV_v 0x466a7e3a82f67ab1d32294fd89774ad6bc3332d0fa1ccd18a77a81f50667c8d7
 
 
 {% macro square(x2, x) %}
 {% macro square(x2, x) %}
     ########################################################
     ########################################################
@@ -110,9 +114,9 @@ constant one 0x0000000000000000000000000000000000000000000000000000000000000001
     enforce
     enforce
 {% endmacro %}
 {% endmacro %}
 
 
-{% macro assert_not_small_order(p, u, v) %}
+{% macro jubjub_assert_not_small_order(p, u, v) %}
     ########################################################
     ########################################################
-    # assert_not_small_order({{p}}, {{u}}, {{v}})
+    # jubjub_assert_not_small_order({{p}}, {{u}}, {{v}})
     ########################################################
     ########################################################
 
 
     # First doubling
     # First doubling
@@ -142,11 +146,94 @@ constant one 0x0000000000000000000000000000000000000000000000000000000000000001
     enforce
     enforce
 {% endmacro %}
 {% endmacro %}
 
 
+{% macro jubjub_add(P, x1, y1, x2, y2) -%}
+    # Compute U = (x1 + y1) * (y2 - EDWARDS_A*x2)
+    #           = (x1 + y1) * (x2 + y2)
+    private {{P}}_U
+    set {{P}}_U {{ x1 }}
+    add {{P}}_U {{ y1 }}
+    local {{P}}_tmp
+    set {{P}}_tmp {{ x2 }}
+    add {{P}}_tmp {{ y2 }}
+    mul {{P}}_U {{P}}_tmp
+
+    # assert (x1 + y1) * (x2 + y2) == U
+    lc0_add {{ x1 }}
+    lc0_add {{ y1 }}
+    lc1_add {{ x2 }}
+    lc1_add {{ y2 }}
+    lc2_add {{P}}_U
+    enforce
+
+    # Compute A = y2 * x1
+    private {{P}}_A
+    set {{P}}_A {{ y2 }}
+    mul {{P}}_A {{ x1 }}
+    # Compute B = x2 * y1
+    private {{P}}_B
+    set {{P}}_B {{ x2 }}
+    mul {{P}}_B {{ y1 }}
+    # Compute C = d*A*B
+    private {{P}}_C
+    load {{P}}_C d
+    mul {{P}}_C {{P}}_A
+    mul {{P}}_C {{P}}_B
+
+    # assert (d * A) * (B) == C
+    lc0_add_coeff d {{P}}_A
+    lc1_add {{P}}_B
+    lc2_add {{P}}_C
+    enforce
+
+    # Compute P.x = (A + B) / (1 + C)
+    private {{P}}_x
+    set {{P}}_x {{P}}_A
+    add {{P}}_x {{P}}_B
+    local {{P}}_x_denom
+    load {{P}}_x_denom one
+    add {{P}}_x_denom {{P}}_C
+    divide {{P}}_x {{P}}_x_denom
+
+    lc0_add_one
+    lc0_add {{P}}_C
+    lc1_add {{P}}_x
+    lc2_add {{P}}_A
+    lc2_add {{P}}_B
+    enforce
+
+    # Compute P.y = (U - A - B) / (1 - C)
+    private {{P}}_y
+    set {{P}}_y {{P}}_U
+    sub {{P}}_y {{P}}_A
+    sub {{P}}_y {{P}}_B
+    local {{P}}_y_denom
+    load {{P}}_y_denom one
+    sub {{P}}_y_denom {{P}}_C
+    divide {{P}}_y {{P}}_y_denom
+
+    lc0_add_one
+    lc0_sub {{P}}_C
+    lc1_add {{P}}_y
+    lc2_add {{P}}_U
+    lc2_sub {{P}}_A
+    lc2_sub {{P}}_B
+    enforce
+{%- endmacro %}
+
 contract mint_contract
 contract mint_contract
     param public_u
     param public_u
     param public_v
     param public_v
     {{ jubjub_witness("public", "public_u", "public_v") }}
     {{ jubjub_witness("public", "public_u", "public_v") }}
-    {{ assert_not_small_order("not_small", "public_u", "public_v") }}
+    {{ jubjub_assert_not_small_order("not_small", "public_u", "public_v") }}
+
+    {% for i in range(256) %}
+        param vc_randomness_{{i}}
+
+        lc0_add vc_randomness_{{i}}
+        enforce
+    {% endfor %}
+
+    #############
     {#
     {#
         {{ jubjub_double("pub_dbl", "public_u", "public_v") }}
         {{ jubjub_double("pub_dbl", "public_u", "public_v") }}
 
 

+ 12 - 0
src/jubjub.rs

@@ -10,7 +10,19 @@ const EDWARDS_D: Scalar = Scalar::from_raw([
     0x2a93_18e7_4bfa_2b48,
     0x2a93_18e7_4bfa_2b48,
 ]);
 ]);
 
 
+fn print_generators() {
+    use group::{Curve, Group, GroupEncoding};
+    use zcash_primitives::constants::*;
+
+    let x = jubjub::ExtendedPoint::from(VALUE_COMMITMENT_RANDOMNESS_GENERATOR.clone()).to_affine();
+    println!("G_VCR: {:?}", x);
+    let x = jubjub::ExtendedPoint::from(VALUE_COMMITMENT_VALUE_GENERATOR.clone()).to_affine();
+    println!("G_VCV: {:?}", x);
+}
+
 fn main() {
 fn main() {
+    print_generators();
+
     let g = SubgroupPoint::from_raw_unchecked(
     let g = SubgroupPoint::from_raw_unchecked(
         bls12_381::Scalar::from_raw([
         bls12_381::Scalar::from_raw([
             0xb981_9dc8_2d90_607e,
             0xb981_9dc8_2d90_607e,

+ 53 - 1
src/mint2.rs

@@ -1,20 +1,72 @@
 use bls12_381::Scalar;
 use bls12_381::Scalar;
+use ff::{PrimeField, Field};
 use group::{Curve, Group, GroupEncoding};
 use group::{Curve, Group, GroupEncoding};
 
 
 mod mint2_contract;
 mod mint2_contract;
 mod vm;
 mod vm;
 use mint2_contract::load_zkvm;
 use mint2_contract::load_zkvm;
 
 
+fn unpack<F: PrimeField>(value: F) -> Vec<Scalar> {
+    let mut bits = Vec::new();
+    for (i, bit) in value.to_le_bits().into_iter().cloned().enumerate() {
+        match bit {
+            true => bits.push(Scalar::one()),
+            false => bits.push(Scalar::zero()),
+        }
+    }
+    bits
+}
+
+fn do_vcr_test(value: &jubjub::Fr) {
+    let mut curbase = zcash_primitives::constants::VALUE_COMMITMENT_RANDOMNESS_GENERATOR;
+    let mut result = curbase.clone();
+    for (i, bit) in value.to_le_bits().into_iter().cloned().enumerate() {
+        let thisbase = if bit {
+            curbase.clone()
+        } else {
+            jubjub::SubgroupPoint::identity()
+        };
+        result += thisbase;
+        curbase = curbase.double();
+    }
+    let result = jubjub::ExtendedPoint::from(result).to_affine();
+    println!("cvr1: {:?}", result);
+    let randomness_commit =
+        zcash_primitives::constants::VALUE_COMMITMENT_RANDOMNESS_GENERATOR * value;
+    let randomness_commit = jubjub::ExtendedPoint::from(result).to_affine();
+    println!("cvr2: {:?}", randomness_commit);
+}
+
 fn main() -> std::result::Result<(), vm::ZKVMError> {
 fn main() -> std::result::Result<(), vm::ZKVMError> {
     use rand::rngs::OsRng;
     use rand::rngs::OsRng;
     let public_point = jubjub::ExtendedPoint::from(jubjub::SubgroupPoint::random(&mut OsRng));
     let public_point = jubjub::ExtendedPoint::from(jubjub::SubgroupPoint::random(&mut OsRng));
     let public_affine = public_point.to_affine();
     let public_affine = public_point.to_affine();
 
 
+    let value = 110;
+    let randomness_value: jubjub::Fr = jubjub::Fr::random(&mut OsRng);
+    let value_commit = (zcash_primitives::constants::VALUE_COMMITMENT_VALUE_GENERATOR
+        * jubjub::Fr::from(value))
+        + (zcash_primitives::constants::VALUE_COMMITMENT_RANDOMNESS_GENERATOR * randomness_value);
+
+    /////
+    let randomness_commit =
+        zcash_primitives::constants::VALUE_COMMITMENT_RANDOMNESS_GENERATOR * randomness_value;
+    /////
+    do_vcr_test(&randomness_value);
+
     let mut vm = load_zkvm();
     let mut vm = load_zkvm();
 
 
     vm.setup();
     vm.setup();
 
 
-    let params = vec![(0, public_affine.get_u()), (1, public_affine.get_v())];
+    let mut params = vec![
+        (0, public_affine.get_u()),
+        (1, public_affine.get_v()),
+        //(2, randomness_value),
+    ];
+    for x in unpack(randomness_value) {
+        params.push((params.len(), x));
+    }
+    println!("Size of params: {}", params.len());
     vm.initialize(&params)?;
     vm.initialize(&params)?;
 
 
     let proof = vm.prove();
     let proof = vm.prove();