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

Non-zero constraint in UInt tests #392

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions symbolic-base/src/ZkFold/Symbolic/Data/UInt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ instance
(zs, b) <- flip runStateT b0 $ traverse StateT (Haskell.zipWith (fullSub r) is js)
d <- newAssigned (\v -> v i' - v j')
s'0 <- newAssigned (\v -> v d + v b + fromConstant t)
(s', _) <- splitExpansion (highRegisterSize @(BaseField c) @n @r) 1 s'0
(s', _) <- splitExpansion (highRegisterSize @(BaseField c) @n @r) (r + 1 -! highRegisterSize @(BaseField c) @n @r) s'0
TurtlePU marked this conversation as resolved.
Show resolved Hide resolved
return (k : zs <> [s'])

negate :: UInt n r c -> UInt n r c
Expand Down Expand Up @@ -452,7 +452,8 @@ instance (Symbolic c, KnownNat n, KnownRegisterSize rs) => MultiplicativeSemigro
p'0 <- foldrM (\k l -> do
k' <- newAssigned (\v -> v (cs ! k) * v (ds ! (r -! (k + 1))))
newAssigned (\v -> v k' + v l)) c' [0 .. r -! 1]
(p', _) <- splitExpansion (highRegisterSize @(BaseField c) @n @rs) (maxOverflow @(BaseField c) @n @rs) p'0
let highOverflow = registerSize @(BaseField c) @n @rs + maxOverflow @(BaseField c) @n @rs -! highRegisterSize @(BaseField c) @n @rs
(p', _) <- splitExpansion (highRegisterSize @(BaseField c) @n @rs) highOverflow p'0
return (p : ps <> [p'])

instance
Expand Down
11 changes: 5 additions & 6 deletions symbolic-base/test/Tests/UInt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ import ZkFold.Symbolic.Class (Arithmetic)
import ZkFold.Symbolic.Compiler (ArithmeticCircuit, exec)
import ZkFold.Symbolic.Data.Bool
import ZkFold.Symbolic.Data.ByteString
import ZkFold.Symbolic.Data.Combinators (Ceil, GetRegisterSize, Iso (..),
KnownRegisterSize (regSize), NumberOfRegisters,
RegisterSize (..))
import ZkFold.Symbolic.Data.Combinators (Ceil, GetRegisterSize, Iso (..), KnownRegisterSize,
NumberOfRegisters, RegisterSize (..))
import ZkFold.Symbolic.Data.Eq
import ZkFold.Symbolic.Data.Ord
import ZkFold.Symbolic.Data.UInt
Expand Down Expand Up @@ -116,8 +115,8 @@ specUInt' = hspec $ do
it "negates correctly" $ do
x <- toss m
return $ execAcUint @(Zp p) @n @rs (negate (fromConstant x)) === execZpUint @_ @n @rs (negate (fromConstant x))
when (regSize @rs == Auto) $ it "subtracts correctly" $ isHom @n @p @rs (-) (-) (overflowSub @n) <$> toss m <*> toss m
when (regSize @rs == Auto) $ it "multiplies correctly" $ isHom @n @p @rs (*) (*) (*) <$> toss m <*> toss m
it "multiplies correctly" $ isHom @n @p @rs (*) (*) (*) <$> toss m <*> toss m
it "subtracts correctly" $ isHom @n @p @rs (-) (-) (overflowSub @n) <$> toss m <*> toss m
it "iso uint correctly" $ do
x <- toss m
let bx = fromConstant x :: ByteString n (AC (Zp p))
Expand All @@ -129,7 +128,7 @@ specUInt' = hspec $ do
bx = fromConstant x :: ByteString n (AC (Zp p))
return $ evalBS (from ux :: ByteString n (AC (Zp p))) === evalBS bx

when (n <= 128 && regSize @rs == Auto) $ it "performs divMod correctly" $ withMaxSuccess 10 $ do
when (n <= 128) $ it "performs divMod correctly" $ withMaxSuccess 10 $ do
num <- toss m
d <- toss m
let (acQ, acR) = (fromConstant num :: UInt n rs (AC (Zp p))) `divMod` fromConstant d
Expand Down