Jelajahi Sumber

added code for mint contract

narodnik 5 tahun lalu
induk
melakukan
6e9c86a6de
6 mengubah file dengan 55 tambahan dan 16 penghapusan
  1. 35 4
      proofs/mint.pism
  2. 2 1
      run_mint_contract.sh
  3. 3 0
      scripts/codegen.py
  4. 10 8
      scripts/pism.py
  5. 2 0
      scripts/pism.vim
  6. 3 3
      src/mint.rs

+ 35 - 4
proofs/mint.pism

@@ -16,6 +16,7 @@ contract mint_contract
 
     param public Point
 start
+    # Witness input values
     u64_as_binary_le value param:value
     fr_as_binary_le randomness_value param:randomness_value
     fr_as_binary_le serial param:serial
@@ -24,19 +25,49 @@ start
     witness public param:public
     assert_not_small_order public
 
+    # Make value commitment
+    # V = v * G_VCV + r * G_VCR
+
     ec_mul_const vcv value G_VCV
     ec_mul_const rcv randomness_value G_VCR
     ec_add cv vcv rcv
     # emit cv
     emit_ec cv
 
+    # Build the preimage to hash
+    # coin = Hash(public_key, value, serial, randomness_coin)
+
     alloc_binary preimage
+
+    # public_key
     ec_repr repr_public public
     binary_extend preimage repr_public
-    #binary_extend preimage value
-    #binary_extend preimage serial
-    #binary_extend preimage randomness_coin
-    #static_assert_binary_size preimage 824
+
+    # value
+    binary_extend preimage value
+
+    # serial
+    # This value is 252 bits so we need to pad it with extra 0s
+    # to match the Rust values which are 256 bits
+    binary_extend preimage serial
+{% for n in range(4) %}
+    alloc_const_bit zero_bit false
+    binary_push preimage zero_bit
+{% endfor %}
+
+    # randomness_coin
+    binary_extend preimage randomness_coin
+{% for n in range(4) %}
+    alloc_const_bit zero_bit false
+    binary_push preimage zero_bit
+{% endfor %}
+
+    # Public key:       SubgroupPoint   = 256 bits
+    # Value:            u64             = 64 bits
+    # Serial:           Fr              = 252 + 4 bits padding
+    # Randomness coin   Fr              = 252 + 4 bits padding
+    # TOTAL: 832 bits for preimage
+    static_assert_binary_size preimage 832
     blake2s coin preimage CRH_IVK
     emit_binary coin
 end

+ 2 - 1
run_mint_contract.sh

@@ -1,3 +1,4 @@
 #!/bin/bash -x
-python scripts/pism.py proofs/mint.pism | rustfmt > src/mint_contract.rs
+python scripts/preprocess.py proofs/mint.pism > /tmp/mint.pism
+python scripts/pism.py /tmp/mint.pism proofs/mint.aux | rustfmt > src/mint_contract.rs
 cargo run --release --bin mint

+ 3 - 0
scripts/codegen.py

@@ -91,6 +91,9 @@ r"""let %s = boolean::Boolean::from(boolean::AllocatedBit::alloc(
     %s
 )?);""" % (out, line, value)
 
+def alloc_const_bit(line, out, value):
+    return "let %s = Boolean::constant(%s);" % (out, value)
+
 def clone_bit(line, out, value):
     return "let %s = %s.clone();" % (out, value)
 

+ 10 - 8
scripts/pism.py

@@ -101,6 +101,10 @@ command_desc = {
         ("Boolean",         True),
         ("Bool",            False),
     ),
+    "alloc_const_bit": (
+        ("Boolean",         True),
+        ("BOOL_CONST",      False),
+    ),
     "clone_bit": (
         ("Boolean",         True),
         ("Boolean",         False),
@@ -347,7 +351,7 @@ use zcash_proofs::circuit::{ecc, pedersen_hash};
             if new_val:
                 continue
 
-            if expected_type == "INTEGER":
+            if expected_type == "INTEGER" or expected_type == "BOOL_CONST":
                 continue
 
             if is_param:
@@ -381,7 +385,7 @@ use zcash_proofs::circuit::{ecc, pedersen_hash};
             if arg in self.constants:
                 continue
 
-            if expected_type == "INTEGER":
+            if expected_type == "INTEGER" or expected_type == "BOOL_CONST":
                 continue
 
             eprint("error: cannot find '%s' in the stack" % arg)
@@ -493,16 +497,14 @@ def process(contents, aux):
     return True
 
 def main(argv):
-    if len(argv) != 2:
-        eprint("pism FILENAME")
+    if len(argv) != 3:
+        eprint("pism FILENAME AUX_FILENAME")
         return -1
 
-    src_filename = argv[1]
-
-    basename, _ = os.path.splitext(src_filename)
-    aux_filename = basename + ".aux"
+    aux_filename = argv[2]
     aux = json.loads(open(aux_filename).read())
 
+    src_filename = argv[1]
     contents = open(src_filename).read()
     if not process(contents, aux):
         return -2

+ 2 - 0
scripts/pism.vim

@@ -19,6 +19,7 @@ syn match sapviFunction "^[ ]*[a-z_0-9]* "
 syn match sapviComment "#.*$"
 syn match sapviNumber ' \zs\d\+\ze'
 syn match sapviConst '[A-Z_]\{2,}[A-Z0-9_]*'
+syn keyword sapviBoolVal true false
 syn match sapviPreproc "{%.*%}"
 syn match sapviPreproc2 "{{.*}}"
 
@@ -31,5 +32,6 @@ hi def link sapviFunction   Function
 hi def link sapviComment    Comment
 hi def link sapviNumber     Constant
 hi def link sapviConst      Constant
+hi def link sapviBoolVal    Constant
 
 let b:current_syntax = "pism"

+ 3 - 3
src/mint.rs

@@ -33,9 +33,9 @@ impl MintRevealedValues {
                 .personal(zcash_primitives::constants::CRH_IVK_PERSONALIZATION)
                 .to_state()
                 .update(&public.to_bytes())
-                //.update(&value.to_bytes())
-                //.update(&serial.to_bytes())
-                //.update(&randomness_coin.to_bytes())
+                .update(&value.to_le_bytes())
+                .update(&serial.to_bytes())
+                .update(&randomness_coin.to_bytes())
                 .finalize()
                 .as_bytes(),
         );