main.rs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. use rand::rngs::OsRng;
  2. use std::{marker::PhantomData, time::Instant};
  3. use darkfi_sdk::{
  4. crypto::{
  5. constants::{
  6. sinsemilla::{OrchardCommitDomains, OrchardHashDomains},
  7. util::gen_const_array,
  8. NullifierK, OrchardFixedBases, OrchardFixedBasesFull, ValueCommitV,
  9. MERKLE_DEPTH_ORCHARD,
  10. },
  11. pallas,
  12. pasta_prelude::*,
  13. },
  14. pasta::group::GroupEncoding,
  15. };
  16. use halo2_gadgets::{
  17. ecc::{
  18. chip::{EccChip, EccConfig},
  19. FixedPoint, FixedPointBaseField, FixedPointShort, NonIdentityPoint, Point, ScalarFixed,
  20. ScalarFixedShort, ScalarVar,
  21. },
  22. utilities::lookup_range_check::LookupRangeCheckConfig,
  23. };
  24. use halo2_proofs::{
  25. circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value},
  26. plonk::{
  27. Advice, Circuit, Column, ConstraintSystem, Error, Fixed, Instance as InstanceColumn,
  28. Selector,
  29. },
  30. poly::Rotation,
  31. };
  32. use darkfi::zk::{
  33. assign_free_advice,
  34. gadget::arithmetic::{ArithChip, ArithConfig, ArithInstruction},
  35. proof::{Proof, ProvingKey, VerifyingKey},
  36. };
  37. mod circuit;
  38. trait NumericInstructions: Chip<pallas::Base> {
  39. /// Variable representing a number.
  40. type Num;
  41. fn load_private(
  42. &self,
  43. layouter: impl Layouter<pallas::Base>,
  44. a: Value<pallas::Base>,
  45. ) -> Result<Self::Num, Error>;
  46. fn load_constant(
  47. &self,
  48. layouter: impl Layouter<pallas::Base>,
  49. constant: pallas::Base,
  50. ) -> Result<Self::Num, Error>;
  51. fn mul(
  52. &self,
  53. layouter: impl Layouter<pallas::Base>,
  54. a: Self::Num,
  55. b: Self::Num,
  56. ) -> Result<Self::Num, Error>;
  57. fn expose_public(
  58. &self,
  59. layouter: impl Layouter<pallas::Base>,
  60. num: Self::Num,
  61. row: usize,
  62. ) -> Result<(), Error>;
  63. }
  64. /// The chip that will implement our instructions! Chips store their own
  65. /// config, as well as type markers if necessary.
  66. struct FieldChip {
  67. config: FieldConfig,
  68. }
  69. /// Chip state is stored in a config struct. This is generated by the chip
  70. /// during configuration, and then stored inside the chip.
  71. #[derive(Clone, Debug)]
  72. struct FieldConfig {
  73. /// For this chip, we will use two advice columns to implement our instructions.
  74. /// These are also the columns through which we communicate with other parts of
  75. /// the circuit.
  76. advice: [Column<Advice>; 2],
  77. /// This is the public input (instance) column.
  78. instance: Column<InstanceColumn>,
  79. // We need a selector to enable the multiplication gate, so that we aren't placing
  80. // any constraints on cells where `NumericInstructions::mul` is not being used.
  81. // This is important when building larger circuits, where columns are used by
  82. // multiple sets of instructions.
  83. s_mul: Selector,
  84. }
  85. impl FieldChip {
  86. fn construct(config: <Self as Chip<pallas::Base>>::Config) -> Self {
  87. Self { config }
  88. }
  89. fn configure(
  90. meta: &mut ConstraintSystem<pallas::Base>,
  91. advice: [Column<Advice>; 2],
  92. instance: Column<InstanceColumn>,
  93. constant: Column<Fixed>,
  94. ) -> <Self as Chip<pallas::Base>>::Config {
  95. meta.enable_equality(instance);
  96. meta.enable_constant(constant);
  97. for column in &advice {
  98. meta.enable_equality(*column);
  99. }
  100. let s_mul = meta.selector();
  101. // Define our multiplication gate!
  102. meta.create_gate("mul", |meta| {
  103. // To implement multiplication, we need three advice cells and a selector
  104. // cell. We arrange them like so:
  105. //
  106. // | a0 | a1 | s_mul |
  107. // |-----|-----|-------|
  108. // | lhs | rhs | s_mul |
  109. // | out | | |
  110. //
  111. // Gates may refer to any relative offsets we want, but each distinct
  112. // offset adds a cost to the proof. The most common offsets are 0 (the
  113. // current row), 1 (the next row), and -1 (the previous row), for which
  114. // `Rotation` has specific constructors.
  115. let lhs = meta.query_advice(advice[0], Rotation::cur());
  116. let rhs = meta.query_advice(advice[1], Rotation::cur());
  117. let out = meta.query_advice(advice[0], Rotation::next());
  118. let s_mul = meta.query_selector(s_mul);
  119. // Finally, we return the polynomial expressions that constrain this gate.
  120. // For our multiplication gate, we only need a single polynomial constraint.
  121. //
  122. // The polynomial expressions returned from `create_gate` will be
  123. // constrained by the proving system to equal zero. Our expression
  124. // has the following properties:
  125. // - When s_mul = 0, any value is allowed in lhs, rhs, and out.
  126. // - When s_mul != 0, this constrains lhs * rhs = out.
  127. vec![s_mul * (lhs * rhs - out)]
  128. });
  129. FieldConfig { advice, instance, s_mul }
  130. }
  131. }
  132. impl Chip<pallas::Base> for FieldChip {
  133. type Config = FieldConfig;
  134. type Loaded = ();
  135. fn config(&self) -> &Self::Config {
  136. &self.config
  137. }
  138. fn loaded(&self) -> &Self::Loaded {
  139. &()
  140. }
  141. }
  142. /// A variable representing a number.
  143. #[derive(Clone)]
  144. struct Number(AssignedCell<pallas::Base, pallas::Base>);
  145. impl NumericInstructions for FieldChip {
  146. type Num = Number;
  147. fn load_private(
  148. &self,
  149. mut layouter: impl Layouter<pallas::Base>,
  150. value: Value<pallas::Base>,
  151. ) -> Result<Self::Num, Error> {
  152. let config = self.config();
  153. layouter.assign_region(
  154. || "load private",
  155. |mut region| {
  156. region.assign_advice(|| "private input", config.advice[0], 0, || value).map(Number)
  157. },
  158. )
  159. }
  160. fn load_constant(
  161. &self,
  162. mut layouter: impl Layouter<pallas::Base>,
  163. constant: pallas::Base,
  164. ) -> Result<Self::Num, Error> {
  165. let config = self.config();
  166. layouter.assign_region(
  167. || "load constant",
  168. |mut region| {
  169. region
  170. .assign_advice_from_constant(|| "constant value", config.advice[0], 0, constant)
  171. .map(Number)
  172. },
  173. )
  174. }
  175. fn mul(
  176. &self,
  177. mut layouter: impl Layouter<pallas::Base>,
  178. a: Self::Num,
  179. b: Self::Num,
  180. ) -> Result<Self::Num, Error> {
  181. let config = self.config();
  182. layouter.assign_region(
  183. || "mul",
  184. |mut region: Region<'_, pallas::Base>| {
  185. // We only want to use a single multiplication gate in this region,
  186. // so we enable it at region offset 0; this means it will constrain
  187. // cells at offsets 0 and 1.
  188. config.s_mul.enable(&mut region, 0)?;
  189. // The inputs we've been given could be located anywhere in the circuit,
  190. // but we can only rely on relative offsets inside this region. So we
  191. // assign new cells inside the region and constrain them to have the
  192. // same values as the inputs.
  193. a.0.copy_advice(|| "lhs", &mut region, config.advice[0], 0)?;
  194. b.0.copy_advice(|| "rhs", &mut region, config.advice[1], 0)?;
  195. // Now we can assign the multiplication result, which is to be assigned
  196. // into the output position.
  197. let value = a.0.value().copied() * b.0.value();
  198. // Finally, we do the assignment to the output, returning a
  199. // variable to be used in another part of the circuit.
  200. region.assign_advice(|| "lhs * rhs", config.advice[0], 1, || value).map(Number)
  201. },
  202. )
  203. }
  204. fn expose_public(
  205. &self,
  206. mut layouter: impl Layouter<pallas::Base>,
  207. num: Self::Num,
  208. row: usize,
  209. ) -> Result<(), Error> {
  210. let config = self.config();
  211. layouter.constrain_instance(num.0.cell(), config.instance, row)
  212. }
  213. }
  214. #[derive(Clone)]
  215. pub struct MainConfig {
  216. primary: Column<InstanceColumn>,
  217. advices: [Column<Advice>; 10],
  218. ecc_config: EccConfig<OrchardFixedBases>,
  219. arith_config: ArithConfig,
  220. }
  221. impl MainConfig {
  222. fn ecc_chip(&self) -> EccChip<OrchardFixedBases> {
  223. EccChip::construct(self.ecc_config.clone())
  224. }
  225. fn arithmetic_chip(&self) -> ArithChip {
  226. ArithChip::construct(self.arith_config.clone())
  227. }
  228. }
  229. #[derive(Default)]
  230. struct MyCircuit {
  231. g1: Value<pallas::Point>,
  232. //g2: Value<pallas::Point>,
  233. //g3: Value<pallas::Point>,
  234. //g4: Value<pallas::Point>,
  235. s1: Value<pallas::Base>,
  236. //s2: Value<pallas::Scalar>,
  237. //s3: Value<pallas::Scalar>,
  238. //s4: Value<pallas::Scalar>,
  239. }
  240. impl Circuit<pallas::Base> for MyCircuit {
  241. // Since we are using a single chip for everything, we can just reuse its config.
  242. type Config = MainConfig;
  243. type FloorPlanner = SimpleFloorPlanner;
  244. fn without_witnesses(&self) -> Self {
  245. Self::default()
  246. }
  247. fn configure(meta: &mut ConstraintSystem<pallas::Base>) -> Self::Config {
  248. // Advice columns used in the circuit
  249. let advices = [
  250. meta.advice_column(),
  251. meta.advice_column(),
  252. meta.advice_column(),
  253. meta.advice_column(),
  254. meta.advice_column(),
  255. meta.advice_column(),
  256. meta.advice_column(),
  257. meta.advice_column(),
  258. meta.advice_column(),
  259. meta.advice_column(),
  260. ];
  261. // Fixed columns for the Sinsemilla generator lookup table
  262. let table_idx = meta.lookup_table_column();
  263. let lookup = (table_idx, meta.lookup_table_column(), meta.lookup_table_column());
  264. // Instance column used for public inputs
  265. let primary = meta.instance_column();
  266. meta.enable_equality(primary);
  267. // Permutation over all advice columns
  268. for advice in advices.iter() {
  269. meta.enable_equality(*advice);
  270. }
  271. // Poseidon requires four advice columns, while ECC incomplete addition
  272. // requires six. We can reduce the proof size by sharing fixed columns
  273. // between the ECC and Poseidon chips.
  274. // TODO: For multiple invocations perhaps they could/should be configured
  275. // in parallel rather than sharing?
  276. let lagrange_coeffs = [
  277. meta.fixed_column(),
  278. meta.fixed_column(),
  279. meta.fixed_column(),
  280. meta.fixed_column(),
  281. meta.fixed_column(),
  282. meta.fixed_column(),
  283. meta.fixed_column(),
  284. meta.fixed_column(),
  285. ];
  286. //let rc_a = lagrange_coeffs[2..5].try_into().unwrap();
  287. //let rc_b = lagrange_coeffs[5..8].try_into().unwrap();
  288. // Also use the first Lagrange coefficient column for loading global constants.
  289. meta.enable_constant(lagrange_coeffs[0]);
  290. // Use one of the right-most advice columns for all of our range checks.
  291. let range_check = LookupRangeCheckConfig::configure(meta, advices[9], table_idx);
  292. // Configuration for curve point operations.
  293. // This uses 10 advice columns and spans the whole circuit.
  294. let ecc_config =
  295. EccChip::<OrchardFixedBases>::configure(meta, advices, lagrange_coeffs, range_check);
  296. // Configuration for the Poseidon hash
  297. //let poseidon_config = PoseidonChip::configure::<poseidon::P128Pow5T3>(
  298. // meta,
  299. // advices[6..9].try_into().unwrap(),
  300. // advices[5],
  301. // rc_a,
  302. // rc_b,
  303. //);
  304. // Configuration for the Arithmetic chip
  305. let arith_config = ArithChip::configure(meta, advices[7], advices[8], advices[6]);
  306. // Configuration for a Sinsemilla hash instantiation and a
  307. // Merkle hash instantiation using this Sinsemilla instance.
  308. // Since the Sinsemilla config uses only 5 advice columns,
  309. // we can fit two instances side-by-side.
  310. //let (sinsemilla_cfg1, merkle_cfg1) = {
  311. // let sinsemilla_cfg1 = SinsemillaChip::configure(
  312. // meta,
  313. // advices[..5].try_into().unwrap(),
  314. // advices[6],
  315. // lagrange_coeffs[0],
  316. // lookup,
  317. // range_check,
  318. // );
  319. // let merkle_cfg1 = MerkleChip::configure(meta, sinsemilla_cfg1.clone());
  320. // (sinsemilla_cfg1, merkle_cfg1)
  321. //};
  322. //let (_sinsemilla_cfg2, merkle_cfg2) = {
  323. // let sinsemilla_cfg2 = SinsemillaChip::configure(
  324. // meta,
  325. // advices[5..].try_into().unwrap(),
  326. // advices[7],
  327. // lagrange_coeffs[1],
  328. // lookup,
  329. // range_check,
  330. // );
  331. // let merkle_cfg2 = MerkleChip::configure(meta, sinsemilla_cfg2.clone());
  332. // (sinsemilla_cfg2, merkle_cfg2)
  333. //};
  334. // K-table for 64 bit range check lookups
  335. let k_values_table_64 = meta.lookup_table_column();
  336. //let native_64_range_check_config =
  337. // NativeRangeCheckChip::<3, 64, 22>::configure(meta, advices[8], k_values_table_64);
  338. // K-table for 253 bit range check lookups
  339. let k_values_table_253 = meta.lookup_table_column();
  340. //let native_253_range_check_config =
  341. // NativeRangeCheckChip::<3, 253, 85>::configure(meta, advices[8], k_values_table_253);
  342. // TODO: FIXME: Configure these better, this is just a stop-gap
  343. let z1 = meta.advice_column();
  344. let z2 = meta.advice_column();
  345. //
  346. //let lessthan_config = LessThanChip::<3, 253, 85>::configure(
  347. // meta,
  348. // advices[6],
  349. // advices[7],
  350. // advices[8],
  351. // z1,
  352. // z2,
  353. // k_values_table_253,
  354. //);
  355. // Configuration for boolean checks, it uses the small_range_check
  356. // chip with a range of 2, which enforces one bit, i.e. 0 or 1.
  357. //let boolcheck_config = SmallRangeCheckChip::configure(meta, advices[9], 2);
  358. MainConfig { primary, advices, ecc_config, arith_config }
  359. }
  360. fn synthesize(
  361. &self,
  362. config: Self::Config,
  363. mut layouter: impl Layouter<pallas::Base>,
  364. ) -> Result<(), Error> {
  365. let g1 = NonIdentityPoint::new(
  366. config.ecc_chip(),
  367. layouter.namespace(|| "Witness EcNiPoint"),
  368. self.g1.as_ref().map(|cm| cm.to_affine()),
  369. )?;
  370. let s1 = assign_free_advice(layouter.namespace(|| "load a"), config.advices[0], self.s1)?;
  371. let s1: AssignedCell<pallas::Base, pallas::Base> = s1.into();
  372. let s1 = ScalarVar::from_base(
  373. config.ecc_chip(),
  374. layouter.namespace(|| "EcMul: ScalarFixed::new()"),
  375. &s1,
  376. )?;
  377. let (r, _) = g1.mul(layouter.namespace(|| "EcMul()"), s1)?;
  378. let mut public_inputs_offset = 0;
  379. let point: Point<pallas::Affine, EccChip<OrchardFixedBases>> = r.into();
  380. let r_x = point.inner().x();
  381. let r_y = point.inner().y();
  382. let var: AssignedCell<pallas::Base, pallas::Base> = r_x.into();
  383. layouter.constrain_instance(var.cell(), config.primary, public_inputs_offset)?;
  384. public_inputs_offset += 1;
  385. let var: AssignedCell<pallas::Base, pallas::Base> = r_y.into();
  386. layouter.constrain_instance(var.cell(), config.primary, public_inputs_offset)?;
  387. public_inputs_offset += 1;
  388. Ok(())
  389. }
  390. }
  391. fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
  392. let k = 8;
  393. //let g1 = pallas::Point::random(&mut OsRng);
  394. //println!("{:?}", g1);
  395. //let g1_bytes = g1.to_bytes();
  396. //println!("{}", hex::encode(&g1_bytes));
  397. // G1_x = 2fea7c1d8106d6d407a57bebec987e875ed9073ebf215e52f5b42c0c604c1801
  398. // G1_y = 0dcc7041075d6496102295722cd12f2f0c1e9d49eaa00f10c4bc02155598353b
  399. // G2_x = dfae4ed869484b2b9783c445888db03bac24f96f0260982b90f5b53477994e3e
  400. // G2_y = fa1b4182ef04514624a1e32846d48bfd229ef78975106e8e0614b8061dfe3d1d
  401. // G3_x = 702ddc6514ae63da6e13bcfa439f03b363018a152e16e665126623205ac4d31c
  402. // G3_y = 81cb38e121b6c375150aa2c1b4c92185a87781194a133535cbefb699e3475103
  403. // G4_x = 026b681bf7a0102e78bf3b34af50b5031ef1dd1f152f3df17af8e6eaae69cb3a
  404. // G4_y = c97b4f5ed89f4147eb3410892af8a1ecd21b96f59d43e5e4252872742acbbf24
  405. // s1 = f4537d29a235d6b4bf95ef436aa15fd641419c2da9e9600520be99a14c43ac2c
  406. // s2 = 6d2738d1e1f8bbb1bd154cd8102cca5c0224f8902803da1f7c4563b47103471c
  407. // s3 = ebbaf604f85b3e725e71a5d785e177c9f3ccd4c07394a0d59318cf1504c72a06
  408. // s4 = 5176cd889dd29f19cef07c5d2db9a2d67c568034ae737ff1f95456252d2e2301
  409. // Qx = 6b35d97bcef7928a15aed8e5d9b8ecbcb2a5ca190de7b9957971f6da6ad92c03
  410. // Qy = e485978ca7d9f798fe1b7afac7f74a98326cccc528f1010091948497ae5e7422
  411. // Halo2 points are the x coordinated in little endian order
  412. let g1x_bytes =
  413. hex::decode("2fea7c1d8106d6d407a57bebec987e875ed9073ebf215e52f5b42c0c604c1801")?;
  414. let g1x_bytes = g1x_bytes[..].try_into()?;
  415. let g1x = pallas::Base::from_repr(g1x_bytes).unwrap();
  416. let g1y_bytes =
  417. hex::decode("0dcc7041075d6496102295722cd12f2f0c1e9d49eaa00f10c4bc02155598353b")?;
  418. let g1y_bytes = g1y_bytes[..].try_into()?;
  419. let g1y = pallas::Base::from_repr(g1y_bytes).unwrap();
  420. let g1: pallas::Point = pallas::Affine::from_xy(g1x, g1y).unwrap().into();
  421. //let g2x_bytes = hex::decode("dfae4ed869484b2b9783c445888db03bac24f96f0260982b90f5b53477994e3e")?;
  422. //let g2x_bytes = g2x_bytes[..].try_into()?;
  423. //let g2x = pallas::Base::from_repr(g2x_bytes).unwrap();
  424. //let g2y_bytes = hex::decode("fa1b4182ef04514624a1e32846d48bfd229ef78975106e8e0614b8061dfe3d1d")?;
  425. //let g2y_bytes = g2y_bytes[..].try_into()?;
  426. //let g2y = pallas::Base::from_repr(g2y_bytes).unwrap();
  427. //let g2: pallas::Point = pallas::Affine::from_xy(g2x, g2y).unwrap().into();
  428. //let g3x_bytes = hex::decode("702ddc6514ae63da6e13bcfa439f03b363018a152e16e665126623205ac4d31c")?;
  429. //let g3x_bytes = g3x_bytes[..].try_into()?;
  430. //let g3x = pallas::Base::from_repr(g3x_bytes).unwrap();
  431. //let g3y_bytes = hex::decode("81cb38e121b6c375150aa2c1b4c92185a87781194a133535cbefb699e3475103")?;
  432. //let g3y_bytes = g3y_bytes[..].try_into()?;
  433. //let g3y = pallas::Base::from_repr(g3y_bytes).unwrap();
  434. //let g3: pallas::Point = pallas::Affine::from_xy(g3x, g3y).unwrap().into();
  435. //let g4x_bytes = hex::decode("026b681bf7a0102e78bf3b34af50b5031ef1dd1f152f3df17af8e6eaae69cb3a")?;
  436. //let g4x_bytes = g4x_bytes[..].try_into()?;
  437. //let g4x = pallas::Base::from_repr(g4x_bytes).unwrap();
  438. //let g4y_bytes = hex::decode("c97b4f5ed89f4147eb3410892af8a1ecd21b96f59d43e5e4252872742acbbf24")?;
  439. //let g4y_bytes = g4y_bytes[..].try_into()?;
  440. //let g4y = pallas::Base::from_repr(g4y_bytes).unwrap();
  441. //let g4: pallas::Point = pallas::Affine::from_xy(g4x, g4y).unwrap().into();
  442. //let s1_bytes = hex::decode("f4537d29a235d6b4bf95ef436aa15fd641419c2da9e9600520be99a14c43ac2c")?;
  443. //let s1_bytes = s1_bytes[..].try_into()?;
  444. //let s1 = pallas::Scalar::from_repr(s1_bytes).unwrap();
  445. //let s2_bytes = hex::decode("6d2738d1e1f8bbb1bd154cd8102cca5c0224f8902803da1f7c4563b47103471c")?;
  446. //let s2_bytes = s2_bytes[..].try_into()?;
  447. //let s2 = pallas::Scalar::from_repr(s2_bytes).unwrap();
  448. //let s3_bytes = hex::decode("ebbaf604f85b3e725e71a5d785e177c9f3ccd4c07394a0d59318cf1504c72a06")?;
  449. //let s3_bytes = s3_bytes[..].try_into()?;
  450. //let s3 = pallas::Scalar::from_repr(s3_bytes).unwrap();
  451. //let s4_bytes = hex::decode("5176cd889dd29f19cef07c5d2db9a2d67c568034ae737ff1f95456252d2e2301")?;
  452. //let s4_bytes = s4_bytes[..].try_into()?;
  453. //let s4 = pallas::Scalar::from_repr(s4_bytes).unwrap();
  454. //let qx_bytes = hex::decode("6b35d97bcef7928a15aed8e5d9b8ecbcb2a5ca190de7b9957971f6da6ad92c03")?;
  455. //let qx_bytes = qx_bytes[..].try_into()?;
  456. //let qx = pallas::Base::from_repr(qx_bytes).unwrap();
  457. //let qy_bytes = hex::decode("e485978ca7d9f798fe1b7afac7f74a98326cccc528f1010091948497ae5e7422")?;
  458. //let qy_bytes = qy_bytes[..].try_into()?;
  459. //let qy = pallas::Base::from_repr(qy_bytes).unwrap();
  460. //let q: pallas::Point = pallas::Affine::from_xy(qx, qy).unwrap().into();
  461. //let x = pallas::Scalar::from(2);
  462. //println!("{:?}", x);
  463. //println!("{:?}", x.to_repr());
  464. //let qq = g1*s1 + g2*s2 + g3*s3 + g4*s4;
  465. //println!("{:?}", qq.to_affine());
  466. //assert_eq!(q.to_affine(), qq.to_affine());
  467. let r = g1 * pallas::Scalar::from(2);
  468. let s1 = pallas::Base::from(2);
  469. let circuit = MyCircuit {
  470. g1: Value::known(g1),
  471. //g2: Value::known(g2),
  472. //g3: Value::known(g3),
  473. //g4: Value::known(g4),
  474. s1: Value::known(s1),
  475. //s2: Value::known(s2),
  476. //s3: Value::known(s3),
  477. //s4: Value::known(s4),
  478. };
  479. let r_coords = r.to_affine().coordinates().unwrap();
  480. let r_x = *r_coords.x();
  481. let r_y = *r_coords.y();
  482. let public = vec![r_x, r_y];
  483. let start = Instant::now();
  484. let pk = darkfi::zk::ProvingKey::build(k, &MyCircuit::default());
  485. let vk = darkfi::zk::VerifyingKey::build(k, &MyCircuit::default());
  486. println!("Setup: [{:?}]", start.elapsed());
  487. let start = Instant::now();
  488. let proof = Proof::create(&pk, &[circuit], &public, &mut OsRng)?;
  489. println!("Prove: [{:?}]", start.elapsed());
  490. let start = Instant::now();
  491. assert!(proof.verify(&vk, &public).is_ok());
  492. println!("Verify: [{:?}]", start.elapsed());
  493. Ok(())
  494. }