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

Aggregate log proofs #4

Merged
merged 8 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Allow user to pass a list of values of arbitrary length to generate p…
…roof
  • Loading branch information
Acentelles committed Jul 24, 2018
commit ff6a45c5f5ffa7a721d395c6b42bc88285d58b54
8 changes: 6 additions & 2 deletions Bulletproofs/MultiRangeProof/Prover.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ generateProof upperBound vsAndvBlindings = do
Nothing -> throwE $ NNotPowerOf2 upperBound
Just n -> do
unless (checkRanges n vs) $ throwE $ ValuesNotInRange vs
lift $ generateProofUnsafe upperBound vsAndvBlindings

lift $ generateProofUnsafe upperBound vsAndvBlindingsExp2

where
doubleLogM :: Maybe Integer
Expand All @@ -43,6 +44,10 @@ generateProof upperBound vsAndvBlindings = do
logBase2M x
pure x
vs = fst <$> vsAndvBlindings
m = length vsAndvBlindings
residue = replicate (2 ^ log2Ceil m - m) (0, 0)
-- Vector of values passed must be of length 2^x
vsAndvBlindingsExp2 = vsAndvBlindings ++ residue


-- | Generate range proof from valid inputs
Expand Down Expand Up @@ -94,7 +99,6 @@ generateProofUnsafe upperBound vsAndvBlindings = do
panic "Error on: t1 = dotp l1 r0 + dotp l0 r1"

let tBlinding = sum (zipWith (\vBlindingFq j -> fqPower z (j + 1) * vBlindingFq) vBlindingsFq [1..m])
{-foldl' (\acc (vBlindingFq, j) -> acc + (fqPower z (j+1) * vBlindingFq)) (Fq.new 0) (zip vBlindingsFq [1..m])-}
+ (t2Blinding * fqSquare x)
+ (t1Blinding * x)
mu = aBlinding + (sBlinding * x)
Expand Down
10 changes: 7 additions & 3 deletions Bulletproofs/MultiRangeProof/Verifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ verifyProof
-> Bool
verifyProof upperBound vCommits proof@RangeProof{..}
= and
[ verifyTPoly n vCommits proof x y z
, verifyLRCommitment n m proof x y z
[ verifyTPoly n vCommitsExp2 proof x y z
, verifyLRCommitment n mExp2 proof x y z
]
where
x = shamirX aCommit sCommit t1Commit t2Commit y z
y = shamirY aCommit sCommit
z = shamirZ aCommit sCommit y
n = logBase2 upperBound
m = fromIntegral $ length vCommits
m = length vCommits
-- Vector of values passed must be of length 2^x
vCommitsExp2 = vCommits ++ residueCommits
residueCommits = replicate (2 ^ log2Ceil m - m) Crypto.PointO
mExp2 = fromIntegral $ length vCommitsExp2

-- | Verify the constant term of the polynomial t
-- t = t(x) = t0 + t1*x + t2*x^2
Expand Down
8 changes: 2 additions & 6 deletions Bulletproofs/RangeProof/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ obfuscateEncodedBits n aL aR y z
where
yN = powerVector y n

-- Convert obfuscateEncodedBits into aCommit sCommit single inner product.
-- Convert obfuscateEncodedBits into a single inner product.
-- We can afford for this factorization to leave terms “dangling”, but
-- what’s important is that the aL , aR terms be kept inside
-- (since they can’t be shared with the Verifier):
Expand Down Expand Up @@ -214,9 +214,6 @@ computeLRCommitment n m aCommit sCommit t tBlinding mu x y z hs'
`addP`
foldl' addP Crypto.PointO (zipWith mulP hExp hs') -- (hExp Hs')
`addP`
{-foldl' addP Crypto.PointO (zipWith mulP ((*) (fqSquare z) <$> powerVector 2 n) (sliceHs' 1)) -- (hExp Hs')-}
{-`addP`-}
{-foldl' addP Crypto.PointO (zipWith mulP ((*) (fqCube z) <$> powerVector 2 n) (sliceHs' 2)) -- (hExp Hs')-}
foldl'
(\acc j -> acc `addP` foldl' addP Crypto.PointO (zipWith mulP (hExp' j) (sliceHs' j)))
Crypto.PointO
Expand All @@ -227,8 +224,7 @@ computeLRCommitment n m aCommit sCommit t tBlinding mu x y z hs'
(t `mulP` u)
where
gsSum = foldl' addP Crypto.PointO (take (fromIntegral nm) gs)
hExp = ((*) z <$> powerVector y nm)
{-`fqAddV` ((*) (fqSquare z) <$> powerVector 2 n)-}
hExp = (*) z <$> powerVector y nm
hExp' j = (*) (fqPower z (j+1)) <$> powerVector 2 n
sliceHs' j = slice n j hs'
uChallenge = shamirU tBlinding mu t
Expand Down
31 changes: 30 additions & 1 deletion Bulletproofs/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module Bulletproofs.Utils (
powerVector,
logBase2,
logBase2M,
slice
log2Ceil,
slice,
padToNearestPowerOfTwo
) where

import Protolude
Expand Down Expand Up @@ -73,6 +75,33 @@ logBase2M x
slice :: Integer -> Integer -> [a] -> [a]
slice n j vs = take (fromIntegral $ j * n - (j - 1)*n) (drop (fromIntegral $ (j - 1) * n) vs)

-- | Append minimal amount of zeroes until the list has a length which
-- is a power of two.
padToNearestPowerOfTwo
:: Num f => [f] -> [f]
padToNearestPowerOfTwo [] = []
padToNearestPowerOfTwo xs = padToNearestPowerOfTwoOf (length xs) xs

-- | Given n, append zeroes until the list has length 2^n.
padToNearestPowerOfTwoOf
:: Num f
=> Int -- ^ n
-> [f] -- ^ list which should have length <= 2^n
-> [f] -- ^ list which will have length 2^n
padToNearestPowerOfTwoOf i xs = xs ++ replicate padLength 0
where
padLength = nearestPowerOfTwo - length xs
nearestPowerOfTwo = 2 ^ log2Ceil i

-- | Calculate ceiling of log base 2 of an integer.
log2Ceil :: Int -> Int
log2Ceil x = floorLog + correction
where
floorLog = finiteBitSize x - 1 - countLeadingZeros x
correction = if countTrailingZeros x < floorLog
then 1
else 0

--------------------------------------------------
-- Fiat-Shamir transformations
--------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/TestProtocol.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Test.QuickCheck
import qualified Test.QuickCheck.Monadic as QCM

import Crypto.Random.Types (MonadRandom(..))
import Crypto.Number.Generate (generateMax)
import Crypto.Number.Generate (generateMax, generateBetween)
import qualified Crypto.PubKey.ECC.Generate as Crypto
import qualified Crypto.PubKey.ECC.Prim as Crypto
import qualified Crypto.PubKey.ECC.Types as Crypto
Expand Down Expand Up @@ -205,7 +205,7 @@ test_completeness :: TestTree
test_completeness = localOption (QuickCheckTests 10) $
testProperty "Test multi range proof completeness" $ QCM.monadicIO $ do
n <- QCM.run $ (2 ^) <$> generateMax 8
m <- QCM.run $ (2 ^) <$> generateMax 3
m <- QCM.run $ generateBetween 1 10
ctx <- QCM.run $ replicateM (fromIntegral m) (setupV n)
let upperBound = getUpperBound n

Expand Down