codegen.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Functions here are called from pism.py using getattr()
  2. # and the function name as a string.
  3. def witness(line, out, point):
  4. return \
  5. r"""let %s = ecc::EdwardsPoint::witness(
  6. cs.namespace(|| "%s"),
  7. %s.map(jubjub::ExtendedPoint::from))?;""" % (out, line, point)
  8. def assert_not_small_order(line, point):
  9. return '%s.assert_not_small_order(cs.namespace(|| "%s"))?;' % (point, line)
  10. def u64_as_binary_le(line, out, val):
  11. return \
  12. r"""let %s = boolean::u64_into_boolean_vec_le(
  13. cs.namespace(|| "%s"),
  14. %s,
  15. )?;""" % (out, line, val)
  16. def fr_as_binary_le(line, out, fr):
  17. return \
  18. r"""let %s = boolean::field_into_boolean_vec_le(
  19. cs.namespace(|| "%s"), %s)?;""" % (out, line, fr)
  20. def ec_mul_const(line, out, fr, base):
  21. return \
  22. r"""let %s = ecc::fixed_base_multiplication(
  23. cs.namespace(|| "%s"),
  24. &%s,
  25. &%s,
  26. )?;""" % (out, line, base, fr)
  27. def ec_mul(line, out, fr, base):
  28. return 'let %s = %s.mul(cs.namespace(|| "%s"), &%s)?;' % (
  29. out, base, line, fr)
  30. def ec_add(line, out, a, b):
  31. return 'let %s = %s.add(cs.namespace(|| "%s"), &%s)?;' % (out, a, line, b)
  32. def ec_repr(line, out, point):
  33. return 'let %s = %s.repr(cs.namespace(|| "%s"))?;' % (out, point, line)
  34. def ec_get_u(line, out, point):
  35. return "let mut %s = %s.get_u().clone();" % (out, point)
  36. def emit_ec(line, point):
  37. return '%s.inputize(cs.namespace(|| "%s"))?;' % (point, line)
  38. def alloc_binary(line, out):
  39. return "let mut %s = vec![];" % out
  40. def binary_clone(line, out, binary):
  41. return "let mut %s: Vec<_> = %s.iter().cloned().collect();" % (out, binary)
  42. def binary_extend(line, binary, value):
  43. return "%s.extend(%s);" % (binary, value)
  44. def binary_push(line, binary, bit):
  45. return "%s.push(%s);" % (binary, bit)
  46. def binary_truncate(line, binary, size):
  47. return "%s.truncate(%s);" % (binary, size)
  48. def static_assert_binary_size(line, binary, size):
  49. return "assert_eq!(%s.len(), %s);" % (binary, size)
  50. def blake2s(line, out, input, personalization):
  51. return \
  52. r"""let mut %s = blake2s::blake2s(
  53. cs.namespace(|| "%s"),
  54. &%s,
  55. %s,
  56. )?;""" % (out, line, input, personalization)
  57. def pedersen_hash(line, out, input, personalization):
  58. return \
  59. r"""let mut %s = pedersen_hash::pedersen_hash(
  60. cs.namespace(|| "%s"),
  61. %s,
  62. &%s,
  63. )?;""" % (out, line, personalization, input)
  64. def emit_binary(line, binary):
  65. return 'multipack::pack_into_inputs(cs.namespace(|| "%s"), &%s)?;' % (
  66. line, binary)
  67. def alloc_bit(line, out, value):
  68. return \
  69. r"""let %s = boolean::Boolean::from(boolean::AllocatedBit::alloc(
  70. cs.namespace(|| "%s"),
  71. %s
  72. )?);""" % (out, line, value)
  73. def alloc_const_bit(line, out, value):
  74. return "let %s = Boolean::constant(%s);" % (out, value)
  75. def clone_bit(line, out, value):
  76. return "let %s = %s.clone();" % (out, value)
  77. def alloc_scalar(line, out, scalar):
  78. return \
  79. r"""let %s =
  80. num::AllocatedNum::alloc(cs.namespace(|| "%s"), || Ok(*%s.get()?))?;""" % (
  81. out, line, scalar)
  82. def scalar_as_binary(line, out, scalar):
  83. return 'let %s = %s.to_bits_le(cs.namespace(|| "%s"))?;' % (out, scalar,
  84. line)
  85. def emit_scalar(line, scalar):
  86. return '%s.inputize(cs.namespace(|| "%s"))?;' % (scalar, line)
  87. def scalar_enforce_equal(line, scalar_left, scalar_right):
  88. return \
  89. r"""cs.enforce(
  90. || "%s",
  91. |lc| lc + %s.get_variable(),
  92. |lc| lc + CS::one(),
  93. |lc| lc + %s.get_variable(),
  94. );""" % (line, scalar_left, scalar_right)
  95. def conditionally_reverse(line, out_left, out_right, in_left, in_right,
  96. condition):
  97. return \
  98. r"""let (%s, %s) = num::AllocatedNum::conditionally_reverse(
  99. cs.namespace(|| "%s"),
  100. &%s,
  101. &%s,
  102. &%s,
  103. )?;""" % (out_left, out_right, line, in_left, in_right, condition)