codegen.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_truncate(line, binary, size):
  45. return "%s.truncate(%s);" % (binary, size)
  46. def static_assert_binary_size(line, binary, size):
  47. return "assert_eq!(%s.len(), %s);" % (binary, size)
  48. def blake2s(line, out, input, personalization):
  49. return \
  50. r"""let mut %s = blake2s::blake2s(
  51. cs.namespace(|| "%s"),
  52. &%s,
  53. %s,
  54. )?;""" % (out, line, input, personalization)
  55. def pedersen_hash(line, out, input, personalization):
  56. return \
  57. r"""let mut %s = pedersen_hash::pedersen_hash(
  58. cs.namespace(|| "%s"),
  59. %s,
  60. &%s,
  61. )?;""" % (out, line, personalization, input)
  62. def emit_binary(line, binary):
  63. return 'multipack::pack_into_inputs(cs.namespace(|| "%s"), &%s)?;' % (
  64. line, binary)