فهرست منبع

add pedersen_hash related functionality

narodnik 5 سال پیش
والد
کامیت
58ee64a837
5فایلهای تغییر یافته به همراه73 افزوده شده و 13 حذف شده
  1. 8 4
      proofs/simple.aux
  2. 9 1
      proofs/simple.pism
  3. 26 1
      scripts/codegen.py
  4. 28 7
      scripts/pism.py
  5. 2 0
      src/simple.rs

+ 8 - 4
proofs/simple.aux

@@ -1,12 +1,16 @@
 {
     "constants": {
         "G_SPEND": {
-            "maps_to": "SPENDING_KEY_GENERATOR",
-            "module_includes": "zcash_proofs::constants"
+            "maps_to": "zcash_proofs::constants::SPENDING_KEY_GENERATOR"
         },
         "CRH_IVK": {
-            "maps_to": "CRH_IVK_PERSONALIZATION",
-            "module_includes": "zcash_primitives::constants"
+            "maps_to": "zcash_primitives::constants::CRH_IVK_PERSONALIZATION"
+        },
+        "JUBJUB_FR_CAPACITY": {
+            "maps_to": "jubjub::Fr::CAPACITY as usize"
+        },
+        "NOTE_COMMIT": {
+            "maps_to": "pedersen_hash::Personalization::NoteCommitment"
         }
     }
 }

+ 9 - 1
proofs/simple.pism

@@ -2,10 +2,13 @@
 # :source ../scripts/pism.vim
 constant G_SPEND FixedGenerator
 constant CRH_IVK BlakePersonalization
+constant JUBJUB_FR_CAPACITY BinarySize
+constant NOTE_COMMIT PedersenPersonalization
 
 contract input_spend
     param secret Fr
     param ak Point
+    param value U64
 start
     witness ak param:ak
     assert_not_small_order ak
@@ -18,10 +21,15 @@ start
 
     alloc_binary preimage
     ec_repr repr_ak ak
-    #binary_clone repr_ak2 repr_ak
     binary_extend preimage repr_ak
     static_assert_binary_size preimage 256
     blake2s ivk preimage CRH_IVK
+    binary_clone ivk2 ivk
     emit_binary ivk
+    binary_truncate ivk2 JUBJUB_FR_CAPACITY
+    u64_as_binary_le value_bits param:value
+    ec_mul pk_d value_bits public
+    pedersen_hash cm ivk NOTE_COMMIT
+    ec_get_u cur cm
 end
 

+ 26 - 1
scripts/codegen.py

