|
6 | 6 | "crypto/rand" |
7 | 7 | "encoding/binary" |
8 | 8 | "math/big" |
| 9 | + "sync" |
| 10 | + "time" |
9 | 11 |
|
10 | 12 | "github.com/cloudflare/circl/sign/ed448" |
11 | 13 | "github.com/iden3/go-iden3-crypto/poseidon" |
@@ -593,3 +595,75 @@ func (w *WesolowskiFrameProver) VerifyWeakRecursiveProof( |
593 | 595 | return false |
594 | 596 | } |
595 | 597 | } |
| 598 | + |
| 599 | +func (w *WesolowskiFrameProver) CalculateChallengeProof( |
| 600 | + challenge []byte, |
| 601 | + parallelism uint32, |
| 602 | + skew int64, |
| 603 | +) (int64, [][]byte, error) { |
| 604 | + now := time.Now().UnixMilli() |
| 605 | + input := binary.BigEndian.AppendUint64([]byte{}, uint64(now)) |
| 606 | + input = append(input, challenge...) |
| 607 | + outputs := make([][]byte, parallelism) |
| 608 | + |
| 609 | + wg := sync.WaitGroup{} |
| 610 | + wg.Add(int(parallelism)) |
| 611 | + |
| 612 | + for i := uint32(0); i < parallelism; i++ { |
| 613 | + i := i |
| 614 | + go func() { |
| 615 | + instanceInput := binary.BigEndian.AppendUint32([]byte{}, i) |
| 616 | + instanceInput = append(instanceInput, input...) |
| 617 | + b := sha3.Sum256(input) |
| 618 | + |
| 619 | + // 4.5 minutes = 270 seconds, one increment should be ten seconds |
| 620 | + proofDuration := 270 * 1000 |
| 621 | + calibratedDifficulty := (int64(proofDuration) / skew) * 10000 |
| 622 | + |
| 623 | + v := vdf.New(uint32(calibratedDifficulty), b) |
| 624 | + |
| 625 | + v.Execute() |
| 626 | + o := v.GetOutput() |
| 627 | + |
| 628 | + outputs[i] = make([]byte, 516) |
| 629 | + copy(outputs[i][:], o[:]) |
| 630 | + wg.Done() |
| 631 | + }() |
| 632 | + } |
| 633 | + |
| 634 | + wg.Wait() |
| 635 | + return now, outputs, nil |
| 636 | +} |
| 637 | + |
| 638 | +func (w *WesolowskiFrameProver) VerifyChallengeProof( |
| 639 | + challenge []byte, |
| 640 | + timestamp int64, |
| 641 | + assertedDifficulty int64, |
| 642 | + proof [][]byte, |
| 643 | +) bool { |
| 644 | + input := binary.BigEndian.AppendUint64([]byte{}, uint64(timestamp)) |
| 645 | + input = append(input, challenge...) |
| 646 | + |
| 647 | + for i := uint32(0); i < uint32(len(proof)); i++ { |
| 648 | + if len(proof[i]) != 516 { |
| 649 | + return false |
| 650 | + } |
| 651 | + |
| 652 | + instanceInput := binary.BigEndian.AppendUint32([]byte{}, i) |
| 653 | + instanceInput = append(instanceInput, input...) |
| 654 | + b := sha3.Sum256(input) |
| 655 | + |
| 656 | + // 4.5 minutes = 270 seconds, one increment should be ten seconds |
| 657 | + proofDuration := 270 * 1000 |
| 658 | + skew := (assertedDifficulty * 12) / 10 |
| 659 | + calibratedDifficulty := (int64(proofDuration) / skew) * 10000 |
| 660 | + |
| 661 | + v := vdf.New(uint32(calibratedDifficulty), b) |
| 662 | + check := v.Verify([516]byte(proof[i])) |
| 663 | + if !check { |
| 664 | + return false |
| 665 | + } |
| 666 | + } |
| 667 | + |
| 668 | + return true |
| 669 | +} |
0 commit comments