Advertisement
Guest User

High-order Boolean to arithmetic conversion

a guest
Mar 14th, 2017
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.45 KB | None | 0 0
  1. // This program is free software; you can redistribute it and/or modify it
  2. // under the terms of the GNU General Public License version 2 as published
  3. // by the Free Software Foundation.
  4.  
  5. //#define DEBUG
  6.  
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <time.h>
  11.  
  12. static uint32_t x=123456789, y=362436069, z=521288629;
  13.  
  14. uint32_t xorshf96(void) {  
  15.   uint32_t t;
  16.  
  17.   x ^= x << 16;
  18.   x ^= x >> 5;
  19.   x ^= x << 1;
  20.  
  21.   t = x;
  22.   x = y;
  23.   y = z;
  24.   z = t ^ x ^ y;
  25.   return z;
  26. }
  27.  
  28. void share(uint32_t x,uint32_t a[],int n)
  29. {
  30.   int i;
  31.   a[0]=x;
  32.   for(i=1;i<n;i++)
  33.     a[i]=0;
  34. }
  35.  
  36. void refresh(uint32_t a[],int n)
  37. {
  38.   int i;
  39.   for(i=0;i<n-1;i++)
  40.   {
  41.     uint32_t tmp=xorshf96(); //rand();
  42.     a[n-1]=a[n-1] ^ tmp;
  43.     a[i]=a[i] ^ tmp;
  44.   }
  45. }
  46.  
  47. uint32_t xorop(uint32_t a[],int n)
  48. {
  49.   int i;
  50.   uint32_t r=0;
  51.   for(i=0;i<n;i++)
  52.     r^=a[i];
  53.   return r;
  54. }
  55.  
  56. uint32_t addop(uint32_t a[],int n)
  57. {
  58.   int i;
  59.   uint32_t r=0;
  60.   for(i=0;i<n;i++)
  61.     r+=a[i];
  62.   return r;
  63. }
  64.  
  65. uint32_t Psi(uint32_t x,uint32_t y)
  66. {
  67.   return (x ^ y)-y;
  68. }
  69.  
  70. uint32_t Psi0(uint32_t x,uint32_t y,int n)
  71. {
  72.   return Psi(x,y) ^ ((~n & 1) * x);
  73. }
  74.  
  75. void copy(uint32_t *x,uint32_t *y,int n)
  76. {
  77.   for(int i=0;i<n;i++) x[i]=y[i];
  78. }
  79.  
  80. void convBA(uint32_t *D,uint32_t *x,int n)
  81. {
  82.   if (n==2)
  83.   {
  84.     uint32_t s=xorshf96();
  85.     uint32_t a1=x[0] ^ s;
  86.     uint32_t a2=x[1] ^ s;
  87.  
  88.     uint32_t r=xorshf96();
  89.  
  90.     D[0]=(a1 ^ Psi(a1,r ^ a2)) ^ Psi(a1,r);
  91.     D[1]=a2;
  92.  
  93.     #ifdef DEBUG
  94.       assert((x[0] ^ x[1])==(D[0] + D[1]));
  95.     #endif
  96.  
  97.     return;
  98.   }
  99.  
  100.   uint32_t a[n+1];
  101.   copy(a,x,n);
  102.   a[n]=0;
  103.  
  104.   refresh(a,n+1);
  105.  
  106.   uint32_t b[n];
  107.  
  108.   b[0]=Psi0(a[0],a[1],n);
  109.   for(int i=1;i<n;i++)
  110.     b[i]=Psi(a[0],a[i+1]);
  111.  
  112.   #ifdef DEBUG
  113.     assert(xorop(x,n)==(xorop(a+1,n)+xorop(b,n)));
  114.   #endif
  115.  
  116.   uint32_t c[n];
  117.   copy(c,a+1,n);
  118.   refresh(c,n);
  119.  
  120.   uint32_t d[n];
  121.   copy(d,b,n);
  122.   refresh(d,n);
  123.  
  124.   #ifdef DEBUG
  125.     assert(xorop(x,n)==(xorop(c,n)+xorop(d,n)));
  126.   #endif
  127.  
  128.   c[n-2]^=c[n-1];
  129.   d[n-2]^=d[n-1];
  130.  
  131.   #ifdef DEBUG
  132.     assert(xorop(x,n)==(xorop(c,n-1)+xorop(d,n-1)));
  133.   #endif
  134.  
  135.   uint32_t A[n-1],B[n-1];
  136.   convBA(A,c,n-1);
  137.   convBA(B,d,n-1);
  138.  
  139.   for(int i=0;i<n-2;i++)
  140.     D[i]=A[i]+B[i];
  141.  
  142.   D[n-2]=A[n-2];
  143.   D[n-1]=B[n-2];
  144.  
  145.   #ifdef DEBUG
  146.   assert(xorop(x,n)==addop(D,n));
  147.   #endif
  148. }
  149.  
  150. void timings()
  151. {
  152.   int nt=1000;
  153.   int n;
  154.  
  155.   for(int t=2;t<13;t++)
  156.   {
  157.     if ((t==7) || (t==9) || (t==11)) continue;
  158.     n=t+1;
  159.     uint32_t xin=242;
  160.     uint32_t x[n+1];
  161.     share(xin,x,n);
  162.     refresh(x,n);
  163.    
  164.     uint32_t D[n];
  165.  
  166.     clock_t start=clock();
  167.     for(int i=0;i<nt;i++)
  168.       convBA(D,x,n);
  169.     clock_t end=clock();
  170.     float dt=((float) (end-start))/CLOCKS_PER_SEC;
  171.    
  172.     printf(" $%d$ &",(int) (dt*1000000));
  173.  
  174.     // printf("order=%d t=%f\n",t,dt);
  175.   }
  176.   printf("\n");
  177. }
  178.  
  179.  
  180. int main()
  181. {
  182.   timings();
  183.   return 0;
  184.   int n=5;
  185.   int nt=1000;
  186.  
  187.   uint32_t xin=242;
  188.  
  189.   uint32_t x[n+1];
  190.  
  191.   share(xin,x,n);
  192.   refresh(x,n);
  193.  
  194.   printf("Input shares: ");
  195.   for(int i=0;i<n;i++)
  196.     printf("%u ",x[i]);
  197.   printf("\n");
  198.  
  199.   clock_t start=clock();
  200.  
  201.   uint32_t D[n];
  202.  
  203.   for(int i=0;i<nt;i++)
  204.     convBA(D,x,n);
  205.  
  206.   clock_t end=clock();
  207.   float dt=((float) (end-start))/CLOCKS_PER_SEC;
  208.  
  209.  
  210.   printf("Ouput shares: ");
  211.   for(int i=0;i<n;i++)
  212.     printf("%u ",D[i]);
  213.   printf("\n");
  214.  
  215.   printf("check: %u %u\n",xorop(x,n),addop(D,n));
  216.   printf("running time: %f\n",dt);
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement