github.com/dusk-network/dusk-crypto@v0.1.3/rangeproof/bitCommitment.go (about) 1 package rangeproof 2 3 import ( 4 "errors" 5 "math/big" 6 7 ristretto "github.com/bwesterb/go-ristretto" 8 ) 9 10 // BitCommitment will be a struct used to hold the values aL and aR 11 type BitCommitment struct { 12 AL, AR []ristretto.Scalar 13 } 14 15 // BitCommit will take the value v producing aL and aR 16 // N.B. This has been specialised for N <= 64 17 func BitCommit(v *big.Int) BitCommitment { 18 19 bc := BitCommitment{ 20 AL: make([]ristretto.Scalar, N), 21 AR: make([]ristretto.Scalar, N), 22 } 23 24 var zero ristretto.Scalar 25 zero.SetZero() 26 var one ristretto.Scalar 27 one.SetOne() 28 var minusOne ristretto.Scalar 29 minusOne.Neg(&one) 30 31 num := v.Uint64() 32 33 for i := 0; i < N; i++ { 34 35 var rem uint64 36 37 rem = num % 2 38 num = num >> 1 39 40 if rem == 0 { 41 bc.AL[i] = zero 42 bc.AR[i] = minusOne 43 } else { 44 bc.AL[i] = one 45 bc.AR[i] = zero 46 } 47 48 } 49 50 return bc 51 } 52 53 // Debug makes sure we have calculated 54 // the correct aR and aL values 55 func (b *BitCommitment) Debug(v *big.Int) error { 56 57 var zero ristretto.Scalar 58 zero.SetZero() 59 var one ristretto.Scalar 60 one.SetOne() 61 62 testAL := big.NewInt(0) 63 testAR := big.NewInt(0) 64 65 for i := 0; i < N; i++ { 66 67 var basePow, e = big.NewInt(2), big.NewInt(int64(i)) 68 basePow.Exp(basePow, e, nil) 69 70 if b.AL[i].Equals(&one) { 71 testAL = testAL.Add(testAL, basePow) 72 } 73 if b.AR[i].Equals(&zero) { 74 testAR = testAR.Add(testAR, basePow) 75 } 76 } 77 78 if testAL.Cmp(v) != 0 { 79 return errors.New("[BitCommit(Debug)]: Wrong Value for AL") 80 } 81 82 if testAR.Cmp(v) != 0 { 83 return errors.New("[BitCommit(Debug)]: Wrong Value for AL") 84 } 85 86 return nil 87 }