@@ -10,6 +10,13 @@ r"""let %s = ecc::EdwardsPoint::witness(
 def assert_not_small_order(line, point):
     return '%s.assert_not_small_order(cs.namespace(|| "%s"))?;' % (point, line)
 
+def u64_as_binary_le(line, out, val):
+    return \
+r"""let %s = boolean::u64_into_boolean_vec_le(
+    cs.namespace(|| "%s"),
+    %s,
+)?;""" % (out, line, val)
+
 def fr_as_binary_le(line, out, fr):
     return \
 r"""let %s = boolean::field_into_boolean_vec_le(
@@ -23,12 +30,19 @@ r"""let %s = ecc::fixed_base_multiplication(
     &%s,
 )?;""" % (out, line, base, fr)
 
+def ec_mul(line, out, fr, base):
+    return 'let %s = %s.mul(cs.namespace(|| "%s"), &%s)?;' % (
+        out, base, line, fr)
+
 def ec_add(line, out, a, b):
     return 'let %s = %s.add(cs.namespace(|| "%s"), &%s)?;' % (out, a, line, b)
 
 def ec_repr(line, out, point):
     return 'let %s = %s.repr(cs.namespace(|| "%s"))?;' % (out, point, line)
 
+def ec_get_u(line, out, point):
+    return "let mut %s = %s.get_u().clone();" % (out, point)
+
 def emit_ec(line, point):
     return '%s.inputize(cs.namespace(|| "%s"))?;' % (point, line)
 
@@ -36,11 +50,14 @@ def alloc_binary(line, out):
     return "let mut %s = vec![];" % out
 
 def binary_clone(line, out, binary):
-    return "let %s = %s.iter().cloned();" % (out, binary)
+    return "let mut %s: Vec<_> = %s.iter().cloned().collect();" % (out, binary)
 
 def binary_extend(line, binary, value):
     return "%s.extend(%s);" % (binary, value)
 
+def binary_truncate(line, binary, size):
+    return "%s.truncate(%s);" % (binary, size)
+
 def static_assert_binary_size(line, binary, size):
     return "assert_eq!(%s.len(), %s);" % (binary, size)
 
@@ -52,6 +69,14 @@ r"""let mut %s = blake2s::blake2s(
     %s,
 )?;""" % (out, line, input, personalization)
 
+def pedersen_hash(line, out, input, personalization):
+    return \
+r"""let mut %s = pedersen_hash::pedersen_hash(
+    cs.namespace(|| "%s"),
+    %s,
+    &%s,
+)?;""" % (out, line, personalization, input)
+
 def emit_binary(line, binary):
     return 'multipack::pack_into_inputs(cs.namespace(|| "%s"), &%s)?;' % (
         line, binary)

+ 28 - 7
scripts/pism.py

@@ -27,6 +27,10 @@ command_desc = {
     "assert_not_small_order": (
         ("EdwardsPoint",    False),
     ),
+    "u64_as_binary_le": (
+        ("Vec<Boolean>",    True),
+        ("U64",             False),
+    ),
     "fr_as_binary_le": (
         ("Vec<Boolean>",    True),
         ("Fr",              False)
@@ -36,6 +40,11 @@ command_desc = {
         ("Vec<Boolean>",    False),
         ("FixedGenerator",  False)
     ),
+    "ec_mul": (
+        ("EdwardsPoint",    True),
+        ("Vec<Boolean>",    False),
+        ("EdwardsPoint",    False),
+    ),
     "ec_add": (
         ("EdwardsPoint",    True),
         ("EdwardsPoint",    False),
@@ -45,6 +54,10 @@ command_desc = {
         ("Vec<Boolean>",    True),
         ("EdwardsPoint",    False),
     ),
+    "ec_get_u": (
+        ("Scalar",          True),
+        ("EdwardsPoint",    False),
+    ),
     "emit_ec": (
         ("EdwardsPoint",    False),
     ),
@@ -59,6 +72,10 @@ command_desc = {
         ("Vec<Boolean>",    False),
         ("Vec<Boolean>",    False),
     ),
+    "binary_truncate": (
+        ("Vec<Boolean>",    False),
+        ("BinarySize",      False),
+    ),
     "static_assert_binary_size": (
         ("Vec<Boolean>",    False),
         ("INTEGER",         False),
@@ -68,6 +85,11 @@ command_desc = {
         ("Vec<Boolean>",    False),
         ("BlakePersonalization", False),
     ),
+    "pedersen_hash": (
+        ("EdwardsPoint",    True),
+        ("Vec<Boolean>",    False),
+        ("PedersenPersonalization", False),
+    ),
     "emit_binary": (
         ("Vec<Boolean>",    False),
     ),
@@ -95,7 +117,7 @@ class Line:
         return bool(self.text)
 
     def __repr__(self):
-        return "Line %s: %s" % (self.lineno, self.orig)
+        return "Line %s: %s" % (self.lineno, self.orig.lstrip())
 
     def command(self):
         if not self.is_empty():
@@ -236,7 +258,7 @@ r"""use bellman::{
 use bls12_381::Bls12;
 use ff::{PrimeField, Field};
 use group::Curve;
-use zcash_proofs::circuit::ecc;
+use zcash_proofs::circuit::{ecc, pedersen_hash};
 """
 
     def _compile_header(self):
@@ -257,7 +279,8 @@ use zcash_proofs::circuit::ecc;
         for command, args, line in self.program:
             if (code_text := self._compile_line(command, args, line)) is None:
                 return None
-            code += code_text + "\n"
+            code += "// %s\n" % str(line)
+            code += code_text + "\n\n"
         return code
 
     def _preprocess_args(self, args, line):
@@ -382,14 +405,12 @@ use zcash_proofs::circuit::ecc;
         self.rename_consts = {}
         if "constants" in aux:
             for const_name, value in aux["constants"].items():
-                if "module_includes" not in value:
-                    continue
                 if "maps_to" not in value:
                     eprint("error: bad aux config '%s', missing maps_to" %
                            const_name)
-                mapped_type = value["maps_to"]
-                code += "use %s::%s;\n" % (value["module_includes"], mapped_type)
+                    return None
 
+                mapped_type = value["maps_to"]
                 self.rename_consts[const_name] = mapped_type
 
         code += "\n"

+ 2 - 0
src/simple.rs

@@ -20,6 +20,7 @@ fn main() {
         let c = InputSpend {
             secret: None,
             ak: None,
+            value: None,
         };
         groth16::generate_random_parameters::<Bls12, _, _>(c, &mut OsRng).unwrap()
     };
@@ -28,6 +29,7 @@ fn main() {
     let c = InputSpend {
         secret: Some(secret),
         ak: Some(ak),
+        value: Some(110),
     };
 
     let proof = groth16::create_random_proof(c, &params, &mut OsRng).unwrap();