~edwargix/tallyard

e637a23a9be2691e4aadd77b95c76dac733a6680 — David Florness 5 years ago 93f7e6d
Wrap secret-sharing client code in main module
1 files changed, 51 insertions(+), 50 deletions(-)

M secret-sharing/client.rkt
M secret-sharing/client.rkt => secret-sharing/client.rkt +51 -50
@@ 24,62 24,63 @@
         (for/sum ([i (in-range degree)])
           (* (expt x (add1 i)) (vector-ref coefficients i)))))))

(define username (readline "Your name: "))
(module+ main
  (define username (readline "Your name: "))

(define point (gen))
(displayln (format "point: ~a" point))
  (define point (gen))
  (displayln (format "point: ~a" point))

(define bulletin
  (update-port (update-host json-requester "localhost") 1984))
  (define bulletin
    (update-port (update-host json-requester "localhost") 1984))

(with-handlers ([exn:fail:network? (λ (exn)
                                     (begin
                                       (displayln "Err: cannot connect to bulletin")
                                       (exit)))])
  (displayln "attempting to register...")
  (post bulletin "/register"
        #:data
        (jsexpr->string (hasheq 'input (bytes->hex-string
                                        (integer->bytes point num-bytes #f #t))
                                'name username))))
  (with-handlers ([exn:fail:network? (λ (exn)
                                       (begin
                                         (displayln "Err: cannot connect to bulletin")
                                         (exit)))])
    (displayln "attempting to register...")
    (post bulletin "/register"
          #:data
          (jsexpr->string (hasheq 'input (bytes->hex-string
                                          (integer->bytes point num-bytes #f #t))
                                  'name username))))

(define peers
  (json-response-body
   (let loop ([wait 0])
     (sleep wait)
     (displayln "attempting to retrieve peers...")
     (with-handlers ([exn:fail:network:http:error? (λ (exn)
                                                     ;; (displayln exn)
                                                     (loop 3))])
       (get bulletin "/peers")))))
  (define peers
    (json-response-body
     (let loop ([wait 0])
       (sleep wait)
       (displayln "attempting to retrieve peers...")
       (with-handlers ([exn:fail:network:http:error? (λ (exn)
                                                       ;; (displayln exn)
                                                       (loop 3))])
         (get bulletin "/peers")))))

(displayln (format "peers: ~a" peers))
(displayln (string? (car peers)))
  (displayln (format "peers: ~a" peers))
  (displayln (string? (car peers)))

(define candidates
  (json-response-body
   (with-handlers ([exn:fail:network:errno? (λ ()
                                              (displayln "Lost connection to bulletin!")
                                              (exit))])
     (get bulletin "/candidates"))))
  (define candidates
    (json-response-body
     (with-handlers ([exn:fail:network:errno? (λ ()
                                                (displayln "Lost connection to bulletin!")
                                                (exit))])
       (get bulletin "/candidates"))))

(define vote
  (let loop ()
    (let [(n (string->number (readline "Vote: ")))]
      (if (eq? n #f)
          (begin
            (displayln "Please enter a valid number")
            (loop))
          n))))
  (define vote
    (let loop ()
      (let [(n (string->number (readline "Vote: ")))]
        (if (eq? n #f)
            (begin
              (displayln "Please enter a valid number")
              (loop))
            n))))

(define poly (random-poly (length peers) vote))
  (define poly (random-poly (length peers) vote))

(post bulletin "/outputs"
      (make-hasheqv
       (for/list ([p (in-list peers)])
         (let ([output (poly (bytes->integer (hex-string->bytes p) #f #t))])
           (cons p (bytes->hex-string
                    (integer->bytes
                     output
                     (add1 (integer-bytes-length output #f))
                     #t)))))))
  (post bulletin "/outputs"
        (make-hasheqv
         (for/list ([p (in-list peers)])
           (let ([output (poly (bytes->integer (hex-string->bytes p) #f #t))])
             (cons p (bytes->hex-string
                      (integer->bytes
                       output
                       (add1 (integer-bytes-length output #f))
                       #t))))))))