~edwargix/tallyard

930c56947e7ef1be14b33618dcb10136fc5d04a4 — David Florness 5 years ago 2f15746
Move polynomial functionality to separate file
2 files changed, 26 insertions(+), 14 deletions(-)

M client.rkt
A poly.rkt
M client.rkt => client.rkt +1 -14
@@ 33,20 33,7 @@
  [#:struct (exn:fail:network:http:error exn:fail:network) ([code : Any] [type : Any])])
(require/typed crypto
  [crypto-random-bytes (Natural -> Bytes)])

(define num-bytes (assert (/ 1024 8) natural?))

(define (gen)
  (let* ([bstr (crypto-random-bytes num-bytes)])
    (cast (bytes->integer bstr #f #t 0 num-bytes) Natural)))

(define (random-poly [degree : Natural] [constant : Natural])
  (let ([coefficients (build-vector degree (λ (_) (gen)))])
    (λ ([x : Natural]) : Natural
       (cast (+ constant
                (for/sum : Integer ([i degree])
                  (* (expt x (add1 i)) (vector-ref coefficients i))))
             Natural))))
(require "poly.rkt")

(define (natural->hex-string [n : Natural])
  (bytes->hex-string

A poly.rkt => poly.rkt +25 -0
@@ 0,0 1,25 @@
#lang typed/racket
(require/typed binaryio
  [bytes->integer (->* (Bytes Boolean) (Boolean Natural Natural) Integer)]
  [integer->bytes (->* (Integer Natural Boolean) (Boolean Bytes Natural) Bytes)]
  [integer-bytes-length (Integer Boolean -> Natural)])
(require/typed crypto
  [crypto-random-bytes (Natural -> Bytes)])

(define-type Poly (Natural -> Natural))

(define num-bytes (assert (/ 1024 8) natural?))

(define (gen)
  (let* ([bstr (crypto-random-bytes num-bytes)])
    (cast (bytes->integer bstr #f #t 0 num-bytes) Natural)))

(define (random-poly [degree : Natural] [constant : Natural]) : Poly
  (let ([coefficients (build-vector degree (λ (_) (gen)))])
    (λ ([x : Natural]) : Natural
       (cast (+ constant
                (for/sum : Integer ([i degree])
                  (* (expt x (add1 i)) (vector-ref coefficients i))))
             Natural))))

(provide (all-defined-out))