Skip to content

Commit

Permalink
Merge branch 'main' into TurtlePU/Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtlePU authored Dec 2, 2024
2 parents 8b07e55 + beb2dd5 commit a0a80fe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
11 changes: 6 additions & 5 deletions symbolic-base/src/ZkFold/Symbolic/Data/UInt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,22 @@ instance
)
where
t :: BaseField c
t = (one + one) ^ registerSize @(BaseField c) @n @r - one
t = (one + one) ^ registerSize @(BaseField c) @n @r

solve1 :: MonadCircuit i (BaseField c) w m => i -> i -> m [i]
solve1 i j = do
z0 <- newAssigned (\v -> v i - v j + fromConstant (2 ^ registerSize @(BaseField c) @n @r :: Natural))
z0 <- newAssigned (\v -> v i - v j + fromConstant t)
(z, _) <- splitExpansion (highRegisterSize @(BaseField c) @n @r) 1 z0
return [z]

solveN :: MonadCircuit i (BaseField c) w m => (i, i) -> ([i], [i]) -> (i, i) -> m [i]
solveN (i, j) (is, js) (i', j') = do
s <- newAssigned (\v -> v i - v j + fromConstant (t + one))
s <- newAssigned (\v -> v i - v j + fromConstant t)
let r = registerSize @(BaseField c) @n @r
(k, b0) <- splitExpansion r 1 s
(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'0 <- newAssigned (\v -> v d + v b + fromConstant (2 ^ highRegisterSize @(BaseField c) @n @r -! 1 :: Natural))
(s', _) <- splitExpansion (highRegisterSize @(BaseField c) @n @r) 1 s'0
return (k : zs <> [s'])

Expand Down Expand Up @@ -451,7 +451,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

0 comments on commit a0a80fe

Please sign in to comment.