Procházet zdrojové kódy

added code for mint contract

narodnik před 5 roky
rodič
revize
6e9c86a6de
6 změnil soubory, kde provedl 55 přidání a 16 odebrání
  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
     param public Point
 start
 start
+    # Witness input values
     u64_as_binary_le value param:value
     u64_as_binary_le value param:value
     fr_as_binary_le randomness_value param:randomness_value
     fr_as_binary_le randomness_value param:randomness_value
     fr_as_binary_le serial param:serial
     fr_as_binary_le serial param:serial
@@ -24,19 +25,49 @@ start
     witness public param:public
     witness public param:public
     assert_not_small_order 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 vcv value G_VCV
     ec_mul_const rcv randomness_value G_VCR
     ec_mul_const rcv randomness_value G_VCR
     ec_add cv vcv rcv
     ec_add cv vcv rcv
     # emit cv
     # emit cv
     emit_ec cv
     emit_ec cv
 
 
+    # Build the preimage to hash
+    # coin = Hash(public_key, value, serial, randomness_coin)
+
     alloc_binary preimage
     alloc_binary preimage
+
+    # public_key
     ec_repr repr_public public
     ec_repr repr_public public
     binary_extend preimage repr_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
     blake2s coin preimage CRH_IVK
     emit_binary coin
     emit_binary coin
 end
 end

+ 2 - 1
run_mint_contract.sh

@@ -1,3 +1,4 @@
 #!/bin/bash -x
 #!/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
 cargo run --release --bin mint

+ 3 - 0
scripts/codegen.py

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

+ 10 - 8
scripts/pism.py

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

+ 2 - 0
scripts/pism.vim

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

+ 3 - 3
src/mint.rs

@@ -33,9 +33,9 @@ impl MintRevealedValues {
                 .personal(zcash_primitives::constants::CRH_IVK_PERSONALIZATION)
                 .personal(zcash_primitives::constants::CRH_IVK_PERSONALIZATION)
                 .to_state()
                 .to_state()
                 .update(&public.to_bytes())
                 .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()
                 .finalize()
                 .as_bytes(),
                 .as_bytes(),
         );
         );