subproblem.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef __SUBPROBLEM_H__
  2. #define __SUBPROBLEM_H__
  3. extern "C" {
  4. #include <event2/bufferevent.h>
  5. }
  6. #include <NTL/ZZ.h>
  7. #include <ostream>
  8. #include <string.h>
  9. NTL_CLIENT
  10. #define SUBPROBLEM_DESC_LEN (2 + (WORDS*3 + 3 + 1)*sizeof(unsigned int))
  11. struct Subproblem {
  12. unsigned short problemid;
  13. ZZ base;
  14. ZZ target;
  15. ZZ modulus;
  16. ZZ order;
  17. unsigned int dpfreq;
  18. unsigned char desc[1 + SUBPROBLEM_DESC_LEN];
  19. Subproblem(unsigned short id, const ZZ &b, const ZZ &t, const ZZ &m,
  20. const ZZ &o, unsigned int dpf) : problemid(id), base(b),
  21. target(t), modulus(m), order(o), dpfreq(dpf) {
  22. desc[0] = 'P';
  23. memmove(desc+1, &problemid, 2);
  24. BytesFromZZ(desc+3, base, WORDS*sizeof(unsigned int));
  25. BytesFromZZ(desc+3+WORDS*sizeof(unsigned int), target,
  26. WORDS*sizeof(unsigned int));
  27. BytesFromZZ(desc+3+2*WORDS*sizeof(unsigned int), modulus,
  28. WORDS*sizeof(unsigned int));
  29. BytesFromZZ(desc+3+3*WORDS*sizeof(unsigned int), order,
  30. 3*sizeof(unsigned int));
  31. memmove(desc+3+(3*WORDS+3)*sizeof(unsigned int), &dpfreq,
  32. sizeof(unsigned int));
  33. }
  34. // Initilize the Subproblem from the binary description, *without*
  35. // the leading 'P'
  36. Subproblem(const unsigned char *descnoP) {
  37. desc[0] = 'P';
  38. memmove(desc+1, descnoP, SUBPROBLEM_DESC_LEN);
  39. memmove(&problemid, desc+1, 2);
  40. ZZFromBytes(base, desc+3, WORDS*sizeof(unsigned int));
  41. ZZFromBytes(target, desc+3+WORDS*sizeof(unsigned int),
  42. WORDS*sizeof(unsigned int));
  43. ZZFromBytes(modulus, desc+3+2*WORDS*sizeof(unsigned int),
  44. WORDS*sizeof(unsigned int));
  45. ZZFromBytes(order, desc+3+3*WORDS*sizeof(unsigned int),
  46. 3*sizeof(unsigned int));
  47. memmove(&dpfreq, desc+3+(3*WORDS+3)*sizeof(unsigned int),
  48. sizeof(unsigned int));
  49. }
  50. // Dump for debug purposes
  51. void dump(ostream &os) const {
  52. os << "Subproblem " << problemid << "\n";
  53. os << "base = " << base << "\n";
  54. os << "target = " << target << "\n";
  55. os << "modulus = " << modulus << "\n";
  56. os << "order = " << order << "\n";
  57. os << "dpfreq = " << dpfreq << "\n";
  58. }
  59. // Write the subproblem to the given bufferevent
  60. void bev_write(struct bufferevent *bev) {
  61. bufferevent_write(bev, desc, 1 + SUBPROBLEM_DESC_LEN);
  62. }
  63. };
  64. #endif