Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adder Test(s) #400

Open
DavePearce opened this issue Nov 27, 2024 · 2 comments
Open

Add adder Test(s) #400

DavePearce opened this issue Nov 27, 2024 · 2 comments

Comments

@DavePearce
Copy link
Collaborator

DavePearce commented Nov 27, 2024

Here is my code:

(defcolumns
  (X3 :i16@prove) (X2 :i16@prove) (X1 :i16@prove) (X0 :i16@prove)
  (Y3 :i16@prove) (Y2 :i16@prove) (Y1 :i16@prove) (Y0 :i16@prove)
  (C3 :i1@prove)  (C2 :i1@prove)  (C1 :i1@prove)  (C0 :i1@prove)
  (Z3 :i16)       (Z2 :i16)       (Z1 :i16)       (Z0 :i16))

(defconst OVERFLOW 65536)

(defpurefun (ADDER cin arg1 arg2 out cout)
  (eq! (+ out (* cout OVERFLOW)) (+ arg1 arg2 cin)))

(defconstraint X_Y_Z ()
  (begin
   (ADDER 0 X0 Y0 Z0 C0)
   (ADDER C0 X1 Y1 Z1 C1)
   (ADDER C1 X2 Y2 Z2 C2)
   (ADDER C2 X3 Y3 Z3 C3)))

And an example trace:

{ "<prelude>": {
    "X3": [0], "X2": [0], "X1": [0], "X0": [33000],
    "Y3": [0], "Y2": [0], "Y1": [0], "Y0": [33001],
    "C3": [0], "C2": [0], "C1": [0], "C0": [1],        
    "Z3": [0], "Z2": [0], "Z1": [1], "Z0": [465]
}}

What we could do is actually enumerate all input / output values and check it really works. We could repeat this for other operators related to field agnosticity.

@DavePearce
Copy link
Collaborator Author

(defcolumns
  (X3 :i16@prove) (X2 :i16@prove) (X1 :i16@prove) (X0 :i16@prove)
  (RES :binary@prove)
)

(defpurefun ((IS-ZERO :binary@loob :force) w3 w2 w1 w0) (+ w3 w2 w1 w0))

(defconstraint c1 ()
  (if (IS-ZERO X3 X2 X1 X0)
      (vanishes! RES) (eq! RES 1)))

And an example trace:

{ "<prelude>": {
    "X3": [0], "X2": [0], "X1": [0], "X0": [0],
    "RES": [0]
}}

@DavePearce
Copy link
Collaborator Author

Another example (bit shift):

(module m1)

(defcolumns
  X Y
  ;; X bits
  (x1 :binary@prove)
  (x2 :binary@prove)
  (x3 :binary@prove)
  (x4 :binary@prove)
  ;; Y bits
  (y1 :binary@prove)
  (y2 :binary@prove)
  (y3 :binary@prove)
  (y4 :binary@prove))

;; Combine bits into a nibble
(defpurefun (bits a1 a2 a3 a4)
  (+ (* 1 a1)
     (* 2 a2)
     (* 4 a3)
     (* 8 a4)))

;; For X
(defconstraint X_bits () (eq! X (bits x1 x2 x3 x4)))
;; For Y
(defconstraint Y_bits () (eq! Y (bits y1 y2 y3 y4)))
;; Relating X and Y
(defconstraint X_Y_bits ()
  (begin
   (eq!  0 y1)   
   (eq! x1 y2)
   (eq! x2 y3)
   (eq! x3 y4)))

Which accepts this trace:

{ "m1": {
    "X": [0,1,14],
    "Y": [0,2,12],
    
    "x1": [0,1,0],
    "x2": [0,0,1],
    "x3": [0,0,1],
    "x4": [0,0,1],
    
    "y1": [0,0,0],
    "y2": [0,1,0],
    "y3": [0,0,1],
    "y4": [0,0,1]
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant