Skip to content

Commit

Permalink
add selector calculation in proof-parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
chabanyknikita committed Aug 23, 2024
1 parent c3056b4 commit 10f18b8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 0 additions & 2 deletions internal/config/proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ type ProofParametersConfiger interface {

type ProofParametersConfig struct {
EventID string `fig:"event_id,required"`
SelectorUnique string `fig:"selector_unique"`
SelectorNotUnique string `fig:"selector_not_unique"`
TimestampLowerBound string `fig:"timestamp_lower_bound,required"`
TimestampUpperBound string `fig:"timestamp_upper_bound,required"`
ExpirationDateLowerBound string `fig:"expiration_date_lower_bound,required"`
Expand Down
9 changes: 5 additions & 4 deletions internal/config/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ type Verifiers struct {
func (c *config) Verifiers() Verifiers {
return c.verifier.Do(func() interface{} {
var cfg struct {
AllowedAge int `fig:"allowed_age,required"`
AllowedIdentityTimestamp int64 `fig:"allowed_identity_timestamp,required"`
AllowedAge int `fig:"allowed_age,required"`
VerificationKeyPath string `fig:"verification_key_path,required"`
AllowedIdentityTimestamp int64 `fig:"allowed_identity_timestamp,required"`
}

err := figure.
Expand All @@ -35,8 +36,8 @@ func (c *config) Verifiers() Verifiers {
}

pass, err := zk.NewVerifier(nil,
zk.WithProofType(zk.GeorgianPassport),
zk.WithVerificationKeyFile(passportVerificationKey),
zk.WithProofType(zk.GlobalPassport),
zk.WithVerificationKeyFile(cfg.VerificationKeyPath),
zk.WithPassportRootVerifier(c.passport.ProvideVerifier()),
zk.WithIdentitiesCreationTimestampLimit(cfg.AllowedIdentityTimestamp),
)
Expand Down
33 changes: 20 additions & 13 deletions internal/service/handlers/get_proof_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
Log(r).Debug(userInputs.Uniqueness)
return
}
proofSelector := ProofParameters(r).SelectorUnique
var IdentityCounterUpperBound int32 = 1
TimestampUpperBound := "19000000000"

if !userInputs.Uniqueness {
proofSelector = ProofParameters(r).SelectorNotUnique
IdentityCounterUpperBound = 0
TimestampUpperBound = "0"

TimestampUpperBound := "0"
var IdentityCounterUpperBound int32 = 0
proofSelector := CalculateProofSelector(userInputs.Uniqueness)
if proofSelector&(1<<9) != 0 && proofSelector&(1<<11) != 0 {
TimestampUpperBound = ProofParameters(r).TimestampUpperBound
IdentityCounterUpperBound = 1
}

userIdHash, err := StringToPoseidonHash(userInputs.UserId)
Expand All @@ -64,7 +63,7 @@ func GetProofParameters(w http.ResponseWriter, r *http.Request) {
proofParams := ProofParams{
host: Callback(r).Url,
eventID: ProofParameters(r).EventID,
proofSelector: proofSelector,
proofSelector: strconv.Itoa(proofSelector),
identityCounterUpperBound: IdentityCounterUpperBound,
timestampUpperBound: TimestampUpperBound,
citizenshipMask: utf8ToHex(userInputs.Nationality),
Expand Down Expand Up @@ -142,10 +141,18 @@ func utf8ToHex(input string) string {

func calculateBirthDateHex(ageLowerBound int) string {
currentDate := time.Now().UTC()
birthDateLoweBound := []byte(fmt.Sprintf("%02d", (currentDate.Year()-ageLowerBound)%100) + "0101")
hexBirthDateLoweBound := hexutils.BytesToHex(birthDateLoweBound)

birthYear := (currentDate.Year() - ageLowerBound) % 1e2
birthDateLowerBound := []byte(strconv.Itoa(birthYear) + "0101")
hexString := hexutils.BytesToHex(birthDateLowerBound)
return hexBirthDateLoweBound
}

return hexString
func CalculateProofSelector(uniqueness bool) int {
var bitLine uint32 = 0
if uniqueness {
bitLine |= 1 << 9
bitLine |= 1 << 11
}

return int(bitLine)
}
14 changes: 10 additions & 4 deletions internal/service/handlers/verification_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {
}
if verifiedUser == nil {
Log(r).Error("user is empty")
ape.RenderErr(w, problems.InternalError())
ape.RenderErr(w, problems.NotFound())
return
}

selectorInt, err := strconv.Atoi(selector)
selectorInt, err := strconv.Atoi(proof.PubSignals[zk.Selector])
if err != nil {
fmt.Println("Error during conversion")
return
}
identityCounterUpperBound, err := strconv.ParseInt(proof.PubSignals[zk.IdentityCounterUpperBound], 10, 64)
if err != nil {
Log(r).Error("cannot extract identityUpperBound from public signals")
ape.RenderErr(w, problems.InternalError())
return
}
if verifiedUser.Uniqueness {
if proof.PubSignals[zk.TimestampUpperBound] == ProofParameters(r).TimestampUpperBound {
if selectorInt&1<<9 == 0 {
Expand All @@ -76,8 +82,8 @@ func VerificationCallback(w http.ResponseWriter, r *http.Request) {

var verifyOpts = []zk.VerifyOption{
zk.WithCitizenships(verifiedUser.Nationality),
zk.WithProofSelectorValue(selector),
zk.WithIdentitiesCounter(1),
zk.WithProofSelectorValue(proof.PubSignals[zk.Selector]),
zk.WithIdentitiesCounter(identityCounterUpperBound),
zk.WithAgeAbove(verifiedUser.AgeLowerBound),
zk.WithEventID(ProofParameters(r).EventID),
}
Expand Down

0 comments on commit 10f18b8

Please sign in to comment.